Skip to content

Commit 3300ab5

Browse files
authored
chore(samples): add samples for spanner editions (#2102)
This PR contains create and update samples for Spanner Editions.
1 parent d8ab278 commit 3300ab5

File tree

6 files changed

+148
-6
lines changed

6 files changed

+148
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre
133133
| 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) |
134134
| 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) |
135135
| 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) |
136+
| 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) |
136137
| 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) |
137138
| 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) |
138139
| 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) |

samples/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ and automatic, synchronous replication for high availability.
5858
* [Lists the instance configuration operations.](#lists-the-instance-configuration-operations.)
5959
* [Updates a user-managed instance configuration.](#updates-a-user-managed-instance-configuration.)
6060
* [Creates a new instance partition](#creates-a-new-instance-partition)
61+
* [Updates an instance.](#updates-an-instance.)
6162
* [Creates a instance with autoscaling config.](#creates-a-instance-with-autoscaling-config.)
6263
* [Instance-with-processing-units](#instance-with-processing-units)
6364
* [Instance](#instance)
@@ -883,6 +884,23 @@ __Usage:__
883884

884885

885886

887+
### Updates an instance.
888+
889+
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-update.js).
890+
891+
[![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)
892+
893+
__Usage:__
894+
895+
896+
`node instance-update.js <INSTANCE_ID> <PROJECT_ID>`
897+
898+
899+
-----
900+
901+
902+
903+
886904
### Creates a instance with autoscaling config.
887905

888906
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-with-autoscaling-config.js).

samples/instance-update.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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));

samples/instance.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async function createInstance(instanceId, projectId) {
2020
// [START spanner_create_instance]
2121

2222
// Imports the Google Cloud client library
23-
const {Spanner} = require('@google-cloud/spanner');
23+
const {Spanner, protos} = require('@google-cloud/spanner');
2424

2525
// Creates a client
2626
const spanner = new Spanner({
@@ -56,6 +56,8 @@ async function createInstance(instanceId, projectId) {
5656
cloud_spanner_samples: 'true',
5757
created: Math.round(Date.now() / 1000).toString(), // current time
5858
},
59+
edition:
60+
protos.google.spanner.admin.instance.v1.Instance.Edition.STANDARD, //optional
5961
},
6062
});
6163

samples/system-test/spanner.test.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,21 +259,41 @@ describe('Autogenerated Admin Clients', () => {
259259
await sample_instance.delete();
260260
});
261261

262-
// create_instance
263-
it('should create an example instance', async () => {
264-
const output = execSync(
262+
// create_and_update_instance
263+
it('should create and update an example instance with spanner editions', async () => {
264+
const createInstanceOutput = execSync(
265265
`${instanceCmd} createInstance "${SAMPLE_INSTANCE_ID}" ${PROJECT_ID}`
266266
);
267267
assert.match(
268-
output,
268+
createInstanceOutput,
269269
new RegExp(
270270
`Waiting for operation on ${SAMPLE_INSTANCE_ID} to complete...`
271271
)
272272
);
273273
assert.match(
274-
output,
274+
createInstanceOutput,
275275
new RegExp(`Created instance ${SAMPLE_INSTANCE_ID}.`)
276276
);
277+
278+
const updateInstanceOutput = execSync(
279+
`node instance-update "${SAMPLE_INSTANCE_ID}" ${PROJECT_ID}`
280+
);
281+
assert.match(
282+
updateInstanceOutput,
283+
new RegExp(
284+
`Waiting for operation on ${SAMPLE_INSTANCE_ID} to complete...`
285+
)
286+
);
287+
assert.match(
288+
updateInstanceOutput,
289+
new RegExp(`Updated instance ${SAMPLE_INSTANCE_ID}.`)
290+
);
291+
assert.match(
292+
updateInstanceOutput,
293+
new RegExp(
294+
`Instance ${SAMPLE_INSTANCE_ID} has been updated with the ENTERPRISE edition.`
295+
)
296+
);
277297
});
278298

279299
// create_instance_with_processing_units

test/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,19 @@ describe('Spanner', () => {
779779
spanner.createInstance(NAME, config, assert.ifError);
780780
});
781781

782+
it('should accept the edition', done => {
783+
const edition =
784+
protos.google.spanner.admin.instance.v1.Instance.Edition.STANDARD;
785+
const config = Object.assign({}, CONFIG, {edition});
786+
787+
spanner.request = config => {
788+
assert.strictEqual(config.reqOpts.instance.edition, edition);
789+
done();
790+
};
791+
792+
spanner.createInstance(NAME, config, assert.ifError);
793+
});
794+
782795
it('should create an instance with processing units', done => {
783796
const processingUnits = 500;
784797
const config = Object.assign({}, CONFIG, {processingUnits});

0 commit comments

Comments
 (0)