Skip to content

Commit bfdfe28

Browse files
author
Joanna Grycz
committed
feat: Add compute_reservation_delete sample
1 parent 2a47751 commit bfdfe28

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
let operation = response.latestResponse;
43+
// Wait for the delete reservation operation to complete.
44+
while (operation.status !== 'DONE') {
45+
[operation] = await zoneOperationsClient.wait({
46+
operation: operation.name,
47+
project: projectId,
48+
zone: operation.zone.split('/').pop(),
49+
});
50+
}
51+
}
52+
await callDeleteReservation();
53+
// [END compute_reservation_delete]
54+
}
55+
main().catch(err => {
56+
console.error(err);
57+
process.exitCode = 1;
58+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
const path = require('path');
18+
const {before, describe, it} = require('mocha');
19+
const {expect} = require('chai');
20+
const cp = require('child_process');
21+
const {ReservationsClient} = require('@google-cloud/compute').v1;
22+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
23+
const cwd = path.join(__dirname, '..');
24+
25+
describe('Delete compute reservation', async () => {
26+
const reservationName = 'reservation-01';
27+
const zone = 'us-central1-a';
28+
const reservationsClient = new ReservationsClient();
29+
let projectId;
30+
31+
before(async () => {
32+
projectId = await reservationsClient.getProjectId();
33+
});
34+
35+
it('should delete reservation', async () => {
36+
// Create reservation
37+
execSync('node ./reservations/createReservationFromProperties.js', {
38+
cwd,
39+
});
40+
41+
// Delete created reservation
42+
execSync('node ./reservations/deleteReservation.js', {
43+
cwd,
44+
});
45+
46+
try {
47+
// Try to get the deleted reservation
48+
await reservationsClient.get({
49+
project: projectId,
50+
zone,
51+
reservation: reservationName,
52+
});
53+
54+
// If the reservation is found, the test should fail
55+
throw new Error('Reservation was not deleted.');
56+
} catch (error) {
57+
// Assert that the error message indicates the reservation wasn't found
58+
expect(error.message).to.include(
59+
`The resource 'projects/${projectId}/zones/${zone}/reservations/reservation-01' was not found`
60+
);
61+
}
62+
});
63+
});

0 commit comments

Comments
 (0)