Skip to content

Commit 38af8df

Browse files
authored
feat: add compute_reservations_delete/get/list (#3834)
* feat: Add compute_reservation_delete sample * feat: compute_reservation_get * feat: compute_reservation_list * feat: add compute_reservations_delete/get/list
1 parent 2a47751 commit 38af8df

File tree

5 files changed

+221
-15
lines changed

5 files changed

+221
-15
lines changed

compute/reservations/createReservationFromProperties.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ async function main() {
3030
/**
3131
* TODO(developer): Update these variables before running the sample.
3232
*/
33-
// The ID of the project where you want to reserve resources and where the instance template exists.
33+
// The ID of the project where you want to reserve resources.
3434
const projectId = await reservationsClient.getProjectId();
3535
// The zone in which to reserve resources.
3636
const zone = 'us-central1-a';
Lines changed: 60 additions & 0 deletions
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+
'use strict';
17+
async function main() {
18+
// [START compute_reservation_delete]
19+
// Import the Compute library
20+
const computeLib = require('@google-cloud/compute');
21+
// Instantiate a reservationsClient
22+
const reservationsClient = new computeLib.ReservationsClient();
23+
// Instantiate a zoneOperationsClient
24+
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
25+
/**
26+
* TODO(developer): Update these variables before running the sample.
27+
*/
28+
// The ID of the project where your reservation is located.
29+
const projectId = await reservationsClient.getProjectId();
30+
// The zone where your reservation is located.
31+
const zone = 'us-central1-a';
32+
// The name of the reservation to delete.
33+
const reservationName = 'reservation-01';
34+
35+
async function callDeleteReservation() {
36+
// Delete the reservation
37+
const [response] = await reservationsClient.delete({
38+
project: projectId,
39+
reservation: reservationName,
40+
zone,
41+
});
42+
43+
let operation = response.latestResponse;
44+
45+
// Wait for the delete reservation operation to complete.
46+
while (operation.status !== 'DONE') {
47+
[operation] = await zoneOperationsClient.wait({
48+
operation: operation.name,
49+
project: projectId,
50+
zone: operation.zone.split('/').pop(),
51+
});
52+
}
53+
}
54+
await callDeleteReservation();
55+
// [END compute_reservation_delete]
56+
}
57+
main().catch(err => {
58+
console.error(err);
59+
process.exitCode = 1;
60+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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() {
20+
// [START compute_reservation_get]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
24+
// Instantiate a reservationsClient
25+
const reservationsClient = new computeLib.ReservationsClient();
26+
27+
/**
28+
* TODO(developer): Update these variables before running the sample.
29+
*/
30+
// The ID of the project where your reservation is located.
31+
const projectId = await reservationsClient.getProjectId();
32+
// The zone where your reservation is located.
33+
const zone = 'us-central1-a';
34+
// The name of the reservation to return.
35+
const reservationName = 'reservation-01';
36+
37+
async function callGetReservation() {
38+
const requestedReservation = (
39+
await reservationsClient.get({
40+
project: projectId,
41+
zone,
42+
reservation: reservationName,
43+
})
44+
)[0];
45+
46+
console.log(JSON.stringify(requestedReservation));
47+
}
48+
49+
await callGetReservation();
50+
// [END compute_reservation_get]
51+
}
52+
53+
main().catch(err => {
54+
console.error(err);
55+
process.exitCode = 1;
56+
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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() {
20+
// [START compute_reservation_list]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
24+
// Instantiate a reservationsClient
25+
const reservationsClient = new computeLib.ReservationsClient();
26+
27+
/**
28+
* TODO(developer): Update these variables before running the sample.
29+
*/
30+
// The ID of the project where your reservations are located.
31+
const projectId = await reservationsClient.getProjectId();
32+
// The zone where your reservations are located.
33+
const zone = 'us-central1-a';
34+
35+
async function callGetReservations() {
36+
const reservations = (
37+
await reservationsClient.list({
38+
project: projectId,
39+
zone,
40+
})
41+
)[0];
42+
43+
console.log(JSON.stringify(reservations));
44+
}
45+
46+
await callGetReservations();
47+
// [END compute_reservation_list]
48+
}
49+
50+
main().catch(err => {
51+
console.error(err);
52+
process.exitCode = 1;
53+
});

compute/test/createReservationFromProperties.test.js renamed to compute/test/reservations.test.js

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,25 @@
1818

1919
const path = require('path');
2020
const assert = require('node:assert/strict');
21-
const {after, before, describe, it} = require('mocha');
21+
const {describe, it} = require('mocha');
22+
const {expect} = require('chai');
2223
const cp = require('child_process');
2324
const {ReservationsClient} = require('@google-cloud/compute').v1;
2425

2526
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2627
const cwd = path.join(__dirname, '..');
2728

28-
describe('Create compute reservation by specyfing properties directly', async () => {
29+
describe('Compute reservation', async () => {
2930
const reservationName = 'reservation-01';
3031
const zone = 'us-central1-a';
3132
const reservationsClient = new ReservationsClient();
3233
let projectId;
34+
let reservation;
3335

3436
before(async () => {
3537
projectId = await reservationsClient.getProjectId();
3638
});
3739

38-
after(async () => {
39-
await reservationsClient.delete({
40-
project: projectId,
41-
reservation: reservationName,
42-
zone,
43-
});
44-
});
45-
4640
it('should create a new reservation', () => {
4741
const instanceProperties = {
4842
_machineType: 'machineType',
@@ -67,17 +61,60 @@ describe('Create compute reservation by specyfing properties directly', async ()
6761
minCpuPlatform: 'Intel Skylake',
6862
};
6963

70-
const response = JSON.parse(
64+
reservation = JSON.parse(
7165
execSync('node ./reservations/createReservationFromProperties.js', {
7266
cwd,
7367
})
7468
);
7569

76-
assert.equal(response.name, reservationName);
77-
assert.equal(response.specificReservation.count, '3');
70+
assert.equal(reservation.name, reservationName);
71+
assert.equal(reservation.specificReservation.count, '3');
7872
assert.deepEqual(
79-
response.specificReservation.instanceProperties,
73+
reservation.specificReservation.instanceProperties,
8074
instanceProperties
8175
);
8276
});
77+
78+
it('should return reservation', () => {
79+
const response = JSON.parse(
80+
execSync('node ./reservations/getReservation.js', {
81+
cwd,
82+
})
83+
);
84+
85+
assert.deepEqual(response, reservation);
86+
});
87+
88+
it('should return list of reservations', () => {
89+
const response = JSON.parse(
90+
execSync('node ./reservations/getReservations.js', {
91+
cwd,
92+
})
93+
);
94+
95+
assert.deepEqual(response, [reservation]);
96+
});
97+
98+
it('should delete reservation', async () => {
99+
execSync('node ./reservations/deleteReservation.js', {
100+
cwd,
101+
});
102+
103+
try {
104+
// Try to get the deleted reservation
105+
await reservationsClient.get({
106+
project: projectId,
107+
zone,
108+
reservation: reservationName,
109+
});
110+
111+
// If the reservation is found, the test should fail
112+
throw new Error('Reservation was not deleted.');
113+
} catch (error) {
114+
// Assert that the error message indicates the reservation wasn't found
115+
expect(error.message).to.include(
116+
`The resource 'projects/${projectId}/zones/${zone}/reservations/${reservationName}' was not found`
117+
);
118+
}
119+
});
83120
});

0 commit comments

Comments
 (0)