Skip to content

Commit bf16afd

Browse files
rajatbhattaolavloitegcf-owl-bot[bot]
authored
samples: add support for CMMR Phase 2 (#1718)
* feat: support customer managed instance configurations Co-authored-by: Knut Olav Løite <koloite@gmail.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent bbe8f69 commit bf16afd

File tree

7 files changed

+453
-0
lines changed

7 files changed

+453
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre
119119
| Reads data using an existing storing index. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/index-read-data-with-storing.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/index-read-data-with-storing.js,samples/README.md) |
120120
| Read data using an existing index. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/index-read-data.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/index-read-data.js,samples/README.md) |
121121
| Indexing | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/indexing.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/indexing.js,samples/README.md) |
122+
| Creates a user-managed instance configuration. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-config-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-config-create.js,samples/README.md) |
123+
| Deletes a user-managed instance configuration. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-config-delete.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-delete.js,samples/README.md) |
124+
| 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) |
125+
| 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) |
122126
| 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) |
123127
| 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) |
124128
| Json-add-column | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/json-add-column.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/json-add-column.js,samples/README.md) |

samples/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ and automatic, synchronous replication for high availability.
4444
* [Reads data using an existing storing index.](#reads-data-using-an-existing-storing-index.)
4545
* [Read data using an existing index.](#read-data-using-an-existing-index.)
4646
* [Indexing](#indexing)
47+
* [Creates a user-managed instance configuration.](#creates-a-user-managed-instance-configuration.)
48+
* [Deletes a user-managed instance configuration.](#deletes-a-user-managed-instance-configuration.)
49+
* [Lists the instance configuration operations.](#lists-the-instance-configuration-operations.)
50+
* [Updates a user-managed instance configuration.](#updates-a-user-managed-instance-configuration.)
4751
* [Instance-with-processing-units](#instance-with-processing-units)
4852
* [Instance](#instance)
4953
* [Json-add-column](#json-add-column)
@@ -609,6 +613,74 @@ __Usage:__
609613

610614

611615

616+
### Creates a user-managed instance configuration.
617+
618+
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-config-create.js).
619+
620+
[![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-create.js,samples/README.md)
621+
622+
__Usage:__
623+
624+
625+
`node instance-config-create <INSTANCE_CONFIG_ID> <BASE_INSTANCE_CONFIG_ID> <PROJECT_ID>`
626+
627+
628+
-----
629+
630+
631+
632+
633+
### Deletes a user-managed instance configuration.
634+
635+
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-config-delete.js).
636+
637+
[![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-delete.js,samples/README.md)
638+
639+
__Usage:__
640+
641+
642+
`node instance-config-delete <INSTANCE_CONFIG_ID> <PROJECT_ID>`
643+
644+
645+
-----
646+
647+
648+
649+
650+
### Lists the instance configuration operations.
651+
652+
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-config-get-operations.js).
653+
654+
[![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)
655+
656+
__Usage:__
657+
658+
659+
`node instance-config-get-operations <PROJECT_ID>`
660+
661+
662+
-----
663+
664+
665+
666+
667+
### Updates a user-managed instance configuration.
668+
669+
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-config-update.js).
670+
671+
[![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)
672+
673+
__Usage:__
674+
675+
676+
`node instance-config-update <INSTANCE_CONFIG_ID> <PROJECT_ID>`
677+
678+
679+
-----
680+
681+
682+
683+
612684
### Instance-with-processing-units
613685

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

samples/instance-config-create.js

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

samples/instance-config-delete.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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: Deletes a user-managed instance configuration.
18+
// usage: node instance-config-delete <INSTANCE_CONFIG_ID> <PROJECT_ID>
19+
20+
'use strict';
21+
22+
function main(
23+
instanceConfigId = 'custom-my-instance-config',
24+
projectId = 'my-project-id'
25+
) {
26+
// [START spanner_delete_instance_config]
27+
28+
/**
29+
* TODO(developer): Uncomment the following lines before running the sample.
30+
*/
31+
// const instanceConfigId = 'custom-my-instance-config-id';
32+
// const projectId = 'my-project-id';
33+
34+
// Imports the Google Cloud client library
35+
const {Spanner} = require('@google-cloud/spanner');
36+
37+
// Creates a client
38+
const spanner = new Spanner({
39+
projectId: projectId,
40+
});
41+
async function deleteInstanceConfig() {
42+
// Deletes an instance config.
43+
const instanceConfig = spanner.instanceConfig(instanceConfigId);
44+
try {
45+
// Delete the instance config.
46+
console.log(`Deleting ${instanceConfig.id}...\n`);
47+
await instanceConfig.delete();
48+
// Verify that the instance config no longer exists
49+
const exists = await instanceConfig.exists();
50+
if (exists) {
51+
console.error(
52+
'Error: Instance config ',
53+
instanceConfigId,
54+
' still exists'
55+
);
56+
} else {
57+
console.log(`Deleted instance config ${instanceConfigId}.\n`);
58+
}
59+
} catch (err) {
60+
console.error(
61+
'ERROR: Deleting instance config ',
62+
instanceConfigId,
63+
' failed with error message ',
64+
err
65+
);
66+
}
67+
}
68+
deleteInstanceConfig();
69+
// [END spanner_delete_instance_config]
70+
}
71+
72+
process.on('unhandledRejection', err => {
73+
console.error(err.message);
74+
process.exitCode = 1;
75+
});
76+
main(...process.argv.slice(2));
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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: Lists the instance configuration operations.
18+
// usage: node instance-config-get-operations <PROJECT_ID>
19+
20+
'use strict';
21+
22+
function main(projectId = 'my-project-id') {
23+
// [START spanner_list_instance_config_operations]
24+
25+
/**
26+
* TODO(developer): Uncomment the following lines before running the sample.
27+
*/
28+
// const projectId = 'my-project-id';
29+
30+
// Imports the Google Cloud client library
31+
const {Spanner, protos} = require('@google-cloud/spanner');
32+
33+
// Creates a client
34+
const spanner = new Spanner({
35+
projectId: projectId,
36+
});
37+
async function getInstanceConfigOperations() {
38+
// Lists the instance config operations.
39+
try {
40+
console.log(
41+
`Getting list of instance config operations on project ${projectId}...\n`
42+
);
43+
const [instanceConfigOperations] =
44+
await spanner.getInstanceConfigOperations({
45+
filter:
46+
'(metadata.@type=type.googleapis.com/google.spanner.admin.instance.v1.CreateInstanceConfigMetadata)',
47+
});
48+
console.log(
49+
`Available instance config operations for project ${projectId}:`
50+
);
51+
instanceConfigOperations.forEach(instanceConfigOperation => {
52+
const metadata = instanceConfigOperation.metadata;
53+
const instanceConfig =
54+
protos.google.spanner.admin.instance.v1.CreateInstanceConfigMetadata.decode(
55+
instanceConfigOperation.metadata.value
56+
).instanceConfig;
57+
console.log(
58+
`Instance config operation for ${instanceConfig.name} of type` +
59+
` ${metadata.type_url} has status ${
60+
instanceConfigOperation.done ? 'done' : 'running'
61+
}.`
62+
);
63+
});
64+
} catch (err) {
65+
console.error('ERROR:', err);
66+
}
67+
}
68+
getInstanceConfigOperations();
69+
// [END spanner_list_instance_config_operations]
70+
}
71+
72+
process.on('unhandledRejection', err => {
73+
console.error(err.message);
74+
process.exitCode = 1;
75+
});
76+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)