diff --git a/README.md b/README.md index 8e88ef2e6..277d9feb9 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre | Lists the instance configuration operations. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-config-get-operations.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/instance-config-get-operations.js,samples/README.md) | | Updates a user-managed instance configuration. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-config-update.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/instance-config-update.js,samples/README.md) | | Creates a new instance partition | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-partition-create.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/instance-partition-create.js,samples/README.md) | +| Updates an instance. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-update.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/instance-update.js,samples/README.md) | | Creates a instance with autoscaling config. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-with-autoscaling-config.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/instance-with-autoscaling-config.js,samples/README.md) | | Instance-with-processing-units | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-with-processing-units.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/instance-with-processing-units.js,samples/README.md) | | Instance | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/instance.js,samples/README.md) | diff --git a/samples/README.md b/samples/README.md index 5276e8710..904cf9a5f 100644 --- a/samples/README.md +++ b/samples/README.md @@ -58,6 +58,7 @@ and automatic, synchronous replication for high availability. * [Lists the instance configuration operations.](#lists-the-instance-configuration-operations.) * [Updates a user-managed instance configuration.](#updates-a-user-managed-instance-configuration.) * [Creates a new instance partition](#creates-a-new-instance-partition) + * [Updates an instance.](#updates-an-instance.) * [Creates a instance with autoscaling config.](#creates-a-instance-with-autoscaling-config.) * [Instance-with-processing-units](#instance-with-processing-units) * [Instance](#instance) @@ -883,6 +884,23 @@ __Usage:__ +### Updates an instance. + +View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-update.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/instance-update.js,samples/README.md) + +__Usage:__ + + +`node instance-update.js ` + + +----- + + + + ### Creates a instance with autoscaling config. View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-with-autoscaling-config.js). diff --git a/samples/instance-update.js b/samples/instance-update.js new file mode 100644 index 000000000..8db5ca0ca --- /dev/null +++ b/samples/instance-update.js @@ -0,0 +1,88 @@ +/** + * Copyright 2024 Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// sample-metadata: +// title: Updates an instance. +// usage: node instance-update.js + +'use strict'; + +function main(instanceId, projectId) { + async function updateInstance() { + // [START spanner_update_instance] + + // Imports the Google Cloud client library + const {Spanner, protos} = require('@google-cloud/spanner'); + + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = 'my-project-id'; + // const instanceId = 'my-instance'; + + // Creates a client + const spanner = new Spanner({ + projectId: projectId, + }); + + const instanceAdminClient = spanner.getInstanceAdminClient(); + + // Updates an instance + try { + console.log( + `Updating instance ${instanceAdminClient.instancePath( + projectId, + instanceId + )}.` + ); + const [operation] = await instanceAdminClient.updateInstance({ + instance: { + name: instanceAdminClient.instancePath(projectId, instanceId), + labels: { + updated: 'true', + created: Math.round(Date.now() / 1000).toString(), // current time + }, + edition: + protos.google.spanner.admin.instance.v1.Instance.Edition.ENTERPRISE, //optional + }, + // Field mask specifying fields that should get updated in an Instance + fieldMask: (protos.google.protobuf.FieldMask = { + paths: ['labels', 'edition'], + }), + }); + + console.log(`Waiting for operation on ${instanceId} to complete...`); + await operation.promise(); + console.log(`Updated instance ${instanceId}.`); + const [metadata] = await instanceAdminClient.getInstance({ + name: instanceAdminClient.instancePath(projectId, instanceId), + }); + console.log( + `Instance ${instanceId} has been updated with the ${metadata.edition} ` + + 'edition.' + ); + } catch (err) { + console.error('ERROR:', err); + } + // [END spanner_update_instance] + } + updateInstance(); +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/samples/instance.js b/samples/instance.js index 9098abb67..cad7b2bee 100644 --- a/samples/instance.js +++ b/samples/instance.js @@ -20,7 +20,7 @@ async function createInstance(instanceId, projectId) { // [START spanner_create_instance] // Imports the Google Cloud client library - const {Spanner} = require('@google-cloud/spanner'); + const {Spanner, protos} = require('@google-cloud/spanner'); // Creates a client const spanner = new Spanner({ @@ -56,6 +56,8 @@ async function createInstance(instanceId, projectId) { cloud_spanner_samples: 'true', created: Math.round(Date.now() / 1000).toString(), // current time }, + edition: + protos.google.spanner.admin.instance.v1.Instance.Edition.STANDARD, //optional }, }); diff --git a/samples/system-test/spanner.test.js b/samples/system-test/spanner.test.js index 1208b012b..52437c641 100644 --- a/samples/system-test/spanner.test.js +++ b/samples/system-test/spanner.test.js @@ -259,21 +259,41 @@ describe('Autogenerated Admin Clients', () => { await sample_instance.delete(); }); - // create_instance - it('should create an example instance', async () => { - const output = execSync( + // create_and_update_instance + it('should create and update an example instance with spanner editions', async () => { + const createInstanceOutput = execSync( `${instanceCmd} createInstance "${SAMPLE_INSTANCE_ID}" ${PROJECT_ID}` ); assert.match( - output, + createInstanceOutput, new RegExp( `Waiting for operation on ${SAMPLE_INSTANCE_ID} to complete...` ) ); assert.match( - output, + createInstanceOutput, new RegExp(`Created instance ${SAMPLE_INSTANCE_ID}.`) ); + + const updateInstanceOutput = execSync( + `node instance-update "${SAMPLE_INSTANCE_ID}" ${PROJECT_ID}` + ); + assert.match( + updateInstanceOutput, + new RegExp( + `Waiting for operation on ${SAMPLE_INSTANCE_ID} to complete...` + ) + ); + assert.match( + updateInstanceOutput, + new RegExp(`Updated instance ${SAMPLE_INSTANCE_ID}.`) + ); + assert.match( + updateInstanceOutput, + new RegExp( + `Instance ${SAMPLE_INSTANCE_ID} has been updated with the ENTERPRISE edition.` + ) + ); }); // create_instance_with_processing_units diff --git a/test/index.ts b/test/index.ts index a2421bf9d..9be38bb6f 100644 --- a/test/index.ts +++ b/test/index.ts @@ -779,6 +779,19 @@ describe('Spanner', () => { spanner.createInstance(NAME, config, assert.ifError); }); + it('should accept the edition', done => { + const edition = + protos.google.spanner.admin.instance.v1.Instance.Edition.STANDARD; + const config = Object.assign({}, CONFIG, {edition}); + + spanner.request = config => { + assert.strictEqual(config.reqOpts.instance.edition, edition); + done(); + }; + + spanner.createInstance(NAME, config, assert.ifError); + }); + it('should create an instance with processing units', done => { const processingUnits = 500; const config = Object.assign({}, CONFIG, {processingUnits});