|
| 1 | +/** |
| 2 | + * Copyright 2024 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: Updates an instance. |
| 18 | +// usage: node instance-update.js <INSTANCE_ID> <PROJECT_ID> |
| 19 | + |
| 20 | +'use strict'; |
| 21 | + |
| 22 | +function main(instanceId, projectId) { |
| 23 | + async function updateInstance() { |
| 24 | + // [START spanner_update_instance] |
| 25 | + |
| 26 | + // Imports the Google Cloud client library |
| 27 | + const {Spanner, protos} = require('@google-cloud/spanner'); |
| 28 | + |
| 29 | + /** |
| 30 | + * TODO(developer): Uncomment the following lines before running the sample. |
| 31 | + */ |
| 32 | + // const projectId = 'my-project-id'; |
| 33 | + // const instanceId = 'my-instance'; |
| 34 | + |
| 35 | + // Creates a client |
| 36 | + const spanner = new Spanner({ |
| 37 | + projectId: projectId, |
| 38 | + }); |
| 39 | + |
| 40 | + const instanceAdminClient = spanner.getInstanceAdminClient(); |
| 41 | + |
| 42 | + // Updates an instance |
| 43 | + try { |
| 44 | + console.log( |
| 45 | + `Updating instance ${instanceAdminClient.instancePath( |
| 46 | + projectId, |
| 47 | + instanceId |
| 48 | + )}.` |
| 49 | + ); |
| 50 | + const [operation] = await instanceAdminClient.updateInstance({ |
| 51 | + instance: { |
| 52 | + name: instanceAdminClient.instancePath(projectId, instanceId), |
| 53 | + labels: { |
| 54 | + updated: 'true', |
| 55 | + created: Math.round(Date.now() / 1000).toString(), // current time |
| 56 | + }, |
| 57 | + edition: |
| 58 | + protos.google.spanner.admin.instance.v1.Instance.Edition.ENTERPRISE, //optional |
| 59 | + }, |
| 60 | + // Field mask specifying fields that should get updated in an Instance |
| 61 | + fieldMask: (protos.google.protobuf.FieldMask = { |
| 62 | + paths: ['labels', 'edition'], |
| 63 | + }), |
| 64 | + }); |
| 65 | + |
| 66 | + console.log(`Waiting for operation on ${instanceId} to complete...`); |
| 67 | + await operation.promise(); |
| 68 | + console.log(`Updated instance ${instanceId}.`); |
| 69 | + const [metadata] = await instanceAdminClient.getInstance({ |
| 70 | + name: instanceAdminClient.instancePath(projectId, instanceId), |
| 71 | + }); |
| 72 | + console.log( |
| 73 | + `Instance ${instanceId} has been updated with the ${metadata.edition} ` + |
| 74 | + 'edition.' |
| 75 | + ); |
| 76 | + } catch (err) { |
| 77 | + console.error('ERROR:', err); |
| 78 | + } |
| 79 | + // [END spanner_update_instance] |
| 80 | + } |
| 81 | + updateInstance(); |
| 82 | +} |
| 83 | + |
| 84 | +process.on('unhandledRejection', err => { |
| 85 | + console.error(err.message); |
| 86 | + process.exitCode = 1; |
| 87 | +}); |
| 88 | +main(...process.argv.slice(2)); |
0 commit comments