From 3cb44f5108a9e4228eaa71839ea866331b5a19ab Mon Sep 17 00:00:00 2001 From: elkezza Date: Fri, 15 Nov 2024 16:39:36 +0000 Subject: [PATCH] fixing the ignored --zone flag bug, when the user define the zone of the sos to list it's buckets. --- cmd/storage_list.go | 2 +- pkg/storage/sos/client.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/cmd/storage_list.go b/cmd/storage_list.go index 9a9c046b..8317b6d8 100644 --- a/cmd/storage_list.go +++ b/cmd/storage_list.go @@ -82,7 +82,7 @@ Supported output template annotations: storage, err := sos.NewStorageClient( gContext, - sos.ClientOptZoneFromBucket(gContext, bucket), + sos.ClientOptZoneFromBucketWithZone(gContext, bucket, zone), ) if err != nil { return fmt.Errorf("unable to initialize storage client: %w", err) diff --git a/pkg/storage/sos/client.go b/pkg/storage/sos/client.go index d46e10fa..9612cac9 100644 --- a/pkg/storage/sos/client.go +++ b/pkg/storage/sos/client.go @@ -184,6 +184,39 @@ func ClientOptZoneFromBucket(ctx context.Context, bucket string) ClientOpt { } } + +func ClientOptZoneFromBucketWithZone(ctx context.Context, bucket string, zone string) ClientOpt { + return func(c *Client) error { + cfg, err := awsconfig.LoadDefaultConfig( + ctx, + append(CommonConfigOptFns, + awsconfig.WithEndpointResolver(aws.EndpointResolverFunc( + func(service, region string) (aws.Endpoint, error) { + sosURL := strings.Replace( + account.CurrentAccount.SosEndpoint, + "{zone}", + zone, + 1, + ) + return aws.Endpoint{URL: sosURL}, nil + })), + )...) + if err != nil { + return err + } + + region, err := s3manager.GetBucketRegion(ctx, s3.NewFromConfig(cfg), bucket, func(o *s3.Options) { + o.UsePathStyle = true + }) + if err != nil { + return err + } + + c.Zone = region + return nil + } +} + func NewStorageClient(ctx context.Context, opts ...ClientOpt) (*Client, error) { var ( client = Client{