Skip to content

Commit 7a34d18

Browse files
author
Joanna Grycz
committed
feat: compute_snapshot_schedule_create/delete
1 parent 06b3e8c commit 7a34d18

File tree

3 files changed

+248
-0
lines changed

3 files changed

+248
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
async function main(snapshotScheduleName, region) {
20+
// [START compute_snapshot_schedule_create]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
const compute = computeLib.protos.google.cloud.compute.v1;
24+
25+
// Instantiate a resourcePoliciesClient
26+
const resourcePoliciesClient = new computeLib.ResourcePoliciesClient();
27+
// Instantiate a regionOperationsClient
28+
const regionOperationsClient = new computeLib.RegionOperationsClient();
29+
30+
/**
31+
* TODO(developer): Update/uncomment these variables before running the sample.
32+
*/
33+
// The project name.
34+
const projectId = await resourcePoliciesClient.getProjectId();
35+
36+
// The location of the snapshot schedule resource policy.
37+
// region = 'europe-central2';
38+
39+
// The name of the snapshot schedule.
40+
// snapshotScheduleName = 'snapshot-schedule-name';
41+
42+
// The description of the snapshot schedule.
43+
const snapshotScheduleDescription = 'snapshot schedule description...';
44+
45+
async function callCreateSnapshotSchedule() {
46+
const [response] = await resourcePoliciesClient.insert({
47+
project: projectId,
48+
region,
49+
resourcePolicyResource: new compute.ResourcePolicy({
50+
name: snapshotScheduleName,
51+
description: snapshotScheduleDescription,
52+
snapshotSchedulePolicy:
53+
new compute.ResourcePolicyInstanceSchedulePolicySchedule({
54+
retentionPolicy:
55+
new compute.ResourcePolicySnapshotSchedulePolicyRetentionPolicy({
56+
maxRetentionDays: 5,
57+
}),
58+
schedule: new compute.ResourcePolicySnapshotSchedulePolicySchedule({
59+
// Similarly, you can create a weekly or monthly schedule.
60+
// Review the resourcePolicies.insert method for details specific to setting a weekly or monthly schedule.
61+
dailySchedule: new compute.ResourcePolicyDailyCycle({
62+
startTime: '12:00',
63+
daysInCycle: 1,
64+
}),
65+
}),
66+
snapshotProperties:
67+
new compute.ResourcePolicySnapshotSchedulePolicySnapshotProperties(
68+
{
69+
guestFlush: false,
70+
labels: {
71+
env: 'dev',
72+
media: 'images',
73+
},
74+
// OPTIONAL: the storage location. If you omit this flag, the default storage location is used.
75+
// storageLocations: 'storage-location',
76+
}
77+
),
78+
}),
79+
}),
80+
});
81+
82+
let operation = response.latestResponse;
83+
84+
// Wait for the create operation to complete.
85+
while (operation.status !== 'DONE') {
86+
[operation] = await regionOperationsClient.wait({
87+
operation: operation.name,
88+
project: projectId,
89+
region,
90+
});
91+
}
92+
93+
console.log(`Snapshot schedule: ${snapshotScheduleName} created.`);
94+
}
95+
96+
await callCreateSnapshotSchedule();
97+
// [END compute_snapshot_schedule_create]
98+
}
99+
100+
main(...process.argv.slice(2)).catch(err => {
101+
console.error(err);
102+
process.exitCode = 1;
103+
});
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
async function main(snapshotScheduleName, region) {
20+
// [START compute_snapshot_schedule_delete]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
24+
// Instantiate a resourcePoliciesClient
25+
const resourcePoliciesClient = new computeLib.ResourcePoliciesClient();
26+
// Instantiate a regionOperationsClient
27+
const regionOperationsClient = new computeLib.RegionOperationsClient();
28+
29+
/**
30+
* TODO(developer): Update/uncomment these variables before running the sample.
31+
*/
32+
// The project name.
33+
const projectId = await resourcePoliciesClient.getProjectId();
34+
35+
// The location of the snapshot schedule resource policy.
36+
// region = 'europe-central2';
37+
38+
// The name of the snapshot schedule.
39+
// snapshotScheduleName = 'snapshot-schedule-name'
40+
41+
async function callDeleteSnapshotSchedule() {
42+
// If the snapshot schedule is already attached to a disk, you will receive an error.
43+
const [response] = await resourcePoliciesClient.delete({
44+
project: projectId,
45+
region,
46+
resourcePolicy: snapshotScheduleName,
47+
});
48+
49+
let operation = response.latestResponse;
50+
51+
// Wait for the delete operation to complete.
52+
while (operation.status !== 'DONE') {
53+
[operation] = await regionOperationsClient.wait({
54+
operation: operation.name,
55+
project: projectId,
56+
region,
57+
});
58+
}
59+
60+
console.log(`Snapshot schedule: ${snapshotScheduleName} deleted.`);
61+
}
62+
63+
await callDeleteSnapshotSchedule();
64+
// [END compute_snapshot_schedule_delete]
65+
}
66+
67+
main(...process.argv.slice(2)).catch(err => {
68+
console.error(err);
69+
process.exitCode = 1;
70+
});
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
const path = require('path');
20+
const assert = require('node:assert/strict');
21+
const {describe, it} = require('mocha');
22+
const cp = require('child_process');
23+
const uuid = require('uuid');
24+
25+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
26+
const cwd = path.join(__dirname, '..');
27+
28+
describe('Snapshot schedule', async () => {
29+
const region = 'europe-central2';
30+
31+
it('should create snapshot schedule', () => {
32+
const snapshotScheduleName = `snapshot-schedule-name-${uuid.v4()}`;
33+
34+
const response = execSync(
35+
`node ./snapshotSchedule/createSnapshotSchedule.js ${snapshotScheduleName} ${region}`,
36+
{
37+
cwd,
38+
}
39+
);
40+
41+
assert(
42+
response.includes(`Snapshot schedule: ${snapshotScheduleName} created.`)
43+
);
44+
45+
// Delete resource
46+
execSync(
47+
`node ./snapshotSchedule/deleteSnapshotSchedule.js ${snapshotScheduleName} ${region}`,
48+
{
49+
cwd,
50+
}
51+
);
52+
});
53+
54+
it('should delete snapshot schedule', () => {
55+
const snapshotScheduleName = `snapshot-schedule-name-${uuid.v4()}`;
56+
// Create snapshot
57+
execSync(
58+
`node ./snapshotSchedule/createSnapshotSchedule.js ${snapshotScheduleName} ${region}`,
59+
{
60+
cwd,
61+
}
62+
);
63+
64+
const response = execSync(
65+
`node ./snapshotSchedule/deleteSnapshotSchedule.js ${snapshotScheduleName} ${region}`,
66+
{
67+
cwd,
68+
}
69+
);
70+
71+
assert(
72+
response.includes(`Snapshot schedule: ${snapshotScheduleName} deleted.`)
73+
);
74+
});
75+
});

0 commit comments

Comments
 (0)