MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

spanner/instance-with-autoscaling-config.js:22–118  ·  view source on GitHub ↗
(instanceId = 'my-instance', projectId = 'my-project-id')

Source from the content-addressed store, hash-verified

20'use strict';
21
22function main(instanceId = 'my-instance', projectId = 'my-project-id') {
23 async function createInstanceWithAutoscalingConfig() {
24 // [START spanner_create_instance_with_autoscaling_config]
25 // Imports the Google Cloud client library
26 const {Spanner, protos} = require('@google-cloud/spanner');
27
28 /**
29 * TODO(developer): Uncomment the following lines before running the sample.
30 */
31 // const projectId = 'my-project-id';
32 // const instanceId = 'my-instance';
33
34 // Creates a client
35 const spanner = new Spanner({
36 projectId: projectId,
37 });
38
39 // Get the instance admin client
40 const instanceAdminClient = spanner.getInstanceAdminClient();
41
42 const autoscalingConfig =
43 protos.google.spanner.admin.instance.v1.AutoscalingConfig.create({
44 // Only one of minNodes/maxNodes or minProcessingUnits/maxProcessingUnits can be set.
45 autoscalingLimits:
46 protos.google.spanner.admin.instance.v1.AutoscalingConfig.AutoscalingLimits.create(
47 {
48 minNodes: 1,
49 maxNodes: 2,
50 }
51 ),
52 // highPriorityCpuUtilizationPercent and storageUtilizationPercent are both
53 // percentages and must lie between 0 and 100.
54 autoscalingTargets:
55 protos.google.spanner.admin.instance.v1.AutoscalingConfig.AutoscalingTargets.create(
56 {
57 highPriorityCpuUtilizationPercent: 65,
58 storageUtilizationPercent: 95,
59 }
60 ),
61 });
62
63 // Creates a new instance with autoscaling configuration
64 // When autoscalingConfig is enabled, nodeCount and processingUnits fields
65 // need not be specified.
66 try {
67 const instancePath = instanceAdminClient.instancePath(
68 projectId,
69 instanceId
70 );
71
72 console.log(`Creating instance ${instancePath}.`);
73
74 const [operation] = await instanceAdminClient.createInstance({
75 instanceId: instanceId,
76 parent: instanceAdminClient.projectPath(projectId),
77 instance: {
78 config: instanceAdminClient.instanceConfigPath(
79 projectId,

Tested by

no test coverage detected