Skip to content

Commit 0397137

Browse files
author
Joanna Grycz
committed
feat: compute_snapshot_schedule_list
1 parent d9523da commit 0397137

6 files changed

+127
-8
lines changed

compute/snapshotSchedule/attachSnapshotSchedule.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ async function main(snapshotScheduleName, diskName, region, zone) {
3333
// The project name.
3434
const projectId = await disksClient.getProjectId();
3535

36-
// The zone where the disk is located.
37-
// zone = 'europe-central2-a';
38-
3936
// The region where the snapshot schedule was created.
40-
// region = 'europe-central2';
37+
// region = 'us-central1';
38+
39+
// The zone where the disk is located.
40+
// zone = `${region}-f`;
4141

4242
// The name of the disk.
4343
// diskName = 'disk-name';

compute/snapshotSchedule/createSnapshotSchedule.js

+1-1
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';

compute/snapshotSchedule/deleteSnapshotSchedule.js

+1-1
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'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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(region) {
20+
// [START compute_snapshot_schedule_list]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
24+
// Instantiate a resourcePoliciesClient
25+
const resourcePoliciesClient = new computeLib.ResourcePoliciesClient();
26+
27+
/**
28+
* TODO(developer): Update/uncomment these variables before running the sample.
29+
*/
30+
// The project name.
31+
const projectId = await resourcePoliciesClient.getProjectId();
32+
33+
// The region of the snapshot schedules.
34+
// region = 'us-central1';
35+
36+
async function callSnapshotScheduleList() {
37+
const aggListRequest = resourcePoliciesClient.aggregatedListAsync({
38+
project: projectId,
39+
filter: `region = "https://www.googleapis.com/compute/v1/projects/${projectId}/regions/${region}"`,
40+
});
41+
const result = [];
42+
43+
for await (const [, listObject] of aggListRequest) {
44+
const {resourcePolicies} = listObject;
45+
if (resourcePolicies.length > 0) {
46+
result.push(...resourcePolicies);
47+
}
48+
}
49+
50+
console.log(JSON.stringify(result));
51+
}
52+
53+
await callSnapshotScheduleList();
54+
// [END compute_snapshot_schedule_list]
55+
}
56+
57+
main(...process.argv.slice(2)).catch(err => {
58+
console.error(err);
59+
process.exitCode = 1;
60+
});

compute/test/attachSnapshotSchedule.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ async function deleteDisk(projectId, zone, diskName) {
7171
describe('Attach snapshot schedule', async () => {
7272
const snapshotScheduleName = `snapshot-schedule-name-${uuid.v4().split('-')[0]}`;
7373
const diskName = `disk-name-with-schedule-attached-${uuid.v4().split('-')[0]}`;
74-
const region = 'europe-central2';
75-
const zone = 'europe-central2-a';
74+
const region = 'us-central1';
75+
const zone = `${region}-f`;
7676
let projectId;
7777

7878
before(async () => {
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
25+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
26+
const cwd = path.join(__dirname, '..');
27+
28+
describe('Snapshot schedule list', async () => {
29+
const snapshotScheduleName = `snapshot-list-name-${uuid.v4().split('-')[0]}`;
30+
const region = 'us-central1';
31+
32+
before(() => {
33+
execSync(
34+
`node ./snapshotSchedule/createSnapshotSchedule.js ${snapshotScheduleName} ${region}`,
35+
{
36+
cwd,
37+
}
38+
);
39+
});
40+
41+
after(() => {
42+
execSync(
43+
`node ./snapshotSchedule/deleteSnapshotSchedule.js ${snapshotScheduleName} ${region}`,
44+
{
45+
cwd,
46+
}
47+
);
48+
});
49+
50+
it('should return list of snapshot schedules', () => {
51+
const response = JSON.parse(
52+
execSync(`node ./snapshotSchedule/snapshotScheduleList.js ${region}`, {
53+
cwd,
54+
})
55+
);
56+
57+
assert(Array.isArray(response));
58+
});
59+
});

0 commit comments

Comments
 (0)