Skip to content

Commit b91e284

Browse files
authored
feat(spanner): add samples for instance partitions (#2083)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://togithub.com/googleapis/nodejs-spanner/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes #<issue_number_goes_here> 🦕
1 parent 217d256 commit b91e284

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre
132132
| 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) |
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) |
135+
| 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) |
135136
| 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) |
136137
| 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) |
137138
| 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
@@ -57,6 +57,7 @@ and automatic, synchronous replication for high availability.
5757
* [Deletes a user-managed instance configuration.](#deletes-a-user-managed-instance-configuration.)
5858
* [Lists the instance configuration operations.](#lists-the-instance-configuration-operations.)
5959
* [Updates a user-managed instance configuration.](#updates-a-user-managed-instance-configuration.)
60+
* [Creates a new instance partition](#creates-a-new-instance-partition)
6061
* [Creates a instance with autoscaling config.](#creates-a-instance-with-autoscaling-config.)
6162
* [Instance-with-processing-units](#instance-with-processing-units)
6263
* [Instance](#instance)
@@ -865,6 +866,23 @@ __Usage:__
865866

866867

867868

869+
### Creates a new instance partition
870+
871+
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-partition-create.js).
872+
873+
[![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)
874+
875+
__Usage:__
876+
877+
878+
`node instance-partition-create.js <INSTANCE_ID> <INSTANCE_PARTITION_ID> <PROJECT_ID>`
879+
880+
881+
-----
882+
883+
884+
885+
868886
### Creates a instance with autoscaling config.
869887

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

samples/instance-partition-create.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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: Creates a new instance partition
18+
// usage: node instance-partition-create.js <INSTANCE_ID> <INSTANCE_PARTITION_ID> <PROJECT_ID>
19+
20+
'use strict';
21+
22+
function main(
23+
instanceId = 'my-instance',
24+
instancePartitionId = 'my-instance-partition',
25+
projectId = 'my-project-id'
26+
) {
27+
async function createInstancePartition() {
28+
// [START spanner_create_instance_partition]
29+
// Imports the Google Cloud client library
30+
const {Spanner} = require('@google-cloud/spanner');
31+
32+
/**
33+
* TODO(developer): Uncomment the following lines before running the sample.
34+
*/
35+
// const projectId = 'my-project-id';
36+
// const instanceId = 'my-instance';
37+
// const instancePartitionId = 'my-instance-partition';
38+
39+
// Creates a client
40+
const spanner = new Spanner({
41+
projectId: projectId,
42+
});
43+
44+
// Get the instance admin client
45+
const instanceAdminClient = spanner.getInstanceAdminClient();
46+
47+
// Creates a new instance partition
48+
try {
49+
console.log(
50+
`Creating instance partition ${instanceAdminClient.instancePartitionPath(
51+
projectId,
52+
instanceId,
53+
instancePartitionId
54+
)}.`
55+
);
56+
const [operation] = await instanceAdminClient.createInstancePartition({
57+
instancePartitionId: instancePartitionId,
58+
parent: instanceAdminClient.instancePath(projectId, instanceId),
59+
instancePartition: {
60+
config: instanceAdminClient.instanceConfigPath(projectId, 'nam3'),
61+
nodeCount: 1,
62+
displayName: 'Test instance partition',
63+
},
64+
});
65+
66+
console.log(
67+
`Waiting for operation on ${instancePartitionId} to complete...`
68+
);
69+
await operation.promise();
70+
71+
console.log(`Created instance partition ${instancePartitionId}.`);
72+
} catch (err) {
73+
console.error('ERROR:', err);
74+
}
75+
// [END spanner_create_instance_partition]
76+
}
77+
createInstancePartition();
78+
}
79+
80+
process.on('unhandledRejection', err => {
81+
console.error(err.message);
82+
process.exitCode = 1;
83+
});
84+
main(...process.argv.slice(2));

samples/system-test/spanner.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,44 @@ describe('Autogenerated Admin Clients', () => {
17321732
});
17331733
});
17341734

1735+
describe('instance partition', () => {
1736+
before(async () => {
1737+
const instance = spanner.instance(SAMPLE_INSTANCE_ID);
1738+
const [, operation] = await instance.create({
1739+
config: 'regional-us-central1',
1740+
nodes: 1,
1741+
displayName: 'Instance partitions test',
1742+
labels: {
1743+
['cloud_spanner_samples']: 'true',
1744+
created: Math.round(Date.now() / 1000).toString(), // current time
1745+
},
1746+
});
1747+
await operation.promise();
1748+
});
1749+
1750+
after(async () => {
1751+
const instance = spanner.instance(SAMPLE_INSTANCE_ID);
1752+
await instance.delete();
1753+
});
1754+
1755+
// create_instance_partition
1756+
it('should create an instance partition', async () => {
1757+
const output = execSync(
1758+
`node instance-partition-create.js "${SAMPLE_INSTANCE_ID}" "my-instance-partition" "${PROJECT_ID}"`
1759+
);
1760+
assert.match(
1761+
output,
1762+
new RegExp(
1763+
'Waiting for operation on my-instance-partition to complete...'
1764+
)
1765+
);
1766+
assert.match(
1767+
output,
1768+
new RegExp('Created instance partition my-instance-partition.')
1769+
);
1770+
});
1771+
});
1772+
17351773
describe('postgreSQL', () => {
17361774
before(async () => {
17371775
const instance = spanner.instance(SAMPLE_INSTANCE_ID);

0 commit comments

Comments
 (0)