From 11392e27af557ea46ff4c8f9a149d24e76d51dfb Mon Sep 17 00:00:00 2001 From: hoanglm Date: Mon, 18 Nov 2024 23:20:04 +0700 Subject: [PATCH] [Object Storage] Fix: Address linting issues for improved code quality --- ...asource_object_storage_bucket_lifecycle.go | 5 +- ...tasource_object_storage_sub_user_detail.go | 25 +++++++--- .../object-storage/resource_access_key.go | 35 +++++++++++--- fptcloud/object-storage/resource_bucket.go | 6 ++- .../object-storage/resource_bucket_acl.go | 16 +++++-- .../object-storage/resource_bucket_cors.go | 13 ++++-- .../resource_bucket_lifecycle.go | 17 +++++-- .../object-storage/resource_bucket_policy.go | 6 ++- .../resource_bucket_static_website.go | 14 ++++-- .../resource_bucket_versioning.go | 5 +- fptcloud/object-storage/resource_sub_user.go | 20 ++------ .../object-storage/resource_sub_user_keys.go | 14 ++++-- fptcloud/provider.go | 46 +++++++++---------- main.go | 1 - 14 files changed, 145 insertions(+), 78 deletions(-) diff --git a/fptcloud/object-storage/datasource_object_storage_bucket_lifecycle.go b/fptcloud/object-storage/datasource_object_storage_bucket_lifecycle.go index 5c1ffe3..4fcee2f 100644 --- a/fptcloud/object-storage/datasource_object_storage_bucket_lifecycle.go +++ b/fptcloud/object-storage/datasource_object_storage_bucket_lifecycle.go @@ -143,7 +143,10 @@ func dataSourceBucketLifecycleRead(ctx context.Context, d *schema.ResourceData, d.SetId(bucketName) var formattedData []interface{} if lifeCycleResponse.Total == 0 { - d.Set("life_cycle_rules", make([]interface{}, 0)) + if err := d.Set("life_cycle_rules", make([]interface{}, 0)); err != nil { + d.SetId("") + return diag.FromErr(err) + } } for _, lifecycleRule := range lifeCycleResponse.Rules { data := map[string]interface{}{ diff --git a/fptcloud/object-storage/datasource_object_storage_sub_user_detail.go b/fptcloud/object-storage/datasource_object_storage_sub_user_detail.go index 9660399..8befde7 100644 --- a/fptcloud/object-storage/datasource_object_storage_sub_user_detail.go +++ b/fptcloud/object-storage/datasource_object_storage_sub_user_detail.go @@ -72,16 +72,29 @@ func dataSourceSubUserDetailRead(ctx context.Context, d *schema.ResourceData, m } d.SetId(subUser.UserID) - d.Set("user_id", subUser.UserID) + if err := d.Set("user_id", subUser.UserID); err != nil { + return diag.FromErr(err) + } if subUser.Arn != nil { - d.Set("arn", subUser.Arn) + if err := d.Set("arn", subUser.Arn); err != nil { + return diag.FromErr(err) + } + } + + if err := d.Set("role", subUser.Role); err != nil { + return diag.FromErr(err) + } + if err := d.Set("active", subUser.Active); err != nil { + return diag.FromErr(err) } - d.Set("active", subUser.Active) - d.Set("role", subUser.Role) if subUser.CreatedAt != nil { - d.Set("created_at", subUser.CreatedAt) + if err := d.Set("created_at", subUser.CreatedAt); err != nil { + return diag.FromErr(err) + } + } + if err := d.Set("access_keys", subUser.AccessKeys); err != nil { + return diag.FromErr(err) } - d.Set("access_keys", subUser.AccessKeys) return nil } diff --git a/fptcloud/object-storage/resource_access_key.go b/fptcloud/object-storage/resource_access_key.go index 85b8189..8914685 100644 --- a/fptcloud/object-storage/resource_access_key.go +++ b/fptcloud/object-storage/resource_access_key.go @@ -77,13 +77,25 @@ func resourceAccessKeyCreate(ctx context.Context, d *schema.ResourceData, m inte if resp.Credential.AccessKey != "" { d.SetId(resp.Credential.AccessKey) - d.Set("access_key_id", resp.Credential.AccessKey) - d.Set("secret_access_key", resp.Credential.SecretKey) + if err := d.Set("access_key_id", resp.Credential.AccessKey); err != nil { + d.SetId("") + return diag.FromErr(err) + } + if err := d.Set("secret_access_key", resp.Credential.SecretKey); err != nil { + d.SetId("") + return diag.FromErr(err) + } } - d.Set("status", resp.Status) + if err := d.Set("status", resp.Status); err != nil { + d.SetId("") + return diag.FromErr(err) + } if resp.Message != "" { - d.Set("message", resp.Message) + if err := d.Set("message", resp.Message); err != nil { + d.SetId("") + return diag.FromErr(err) + } } return nil @@ -104,8 +116,14 @@ func resourceAccessKeyRead(ctx context.Context, d *schema.ResourceData, m interf for _, accessKey := range resp.Credentials { for _, key := range accessKey.Credentials { if key.AccessKey == accessKeyId { - d.Set("access_key_id", key.AccessKey) - d.Set("secret_access_key", secretAccessKey) + if err := d.Set("access_key_id", key.AccessKey); err != nil { + d.SetId("") + return diag.FromErr(err) + } + if err := d.Set("secret_access_key", secretAccessKey); err != nil { + d.SetId("") + return diag.FromErr(err) + } break } } @@ -142,7 +160,10 @@ func resourceAccessKeyDelete(ctx context.Context, d *schema.ResourceData, m inte log.Printf("[ERROR] Failed to delete access key %s: %v", accessKeyId, err) return diag.FromErr(err) } - d.Set("status", true) + if err := d.Set("status", true); err != nil { + d.SetId("") + return diag.FromErr(err) + } d.SetId("") return nil } diff --git a/fptcloud/object-storage/resource_bucket.go b/fptcloud/object-storage/resource_bucket.go index 20d9695..366abd8 100644 --- a/fptcloud/object-storage/resource_bucket.go +++ b/fptcloud/object-storage/resource_bucket.go @@ -99,7 +99,7 @@ func resourceBucketCreate(ctx context.Context, d *schema.ResourceData, m interfa bucket := objectStorageService.CreateBucket(req, vpcId, s3ServiceDetail.S3ServiceId) if !bucket.Status { - return diag.Errorf(bucket.Message) + return diag.Errorf("%s", bucket.Message) } return resourceBucketRead(ctx, d, m) } @@ -118,7 +118,9 @@ func resourceBucketRead(_ context.Context, d *schema.ResourceData, m interface{} } for _, b := range bucket.Buckets { if b.Name == d.Get("name").(string) { - d.Set("name", b.Name) + if err := d.Set("name", b.Name); err != nil { + return diag.FromErr(err) + } return nil } } diff --git a/fptcloud/object-storage/resource_bucket_acl.go b/fptcloud/object-storage/resource_bucket_acl.go index 6636813..3d1c0e9 100644 --- a/fptcloud/object-storage/resource_bucket_acl.go +++ b/fptcloud/object-storage/resource_bucket_acl.go @@ -78,10 +78,14 @@ func resourceBucketAclCreate(ctx context.Context, d *schema.ResourceData, m inte r := service.PutBucketAcl(vpcId, s3ServiceDetail.S3ServiceId, bucketName, bucketAclRequest) if !r.Status { - d.Set("status", false) + if err := d.Set("status", false); err != nil { + return diag.Errorf("failed to create bucket ACL for bucket %s", bucketName) + } return diag.Errorf("failed to create bucket ACL for bucket %s", bucketName) } - d.Set("status", true) + if err := d.Set("status", true); err != nil { + return diag.FromErr(err) + } return resourceBucketAclRead(ctx, d, m) } @@ -99,8 +103,12 @@ func resourceBucketAclRead(ctx context.Context, d *schema.ResourceData, m interf if !r.Status { return diag.Errorf("failed to get bucket ACL for bucket %s", bucketName) } - d.Set("canned_acl", r.CannedACL) - d.Set("status", r.Status) + if err := d.Set("canned_acl", r.CannedACL); err != nil { + return diag.FromErr(err) + } + if err := d.Set("status", r.Status); err != nil { + return diag.FromErr(err) + } return nil } diff --git a/fptcloud/object-storage/resource_bucket_cors.go b/fptcloud/object-storage/resource_bucket_cors.go index af36d19..dca724d 100644 --- a/fptcloud/object-storage/resource_bucket_cors.go +++ b/fptcloud/object-storage/resource_bucket_cors.go @@ -112,7 +112,9 @@ func resourceBucketCorsCreate(ctx context.Context, d *schema.ResourceData, m int } r := service.CreateBucketCors(vpcId, s3ServiceDetail.S3ServiceId, bucketName, payload) if !r.Status { - d.Set("status", false) + if err := d.Set("status", false); err != nil { + return diag.FromErr(err) + } return diag.FromErr(fmt.Errorf("%s", r.Message)) } d.SetId(bucketName) @@ -141,7 +143,10 @@ func resourceBucketCorsRead(_ context.Context, d *schema.ResourceData, m interfa d.SetId(bucketName) var formattedData []interface{} if bucketCorsDetails.Total == 0 { - d.Set("bucket_cors_rules", make([]interface{}, 0)) + if err := d.Set("bucket_cors_rules", make([]interface{}, 0)); err != nil { + d.SetId("") + return diag.FromErr(err) + } } for _, corsRuleDetail := range bucketCorsDetails.CorsRules { data := map[string]interface{}{ @@ -198,7 +203,9 @@ func resourceBucketCorsDelete(ctx context.Context, d *schema.ResourceData, m int } r := service.UpdateBucketCors(vpcId, s3ServiceDetail.S3ServiceId, bucketName, payload) if !r.Status { - d.Set("status", false) + if err := d.Set("status", false); err != nil { + return diag.FromErr(err) + } return diag.FromErr(fmt.Errorf("%s", r.Message)) } d.SetId(bucketName) diff --git a/fptcloud/object-storage/resource_bucket_lifecycle.go b/fptcloud/object-storage/resource_bucket_lifecycle.go index 21db918..48b6600 100644 --- a/fptcloud/object-storage/resource_bucket_lifecycle.go +++ b/fptcloud/object-storage/resource_bucket_lifecycle.go @@ -114,12 +114,16 @@ func resourceBucketLifeCycleCreate(ctx context.Context, d *schema.ResourceData, payload["Expiration"] = map[string]interface{}{"ExpiredObjectDeleteMarker": jsonMap.Expiration.ExpiredObjectDeleteMarker} } r := service.PutBucketLifecycle(vpcId, s3ServiceDetail.S3ServiceId, bucketName, payload) + d.SetId(bucketName) if !r.Status { - d.Set("state", false) + if err := d.Set("state", false); err != nil { + d.SetId("") + return diag.FromErr(err) + } return diag.FromErr(fmt.Errorf("%s", r.Message)) } - d.SetId(bucketName) if err := d.Set("state", true); err != nil { + d.SetId("") return diag.FromErr(err) } @@ -145,7 +149,10 @@ func resourceBucketLifeCycleRead(_ context.Context, d *schema.ResourceData, m in d.SetId(bucketName) var formattedData []interface{} if lifeCycleResponse.Total == 0 { - d.Set("life_cycle_rules", make([]interface{}, 0)) + if err := d.Set("life_cycle_rules", make([]interface{}, 0)); err != nil { + d.SetId("") + return diag.FromErr(err) + } } for _, lifecycleRule := range lifeCycleResponse.Rules { data := map[string]interface{}{ @@ -204,7 +211,9 @@ func resourceBucketLifeCycleDelete(ctx context.Context, d *schema.ResourceData, } r := service.DeleteBucketLifecycle(vpcId, s3ServiceDetail.S3ServiceId, bucketName, payload) if !r.Status { - d.Set("state", false) + if err := d.Set("state", false); err != nil { + return diag.FromErr(err) + } return diag.FromErr(fmt.Errorf("%s", r.Message)) } d.SetId(bucketName) diff --git a/fptcloud/object-storage/resource_bucket_policy.go b/fptcloud/object-storage/resource_bucket_policy.go index b1d257d..79cd064 100644 --- a/fptcloud/object-storage/resource_bucket_policy.go +++ b/fptcloud/object-storage/resource_bucket_policy.go @@ -96,8 +96,10 @@ func resourceBucketPolicyCreate(ctx context.Context, d *schema.ResourceData, m i resp := service.PutBucketPolicy(vpcId, s3ServiceDetail.S3ServiceId, bucketName, payload) if !resp.Status { - d.Set("status", false) - return diag.Errorf(fmt.Sprintf("Error create bucket policy: %s", resp.Message)) + if err := d.Set("status", false); err != nil { + return diag.Errorf("failed to create bucket policy: %s", resp.Message) + } + return diag.FromErr(fmt.Errorf("error create bucket policy: %s", resp.Message)) } d.SetId(bucketName) if err := d.Set("status", true); err != nil { diff --git a/fptcloud/object-storage/resource_bucket_static_website.go b/fptcloud/object-storage/resource_bucket_static_website.go index 6636c9f..cbc0c31 100644 --- a/fptcloud/object-storage/resource_bucket_static_website.go +++ b/fptcloud/object-storage/resource_bucket_static_website.go @@ -80,14 +80,18 @@ func resourceBucketStaticWebsiteCreate(ctx context.Context, d *schema.ResourceDa Suffix: indexDocument, Key: errorDocument, }) - + d.SetId(bucketName) if !putBucketWebsite.Status { - diag.Errorf("failed to create bucket website for bucket %s", bucketName) - d.Set("status", false) + if err := d.Set("status", false); err != nil { + d.SetId("") + return diag.Errorf("failed to create bucket website for bucket %s", bucketName) + } return nil } - d.Set("status", true) - d.SetId(bucketName) + if err := d.Set("status", true); err != nil { + d.SetId("") + return diag.FromErr(err) + } return nil } diff --git a/fptcloud/object-storage/resource_bucket_versioning.go b/fptcloud/object-storage/resource_bucket_versioning.go index dd671e4..e744ed4 100644 --- a/fptcloud/object-storage/resource_bucket_versioning.go +++ b/fptcloud/object-storage/resource_bucket_versioning.go @@ -67,7 +67,10 @@ func resourceBucketVersioningCreate(ctx context.Context, d *schema.ResourceData, return diag.FromErr(err) } d.SetId(fmt.Sprintf("%s:%s", bucketName, versioningStatus)) - d.Set("versioning_status", versioningStatus) + if err := d.Set("versioning_status", versioningStatus); err != nil { + d.SetId("") + return diag.FromErr(err) + } return nil } diff --git a/fptcloud/object-storage/resource_sub_user.go b/fptcloud/object-storage/resource_sub_user.go index d2231d1..254e21e 100644 --- a/fptcloud/object-storage/resource_sub_user.go +++ b/fptcloud/object-storage/resource_sub_user.go @@ -62,26 +62,14 @@ func resourceSubUserCreate(ctx context.Context, d *schema.ResourceData, m interf // Set the resource ID after successful creation d.SetId(subUserId) - d.Set("user_id", subUserId) - - return nil -} -func readDetailSubUserOnly(ctx context.Context, d *schema.ResourceData, m interface{}, subUserId string) diag.Diagnostics { - client := m.(*common.Client) - objectStorageService := NewObjectStorageService(client) - vpcId := d.Get("vpc_id").(string) - s3ServiceDetail := getServiceEnableRegion(objectStorageService, vpcId, d.Get("region_name").(string)) - if s3ServiceDetail.S3ServiceId == "" { - return diag.FromErr(fmt.Errorf("region %s is not enabled", d.Get("region_name").(string))) - } - subUser := objectStorageService.DetailSubUser(subUserId, vpcId, s3ServiceDetail.S3ServiceId) - if subUser == nil { + if err := d.Set("user_id", subUserId); err != nil { d.SetId("") - return nil + return diag.FromErr(err) } - d.SetId(subUserId) + return nil } + func resourceSubUserDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { client := m.(*common.Client) objectStorageService := NewObjectStorageService(client) diff --git a/fptcloud/object-storage/resource_sub_user_keys.go b/fptcloud/object-storage/resource_sub_user_keys.go index 36ae2b1..411b707 100644 --- a/fptcloud/object-storage/resource_sub_user_keys.go +++ b/fptcloud/object-storage/resource_sub_user_keys.go @@ -63,8 +63,14 @@ func resourceSubUserAccessKeyCreate(ctx context.Context, d *schema.ResourceData, } d.SetId(resp.Credential.AccessKey) - d.Set("access_key", resp.Credential.AccessKey) - d.Set("secret_key", resp.Credential.SecretKey) + if err := d.Set("access_key", resp.Credential.AccessKey); err != nil { + d.SetId("") + return diag.FromErr(err) + } + if err := d.Set("secret_key", resp.Credential.SecretKey); err != nil { + d.SetId("") + return diag.FromErr(err) + } return nil } @@ -83,7 +89,9 @@ func resourceReadUserDetail(ctx context.Context, d *schema.ResourceData, m inter if subUser.UserID == "" { return diag.Errorf("sub-user with ID %s not found", subUserId) } - d.Set("user_id", subUser.UserID) + if err := d.Set("user_id", subUser.UserID); err != nil { + return diag.FromErr(err) + } return nil } func resourceSubUserAccessKeyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { diff --git a/fptcloud/provider.go b/fptcloud/provider.go index 89c6273..c769de0 100644 --- a/fptcloud/provider.go +++ b/fptcloud/provider.go @@ -63,29 +63,29 @@ func Provider() *schema.Provider { }, }, DataSourcesMap: map[string]*schema.Resource{ - "fptcloud_storage_policy": fptcloud_storage_policy.DataSourceStoragePolicy(), - "fptcloud_storage": fptcloud_storage.DataSourceStorage(), - "fptcloud_ssh_key": fptcloud_ssh.DataSourceSSHKey(), - "fptcloud_vpc": fptcloud_vpc.NewDataSource(), - "fptcloud_flavor": fptcloud_flavor.DataSourceFlavor(), - "fptcloud_image": fptcloud_image.DataSourceImage(), - "fptcloud_security_group": fptcloud_security_group.DataSourceSecurityGroup(), - "fptcloud_instance": fptcloud_instance.DataSourceInstance(), - "fptcloud_instance_group_policy": fptcloud_instance_group_policy.DataSourceInstanceGroupPolicy(), - "fptcloud_instance_group": fptcloud_instance_group.DataSourceInstanceGroup(), - "fptcloud_floating_ip": fptcloud_floating_ip.DataSourceFloatingIp(), - "fptcloud_subnet": fptcloud_subnet.DataSourceSubnet(), - "fptcloud_object_storage_access_key": fptcloud_object_storage.DataSourceAccessKey(), - "fptcloud_object_storage_sub_user": fptcloud_object_storage.DataSourceSubUser(), - "fptcloud_object_storage_bucket": fptcloud_object_storage.DataSourceBucket(), - "fptcloud_object_storage_bucket_policy": fptcloud_object_storage.DataSourceBucketPolicy(), - "fptcloud_object_storage_bucket_cors": fptcloud_object_storage.DataSourceBucketCors(), - "fptcloud_object_storage_bucket_versioning": fptcloud_object_storage.DataSourceBucketVersioning(), - "fptcloud_object_storage_bucket_lifecycle": fptcloud_object_storage.DataSourceBucketLifecycle(), - "fptcloud_object_storage_bucket_static_website": fptcloud_object_storage.DataSourceBucketStaticWebsite(), - "fptcloud_object_storage_sub_user_detail": fptcloud_object_storage.DataSourceSubUserDetail(), - "fptcloud_s3_service_enable": fptcloud_object_storage.DataSourceS3ServiceEnableResponse(), - "fptcloud_object_storage_bucket_acl": fptcloud_object_storage.DataSourceBucketAcl(), + "fptcloud_storage_policy": fptcloud_storage_policy.DataSourceStoragePolicy(), + "fptcloud_storage": fptcloud_storage.DataSourceStorage(), + "fptcloud_ssh_key": fptcloud_ssh.DataSourceSSHKey(), + "fptcloud_vpc": fptcloud_vpc.NewDataSource(), + "fptcloud_flavor": fptcloud_flavor.DataSourceFlavor(), + "fptcloud_image": fptcloud_image.DataSourceImage(), + "fptcloud_security_group": fptcloud_security_group.DataSourceSecurityGroup(), + "fptcloud_instance": fptcloud_instance.DataSourceInstance(), + "fptcloud_instance_group_policy": fptcloud_instance_group_policy.DataSourceInstanceGroupPolicy(), + "fptcloud_instance_group": fptcloud_instance_group.DataSourceInstanceGroup(), + "fptcloud_floating_ip": fptcloud_floating_ip.DataSourceFloatingIp(), + "fptcloud_subnet": fptcloud_subnet.DataSourceSubnet(), + "fptcloud_object_storage_access_key": fptcloud_object_storage.DataSourceAccessKey(), + "fptcloud_object_storage_sub_user": fptcloud_object_storage.DataSourceSubUser(), + "fptcloud_object_storage_bucket": fptcloud_object_storage.DataSourceBucket(), + "fptcloud_object_storage_bucket_policy": fptcloud_object_storage.DataSourceBucketPolicy(), + "fptcloud_object_storage_bucket_cors": fptcloud_object_storage.DataSourceBucketCors(), + "fptcloud_object_storage_bucket_versioning": fptcloud_object_storage.DataSourceBucketVersioning(), + "fptcloud_object_storage_bucket_lifecycle": fptcloud_object_storage.DataSourceBucketLifecycle(), + "fptcloud_object_storage_bucket_static_website": fptcloud_object_storage.DataSourceBucketStaticWebsite(), + "fptcloud_object_storage_sub_user_detail": fptcloud_object_storage.DataSourceSubUserDetail(), + "fptcloud_s3_service_enable": fptcloud_object_storage.DataSourceS3ServiceEnableResponse(), + "fptcloud_object_storage_bucket_acl": fptcloud_object_storage.DataSourceBucketAcl(), }, ResourcesMap: map[string]*schema.Resource{ "fptcloud_storage": fptcloud_storage.ResourceStorage(), diff --git a/main.go b/main.go index 6e615a5..1ec6d5f 100644 --- a/main.go +++ b/main.go @@ -31,7 +31,6 @@ func main() { providerserver.NewProtocol5(fptcloud.NewXplatProvider("dev")()), fptcloud.Provider().GRPCProvider, } - log.Printf("[DEBUG] providers: ", providers) muxServer, err := tf5muxserver.NewMuxServer(ctx, providers...) if err != nil {