Skip to content

Commit

Permalink
feat: compute_reservation_create_shared
Browse files Browse the repository at this point in the history
  • Loading branch information
gryczj committed Sep 10, 2024
1 parent 7a92e49 commit 5471bb3
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 97 deletions.
1 change: 1 addition & 0 deletions compute/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"chai": "^4.2.0",
"mocha": "^10.0.0",
"proxyquire": "^2.0.1",
"sinon": "^18.0.0",
"uuid": "^10.0.0"
}
}
81 changes: 37 additions & 44 deletions compute/reservations/createSharedReservation.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,72 +16,62 @@

'use strict';

async function main() {
// [START compute_reservation_create]
async function main(reservationsClient, zoneOperationsClient) {
// [START compute_reservation_create_shared]
// Import the Compute library
const computeLib = require('@google-cloud/compute');
const compute = computeLib.protos.google.cloud.compute.v1;

/**
* TODO(developer): Uncomment reservationsClient and zoneOperationsClient before running the sample.
*/
// Instantiate a reservationsClient
const reservationsClient = new computeLib.ReservationsClient();
// reservationsClient = new computeLib.ReservationsClient();
// Instantiate a zoneOperationsClient
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
// zoneOperationsClient = new computeLib.ZoneOperationsClient();

/**
* TODO(developer): Update these variables before running the sample.
*/
// The ID of the project where you want to reserve resources and where the instance template exists.
const projectId = await reservationsClient.getProjectId();
const projectId = 'softserve-shared';
// The zone in which to reserve resources.
const zone = 'us-central1-a';
// The name of the reservation to create.
const reservationName = 'reservation-01';
// The number of VMs to reserve.
const vmsNumber = 3;
// Machine type to use for each VM.
const machineType = 'n1-standard-4';
// The name of an existing instance template.
const instanceTemplateName = 'global-instance-template-name';
// The location of the instance template.
const location = 'global';

async function callCreateComputeReservationFromProperties() {
// Create specific reservation for 3 VMs that each use an N1 predefined machine type with 4 vCPUs.
async function callCreateComputeSharedReservation() {
// Create reservation for 3 VMs in zone us-central1-a by specifying a instance template.
const specificReservation = new compute.AllocationSpecificSKUReservation({
count: vmsNumber,
instanceProperties: {
machineType,
// To have the reserved VMs use a specific minimum CPU platform instead of the zone's default CPU platform.
minCpuPlatform: 'Intel Skylake',
// If you want to attach GPUs to your reserved N1 VMs, update and uncomment guestAccelerators if needed.
guestAccelerators: [
{
// The number of GPUs to add per reserved VM.
acceleratorCount: 1,
// Supported GPU model for N1 VMs. Ensure that your chosen GPU model is available in the zone,
// where you want to reserve resources.
acceleratorType: 'nvidia-tesla-t4',
},
],
// If you want to add local SSD disks to each reserved VM, update and uncomment localSsds if needed.
// You can specify up to 24 Local SSD disks. Each Local SSD disk is 375 GB.
localSsds: [
{
diskSizeGb: 375,
// The type of interface you want each Local SSD disk to use. Specify one of the following values: NVME or SCSI.
// Make sure that the machine type you specify for the reserved VMs supports the chosen disk interfaces.
interface: 'NVME',
},
],
},
sourceInstanceTemplate: `projects/${projectId}/${location}/instanceTemplates/${instanceTemplateName}`,
});

// Define share settings
// Create share settings. Share reservation with one customer project.
const shareSettings = new compute.ShareSettings({
shareType:
shareType: 'SPECIFIC_PROJECTS',
projectMap: {
// The IDs of projects that can consume this reservation. You can include up to 100 consumer projects.
// These projects must be in the same organization as the owner project.
// Don't include the owner project. By default, it is already allowed to consume the reservation.
consumer_project_id: {
projectId: 'consumer_project_id',
},
},
});

// Create a reservation.
const reservation = new compute.Reservation({
name: reservationName,
zone,
specificReservation,
specificReservationRequired: true,
shareSettings,
});

const [response] = await reservationsClient.insert({
Expand Down Expand Up @@ -109,14 +99,17 @@ async function main() {
})
)[0];

console.log(JSON.stringify(createdReservation));
console.log(createdReservation);
}

await callCreateComputeReservationFromProperties();
// [END compute_reservation_create]
await callCreateComputeSharedReservation();
// [END compute_reservation_create_shared]
}

main().catch(err => {
console.error(err);
process.exitCode = 1;
});
module.exports = main;

// TODO(developer): Uncomment below lines before running the sample.
// main().catch(err => {
// console.error(err);
// process.exitCode = 1;
// });
104 changes: 51 additions & 53 deletions compute/test/createSharedReservation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,68 +16,66 @@

'use strict';

const path = require('path');
const assert = require('node:assert/strict');
const {after, before, describe, it} = require('mocha');
const cp = require('child_process');
const {ReservationsClient} = require('@google-cloud/compute').v1;
const {after, describe, it} = require('mocha');
const {expect} = require('chai');
const sinon = require('sinon');
const createSharedReservation = require('../reservations/createSharedReservation.js');

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

describe('Create compute reservation by specyfing properties directly', async () => {
describe('Create compute shared reservation using global instance template', async () => {
const reservationName = 'reservation-01';
const zone = 'us-central1-a';
const reservationsClient = new ReservationsClient();
let projectId;

before(async () => {
projectId = await reservationsClient.getProjectId();
});

after(async () => {
await reservationsClient.delete({
project: projectId,
reservation: reservationName,
zone,
});
});
const reservation = {
name: reservationName,
shareSettings: {
shareType: 'SPECIFIC_PROJECTS',
projectMap: {
consumer_project_id: {
projectId: 'consumer_project_id',
},
},
},
};
let reservationsClientMock;
let zoneOperationsClientMock;

it('should create a new reservation', () => {
const instanceProperties = {
_machineType: 'machineType',
_minCpuPlatform: 'minCpuPlatform',
guestAccelerators: [
before(() => {
reservationsClientMock = {
insert: sinon.stub().resolves([
{
_acceleratorCount: 'acceleratorCount',
_acceleratorType: 'acceleratorType',
acceleratorCount: 1,
acceleratorType: 'nvidia-tesla-t4',
latestResponse: {
status: 'DONE',
name: 'operation-1234567890',
zone: {
value: 'us-central1-a',
},
},
},
],
localSsds: [
]),
get: sinon.stub().resolves([reservation]),
};
zoneOperationsClientMock = {
wait: sinon.stub().resolves([
{
diskSizeGb: '375',
interface: 'NVME',
_diskSizeGb: 'diskSizeGb',
_interface: 'interface',
latestResponse: {
status: 'DONE',
},
},
],
machineType: 'n1-standard-4',
minCpuPlatform: 'Intel Skylake',
]),
};
});

const response = JSON.parse(
execSync('node ./reservations/createReservationFromProperties.js', {
cwd,
})
);
after(() => {
sinon.restore();
});

it('should create and log new shared reservation', async () => {
sinon.stub(console, 'log');

assert.equal(response.name, reservationName);
assert.equal(response.specificReservation.count, '3');
assert.deepEqual(
response.specificReservation.instanceProperties,
instanceProperties
await createSharedReservation(
reservationsClientMock,
zoneOperationsClientMock
);

expect(console.log.calledOnce).to.be.true;
expect(console.log.calledWith(reservation)).to.be.true;
});
});
});

0 comments on commit 5471bb3

Please sign in to comment.