Skip to content

Commit

Permalink
chore(samples): Add samples for Cloud Spanner Default Backup Schedules (
Browse files Browse the repository at this point in the history
#2190)

* chore(samples): Add samples for Cloud Spanner Default Backup Schedules

* chore(samples): Add samples for Cloud Spanner Default Backup Schedules

* chore(samples): Add samples for Cloud Spanner Default Backup Schedules

* chore(samples): Add samples for Cloud Spanner Default Backup Schedules

* chore(samples): Add samples for Cloud Spanner Default Backup Schedules

* chore(samples): Add samples for Cloud Spanner Default Backup Schedules.
Fix lint issues

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
Sally-Ye and gcf-owl-bot[bot] authored Nov 15, 2024
1 parent 1be3d5d commit 5dbe3d7
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down Expand Up @@ -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) |



Expand Down
36 changes: 36 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -2131,6 +2150,23 @@ __Usage:__
`node update-backup-schedule.js <PROJECT_ID> <INSTANCE_ID> <DATABASE_ID> <SCHEDULE_ID>`


-----




### 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 <INSTANCE_ID> <PROJECT_ID>`





Expand Down
73 changes: 73 additions & 0 deletions samples/create-instance-without-default-backup-schedules.js
Original file line number Diff line number Diff line change
@@ -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));
27 changes: 27 additions & 0 deletions samples/system-test/spanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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(
Expand Down
74 changes: 74 additions & 0 deletions samples/update-instance-default-backup-schedule-type.js
Original file line number Diff line number Diff line change
@@ -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 <INSTANCE_ID> <PROJECT_ID>

'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));

0 comments on commit 5dbe3d7

Please sign in to comment.