Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 149ac9e

Browse files
author
Joanna Grycz
committedDec 2, 2024·
feat: compute_snapshot_schedule_attach
1 parent 0469e1f commit 149ac9e

File tree

4 files changed

+371
-0
lines changed

4 files changed

+371
-0
lines changed
 
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, diskName, region, zone) {
20+
// [START compute_snapshot_schedule_attach]
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 disksClient
26+
const disksClient = new computeLib.DisksClient();
27+
// Instantiate a zoneOperationsClient
28+
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
29+
30+
/**
31+
* TODO(developer): Update/uncomment these variables before running the sample.
32+
*/
33+
// The project name.
34+
const projectId = await disksClient.getProjectId();
35+
36+
// The zone where the disk is located.
37+
// zone = 'europe-central2-a';
38+
39+
// The region where the snapshot schedule was created.
40+
// region = 'europe-central2';
41+
42+
// The name of the disk.
43+
// diskName = 'disk-name';
44+
45+
// The name of the snapshot schedule that you are applying to the disk.
46+
// snapshotScheduleName = 'snapshot-schedule-name';
47+
48+
// Attach schedule to an existing disk.
49+
async function callAttachSnapshotSchedule() {
50+
const [response] = await disksClient.addResourcePolicies({
51+
project: projectId,
52+
zone,
53+
disk: diskName,
54+
disksAddResourcePoliciesRequestResource:
55+
new compute.DisksAddResourcePoliciesRequest({
56+
resourcePolicies: [
57+
`projects/${projectId}/regions/${region}/resourcePolicies/${snapshotScheduleName}`,
58+
],
59+
}),
60+
});
61+
62+
let operation = response.latestResponse;
63+
64+
// Wait for the attach operation to complete.
65+
while (operation.status !== 'DONE') {
66+
[operation] = await zoneOperationsClient.wait({
67+
operation: operation.name,
68+
project: projectId,
69+
zone: operation.zone.split('/').pop(),
70+
});
71+
}
72+
73+
console.log(
74+
`Snapshot schedule: ${snapshotScheduleName} attached to disk: ${diskName}.`
75+
);
76+
}
77+
78+
await callAttachSnapshotSchedule();
79+
// [END compute_snapshot_schedule_attach]
80+
}
81+
82+
main(...process.argv.slice(2)).catch(err => {
83+
console.error(err);
84+
process.exitCode = 1;
85+
});
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+
});
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+
});
+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 {after, before, describe, it} = require('mocha');
22+
const cp = require('child_process');
23+
const uuid = require('uuid');
24+
const computeLib = require('@google-cloud/compute');
25+
26+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
27+
const cwd = path.join(__dirname, '..');
28+
29+
const disksClient = new computeLib.DisksClient();
30+
31+
async function createDisk(projectId, zone, diskName) {
32+
const [response] = await disksClient.insert({
33+
project: projectId,
34+
zone,
35+
diskResource: {
36+
name: diskName,
37+
},
38+
});
39+
let operation = response.latestResponse;
40+
const operationsClient = new computeLib.ZoneOperationsClient();
41+
42+
// Wait for the create operation to complete.
43+
while (operation.status !== 'DONE') {
44+
[operation] = await operationsClient.wait({
45+
operation: operation.name,
46+
project: projectId,
47+
zone: operation.zone.split('/').pop(),
48+
});
49+
}
50+
}
51+
52+
async function deleteDisk(projectId, zone, diskName) {
53+
const [response] = await disksClient.delete({
54+
project: projectId,
55+
zone,
56+
disk: diskName,
57+
});
58+
let operation = response.latestResponse;
59+
const operationsClient = new computeLib.ZoneOperationsClient();
60+
61+
// Wait for the delete operation to complete.
62+
while (operation.status !== 'DONE') {
63+
[operation] = await operationsClient.wait({
64+
operation: operation.name,
65+
project: projectId,
66+
zone: operation.zone.split('/').pop(),
67+
});
68+
}
69+
}
70+
71+
describe('Attach snapshot schedule', async () => {
72+
const snapshotScheduleName = `snapshot-schedule-name-${uuid.v4().split('-')[0]}`;
73+
const diskName = `disk-name-with-schedule-attached-${uuid.v4().split('-')[0]}`;
74+
const region = 'europe-central2';
75+
const zone = 'europe-central2-a';
76+
let projectId;
77+
78+
before(async () => {
79+
projectId = await disksClient.getProjectId();
80+
await createDisk(projectId, zone, diskName);
81+
execSync(
82+
`node ./snapshotSchedule/createSnapshotSchedule.js ${snapshotScheduleName} ${region}`,
83+
{
84+
cwd,
85+
}
86+
);
87+
});
88+
89+
after(async () => {
90+
await deleteDisk(projectId, zone, diskName);
91+
execSync(
92+
`node ./snapshotSchedule/deleteSnapshotSchedule.js ${snapshotScheduleName} ${region}`,
93+
{
94+
cwd,
95+
}
96+
);
97+
});
98+
99+
it('should attach snapshot schedule to disk', () => {
100+
const response = execSync(
101+
`node ./snapshotSchedule/attachSnapshotSchedule.js ${snapshotScheduleName} ${diskName} ${region} ${zone}`,
102+
{
103+
cwd,
104+
}
105+
);
106+
107+
assert(
108+
response.includes(
109+
`Snapshot schedule: ${snapshotScheduleName} attached to disk: ${diskName}.`
110+
)
111+
);
112+
});
113+
});

0 commit comments

Comments
 (0)
Please sign in to comment.