-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: compute_consume_specific_shared_reservation #3912
Open
gryczj
wants to merge
9
commits into
main
Choose a base branch
from
compute_consume_specific_shared_reservation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a5c5826
feat: compute_consume_specific_shared_reservation
3866c5a
Merge branch 'main' into compute_consume_specific_shared_reservation
iennae ad0e58d
Merge branch 'main' into compute_consume_specific_shared_reservation
iennae a513d9b
Merge branch 'main' into compute_consume_specific_shared_reservation
iennae fd051f3
Merge branch 'main' into compute_consume_specific_shared_reservation
iennae c0e09e4
chore(compute): update comments -- remove obvious comments and add ad…
iennae ed600a9
fix(compute): remove the spaces I introduced
iennae 9313379
Merge branch 'main' into compute_consume_specific_shared_reservation
m-strzelczyk f18ac98
Merge branch 'main' into compute_consume_specific_shared_reservation
glasnt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
compute/reservations/createInstanceToConsumeSharedReservation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
async function main(instancesClient, zoneOperationsClient) { | ||
// [START compute_consume_specific_shared_reservation] | ||
// 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 an instancesClient | ||
// instancesClient = new computeLib.InstancesClient(); | ||
// Instantiate a zoneOperationsClient | ||
// zoneOperationsClient = new computeLib.ZoneOperationsClient(); | ||
|
||
/** | ||
* TODO(developer): Update these variables before running the sample. | ||
*/ | ||
// The ID of the project where instance will be consumed and created. | ||
const reservationConsumerProjectId = 'reservation-consumer-project-id'; | ||
// The ID of the project where reservation is created. | ||
const reservationOwnerProjectId = 'reservation-project-id'; | ||
// The name of the instance to create. | ||
const instanceName = 'instance-01'; | ||
// The name of the reservation to consume. | ||
// Ensure that the specificReservationRequired field in reservation properties is set to true. | ||
const reservationName = 'reservation-1'; | ||
// Machine type to use for VM. | ||
const machineType = 'n1-standard-1'; | ||
// The zone in which to create instance. | ||
const zone = 'us-central1-a'; | ||
|
||
// Create instance to consume shared reservation | ||
async function callCreateInstanceToConsumeSharedReservation() { | ||
// Describe the size and source image of the boot disk to attach to the instance. | ||
// Ensure that the VM's properties match the reservation's VM properties, | ||
// including the zone, machine type (machine family, vCPUs, and memory), | ||
// minimum CPU platform, GPU amount and type, and local SSD interface and size | ||
const disk = new compute.Disk({ | ||
boot: true, | ||
autoDelete: true, | ||
type: 'PERSISTENT', | ||
initializeParams: { | ||
diskSizeGb: '10', | ||
sourceImage: 'projects/debian-cloud/global/images/family/debian-12', | ||
}, | ||
}); | ||
|
||
// Define networkInterface | ||
const networkInterface = new compute.NetworkInterface({ | ||
name: 'global/networks/default', | ||
}); | ||
|
||
// Define reservationAffinity | ||
const reservationAffinity = new compute.ReservationAffinity({ | ||
consumeReservationType: 'SPECIFIC_RESERVATION', | ||
key: 'compute.googleapis.com/reservation-name', | ||
values: [ | ||
`projects/${reservationOwnerProjectId}/reservations/${reservationName}`, | ||
], | ||
}); | ||
|
||
// Create an instance | ||
const instance = new compute.Instance({ | ||
name: instanceName, | ||
machineType: `zones/${zone}/machineTypes/${machineType}`, | ||
disks: [disk], | ||
networkInterfaces: [networkInterface], | ||
reservationAffinity, | ||
}); | ||
|
||
const [response] = await instancesClient.insert({ | ||
project: reservationConsumerProjectId, | ||
instanceResource: instance, | ||
zone, | ||
}); | ||
|
||
let operation = response.latestResponse; | ||
|
||
// Wait for the create instance operation to complete. | ||
while (operation.status !== 'DONE') { | ||
[operation] = await zoneOperationsClient.wait({ | ||
operation: operation.name, | ||
project: reservationConsumerProjectId, | ||
zone: operation.zone.split('/').pop(), | ||
}); | ||
} | ||
|
||
console.log(`Instance ${instanceName} created from shared reservation.`); | ||
return response; | ||
} | ||
|
||
return await callCreateInstanceToConsumeSharedReservation(); | ||
// [END compute_consume_specific_shared_reservation] | ||
} | ||
|
||
module.exports = main; | ||
|
||
// TODO(developer): Uncomment below lines before running the sample. | ||
// main(...process.argv.slice(2)).catch(err => { | ||
// console.error(err); | ||
// process.exitCode = 1; | ||
// }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
compute/test/createInstanceToConsumeSharedReservation.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const {beforeEach, afterEach, describe, it} = require('mocha'); | ||
const assert = require('node:assert/strict'); | ||
const sinon = require('sinon'); | ||
const createInstanceToConsumeSharedReservation = require('../reservations/createInstanceToConsumeSharedReservation.js'); | ||
|
||
describe('Create instance to consume shared reservation', async () => { | ||
const instanceName = 'instance-1'; | ||
let instancesClientMock; | ||
let zoneOperationsClientMock; | ||
|
||
beforeEach(() => { | ||
instancesClientMock = { | ||
insert: sinon.stub().resolves([ | ||
{ | ||
name: instanceName, | ||
latestResponse: { | ||
status: 'DONE', | ||
name: 'operation-1234567890', | ||
zone: { | ||
value: 'us-central1-a', | ||
}, | ||
}, | ||
}, | ||
]), | ||
}; | ||
zoneOperationsClientMock = { | ||
wait: sinon.stub().resolves([ | ||
{ | ||
latestResponse: { | ||
status: 'DONE', | ||
}, | ||
}, | ||
]), | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('should create instance', async () => { | ||
const response = await createInstanceToConsumeSharedReservation( | ||
instancesClientMock, | ||
zoneOperationsClientMock | ||
); | ||
|
||
assert(response.name.includes(instanceName)); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this PR I also moved cleanup methods to after() to prevent keeping not used resources in our project after running the tests and to reduce the code.