Skip to content

Commit

Permalink
Refactor for compute samples
Browse files Browse the repository at this point in the history
  • Loading branch information
gryczj committed Sep 17, 2024
1 parent 7bc56c9 commit 35e5cf8
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 154 deletions.
10 changes: 1 addition & 9 deletions compute/disks/createComputeHyperdisk.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,7 @@ async function main(diskName) {
});
}

const hyperdisk = (
await disksClient.get({
project: projectId,
zone,
disk: diskName,
})
)[0];

console.log(JSON.stringify(hyperdisk));
console.log(`Disk: ${diskName} created.`);
}

await callCreateComputeHyperdisk();
Expand Down
10 changes: 1 addition & 9 deletions compute/disks/createComputeHyperdiskFromPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,7 @@ async function main(diskName, storagePoolName) {
});
}

const hyperdisk = (
await disksClient.get({
project: projectId,
zone,
disk: diskName,
})
)[0];

console.log(JSON.stringify(hyperdisk));
console.log(`Disk: ${diskName} created.`);
}

await callCreateComputeHyperdiskFromPool();
Expand Down
10 changes: 1 addition & 9 deletions compute/disks/createComputeHyperdiskPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,7 @@ async function main(storagePoolName) {
});
}

const createdStoragePool = (
await storagePoolClient.get({
project: projectId,
zone,
storagePool: storagePoolName,
})
)[0];

console.log(JSON.stringify(createdStoragePool));
console.log(`Storage pool: ${storagePoolName} created.`);
}

await callCreateComputeHyperdiskPool();
Expand Down
10 changes: 1 addition & 9 deletions compute/reservations/createReservationFromProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,7 @@ async function main(reservationName) {
});
}

const createdReservation = (
await reservationsClient.get({
project: projectId,
zone,
reservation: reservationName,
})
)[0];

console.log(JSON.stringify(createdReservation));
console.log(`Reservation: ${reservationName} created.`);
}

await callCreateComputeReservationFromProperties();
Expand Down
10 changes: 1 addition & 9 deletions compute/reservations/createReservationInstanceTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,7 @@ async function main(reservationName, location, instanceTemplateName) {
});
}

const createdReservation = (
await reservationsClient.get({
project: projectId,
zone,
reservation: reservationName,
})
)[0];

console.log(JSON.stringify(createdReservation));
console.log(`Reservation: ${reservationName} created.`);
}

await callCreateComputeReservationInstanceTemplate();
Expand Down
10 changes: 1 addition & 9 deletions compute/reservations/createSharedReservation.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,7 @@ async function main(reservationName, instanceTemplateName) {
});
}

const createdReservation = (
await reservationsClient.get({
project: projectId,
zone,
reservation: reservationName,
})
)[0];

console.log(createdReservation);
console.log(`Reservation: ${reservationName} created.`);
}

await callCreateComputeSharedReservation();
Expand Down
2 changes: 2 additions & 0 deletions compute/reservations/deleteReservation.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ async function main(reservationName) {
zone: operation.zone.split('/').pop(),
});
}

console.log(`Reservation: ${reservationName} deleted.`);
}
await callDeleteReservation();
// [END compute_reservation_delete]
Expand Down
10 changes: 1 addition & 9 deletions compute/reservations/reservationVmsUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,7 @@ async function main(reservationName) {
});
}

const updatedReservation = (
await reservationsClient.get({
project: projectId,
zone,
reservation: reservationName,
})
)[0];

console.log(JSON.stringify(updatedReservation));
console.log(`Reservation: ${reservationName} updated.`);
}

await callComputeReservationVmsUpdate();
Expand Down
41 changes: 17 additions & 24 deletions compute/reservations/sharedReservationConsumerProjectsUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,37 @@ async function main(reservationName) {
// The ID of the owner project, which is the project used to create the shared reservation.
const projectId = await reservationsClient.getProjectId();
// The zone where the shared reservation is located.
const zone = 'us-central1-a';
// const zone = 'us-central1-a';
// The name of an existing shared reservation.
// const reservationName = 'reservation-01';
// The ID of project to share the reservation with.
const consumerId = 'newConsumerId';
// const consumerId = 'newConsumerId';

async function callComputeSharedReservationConsumerProjectsUpdate() {
// Update the reservation.
const [response] = await reservationsClient.update({
project: projectId,
reservation: reservationName,
/**
* TODO(developer): Uncomment `paths, zone, reservationResource` variables before running the sample.
* Define `paths` field for each consumer, that you want to allow/not allow to consume a shared resevation, e.g.
* to allow 2 projects to consume a shared reservation, you need to specify 2 paths:
* paths: 'shareSettings.projectMap.{consumerId1}',
* paths: 'shareSettings.projectMap.{consumerId2}'
* */
paths: `shareSettings.projectMap.${consumerId}`,
zone,
reservationResource: {
name: reservationName,
// To stop allowing one or more projects to consume a shared reservation, comment shareSettings object.
shareSettings: {
projectMap: {
newConsumerId: {
projectId: `${consumerId}`,
},
},
},
},
// paths: `shareSettings.projectMap.${consumerId}`,
// zone,
// reservationResource: {
// name: reservationName,
// // To stop allowing one or more projects to consume a shared reservation, comment shareSettings object.
// shareSettings: {
// projectMap: {
// newConsumerId: {
// projectId: `${consumerId}`,
// },
// },
// },
// },
});

let operation = response.latestResponse;
Expand All @@ -77,15 +78,7 @@ async function main(reservationName) {
});
}

const updatedReservation = (
await reservationsClient.get({
project: projectId,
zone,
reservation: reservationName,
})
)[0];

console.log(updatedReservation);
console.log(`Reservation: ${reservationName} updated.`);
}

await callComputeSharedReservationConsumerProjectsUpdate();
Expand Down
11 changes: 6 additions & 5 deletions compute/test/createComputeHyperdisk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
const cwd = path.join(__dirname, '..');

describe('Create compute hyperdisk', async () => {
const diskName = `hyperdisk-name-941ad2d${Math.floor(Math.random() * 10 + 1)}`;
const diskName = `hyperdisk-name-941ad2d${Math.floor(Math.random() * 1000 + 1)}`;
const zone = 'europe-central2-b';
const disksClient = new DisksClient();
let projectId;
Expand All @@ -44,12 +44,13 @@ describe('Create compute hyperdisk', async () => {
});

it('should create a new hyperdisk', () => {
const response = JSON.parse(
execSync(`node ./disks/createComputeHyperdisk.js ${diskName}`, {
const response = execSync(
`node ./disks/createComputeHyperdisk.js ${diskName}`,
{
cwd,
})
}
);

assert.equal(response.name, diskName);
assert.include(response, `Disk: ${diskName} created.`);
});
});
32 changes: 14 additions & 18 deletions compute/test/createComputeHyperdiskFromPool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ async function cleanupResources(projectId, zone, diskName, storagePoolName) {
}

describe('Create compute hyperdisk from pool', async () => {
const diskName = `disk-from-pool-name-745d98${Math.floor(Math.random() * 10 + 1)}f`;
const diskName = `disk-from-pool-name-745d98${Math.floor(Math.random() * 1000 + 1)}f`;
const zone = 'us-central1-a';
const storagePoolName = `storage-pool-name-745d9${Math.floor(Math.random() * 10 + 1)}5f`;
const storagePoolName = `storage-pool-name-745d9${Math.floor(Math.random() * 1000 + 1)}5f`;
const disksClient = new DisksClient();
let projectId;

Expand All @@ -81,28 +81,24 @@ describe('Create compute hyperdisk from pool', async () => {
});

it('should create a new storage pool', () => {
const response = JSON.parse(
execSync(
`node ./disks/createComputeHyperdiskPool.js ${storagePoolName}`,
{
cwd,
}
)
const response = execSync(
`node ./disks/createComputeHyperdiskPool.js ${storagePoolName}`,
{
cwd,
}
);

assert.equal(response.name, storagePoolName);
assert.include(response, `Storage pool: ${storagePoolName} created.`);
});

it('should create a new hyperdisk from pool', () => {
const response = JSON.parse(
execSync(
`node ./disks/createComputeHyperdiskFromPool.js ${diskName} ${storagePoolName}`,
{
cwd,
}
)
const response = execSync(
`node ./disks/createComputeHyperdiskFromPool.js ${diskName} ${storagePoolName}`,
{
cwd,
}
);

assert.equal(response.name, diskName);
assert.include(response, `Disk: ${diskName} created.`);
});
});
20 changes: 9 additions & 11 deletions compute/test/createReservationGlobalInstanceTemplate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'use strict';

const path = require('path');
const assert = require('node:assert/strict');
const {assert} = require('chai');
const {after, before, describe, it} = require('mocha');
const cp = require('child_process');
const {ReservationsClient} = require('@google-cloud/compute').v1;
Expand All @@ -26,8 +26,8 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
const cwd = path.join(__dirname, '..');

describe('Create compute reservation using global instance template', async () => {
const reservationName = `global-reservation-68ef06a${Math.floor(Math.random() * 10 + 1)}`;
const instanceTemplateName = `pernament-global-template-68ef06a${Math.floor(Math.random() * 10 + 1)}`;
const reservationName = `global-reservation-68ef06a${Math.floor(Math.random() * 1000 + 1)}`;
const instanceTemplateName = `pernament-global-template-68ef06a${Math.floor(Math.random() * 1000 + 1)}`;
const location = 'global';
const reservationsClient = new ReservationsClient();
let projectId;
Expand Down Expand Up @@ -58,15 +58,13 @@ describe('Create compute reservation using global instance template', async () =
});

it('should create a new reservation', () => {
const response = JSON.parse(
execSync(
`node ./reservations/createReservationInstanceTemplate.js ${reservationName} ${location} ${instanceTemplateName}`,
{
cwd,
}
)
const response = execSync(
`node ./reservations/createReservationInstanceTemplate.js ${reservationName} ${location} ${instanceTemplateName}`,
{
cwd,
}
);

assert.equal(response.name, reservationName);
assert.include(response, `Reservation: ${reservationName} created.`);
});
});
18 changes: 8 additions & 10 deletions compute/test/createReservationRegionalInstanceTemplate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
'use strict';

const path = require('path');
const assert = require('node:assert/strict');
const {assert} = require('chai');
const {after, describe, it} = require('mocha');
const cp = require('child_process');

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

describe('Create compute reservation using regional instance template', async () => {
const reservationName = `regional-reservation-04bf4ed${Math.floor(Math.random() * 10 + 1)}`;
const reservationName = `regional-reservation-04bf4ed${Math.floor(Math.random() * 1000 + 1)}`;
const instanceTemplateName = 'pernament-region-template-name';
const location = 'regions/us-central1';

Expand All @@ -37,15 +37,13 @@ describe('Create compute reservation using regional instance template', async ()
});

it('should create a new reservation', () => {
const response = JSON.parse(
execSync(
`node ./reservations/createReservationInstanceTemplate.js ${reservationName} ${location} ${instanceTemplateName}`,
{
cwd,
}
)
const response = execSync(
`node ./reservations/createReservationInstanceTemplate.js ${reservationName} ${location} ${instanceTemplateName}`,
{
cwd,
}
);

assert.equal(response.name, reservationName);
assert.include(response, `Reservation: ${reservationName} created.`);
});
});
Loading

0 comments on commit 35e5cf8

Please sign in to comment.