|
| 1 | +/** |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +// sample-metadata: |
| 17 | +// title: Creates a user-managed instance configuration. |
| 18 | +// usage: node instance-config-create <INSTANCE_CONFIG_ID> <BASE_INSTANCE_CONFIG_ID> <PROJECT_ID> |
| 19 | + |
| 20 | +'use strict'; |
| 21 | + |
| 22 | +function main( |
| 23 | + instanceConfigId = 'custom-my-instance-config', |
| 24 | + baseInstanceConfigId = 'my-base-instance-config', |
| 25 | + projectId = 'my-project-id' |
| 26 | +) { |
| 27 | + // [START spanner_create_instance_config] |
| 28 | + |
| 29 | + /** |
| 30 | + * TODO(developer): Uncomment the following lines before running the sample. |
| 31 | + */ |
| 32 | + // const instanceConfigId = 'custom-my-instance-config-id' |
| 33 | + // const baseInstanceConfigId = 'my-base-instance-config-id'; |
| 34 | + // const projectId = 'my-project-id'; |
| 35 | + |
| 36 | + // Imports the Google Cloud client library |
| 37 | + const {Spanner} = require('@google-cloud/spanner'); |
| 38 | + |
| 39 | + // Creates a client |
| 40 | + const spanner = new Spanner({ |
| 41 | + projectId: projectId, |
| 42 | + }); |
| 43 | + async function createInstanceConfig() { |
| 44 | + // Creates a new instance config |
| 45 | + const instanceConfig = spanner.instanceConfig(instanceConfigId); |
| 46 | + try { |
| 47 | + const [baseInstanceConfig] = await spanner.getInstanceConfig( |
| 48 | + baseInstanceConfigId |
| 49 | + ); |
| 50 | + console.log(`Creating instance config ${instanceConfig.formattedName_}.`); |
| 51 | + const [, operation] = await instanceConfig.create({ |
| 52 | + displayName: instanceConfigId, |
| 53 | + baseConfig: baseInstanceConfig.name, |
| 54 | + replicas: baseInstanceConfig.replicas.concat( |
| 55 | + baseInstanceConfig.optionalReplicas[0] |
| 56 | + ), |
| 57 | + }); |
| 58 | + console.log( |
| 59 | + `Waiting for create operation for ${instanceConfig.id} to complete...` |
| 60 | + ); |
| 61 | + await operation.promise(); |
| 62 | + console.log(`Created instance config ${instanceConfigId}.`); |
| 63 | + } catch (err) { |
| 64 | + console.error( |
| 65 | + 'ERROR: Creating instance config ', |
| 66 | + instanceConfigId, |
| 67 | + ' failed with error message ', |
| 68 | + err |
| 69 | + ); |
| 70 | + } |
| 71 | + } |
| 72 | + createInstanceConfig(); |
| 73 | + // [END spanner_create_instance_config] |
| 74 | +} |
| 75 | + |
| 76 | +process.on('unhandledRejection', err => { |
| 77 | + console.error(err.message); |
| 78 | + process.exitCode = 1; |
| 79 | +}); |
| 80 | +main(...process.argv.slice(2)); |
0 commit comments