|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package snippets |
| 16 | + |
| 17 | +// [START compute_reservation_create_shared] |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + |
| 23 | + computepb "cloud.google.com/go/compute/apiv1/computepb" |
| 24 | + "google.golang.org/protobuf/proto" |
| 25 | +) |
| 26 | + |
| 27 | +// Creates shared reservation from given template in particular zone |
| 28 | +func createSharedReservation(w io.Writer, client ClientInterface, projectID, baseProjectId, zone, reservationName, sourceTemplate string) error { |
| 29 | + // client, err := compute.NewReservationsRESTClient(ctx) |
| 30 | + // projectID := "your_project_id". Destination of sharing. |
| 31 | + // baseProjectId := "your_project_id2". Project where the reservation will be created. |
| 32 | + // zone := "us-west3-a" |
| 33 | + // reservationName := "your_reservation_name" |
| 34 | + // sourceTemplate: existing template path. Following formats are allowed: |
| 35 | + // - projects/{project_id}/global/instanceTemplates/{template_name} |
| 36 | + // - projects/{project_id}/regions/{region}/instanceTemplates/{template_name} |
| 37 | + // - https://www.googleapis.com/compute/v1/projects/{project_id}/global/instanceTemplates/instanceTemplate |
| 38 | + // - https://www.googleapis.com/compute/v1/projects/{project_id}/regions/{region}/instanceTemplates/instanceTemplate |
| 39 | + |
| 40 | + ctx := context.Background() |
| 41 | + |
| 42 | + shareSettings := map[string]*computepb.ShareSettingsProjectConfig{ |
| 43 | + projectID: {ProjectId: proto.String(projectID)}, |
| 44 | + } |
| 45 | + |
| 46 | + req := &computepb.InsertReservationRequest{ |
| 47 | + Project: baseProjectId, |
| 48 | + ReservationResource: &computepb.Reservation{ |
| 49 | + Name: proto.String(reservationName), |
| 50 | + Zone: proto.String(zone), |
| 51 | + SpecificReservation: &computepb.AllocationSpecificSKUReservation{ |
| 52 | + Count: proto.Int64(2), |
| 53 | + SourceInstanceTemplate: proto.String(sourceTemplate), |
| 54 | + }, |
| 55 | + ShareSettings: &computepb.ShareSettings{ |
| 56 | + ProjectMap: shareSettings, |
| 57 | + ShareType: proto.String("SPECIFIC_PROJECTS"), |
| 58 | + }, |
| 59 | + }, |
| 60 | + Zone: zone, |
| 61 | + } |
| 62 | + |
| 63 | + op, err := client.Insert(ctx, req) |
| 64 | + if err != nil { |
| 65 | + return fmt.Errorf("unable to create reservation: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + if op != nil { |
| 69 | + if err = op.Wait(ctx); err != nil { |
| 70 | + return fmt.Errorf("unable to wait for the operation: %w", err) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + fmt.Fprintf(w, "Reservation created\n") |
| 75 | + |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +// [END compute_reservation_create_shared] |
0 commit comments