Skip to content

Commit

Permalink
chore(samples): add samples for spanner editions (googleapis#2102)
Browse files Browse the repository at this point in the history
This PR contains create and update samples for Spanner Editions.
  • Loading branch information
alkatrivedi authored Sep 17, 2024
1 parent d8ab278 commit 3300ab5
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
18 changes: 18 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 <INSTANCE_ID> <PROJECT_ID>`


-----




### Creates a instance with autoscaling config.

View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-with-autoscaling-config.js).
Expand Down
88 changes: 88 additions & 0 deletions samples/instance-update.js
Original file line number Diff line number Diff line change
@@ -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 <INSTANCE_ID> <PROJECT_ID>

'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));
4 changes: 3 additions & 1 deletion samples/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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
},
});

Expand Down
30 changes: 25 additions & 5 deletions samples/system-test/spanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down

0 comments on commit 3300ab5

Please sign in to comment.