diff --git a/README.md b/README.md index ae0b2e06c..664bfd536 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre | Batch | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/batch.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/batch.js,samples/README.md) | | Creates a full backup schedule | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/create-full-backup-schedule.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/create-full-backup-schedule.js,samples/README.md) | | Creates an incremental backup schedule | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/create-incremental-backup-schedule.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/create-incremental-backup-schedule.js,samples/README.md) | +| Create-instance-without-default-backup-schedules | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/create-instance-without-default-backup-schedules.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/create-instance-without-default-backup-schedules.js,samples/README.md) | | CRUD | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/crud.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/crud.js,samples/README.md) | | Creates a new database with a specific default leader | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/database-create-with-default-leader.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/database-create-with-default-leader.js,samples/README.md) | | Database-create-with-encryption-key | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/database-create-with-encryption-key.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/database-create-with-encryption-key.js,samples/README.md) | @@ -206,6 +207,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre | Executes a read/write transaction with transaction and request tags | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/transaction-tag.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/transaction-tag.js,samples/README.md) | | Transaction | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/transaction.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/transaction.js,samples/README.md) | | Updates a backup schedule | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/update-backup-schedule.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/update-backup-schedule.js,samples/README.md) | +| Updates an instance. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/update-instance-default-backup-schedule-type.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/update-instance-default-backup-schedule-type.js,samples/README.md) | diff --git a/samples/README.md b/samples/README.md index 5e6612328..47b555e26 100644 --- a/samples/README.md +++ b/samples/README.md @@ -34,6 +34,7 @@ and automatic, synchronous replication for high availability. * [Batch](#batch) * [Creates a full backup schedule](#creates-a-full-backup-schedule) * [Creates an incremental backup schedule](#creates-an-incremental-backup-schedule) + * [Create-instance-without-default-backup-schedules](#create-instance-without-default-backup-schedules) * [CRUD](#crud) * [Creates a new database with a specific default leader](#creates-a-new-database-with-a-specific-default-leader) * [Database-create-with-encryption-key](#database-create-with-encryption-key) @@ -131,6 +132,7 @@ and automatic, synchronous replication for high availability. * [Executes a read/write transaction with transaction and request tags](#executes-a-read/write-transaction-with-transaction-and-request-tags) * [Transaction](#transaction) * [Updates a backup schedule](#updates-a-backup-schedule) + * [Updates an instance.](#updates-an-instance.) ## Before you begin @@ -487,6 +489,23 @@ __Usage:__ +### Create-instance-without-default-backup-schedules + +View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/create-instance-without-default-backup-schedules.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/create-instance-without-default-backup-schedules.js,samples/README.md) + +__Usage:__ + + +`node samples/create-instance-without-default-backup-schedules.js` + + +----- + + + + ### CRUD View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/crud.js). @@ -2131,6 +2150,23 @@ __Usage:__ `node update-backup-schedule.js ` +----- + + + + +### Updates an instance. + +View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/update-instance-default-backup-schedule-type.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/update-instance-default-backup-schedule-type.js,samples/README.md) + +__Usage:__ + + +`node instance-update.js ` + + diff --git a/samples/create-instance-without-default-backup-schedules.js b/samples/create-instance-without-default-backup-schedules.js new file mode 100644 index 000000000..b464f4df6 --- /dev/null +++ b/samples/create-instance-without-default-backup-schedules.js @@ -0,0 +1,73 @@ +/** + * 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. + */ + +'use strict'; + +function main(instanceId, projectId) { + async function createInstanceWithoutDefaultBackupSchedules() { + // [START spanner_create_instance_without_default_backup_schedule] + /** + * TODO(developer): Uncomment the following lines before running the sample. + **/ + // const projectId = 'my-project-id'; + // const instanceId = 'my-instance'; + + // Imports the Google Cloud client library + const {Spanner, protos} = require('@google-cloud/spanner'); + + // Creates a client + const spanner = new Spanner({ + projectId: projectId, + }); + + const instanceAdminClient = await spanner.getInstanceAdminClient(); + // Creates a new instance + try { + const [operation] = await instanceAdminClient.createInstance({ + instanceId: instanceId, + parent: instanceAdminClient.projectPath(projectId), + instance: { + config: instanceAdminClient.instanceConfigPath( + projectId, + 'regional-me-central2' + ), + nodeCount: 1, + displayName: 'Display name for the instance.', + labels: { + cloud_spanner_samples: 'true', + created: Math.round(Date.now() / 1000).toString(), // current time + }, + defaultBackupScheduleType: + protos.google.spanner.admin.instance.v1.Instance + .DefaultBackupScheduleType.NONE, + }, + }); + await operation.promise(); + + console.log( + `Created instance ${instanceId} without default backup schedules.` + ); + } catch (err) { + console.error('ERROR:', err); + } + // [END spanner_create_instance_without_default_backup_schedule] + } + createInstanceWithoutDefaultBackupSchedules(); +} +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/samples/system-test/spanner.test.js b/samples/system-test/spanner.test.js index 0abf87a3a..6e589e49b 100644 --- a/samples/system-test/spanner.test.js +++ b/samples/system-test/spanner.test.js @@ -46,6 +46,10 @@ const backupsCmd = 'node backups.js'; const instanceCmd = 'node instance.js'; const createTableWithForeignKeyDeleteCascadeCommand = 'node table-create-with-foreign-key-delete-cascade.js'; +const createInstanceWithoutDefaultBackupSchedulesCommand = + 'node create-instance-without-default-backup-schedules.js'; +const updateInstanceDefaultBackupScheduleTypeCommand = + 'node update-instance-default-backup-schedule-type.js'; const alterTableWithForeignKeyDeleteCascadeCommand = 'node table-alter-with-foreign-key-delete-cascade.js'; const dropForeignKeyConstraintDeleteCascaseCommand = @@ -299,6 +303,29 @@ describe('Autogenerated Admin Clients', () => { ); }); + // create_and_update_instance_default_backup_schedule_type + it('should create an example instance without default backup schedule type and update the instance to have it', async () => { + const createInstanceOutput = execSync( + `${createInstanceWithoutDefaultBackupSchedulesCommand} "${SAMPLE_INSTANCE_ID}" ${PROJECT_ID}` + ); + assert.match( + createInstanceOutput, + new RegExp( + `Created instance ${SAMPLE_INSTANCE_ID} without default backup schedules.` + ) + ); + + const updateInstanceOutput = execSync( + `${updateInstanceDefaultBackupScheduleTypeCommand} "${SAMPLE_INSTANCE_ID}" ${PROJECT_ID}` + ); + assert.match( + updateInstanceOutput, + new RegExp( + `Instance ${SAMPLE_INSTANCE_ID} has been updated with the AUTOMATIC default backup schedule type.` + ) + ); + }); + // create_instance_with_processing_units it('should create an example instance with processing units', async () => { const output = execSync( diff --git a/samples/update-instance-default-backup-schedule-type.js b/samples/update-instance-default-backup-schedule-type.js new file mode 100644 index 000000000..89bd07cc3 --- /dev/null +++ b/samples/update-instance-default-backup-schedule-type.js @@ -0,0 +1,74 @@ +/** + * 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 + +'use strict'; + +function main(instanceId, projectId) { + async function updateInstanceDefaultBackupScheduleType() { + // [START spanner_update_instance_default_backup_schedule_type] + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = 'my-project-id'; + // const instanceId = 'my-instance'; + + // Imports the Google Cloud client library + const {Spanner, protos} = require('@google-cloud/spanner'); + + // Creates a client + const spanner = new Spanner({ + projectId: projectId, + }); + const instanceAdminClient = await spanner.getInstanceAdminClient(); + + // Updates an instance + try { + const [operation] = await instanceAdminClient.updateInstance({ + instance: { + name: instanceAdminClient.instancePath(projectId, instanceId), + defaultBackupScheduleType: + protos.google.spanner.admin.instance.v1.Instance + .DefaultBackupScheduleType.AUTOMATIC, // optional + }, + // Field mask specifying fields that should get updated in an Instance + fieldMask: (protos.google.protobuf.FieldMask = { + paths: ['default_backup_schedule_type'], + }), + }); + + await operation.promise(); + const [metadata] = await instanceAdminClient.getInstance({ + name: instanceAdminClient.instancePath(projectId, instanceId), + }); + console.log( + `Instance ${instanceId} has been updated with the ${metadata.defaultBackupScheduleType}` + + ' default backup schedule type.' + ); + } catch (err) { + console.error('ERROR:', err); + } + // [END spanner_update_instance_default_backup_schedule_type] + } + updateInstanceDefaultBackupScheduleType(); +} +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2));