-
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 reservation create shared #3832
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d59087a
Add compute_reservation_delete sample
gryczj 52f5eb0
Create shared reservation
gryczj 1013f3c
feat: compute_reservation_create_shared
gryczj c61419c
feat: add compute_reservation_consumer_projects_update
gryczj 49221c3
Fix flaky tests in compute directory
gryczj 46781a2
Changes for shared reservation
gryczj 8dd50c4
Changes for shared reservation
gryczj f2344a5
Refactor for compute samples
gryczj 328d42d
Add mocks to sharedReservation, revert changes in tests
gryczj ed22e4a
improvement: Refactor for compute samples and their tests
gryczj 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
"chai": "^4.5.0", | ||
"mocha": "^10.0.0", | ||
"proxyquire": "^2.0.1", | ||
"sinon": "^18.0.0", | ||
"uuid": "^10.0.0" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* 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(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 | ||
// reservationsClient = new computeLib.ReservationsClient(); | ||
// Instantiate a 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(); | ||
// 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; | ||
// The name of an existing instance template. | ||
const instanceTemplateName = 'global-instance-template-name'; | ||
// The location of the instance template. | ||
const location = 'global'; | ||
|
||
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, | ||
sourceInstanceTemplate: `projects/${projectId}/${location}/instanceTemplates/${instanceTemplateName}`, | ||
}); | ||
|
||
// Create share settings. Share reservation with one customer project. | ||
const shareSettings = new compute.ShareSettings({ | ||
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, | ||
specificReservation, | ||
specificReservationRequired: true, | ||
shareSettings, | ||
}); | ||
|
||
const [response] = await reservationsClient.insert({ | ||
project: projectId, | ||
reservationResource: reservation, | ||
zone, | ||
}); | ||
|
||
let operation = response.latestResponse; | ||
|
||
// Wait for the create reservation operation to complete. | ||
while (operation.status !== 'DONE') { | ||
[operation] = await zoneOperationsClient.wait({ | ||
operation: operation.name, | ||
project: projectId, | ||
zone: operation.zone.split('/').pop(), | ||
}); | ||
} | ||
|
||
console.log(`Reservation: ${reservationName} created.`); | ||
} | ||
|
||
await callCreateComputeSharedReservation(); | ||
// [END compute_reservation_create_shared] | ||
} | ||
|
||
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; | ||
// }); |
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.
Hi @gryczj. My comment here will apply to both modules and to the test. What really are we looking for to know if the sample did what it was intended? We are not unit testing production APIs, we simply expect that our call to an API will have the intended result. What is that? A resource ID? Not returning a failed status code or catching an exception, etc? If you were a developer using this API in production code, what would you expect -- that's the check you could demonstrate right here. The unit test probably doesn't really need mocks and all the extra complexity; isn't it enough, really, if main doesn't throw (the process doesn't exit with a non-zero exit code)? Our samples shouldn't themselves be APIs, so no need to export anything out of this module.
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.
I just want to add your PRs have been great! To clarify here, the goal is that a sample should fully encapsulate the demonstration of what a developer needs to know to use our APIs successfully in their own production code. How is the API intended to be used? Our tests don't need to confirm that APIs work as expected, only that the sample works (we don't want broken samples in our docs for an unexpected reason, but we do expect that service teams have already exhaustively tested their services). Hope this helps!
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.
@subfuzion thank you for your comments and review :) I introduced mocks here because to create shared reservation special permission are required and also another project, that we want to use as consumer and share this reservation with. I can test this sample locally with my credentials, but I have no knowledge about our test environment and projects, which I can use here as an example in tests and this sample will throw errors.
I understand your point. All new changes in the implementation were made to allow me to use mocks and fake clients in tests and on the other hand allow user to use real clients if they want to.
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.
@subfuzion As @gryczj wrote, the shared reservation sample would require a separate project, with a change to organization policy to allow this project to share the reservation with other projects.
I made an "executive" call for the team that writes those samples for Python, Java, Go and Node to use mocks for those samples as a kind of an exception to the rule.
My arguments:
We acknowledge the value of proper testing and that's why mocks were picked as a middle ground between skipping tests for those samples and writing fully functional tests that would make use of multiple projects.
Let me know if I managed to convince you or not :)
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.
Thanks for the added context. I still don't see the need to mock console or treat the sample like its library code and have it export a function for testing, and my points about overall test simplification still remain. I'd like to surface this up to the rest of our team for discussion, so can you please address the points I've raised specifically?
Also, overall test flakiness still seems to be a problem, such as the compute hyperdisk failures for #3834 (which I merged so we can consolidate efforts on a single PR).
Thanks!
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.
Please feel free to ping me directly on internal chat: tonypujals@