Skip to content

Commit

Permalink
[Object Storage] fix: fix linting, remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
hoanglm committed Nov 27, 2024
1 parent 88edf87 commit bfa1e65
Show file tree
Hide file tree
Showing 23 changed files with 241 additions and 229 deletions.
21 changes: 11 additions & 10 deletions fptcloud/object-storage/dataqsource_object_storage_bucket_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,19 @@ func DataSourceBucketAcl() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBucketAclRead,
Schema: map[string]*schema.Schema{
"vpc_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The VPC ID",
},
"bucket_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the bucket to config the ACL",
},
"region_name": {
"vpc_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The region name that's are the same with the region name in the S3 service. Currently, we have: HCM-01, HCM-02, HN-01, HN-02",
Description: "The VPC ID",
},

"canned_acl": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -41,6 +36,12 @@ func DataSourceBucketAcl() *schema.Resource {
Computed: true,
Description: "The status after configuring the bucket ACL",
},
"region_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The region name that's are the same with the region name in the S3 service. Currently, we have: HCM-01, HCM-02, HN-01, HN-02",
},
"bucket_acl": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -104,12 +105,12 @@ func DataSourceBucketAcl() *schema.Resource {
func dataSourceBucketAclRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*common.Client)
service := NewObjectStorageService(client)
vpcId := d.Get("vpc_id").(string)
bucketName := d.Get("bucket_name").(string)
vpcId := d.Get("vpc_id").(string)
regionName := d.Get("region_name").(string)
s3ServiceDetail := getServiceEnableRegion(service, vpcId, regionName)
if s3ServiceDetail.S3ServiceId == "" {
return diag.FromErr(fmt.Errorf("region %s is not enabled", regionName))
return diag.FromErr(fmt.Errorf(regionError, regionName))
}
r := service.GetBucketAcl(vpcId, s3ServiceDetail.S3ServiceId, bucketName)
if !r.Status {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func dataSourceAccessKeyRead(ctx context.Context, d *schema.ResourceData, m inte
regionName := d.Get("region_name").(string)
s3ServiceDetail := getServiceEnableRegion(service, vpcId, regionName)
if s3ServiceDetail.S3ServiceId == "" {
return diag.FromErr(fmt.Errorf("region %s is not enabled", regionName))
return diag.FromErr(fmt.Errorf(regionError, regionName))
}
keys, err := service.ListAccessKeys(vpcId, s3ServiceDetail.S3ServiceId)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func dataSourceBucketRead(ctx context.Context, d *schema.ResourceData, m interfa
regionName := d.Get("region_name").(string)
s3ServiceDetail := getServiceEnableRegion(service, vpcId, regionName)
if s3ServiceDetail.S3ServiceId == "" {
return diag.FromErr(fmt.Errorf("region %s is not enabled", regionName))
return diag.FromErr(fmt.Errorf(regionError, regionName))
}
buckets := service.ListBuckets(vpcId, s3ServiceDetail.S3ServiceId, page, pageSize)
if buckets.Total == 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func dataSourceBucketCorsRead(ctx context.Context, d *schema.ResourceData, m int
vpcId := d.Get("vpc_id").(string)
s3ServiceDetail := getServiceEnableRegion(service, vpcId, d.Get("region_name").(string))
if s3ServiceDetail.S3ServiceId == "" {
return diag.FromErr(fmt.Errorf("region %s is not enabled", d.Get("region_name").(string)))
return diag.FromErr(fmt.Errorf(regionError, d.Get("region_name").(string)))
}
bucketName := d.Get("bucket_name").(string)
page := 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,41 +113,9 @@ func DataSourceBucketLifecycle() *schema.Resource {
},
}
}

func dataSourceBucketLifecycleRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*common.Client)
service := NewObjectStorageService(client)

bucketName := d.Get("bucket_name").(string)
vpcId := d.Get("vpc_id").(string)
regionName := d.Get("region_name").(string)
s3ServiceDetail := getServiceEnableRegion(service, vpcId, regionName)
if s3ServiceDetail.S3ServiceId == "" {
return diag.FromErr(fmt.Errorf("region %s is not enabled", regionName))
}
page := 1
v, ok := d.GetOk("page")
if ok {
page = v.(int)
}
pageSize := 25
v, ok = d.GetOk("page_size")
if ok {
pageSize = v.(int)
}

lifeCycleResponse := service.GetBucketLifecycle(vpcId, s3ServiceDetail.S3ServiceId, bucketName, page, pageSize)
if !lifeCycleResponse.Status {
return diag.FromErr(fmt.Errorf("failed to fetch life cycle rules for bucket %s", bucketName))
}
d.SetId(bucketName)
func parseData(lifeCycleResponse BucketLifecycleResponse) []interface{} {
var formattedData []interface{}
if lifeCycleResponse.Total == 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{}{
"id": lifecycleRule.ID,
Expand Down Expand Up @@ -191,7 +159,42 @@ func dataSourceBucketLifecycleRead(ctx context.Context, d *schema.ResourceData,
}
formattedData = append(formattedData, data)
}
return formattedData
}
func dataSourceBucketLifecycleRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*common.Client)
service := NewObjectStorageService(client)

bucketName := d.Get("bucket_name").(string)
vpcId := d.Get("vpc_id").(string)
regionName := d.Get("region_name").(string)
s3ServiceDetail := getServiceEnableRegion(service, vpcId, regionName)
if s3ServiceDetail.S3ServiceId == "" {
return diag.FromErr(fmt.Errorf(regionError, regionName))
}
page := 1
v, ok := d.GetOk("page")
if ok {
page = v.(int)
}
pageSize := 25
v, ok = d.GetOk("page_size")
if ok {
pageSize = v.(int)
}

lifeCycleResponse := service.GetBucketLifecycle(vpcId, s3ServiceDetail.S3ServiceId, bucketName, page, pageSize)
if !lifeCycleResponse.Status {
return diag.FromErr(fmt.Errorf("failed to fetch life cycle rules for bucket %s", bucketName))
}
if lifeCycleResponse.Total == 0 {
if err := d.Set("life_cycle_rules", make([]interface{}, 0)); err != nil {
d.SetId("")
return diag.FromErr(err)
}
}
d.SetId(bucketName)
formattedData := parseData(lifeCycleResponse)
if err := d.Set("life_cycle_rules", formattedData); err != nil {
d.SetId("")
return diag.FromErr(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func dataSourceBucketPolicyRead(ctx context.Context, d *schema.ResourceData, m i
vpcId := d.Get("vpc_id").(string)
s3ServiceDetail := getServiceEnableRegion(service, vpcId, d.Get("region_name").(string))
if s3ServiceDetail.S3ServiceId == "" {
return diag.FromErr(fmt.Errorf("region %s is not enabled", d.Get("region_name").(string)))
return diag.FromErr(fmt.Errorf(regionError, d.Get("region_name").(string)))
}
policyResponse := service.GetBucketPolicy(vpcId, s3ServiceDetail.S3ServiceId, bucketName)
if !policyResponse.Status {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func dataSourceBucketStaticWebsite(ctx context.Context, d *schema.ResourceData,
vpcId := d.Get("vpc_id").(string)
s3ServiceDetail := getServiceEnableRegion(service, vpcId, d.Get("region_name").(string))
if s3ServiceDetail.S3ServiceId == "" {
return diag.FromErr(fmt.Errorf("region %s is not enabled", d.Get("region_name").(string)))
return diag.FromErr(fmt.Errorf(regionError, d.Get("region_name").(string)))
}

staticWebsiteResponse := service.GetBucketWebsite(vpcId, s3ServiceDetail.S3ServiceId, bucketName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func dataSourceBucketVersioningRead(ctx context.Context, d *schema.ResourceData,
vpcId := d.Get("vpc_id").(string)
s3ServiceDetail := getServiceEnableRegion(service, vpcId, d.Get("region_name").(string))
if s3ServiceDetail.S3ServiceId == "" {
return diag.FromErr(fmt.Errorf("region %s is not enabled", d.Get("region_name").(string)))
return diag.FromErr(fmt.Errorf(regionError, d.Get("region_name").(string)))
}
bucketName := d.Get("bucket_name").(string)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func dataSourceSubUserRead(ctx context.Context, d *schema.ResourceData, m interf
regionName := d.Get("region_name").(string)
s3ServiceDetail := getServiceEnableRegion(service, vpcId, regionName)
if s3ServiceDetail.S3ServiceId == "" {
return diag.FromErr(fmt.Errorf("region %s is not enabled", regionName))
return diag.FromErr(fmt.Errorf(regionError, regionName))
}
page := 1
pageSize := 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,22 @@ func DataSourceSubUserDetail() *schema.Resource {
func dataSourceSubUserDetailRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*common.Client)
objectStorageService := NewObjectStorageService(client)

vpcId := d.Get("vpc_id").(string)
s3ServiceDetail := getServiceEnableRegion(objectStorageService, vpcId, d.Get("region_name").(string))
regionName := d.Get("region_name").(string)

s3ServiceDetail := getServiceEnableRegion(objectStorageService, vpcId, regionName)
if s3ServiceDetail.S3ServiceId == "" {
return diag.FromErr(fmt.Errorf("region %s is not enabled", d.Get("region_name").(string)))
return diag.FromErr(fmt.Errorf(regionError, regionName))
}
subUserId := d.Get("user_id").(string)

subUserId := d.Get("user_id").(string)
subUser := objectStorageService.DetailSubUser(vpcId, s3ServiceDetail.S3ServiceId, subUserId)
d.SetId(subUser.UserID)
if subUser.UserID == "" {
d.SetId("")
return diag.Errorf("sub-user with ID %s not found", subUserId)
}

d.SetId(subUser.UserID)
if err := d.Set("user_id", subUser.UserID); err != nil {
return diag.FromErr(err)
}
Expand Down
4 changes: 4 additions & 0 deletions fptcloud/object-storage/object_storage_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
common "terraform-provider-fptcloud/commons"
)

const (
regionError = "region %s is not enabled"
)

// ObjectStorageService defines the interface for object storage operations
type ObjectStorageService interface {
CheckServiceEnable(vpcId string) S3ServiceEnableResponse
Expand Down
Loading

0 comments on commit bfa1e65

Please sign in to comment.