Skip to content

Commit

Permalink
sample(storage): add storage control quickstart (#3988)
Browse files Browse the repository at this point in the history
* sample(storage): add storage control quickstart

Quickstart for the new Storage Control GAPIC.

Fixes googleapis/google-cloud-go#9661

* update deps and tidy

* fix vet issues and add test
  • Loading branch information
tritone committed Apr 10, 2024
1 parent 8d0f3ca commit 127dc2c
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 88 deletions.
64 changes: 64 additions & 0 deletions storage/control_quickstart/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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.

// [START storage_control_quickstart_sample]
// This sample demonstrates how to set up and make an API call with the
// Storage Control client.
package main

import (
"context"
"flag"
"fmt"
"log"
"time"

control "cloud.google.com/go/storage/control/apiv2"
controlpb "cloud.google.com/go/storage/control/apiv2/controlpb"
)

func main() {
// Set this flag to an existing Cloud Storage bucket when running the sample.
bucketName := flag.String("bucket", "", "Cloud Storage bucket name")
flag.Parse()
log.Printf("bucket : %v", *bucketName)

ctx := context.Background()

// Create a client.
client, err := control.NewStorageControlClient(ctx)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
defer client.Close()

// Create a request to get the storage layout for the bucket.
req := &controlpb.GetStorageLayoutRequest{
Name: fmt.Sprintf("projects/_/buckets/%v/storageLayout", *bucketName),
}

// Set a context timeout and send the request.
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()

res, err := client.GetStorageLayout(ctx, req)
if err != nil {
log.Fatalf("GetStorageLayout: %v", err)
}

// Use response.
fmt.Printf("Bucket %v has location type %v", bucketName, res.LocationType)
}

// [END storage_control_quickstart_sample]
73 changes: 73 additions & 0 deletions storage/control_quickstart/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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 main

import (
"context"
"log"
"strings"
"testing"
"time"

"cloud.google.com/go/storage"
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil"
)

const (
testPrefix = "test-gcs-go-control"
bucketExpiryAge = time.Hour * 24
)

func TestControlQuickstart(t *testing.T) {
tc := testutil.SystemTest(t)
m := testutil.BuildMain(t)

if !m.Built() {
t.Fatalf("failed to build app")
}

ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
t.Fatalf("storage.NewClient: %v", err)
}
t.Cleanup(func() {
// Clean up any old buckets that may remain.
if err := testutil.DeleteExpiredBuckets(client, tc.ProjectID, testPrefix, bucketExpiryAge); err != nil {
log.Printf("DeleteExpiredBuckets: %v", err)
}
})

bucketName, err := testutil.CreateTestBucket(ctx, t, client, tc.ProjectID, testPrefix)
t.Cleanup(func() {
testutil.DeleteBucketIfExists(ctx, client, bucketName)
})
if err != nil {
t.Fatalf("creating bucket: %v", err)
}

stdOut, stdErr, err := m.Run(nil, time.Minute, "--bucket", bucketName)

if err != nil {
t.Errorf("stdout: %v", string(stdOut[:]))
t.Errorf("stderr: %v", string(stdErr[:]))
t.Errorf("execution failed: %v", err)
}

if got, want := string(stdOut[:]), "location type multi-region"; !strings.Contains(got, want) {
t.Errorf("got output: %q, want to contain: %q", got, want)
}

}
52 changes: 26 additions & 26 deletions storage/go.mod
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
module github.com/GoogleCloudPlatform/golang-samples/storage

go 1.19
go 1.21

require (
cloud.google.com/go/iam v1.1.5
cloud.google.com/go/pubsub v1.33.0
cloud.google.com/go/storage v1.36.0
cloud.google.com/go/iam v1.1.7
cloud.google.com/go/pubsub v1.37.0
cloud.google.com/go/storage v1.40.0
github.com/GoogleCloudPlatform/golang-samples v0.0.0-20240111005027-4c7a1933dce2
github.com/aws/aws-sdk-go v1.44.290
github.com/googleapis/gax-go/v2 v2.12.0
google.golang.org/api v0.156.0
google.golang.org/genproto v0.0.0-20240108191215-35c7eff3a6b1
github.com/googleapis/gax-go/v2 v2.12.3
google.golang.org/api v0.172.0
google.golang.org/genproto v0.0.0-20240401170217-c3f982113cda
)

require (
cloud.google.com/go v0.111.0 // indirect
cloud.google.com/go/compute v1.23.3 // indirect
cloud.google.com/go v0.112.2 // indirect
cloud.google.com/go/compute v1.25.1 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/longrunning v0.5.6 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.50.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.50.0 // indirect
go.opentelemetry.io/otel v1.25.0 // indirect
go.opentelemetry.io/otel/metric v1.25.0 // indirect
go.opentelemetry.io/otel/trace v1.25.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/oauth2 v0.19.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240102182953-50ed04b92917 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 // indirect
google.golang.org/grpc v1.60.1 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240401170217-c3f982113cda // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect
google.golang.org/grpc v1.63.2 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)

0 comments on commit 127dc2c

Please sign in to comment.