forked from GoogleCloudPlatform/golang-samples
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into cloudAuditLogs
- Loading branch information
Showing
24 changed files
with
2,834 additions
and
20 deletions.
There are no files selected for viewing
234 changes: 234 additions & 0 deletions
234
compute/instance-templates/create-instance-templates/create_instance_templates_test.go
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,234 @@ | ||
// Copyright 2021 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 | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"math/rand" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
compute "cloud.google.com/go/compute/apiv1" | ||
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil" | ||
computepb "google.golang.org/genproto/googleapis/cloud/compute/v1" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
func TestCreateInstanceTemplatesSnippets(t *testing.T) { | ||
var seededRand *rand.Rand = rand.New( | ||
rand.NewSource(time.Now().UnixNano())) | ||
tc := testutil.SystemTest(t) | ||
zone := "europe-central2-b" | ||
instanceName := "test-instance-" + fmt.Sprint(seededRand.Int()) | ||
templateName1 := "test-template-" + fmt.Sprint(seededRand.Int()) | ||
templateName2 := "test-template-" + fmt.Sprint(seededRand.Int()) | ||
templateName3 := "test-template-" + fmt.Sprint(seededRand.Int()) | ||
machineType := "n1-standard-1" | ||
sourceImage := "projects/debian-cloud/global/images/family/debian-10" | ||
networkName := "global/networks/default" | ||
subnetworkName := "regions/asia-east1/subnetworks/default" | ||
|
||
ctx := context.Background() | ||
|
||
instancesClient, err := compute.NewInstancesRESTClient(ctx) | ||
if err != nil { | ||
t.Fatalf("NewInstancesRESTClient: %v", err) | ||
} | ||
defer instancesClient.Close() | ||
|
||
zoneOperationsClient, err := compute.NewZoneOperationsRESTClient(ctx) | ||
if err != nil { | ||
t.Fatalf("NewZoneOperationsRESTClient: %v", err) | ||
} | ||
defer zoneOperationsClient.Close() | ||
|
||
buf := &bytes.Buffer{} | ||
|
||
if err := createTemplate(buf, tc.ProjectID, templateName1); err != nil { | ||
t.Errorf("createTemplate got err: %v", err) | ||
} | ||
|
||
expectedResult := "Instance template created" | ||
if got := buf.String(); !strings.Contains(got, expectedResult) { | ||
t.Errorf("createTemplate got %q, want %q", got, expectedResult) | ||
} | ||
|
||
buf.Reset() | ||
|
||
if err := listInstanceTemplates(buf, tc.ProjectID); err != nil { | ||
t.Errorf("listInstanceTemplates got err: %v", err) | ||
} | ||
|
||
expectedResult = fmt.Sprintf("- %s %s", templateName1, "e2-standard-4") | ||
if got := buf.String(); !strings.Contains(got, expectedResult) { | ||
t.Errorf("listInstanceTemplates got %q, want %q", got, expectedResult) | ||
} | ||
|
||
buf.Reset() | ||
|
||
req := &computepb.InsertInstanceRequest{ | ||
Project: tc.ProjectID, | ||
Zone: zone, | ||
InstanceResource: &computepb.Instance{ | ||
Name: proto.String(instanceName), | ||
Disks: []*computepb.AttachedDisk{ | ||
{ | ||
InitializeParams: &computepb.AttachedDiskInitializeParams{ | ||
DiskSizeGb: proto.Int64(250), | ||
SourceImage: proto.String(sourceImage), | ||
}, | ||
AutoDelete: proto.Bool(true), | ||
Boot: proto.Bool(true), | ||
Type: proto.String(computepb.AttachedDisk_PERSISTENT.String()), | ||
DeviceName: proto.String("disk-1"), | ||
}, | ||
}, | ||
MachineType: proto.String(fmt.Sprintf("zones/%s/machineTypes/%s", zone, machineType)), | ||
NetworkInterfaces: []*computepb.NetworkInterface{ | ||
{ | ||
Name: proto.String(networkName), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
op, err := instancesClient.Insert(ctx, req) | ||
if err != nil { | ||
t.Errorf("unable to create instance: %v", err) | ||
} | ||
|
||
for { | ||
waitReq := &computepb.WaitZoneOperationRequest{ | ||
Operation: op.Proto().GetName(), | ||
Project: tc.ProjectID, | ||
Zone: zone, | ||
} | ||
zoneOp, err := zoneOperationsClient.Wait(ctx, waitReq) | ||
if err != nil { | ||
t.Errorf("unable to wait for the operation: %v", err) | ||
} | ||
|
||
if *zoneOp.Status.Enum() == computepb.Operation_DONE { | ||
break | ||
} | ||
} | ||
|
||
formattedInstanceName := fmt.Sprintf("projects/%s/zones/%s/instances/%s", tc.ProjectID, zone, instanceName) | ||
if err := createTemplateFromInstance(buf, tc.ProjectID, formattedInstanceName, templateName2); err != nil { | ||
t.Errorf("createTemplateFromInstance got err: %v", err) | ||
} | ||
|
||
expectedResult = "Instance template created" | ||
if got := buf.String(); !strings.Contains(got, expectedResult) { | ||
t.Errorf("createTemplateFromInstance got %q, want %q", got, expectedResult) | ||
} | ||
|
||
template, err := getInstanceTemplate(tc.ProjectID, templateName2) | ||
if err != nil { | ||
t.Errorf("getInstanceTemplate got err: %v", err) | ||
} | ||
|
||
got := template.GetName() | ||
if got != templateName2 { | ||
t.Errorf("template.GetName() got %q, want %q", got, templateName2) | ||
} | ||
|
||
got = template.GetProperties().GetMachineType() | ||
if got != machineType { | ||
t.Errorf("template.GetProperties().GetMachineType() got %q, want %q", got, machineType) | ||
} | ||
|
||
gotDiskSize := template.GetProperties().GetDisks()[0].GetDiskSizeGb() | ||
if gotDiskSize != 250 { | ||
t.Errorf("template.GetProperties().GetDisks()[0].GetDiskSizeGb() got %q, want %q", got, 250) | ||
} | ||
|
||
buf.Reset() | ||
|
||
if err := createTemplateWithSubnet(buf, tc.ProjectID, networkName, subnetworkName, templateName3); err != nil { | ||
t.Errorf("createTemplateWithSubnet got err: %v", err) | ||
} | ||
|
||
expectedResult = "Instance template created" | ||
if got := buf.String(); !strings.Contains(got, expectedResult) { | ||
t.Errorf("createTemplateFromInstance got %q, want %q", got, expectedResult) | ||
} | ||
|
||
buf.Reset() | ||
|
||
if err := deleteInstanceTemplate(buf, tc.ProjectID, templateName1); err != nil { | ||
t.Errorf("deleteInstanceTemplate got err: %v", err) | ||
} | ||
|
||
expectedResult = "Instance template deleted" | ||
if got := buf.String(); !strings.Contains(got, expectedResult) { | ||
t.Errorf("createTemplateFromInstance got %q, want %q", got, expectedResult) | ||
} | ||
|
||
buf.Reset() | ||
|
||
if err := deleteInstanceTemplate(buf, tc.ProjectID, templateName2); err != nil { | ||
t.Errorf("deleteInstanceTemplate got err: %v", err) | ||
} | ||
|
||
expectedResult = "Instance template deleted" | ||
if got := buf.String(); !strings.Contains(got, expectedResult) { | ||
t.Errorf("createTemplateFromInstance got %q, want %q", got, expectedResult) | ||
} | ||
|
||
buf.Reset() | ||
|
||
if err := deleteInstanceTemplate(buf, tc.ProjectID, templateName3); err != nil { | ||
t.Errorf("deleteInstanceTemplate got err: %v", err) | ||
} | ||
|
||
expectedResult = "Instance template deleted" | ||
if got := buf.String(); !strings.Contains(got, expectedResult) { | ||
t.Errorf("createTemplateFromInstance got %q, want %q", got, expectedResult) | ||
} | ||
|
||
buf.Reset() | ||
|
||
deleteReq := &computepb.DeleteInstanceRequest{ | ||
Project: tc.ProjectID, | ||
Zone: zone, | ||
Instance: instanceName, | ||
} | ||
|
||
op, err = instancesClient.Delete(ctx, deleteReq) | ||
if err != nil { | ||
t.Errorf("unable to delete instance: %v", err) | ||
} | ||
|
||
for { | ||
waitReq := &computepb.WaitZoneOperationRequest{ | ||
Operation: op.Proto().GetName(), | ||
Project: tc.ProjectID, | ||
Zone: zone, | ||
} | ||
zoneOp, err := zoneOperationsClient.Wait(ctx, waitReq) | ||
if err != nil { | ||
t.Errorf("unable to wait for the operation: %v", err) | ||
} | ||
|
||
if *zoneOp.Status.Enum() == computepb.Operation_DONE { | ||
break | ||
} | ||
} | ||
|
||
} |
107 changes: 107 additions & 0 deletions
107
compute/instance-templates/create-instance-templates/create_template.go
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 2021 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_template_create] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
compute "cloud.google.com/go/compute/apiv1" | ||
computepb "google.golang.org/genproto/googleapis/cloud/compute/v1" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
// createTemplate creates a new instance template with the provided name and a specific instance configuration. | ||
func createTemplate(w io.Writer, projectID, templateName string) error { | ||
// projectID := "your_project_id" | ||
// templateName := "your_template_name" | ||
|
||
ctx := context.Background() | ||
instanceTemplatesClient, err := compute.NewInstanceTemplatesRESTClient(ctx) | ||
if err != nil { | ||
return fmt.Errorf("NewInstanceTemplatesRESTClient: %v", err) | ||
} | ||
defer instanceTemplatesClient.Close() | ||
|
||
req := &computepb.InsertInstanceTemplateRequest{ | ||
Project: projectID, | ||
InstanceTemplateResource: &computepb.InstanceTemplate{ | ||
Name: proto.String(templateName), | ||
Properties: &computepb.InstanceProperties{ | ||
// The template describes the size and source image of the boot disk | ||
// to attach to the instance. | ||
Disks: []*computepb.AttachedDisk{ | ||
{ | ||
InitializeParams: &computepb.AttachedDiskInitializeParams{ | ||
DiskSizeGb: proto.Int64(250), | ||
SourceImage: proto.String("projects/debian-cloud/global/images/family/debian-11"), | ||
}, | ||
AutoDelete: proto.Bool(true), | ||
Boot: proto.Bool(true), | ||
}, | ||
}, | ||
MachineType: proto.String("e2-standard-4"), | ||
// The template connects the instance to the `default` network, | ||
// without specifying a subnetwork. | ||
NetworkInterfaces: []*computepb.NetworkInterface{ | ||
{ | ||
Name: proto.String("global/networks/default"), | ||
// The template lets the instance use an external IP address. | ||
AccessConfigs: []*computepb.AccessConfig{ | ||
{ | ||
Name: proto.String("External NAT"), | ||
Type: proto.String(computepb.AccessConfig_ONE_TO_ONE_NAT.String()), | ||
NetworkTier: proto.String(computepb.AccessConfig_PREMIUM.String()), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
op, err := instanceTemplatesClient.Insert(ctx, req) | ||
if err != nil { | ||
return fmt.Errorf("unable to create instance template: %v", err) | ||
} | ||
|
||
globalOperationsClient, err := compute.NewGlobalOperationsRESTClient(ctx) | ||
if err != nil { | ||
return fmt.Errorf("NewGlobalOperationsRESTClient: %v", err) | ||
} | ||
defer globalOperationsClient.Close() | ||
|
||
for { | ||
waitReq := &computepb.WaitGlobalOperationRequest{ | ||
Operation: op.Proto().GetName(), | ||
Project: projectID, | ||
} | ||
zoneOp, err := globalOperationsClient.Wait(ctx, waitReq) | ||
if err != nil { | ||
return fmt.Errorf("unable to wait for the operation: %v", err) | ||
} | ||
|
||
if *zoneOp.Status.Enum() == computepb.Operation_DONE { | ||
fmt.Fprintf(w, "Instance template created\n") | ||
break | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// [END compute_template_create] |
Oops, something went wrong.