-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: create shared reservation #4378
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c8957a2
feat: create shared reservation
BigBlackWolf d4770d9
Merge branch 'main' into feat_compute_reservation_create_shared
telpirion 6c25ac9
Merge branch 'main' into feat_compute_reservation_create_shared
BigBlackWolf e4aabc4
Merge branch 'main' into feat_compute_reservation_create_shared
BigBlackWolf 7780af2
Merge branch 'main' into feat_compute_reservation_create_shared
telpirion aeaac59
Merge branch 'main' into feat_compute_reservation_create_shared
telpirion bce489a
adjustments to mocks
telpirion 52ce7d7
Merge branch 'main' into feat_compute_reservation_create_shared
BigBlackWolf 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 hidden or 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 hidden or 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,87 @@ | ||
// 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. | ||
|
||
package snippets | ||
|
||
// [START compute_reservation_create_shared] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
compute "cloud.google.com/go/compute/apiv1" | ||
computepb "cloud.google.com/go/compute/apiv1/computepb" | ||
gax "github.com/googleapis/gax-go/v2" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
type ClientInterface interface { | ||
Close() error | ||
Delete(context.Context, *computepb.DeleteReservationRequest, ...gax.CallOption) (*compute.Operation, error) | ||
Insert(context.Context, *computepb.InsertReservationRequest, ...gax.CallOption) (*compute.Operation, error) | ||
} | ||
|
||
// Creates shared reservation from given template in particular zone | ||
func createSharedReservation(w io.Writer, client ClientInterface, projectID, baseProjectId, zone, reservationName, sourceTemplate string) error { | ||
// client, err := compute.NewReservationsRESTClient(ctx) | ||
// projectID := "your_project_id". Destination of sharing. | ||
// baseProjectId := "your_project_id2". Project where the reservation will be created. | ||
// zone := "us-west3-a" | ||
// reservationName := "your_reservation_name" | ||
// sourceTemplate: existing template path. Following formats are allowed: | ||
// - projects/{project_id}/global/instanceTemplates/{template_name} | ||
// - projects/{project_id}/regions/{region}/instanceTemplates/{template_name} | ||
// - https://www.googleapis.com/compute/v1/projects/{project_id}/global/instanceTemplates/instanceTemplate | ||
// - https://www.googleapis.com/compute/v1/projects/{project_id}/regions/{region}/instanceTemplates/instanceTemplate | ||
|
||
ctx := context.Background() | ||
|
||
shareSettings := map[string]*computepb.ShareSettingsProjectConfig{ | ||
projectID: {ProjectId: proto.String(projectID)}, | ||
} | ||
|
||
req := &computepb.InsertReservationRequest{ | ||
Project: baseProjectId, | ||
ReservationResource: &computepb.Reservation{ | ||
Name: proto.String(reservationName), | ||
Zone: proto.String(zone), | ||
SpecificReservation: &computepb.AllocationSpecificSKUReservation{ | ||
Count: proto.Int64(2), | ||
SourceInstanceTemplate: proto.String(sourceTemplate), | ||
}, | ||
ShareSettings: &computepb.ShareSettings{ | ||
ProjectMap: shareSettings, | ||
ShareType: proto.String("SPECIFIC_PROJECTS"), | ||
}, | ||
}, | ||
Zone: zone, | ||
} | ||
|
||
op, err := client.Insert(ctx, req) | ||
if err != nil { | ||
return fmt.Errorf("unable to create reservation: %w", err) | ||
} | ||
|
||
if op != nil { | ||
if err = op.Wait(ctx); err != nil { | ||
return fmt.Errorf("unable to wait for the operation: %w", err) | ||
} | ||
} | ||
|
||
fmt.Fprintf(w, "Reservation created\n") | ||
|
||
return nil | ||
} | ||
|
||
// [END compute_reservation_create_shared] |
This file contains hidden or 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,39 @@ | ||
// 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. | ||
|
||
// Mock objects for shared project | ||
|
||
package snippets | ||
|
||
import ( | ||
"context" | ||
|
||
compute "cloud.google.com/go/compute/apiv1" | ||
computepb "cloud.google.com/go/compute/apiv1/computepb" | ||
gax "github.com/googleapis/gax-go/v2" | ||
) | ||
|
||
type ReservationsClient struct{} | ||
|
||
func (client ReservationsClient) Close() error { | ||
return nil | ||
} | ||
|
||
func (client ReservationsClient) Insert(context.Context, *computepb.InsertReservationRequest, ...gax.CallOption) (*compute.Operation, error) { | ||
return nil, nil | ||
} | ||
|
||
func (client ReservationsClient) Delete(context.Context, *computepb.DeleteReservationRequest, ...gax.CallOption) (*compute.Operation, error) { | ||
return nil, nil | ||
} |
This file contains hidden or 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
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.
q: Is this used only for
mocking
the client? Is there a way to make a sample fully standalone (it should initialise the client), but still mock the client when running tests?See my example in Python (different sample): GoogleCloudPlatform/python-docs-samples#12598
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.
Yeah, it's done this way to allow for either real or mocked client. I tried to do it in a mocking way without modifying the sample, but in Go it couldn't be done as in Python or Java...
Go doesn't support object replacement in runtime, so the typical solution for that is to make an abstraction (interface) that matches the same signature. I tried to cover the gap in comments to explain how it can be done by the developer, who will run this sample, but agree that it doesn't seem ideal.
Perhaps I am missing something - I am open to any suggestions on how to improve it