Skip to content

Commit a7112b7

Browse files
author
Joanna Grycz
committed
feat: compute_snapshot_schedule_edit
1 parent 26e9cc5 commit a7112b7

File tree

5 files changed

+137
-5
lines changed

5 files changed

+137
-5
lines changed

compute/snapshotSchedule/createSnapshotSchedule.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async function main(snapshotScheduleName, region) {
3434
const projectId = await resourcePoliciesClient.getProjectId();
3535

3636
// The location of the snapshot schedule resource policy.
37-
// region = 'europe-central2';
37+
// region = 'us-central1';
3838

3939
// The name of the snapshot schedule.
4040
// snapshotScheduleName = 'snapshot-schedule-name';
@@ -58,6 +58,7 @@ async function main(snapshotScheduleName, region) {
5858
schedule: new compute.ResourcePolicySnapshotSchedulePolicySchedule({
5959
// Similarly, you can create a weekly or monthly schedule.
6060
// Review the resourcePolicies.insert method for details specific to setting a weekly or monthly schedule.
61+
// To see more details, open: `https://cloud.google.com/compute/docs/disks/scheduled-snapshots?authuser=0#create_snapshot_schedule`
6162
dailySchedule: new compute.ResourcePolicyDailyCycle({
6263
startTime: '12:00',
6364
daysInCycle: 1,

compute/snapshotSchedule/deleteSnapshotSchedule.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async function main(snapshotScheduleName, region) {
3333
const projectId = await resourcePoliciesClient.getProjectId();
3434

3535
// The location of the snapshot schedule resource policy.
36-
// region = 'europe-central2';
36+
// region = 'us-central1';
3737

3838
// The name of the snapshot schedule.
3939
// snapshotScheduleName = 'snapshot-schedule-name'
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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_edit]
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 = 'us-central1';
38+
39+
// The name of the snapshot schedule.
40+
// snapshotScheduleName = 'snapshot-schedule-name';
41+
42+
async function callEditSnapshotSchedule() {
43+
const [response] = await resourcePoliciesClient.patch({
44+
project: projectId,
45+
region,
46+
resourcePolicy: snapshotScheduleName,
47+
resourcePolicyResource: compute.ResourcePolicy({
48+
snapshotSchedulePolicy:
49+
compute.ResourcePolicyInstanceSchedulePolicySchedule({
50+
schedule: compute.ResourcePolicySnapshotSchedulePolicySchedule({
51+
weeklySchedule: compute.ResourcePolicyWeeklyCycle({
52+
dayOfWeeks: [
53+
compute.ResourcePolicyWeeklyCycleDayOfWeek({
54+
day: 'Tuesday',
55+
startTime: '9:00',
56+
}),
57+
],
58+
}),
59+
}),
60+
}),
61+
}),
62+
});
63+
64+
let operation = response.latestResponse;
65+
66+
// Wait for the edit operation to complete.
67+
while (operation.status !== 'DONE') {
68+
[operation] = await regionOperationsClient.wait({
69+
operation: operation.name,
70+
project: projectId,
71+
region,
72+
});
73+
}
74+
75+
console.log(`Snapshot schedule: ${snapshotScheduleName} edited.`);
76+
}
77+
78+
await callEditSnapshotSchedule();
79+
// [END compute_snapshot_schedule_edit]
80+
}
81+
82+
main(...process.argv.slice(2)).catch(err => {
83+
console.error(err);
84+
process.exitCode = 1;
85+
});

compute/snapshotSchedule/getSnapshotSchedule.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async function main(snapshotScheduleName, region) {
3131
const projectId = await resourcePoliciesClient.getProjectId();
3232

3333
// The location of the snapshot schedule resource policy.
34-
// region = 'europe-central2';
34+
// region = 'us-central1';
3535

3636
// The name of the snapshot schedule.
3737
// snapshotScheduleName = 'snapshot-schedule-name';
@@ -43,7 +43,7 @@ async function main(snapshotScheduleName, region) {
4343
resourcePolicy: snapshotScheduleName,
4444
});
4545

46-
console.log(response);
46+
console.log(JSON.stringify(response));
4747
}
4848

4949
await callGetSnapshotSchedule();

compute/test/snapshotSchedule.test.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2626
const cwd = path.join(__dirname, '..');
2727

2828
describe('Snapshot schedule', async () => {
29-
const region = 'europe-central2';
29+
const region = 'us-central1';
3030

3131
it('should create snapshot schedule', () => {
3232
const snapshotScheduleName = `snapshot-schedule-name-${uuid.v4()}`;
@@ -79,6 +79,52 @@ describe('Snapshot schedule', async () => {
7979
);
8080
});
8181

82+
it('should edit snapshot schedule', () => {
83+
const snapshotScheduleName = `snapshot-schedule-name-${uuid.v4()}`;
84+
// Create snapshot
85+
execSync(
86+
`node ./snapshotSchedule/createSnapshotSchedule.js ${snapshotScheduleName} ${region}`,
87+
{
88+
cwd,
89+
}
90+
);
91+
const currentSchedule = JSON.parse(
92+
execSync(
93+
`node ./snapshotSchedule/getSnapshotSchedule.js ${snapshotScheduleName} ${region}`,
94+
{
95+
cwd,
96+
}
97+
)
98+
).snapshotSchedulePolicy.schedule;
99+
100+
// Edit snapshot schedule
101+
execSync(
102+
`node ./snapshotSchedule/editSnapshotSchedule.js ${snapshotScheduleName} ${region}`,
103+
{
104+
cwd,
105+
}
106+
);
107+
108+
const updatedSchedule = JSON.parse(
109+
execSync(
110+
`node ./snapshotSchedule/getSnapshotSchedule.js ${snapshotScheduleName} ${region}`,
111+
{
112+
cwd,
113+
}
114+
)
115+
).snapshotSchedulePolicy.schedule;
116+
117+
assert.notStrictEqual(currentSchedule, updatedSchedule);
118+
119+
// Delete resource
120+
execSync(
121+
`node ./snapshotSchedule/deleteSnapshotSchedule.js ${snapshotScheduleName} ${region}`,
122+
{
123+
cwd,
124+
}
125+
);
126+
});
127+
82128
it('should delete snapshot schedule', () => {
83129
const snapshotScheduleName = `snapshot-schedule-name-${uuid.v4()}`;
84130
// Create snapshot

0 commit comments

Comments
 (0)