forked from openshift/oadp-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OADP-3038 Update region discovery error message.
Signed-off-by: Tiger Kaovilai <[email protected]>
- Loading branch information
Showing
4 changed files
with
143 additions
and
5 deletions.
There are no files selected for viewing
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
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
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,48 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
|
||
"errors" | ||
|
||
"github.com/aws/aws-sdk-go/aws/endpoints" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/s3/s3manager" | ||
// "github.com/pkg/errors" don't need stack trace, not inside velero-plugin | ||
) | ||
|
||
func BucketRegionIsDiscoverable(bucket string) bool { | ||
_, err := GetBucketRegion(bucket) | ||
return err == nil | ||
} | ||
|
||
// GetBucketRegion returns the AWS region that a bucket is in, or an error | ||
// if the region cannot be determined. | ||
// copied from https://github.com/openshift/openshift-velero-plugin/pull/223/files#diff-da482ef606b3938b09ae46990a60eb0ad49ebfb4885eb1af327d90f215bf58b1 | ||
func GetBucketRegion(bucket string) (string, error) { | ||
var region string | ||
|
||
session, err := session.NewSession() | ||
if err != nil { | ||
// return "", errors.WithStack(err) // don't need stack trace, not inside velero-plugin | ||
return "", err | ||
} | ||
|
||
for _, partition := range endpoints.DefaultPartitions() { | ||
for regionHint := range partition.Regions() { | ||
region, _ = s3manager.GetBucketRegion(context.Background(), session, bucket, regionHint) | ||
|
||
// we only need to try a single region hint per partition, so break after the first | ||
break | ||
} | ||
|
||
if region != "" { | ||
return region, nil | ||
} | ||
} | ||
|
||
return "", errors.New("unable to determine bucket's region") | ||
} | ||
|
||
// For tests to use when discoverable bucket is required. | ||
const DiscoverableBucket string = "openshift-velero-plugin-s3-auto-region-test-1" |
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,84 @@ | ||
package aws | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
// from https://github.com/openshift/openshift-velero-plugin/pull/223/files#diff-4f17f1708744bd4d8cb7a4232212efa0e3bfde2b9c7b12e3a23dcc913b9fc2ec | ||
func TestGetBucketRegion(t *testing.T) { | ||
type args struct { | ||
bucket string | ||
} | ||
tests := []struct { | ||
name string | ||
bucket string | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "openshift-velero-plugin-s3-auto-region-test-1", | ||
bucket: "openshift-velero-plugin-s3-auto-region-test-1", | ||
want: "us-east-1", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "openshift-velero-plugin-s3-auto-region-test-2", | ||
bucket: "openshift-velero-plugin-s3-auto-region-test-2", | ||
want: "us-west-1", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "openshift-velero-plugin-s3-auto-region-test-3", | ||
bucket: "openshift-velero-plugin-s3-auto-region-test-3", | ||
want: "eu-central-1", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "openshift-velero-plugin-s3-auto-region-test-4", | ||
bucket: "openshift-velero-plugin-s3-auto-region-test-4", | ||
want: "sa-east-1", | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := GetBucketRegion(tt.bucket) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("GetBucketRegion() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("GetBucketRegion() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestBucketRegionIsDiscoverable(t *testing.T) { | ||
type args struct { | ||
bucket string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
}{ | ||
{ | ||
name: "openshift-velero-plugin-s3-auto-region-test-1 region is discoverable", | ||
args: args{bucket: "openshift-velero-plugin-s3-auto-region-test-1"}, | ||
want: true, | ||
}, | ||
{ | ||
name: "bucketNamesOverSixtyThreeCharactersAndNowItIsAboutTimeToTestThisFunction is an invalid aws bucket name so region is not discoverable", | ||
args: args{bucket: "bucketNamesOverSixtyThreeCharactersAndNowItIsAboutTimeToTestThisFunction"}, | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := BucketRegionIsDiscoverable(tt.args.bucket); got != tt.want { | ||
t.Errorf("BucketRegionIsDiscoverable() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |