Skip to content

Commit

Permalink
Support for the changes of exiting API from object storage services gen2
Browse files Browse the repository at this point in the history
  • Loading branch information
zliang-akamai committed Jan 28, 2025
1 parent c8da727 commit c5574d9
Show file tree
Hide file tree
Showing 16 changed files with 332 additions and 77 deletions.
51 changes: 51 additions & 0 deletions linode/acceptance/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,57 @@ func ModifyProviderMeta(provider *schema.Provider, modifier ProviderMetaModifier
}
}

func GetEndpointType(e linodego.ObjectStorageEndpoint) string {
return string(e.EndpointType)
}

func GetEndpointRegion(e linodego.ObjectStorageEndpoint) string {
return e.Region
}

func GetEndpointCluster(e linodego.ObjectStorageEndpoint) (string, error) {
if e.S3Endpoint == nil {
return "", fmt.Errorf(
"the %q type endpoint is nil for region %q for the user",
e.EndpointType, e.Region,
)
}

endpointURL := *e.S3Endpoint
splittedURL := strings.Split(endpointURL, ".")
if len(splittedURL) == 0 {
return "", fmt.Errorf("invalid s3 endpoint received: %v", splittedURL)
}

return strings.Split(endpointURL, ".")[0], nil
}

// Get an Object Storage services endpoint with non-nil S3Endpoint
func GetRandomObjectStorageEndpoint() (*linodego.ObjectStorageEndpoint, error) {
client, err := GetTestClient()
if err != nil {
return nil, err
}

endpoints, err := client.ListObjectStorageEndpoints(context.Background(), nil)
if err != nil {
return nil, err
}

rand.Shuffle(len(endpoints), func(i, j int) {
endpoints[i], endpoints[j] = endpoints[j], endpoints[i]
})

for i := range endpoints {
if endpoints[i].S3Endpoint != nil {
result := endpoints[i]
return &result, nil
}
}

return nil, errors.New("failed to get an object storage endpoint")
}

// GetRegionsWithCaps returns a list of region IDs that support the given capabilities
// Parameters:
// - capabilities: Required capabilities that the regions must support.
Expand Down
5 changes: 1 addition & 4 deletions linode/obj/framework_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

s3manager "github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
Expand Down Expand Up @@ -137,9 +136,7 @@ func (plan *ResourceModel) ComputeEndpointIfUnknown(ctx context.Context, client
return
}

plan.Endpoint = types.StringValue(
strings.TrimPrefix(bucket.Hostname, fmt.Sprintf("%s.", bucket.Label)),
)
plan.Endpoint = types.StringValue(bucket.S3Endpoint)
}

func (data *ResourceModel) GenerateObjectStorageObjectID(apply bool, preserveKnown bool) string {
Expand Down
10 changes: 7 additions & 3 deletions linode/obj/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ var (
)

func init() {
region, err := acceptance.GetRandomRegionWithCaps([]string{"Object Storage"}, "core")
endpoint, err := acceptance.GetRandomObjectStorageEndpoint()
if err != nil {
log.Fatal(err)
}

testCluster = region + "-1"
testRegion = region
testCluster, err = acceptance.GetEndpointCluster(*endpoint)
if err != nil {
log.Fatal(err)
}

testRegion = acceptance.GetEndpointRegion(*endpoint)
}

func TestAccResourceObject_basic_cluster(t *testing.T) {
Expand Down
20 changes: 12 additions & 8 deletions linode/objbucket/framework_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,21 @@ func (data *DataSourceModel) parseObjectStorageBucket(bucket *linodego.ObjectSto
data.Label = types.StringValue(bucket.Label)
data.Objects = types.Int64Value(int64(bucket.Objects))
data.Size = types.Int64Value(int64(bucket.Size))
data.EndpointType = types.StringValue(string(bucket.EndpointType))
data.S3Endpoint = types.StringValue(bucket.S3Endpoint)
}

type DataSourceModel struct {
Cluster types.String `tfsdk:"cluster"`
Region types.String `tfsdk:"region"`
Created types.String `tfsdk:"created"`
Hostname types.String `tfsdk:"hostname"`
ID types.String `tfsdk:"id"`
Label types.String `tfsdk:"label"`
Objects types.Int64 `tfsdk:"objects"`
Size types.Int64 `tfsdk:"size"`
Cluster types.String `tfsdk:"cluster"`
Region types.String `tfsdk:"region"`
Created types.String `tfsdk:"created"`
Hostname types.String `tfsdk:"hostname"`
ID types.String `tfsdk:"id"`
Label types.String `tfsdk:"label"`
Objects types.Int64 `tfsdk:"objects"`
Size types.Int64 `tfsdk:"size"`
EndpointType types.String `tfsdk:"endpoint_type"`
S3Endpoint types.String `tfsdk:"s3_endpoint"`
}

func (d *DataSource) Read(
Expand Down
8 changes: 8 additions & 0 deletions linode/objbucket/framework_datasource_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ var frameworkDatasourceSchema = schema.Schema{
),
},
},
"endpoint_type": schema.StringAttribute{
Description: "The type of the S3 endpoint of the bucket.",
Computed: true,
},
"s3_endpoint": schema.StringAttribute{
Description: "The S3 endpoint URL of the bucket, based on the `endpoint_type` and `region`.",
Computed: true,
},
"created": schema.StringAttribute{
Description: "When this bucket was created.",
Computed: true,
Expand Down
39 changes: 25 additions & 14 deletions linode/objbucket/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,11 @@ func readResource(
}

tflog.Debug(ctx, "getting bucket access info")
access, err := client.GetObjectStorageBucketAccess(ctx, regionOrCluster, label)
access, err := client.GetObjectStorageBucketAccessV2(ctx, regionOrCluster, label)
if err != nil {
return diag.Errorf("failed to find the access config for the specified Linode ObjectStorageBucket: %s", err)
}

// Functionality requiring direct S3 API access
endpoint := helper.ComputeS3EndpointFromBucket(ctx, *bucket)

_, versioningPresent := d.GetOk("versioning")
_, lifecyclePresent := d.GetOk("lifecycle_rule")

Expand All @@ -105,7 +102,7 @@ func readResource(
defer teardownKeysCleanUp()
}

s3Client, err := helper.S3Connection(ctx, endpoint, objKeys.AccessKey, objKeys.SecretKey)
s3Client, err := helper.S3Connection(ctx, bucket.S3Endpoint, objKeys.AccessKey, objKeys.SecretKey)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -132,7 +129,8 @@ func readResource(
d.Set("hostname", bucket.Hostname)
d.Set("acl", access.ACL)
d.Set("cors_enabled", access.CorsEnabled)
d.Set("endpoint", endpoint)
d.Set("endpoint", bucket.S3Endpoint)
d.Set("endpoint_type", bucket.EndpointType)

return nil
}
Expand All @@ -146,18 +144,31 @@ func createResource(

label := d.Get("label").(string)
acl := d.Get("acl").(string)
corsEnabled := d.Get("cors_enabled").(bool)

createOpts := linodego.ObjectStorageBucketCreateOptions{
Label: label,
ACL: linodego.ObjectStorageACL(acl),
CorsEnabled: &corsEnabled,
Label: label,
ACL: linodego.ObjectStorageACL(acl),
}

if corsEnabled, ok := d.GetOk("cors_enabled"); ok {
corsEnabledBool := corsEnabled.(bool)
createOpts.CorsEnabled = &corsEnabledBool
}

if endpoint, ok := d.GetOk("endpoint"); ok {
createOpts.S3Endpoint = endpoint.(string)
}

if endpointType, ok := d.GetOk("endpoint_type"); ok {
createOpts.EndpointType = linodego.ObjectStorageEndpointType(endpointType.(string))
}

if region, ok := d.GetOk("region"); ok {
createOpts.Region = region.(string)
} else {
createOpts.Cluster = d.Get("cluster").(string)
}

if cluster, ok := d.GetOk("cluster"); ok {
createOpts.Cluster = cluster.(string)
}

tflog.Debug(ctx, "client.CreateObjectStorageBucket(...)", map[string]any{"options": createOpts})
Expand All @@ -166,7 +177,7 @@ func createResource(
return diag.Errorf("failed to create a Linode ObjectStorageBucket: %s", err)
}

d.Set("endpoint", helper.ComputeS3EndpointFromBucket(ctx, *bucket))
d.Set("endpoint", bucket.S3Endpoint)

if bucket.Region != "" {
d.SetId(fmt.Sprintf("%s:%s", bucket.Region, bucket.Label))
Expand Down Expand Up @@ -455,7 +466,7 @@ func updateBucketCert(
}

uploadOptions := expandBucketCert(certSpec[0])
if _, err := client.UploadObjectStorageBucketCert(ctx, regionOrCluster, label, uploadOptions); err != nil {
if _, err := client.UploadObjectStorageBucketCertV2(ctx, regionOrCluster, label, uploadOptions); err != nil {
return fmt.Errorf("failed to upload new bucket cert: %s", err)
}
return nil
Expand Down
Loading

0 comments on commit c5574d9

Please sign in to comment.