| 17 | |
| 18 | // creates an instance using Instance Admin Client |
| 19 | async function createInstance(instanceId, projectId) { |
| 20 | // [START spanner_create_instance] |
| 21 | |
| 22 | // Imports the Google Cloud client library |
| 23 | const {Spanner, protos} = require('@google-cloud/spanner'); |
| 24 | |
| 25 | // Creates a client |
| 26 | const spanner = new Spanner({ |
| 27 | projectId: projectId, |
| 28 | }); |
| 29 | |
| 30 | const instanceAdminClient = spanner.getInstanceAdminClient(); |
| 31 | /** |
| 32 | * TODO(developer): Uncomment the following lines before running the sample. |
| 33 | **/ |
| 34 | // const projectId = 'my-project-id'; |
| 35 | // const instanceId = 'my-instance'; |
| 36 | |
| 37 | // Creates a new instance |
| 38 | try { |
| 39 | const instancePath = instanceAdminClient.instancePath( |
| 40 | projectId, |
| 41 | instanceId |
| 42 | ); |
| 43 | console.log(`Creating instance ${instancePath}.`); |
| 44 | |
| 45 | const [operation] = await instanceAdminClient.createInstance({ |
| 46 | instanceId: instanceId, |
| 47 | parent: instanceAdminClient.projectPath(projectId), |
| 48 | instance: { |
| 49 | config: instanceAdminClient.instanceConfigPath( |
| 50 | projectId, |
| 51 | 'regional-us-central1' |
| 52 | ), |
| 53 | nodeCount: 1, |
| 54 | displayName: 'Display name for the instance.', |
| 55 | labels: { |
| 56 | cloud_spanner_samples: 'true', |
| 57 | created: Math.round(Date.now() / 1000).toString(), // current time |
| 58 | }, |
| 59 | edition: |
| 60 | protos.google.spanner.admin.instance.v1.Instance.Edition.STANDARD, //optional |
| 61 | }, |
| 62 | }); |
| 63 | |
| 64 | console.log(`Waiting for operation on ${instanceId} to complete...`); |
| 65 | await operation.promise(); |
| 66 | |
| 67 | console.log(`Created instance ${instanceId}.`); |
| 68 | } catch (err) { |
| 69 | console.error('ERROR:', err); |
| 70 | } finally { |
| 71 | spanner.close(); |
| 72 | } |
| 73 | // [END spanner_create_instance] |
| 74 | } |
| 75 | |
| 76 | const { |