Skip to content

Commit

Permalink
[Object Storage] Fix: Address linting issues for improved code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
hoanglm committed Nov 18, 2024
1 parent f0d4ee9 commit 11392e2
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
35 changes: 28 additions & 7 deletions fptcloud/object-storage/resource_access_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
}
6 changes: 4 additions & 2 deletions fptcloud/object-storage/resource_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}
}
Expand Down
16 changes: 12 additions & 4 deletions fptcloud/object-storage/resource_bucket_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
}

Expand Down
13 changes: 10 additions & 3 deletions fptcloud/object-storage/resource_bucket_cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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{}{
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 13 additions & 4 deletions fptcloud/object-storage/resource_bucket_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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{}{
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions fptcloud/object-storage/resource_bucket_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 9 additions & 5 deletions fptcloud/object-storage/resource_bucket_static_website.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 4 additions & 1 deletion fptcloud/object-storage/resource_bucket_versioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
20 changes: 4 additions & 16 deletions fptcloud/object-storage/resource_sub_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 11 additions & 3 deletions fptcloud/object-storage/resource_sub_user_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
46 changes: 23 additions & 23 deletions fptcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Loading

0 comments on commit 11392e2

Please sign in to comment.