Skip to content

Commit

Permalink
feat(storage): update dual-region sample using CustomPlacementConfig …
Browse files Browse the repository at this point in the history
…(#2620)

Fixes #2582
Fixes #2644 

Tests pending googleapis/google-cloud-go#6294 along with new library release
  • Loading branch information
Cori1109 committed Jul 22, 2022
1 parent 47d6617 commit 0c183b6
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 34 deletions.
19 changes: 11 additions & 8 deletions storage/buckets/buckets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,29 @@ func TestCreateBucketClassLocation(t *testing.T) {
}

func TestCreateBucketDualRegion(t *testing.T) {
t.Skip("Fails due to backend change, skip until #2620 is merged.")
tc := testutil.SystemTest(t)
buf := new(bytes.Buffer)
bucketName := testutil.UniqueBucketName(testPrefix)
ctx := context.Background()

defer testutil.DeleteBucketIfExists(ctx, client, bucketName)

region1 := "US-WEST1"
region2 := "US-CENTRAL1"
if err := createBucketDualRegion(ioutil.Discard, tc.ProjectID, bucketName, region1, region2); err != nil {
t.Fatalf("createBucketClassLocation: %v", err)
location := "US"
region1 := "US-EAST1"
region2 := "US-WEST1"
if err := createBucketDualRegion(buf, tc.ProjectID, bucketName); err != nil {
t.Fatalf("createBucketDualRegion: %v", err)
}
if got, want := buf.String(), fmt.Sprintf("%s and %s", region1, region2); !strings.Contains(got, want) {
t.Errorf("got %q, want %q", got, want)
}

attrs, err := client.Bucket(bucketName).Attrs(ctx)
if err != nil {
t.Fatalf("Bucket(%q).Attrs: %v", bucketName, err)
}

if !strings.Contains(attrs.Location, region1) || !strings.Contains(attrs.Location, region2) {
t.Errorf("location: got %s, want location with regions %s and %s", attrs.Location, region1, region2)
if got, want := attrs.Location, location; got != want {
t.Errorf("location: got %q, want %q", got, want)
}
if got, want := attrs.LocationType, "dual-region"; got != want {
t.Errorf("location type: got %q, want %q", got, want)
Expand Down
21 changes: 13 additions & 8 deletions storage/buckets/create_bucket_dual_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ import (
)

// createBucketDualRegion creates a new dual-region bucket in the project in the
// provided locations.
func createBucketDualRegion(w io.Writer, projectID, bucketName, region1, region2 string) error {
// provided location and regions.
// See https://cloud.google.com/storage/docs/locations#location-dr for more information.
func createBucketDualRegion(w io.Writer, projectID, bucketName string) error {
// projectID := "my-project-id"
// bucketName := "bucket-name"
// region1 := "US-EAST1"
// region2 := "US-WEST1"
location := "US"
region1 := "US-EAST1"
region2 := "US-WEST1"

ctx := context.Background()

Expand All @@ -43,14 +45,17 @@ func createBucketDualRegion(w io.Writer, projectID, bucketName, region1, region2
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel()

storageLocation := &storage.BucketAttrs{
Location: fmt.Sprintf("%s+%s", region1, region2),
storageDualRegion := &storage.BucketAttrs{
Location: location,
CustomPlacementConfig: &storage.CustomPlacementConfig{
DataLocations: []string{region1, region2},
},
}
bucket := client.Bucket(bucketName)
if err := bucket.Create(ctx, projectID, storageLocation); err != nil {
if err := bucket.Create(ctx, projectID, storageDualRegion); err != nil {
return fmt.Errorf("Bucket(%q).Create: %v", bucketName, err)
}
fmt.Fprintf(w, "Created bucket %v in %v\n", bucketName, storageLocation.Location)
fmt.Fprintf(w, "Created bucket %v in %v and %v\n", bucketName, region1, region2)
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions storage/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ require (
cloud.google.com/go/iam v0.3.0
cloud.google.com/go/kms v1.3.0 // indirect
cloud.google.com/go/pubsub v1.3.1
cloud.google.com/go/storage v1.22.1
cloud.google.com/go/storage v1.24.0
github.com/GoogleCloudPlatform/golang-samples v0.0.0-20220204002944-f20d8abe1519
github.com/aws/aws-sdk-go v1.39.0
github.com/googleapis/gax-go/v2 v2.3.0
google.golang.org/api v0.74.0
google.golang.org/genproto v0.0.0-20220518221133-4f43b3371335
github.com/googleapis/gax-go/v2 v2.4.0
google.golang.org/api v0.85.0
google.golang.org/genproto v0.0.0-20220617124728-180714bec0ad
)
Loading

0 comments on commit 0c183b6

Please sign in to comment.