From bfa1e653e3519ce4c1e81dfb118f9f52af24fb3c Mon Sep 17 00:00:00 2001 From: hoanglm Date: Thu, 28 Nov 2024 01:31:35 +0700 Subject: [PATCH] [Object Storage] fix: fix linting, remove duplicate code --- .../dataqsource_object_storage_bucket_acl.go | 21 +-- .../datasource_object_storage_access_key.go | 2 +- .../datasource_object_storage_bucket.go | 2 +- .../datasource_object_storage_bucket_cors.go | 2 +- ...asource_object_storage_bucket_lifecycle.go | 71 +++++---- ...datasource_object_storage_bucket_policy.go | 2 +- ...ce_object_storage_bucket_static_website.go | 2 +- ...source_object_storage_bucket_versioning.go | 2 +- .../datasource_object_storage_sub_user.go | 2 +- ...tasource_object_storage_sub_user_detail.go | 12 +- .../object-storage/object_storage_service.go | 4 + .../object_storage_service_test.go | 150 +++++++++--------- .../object-storage/resource_access_key.go | 8 +- fptcloud/object-storage/resource_bucket.go | 6 +- .../object-storage/resource_bucket_acl.go | 4 +- .../object-storage/resource_bucket_cors.go | 50 +++--- .../resource_bucket_lifecycle.go | 65 ++++---- .../object-storage/resource_bucket_policy.go | 26 +-- .../resource_bucket_static_website.go | 4 +- .../resource_bucket_versioning.go | 2 +- fptcloud/object-storage/resource_sub_user.go | 4 +- .../object-storage/resource_sub_user_keys.go | 28 ++-- main.go | 1 - 23 files changed, 241 insertions(+), 229 deletions(-) diff --git a/fptcloud/object-storage/dataqsource_object_storage_bucket_acl.go b/fptcloud/object-storage/dataqsource_object_storage_bucket_acl.go index 49ea7b2..0237cd7 100644 --- a/fptcloud/object-storage/dataqsource_object_storage_bucket_acl.go +++ b/fptcloud/object-storage/dataqsource_object_storage_bucket_acl.go @@ -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, @@ -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, @@ -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 { diff --git a/fptcloud/object-storage/datasource_object_storage_access_key.go b/fptcloud/object-storage/datasource_object_storage_access_key.go index 9296b70..7b8e49f 100644 --- a/fptcloud/object-storage/datasource_object_storage_access_key.go +++ b/fptcloud/object-storage/datasource_object_storage_access_key.go @@ -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 { diff --git a/fptcloud/object-storage/datasource_object_storage_bucket.go b/fptcloud/object-storage/datasource_object_storage_bucket.go index f09fc39..d48e3c4 100644 --- a/fptcloud/object-storage/datasource_object_storage_bucket.go +++ b/fptcloud/object-storage/datasource_object_storage_bucket.go @@ -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 { diff --git a/fptcloud/object-storage/datasource_object_storage_bucket_cors.go b/fptcloud/object-storage/datasource_object_storage_bucket_cors.go index 4a3f907..b15ac6c 100644 --- a/fptcloud/object-storage/datasource_object_storage_bucket_cors.go +++ b/fptcloud/object-storage/datasource_object_storage_bucket_cors.go @@ -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 diff --git a/fptcloud/object-storage/datasource_object_storage_bucket_lifecycle.go b/fptcloud/object-storage/datasource_object_storage_bucket_lifecycle.go index 4fcee2f..8f57915 100644 --- a/fptcloud/object-storage/datasource_object_storage_bucket_lifecycle.go +++ b/fptcloud/object-storage/datasource_object_storage_bucket_lifecycle.go @@ -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, @@ -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) diff --git a/fptcloud/object-storage/datasource_object_storage_bucket_policy.go b/fptcloud/object-storage/datasource_object_storage_bucket_policy.go index b7d158b..7f73ecc 100644 --- a/fptcloud/object-storage/datasource_object_storage_bucket_policy.go +++ b/fptcloud/object-storage/datasource_object_storage_bucket_policy.go @@ -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 { diff --git a/fptcloud/object-storage/datasource_object_storage_bucket_static_website.go b/fptcloud/object-storage/datasource_object_storage_bucket_static_website.go index 882334a..39d64de 100644 --- a/fptcloud/object-storage/datasource_object_storage_bucket_static_website.go +++ b/fptcloud/object-storage/datasource_object_storage_bucket_static_website.go @@ -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) diff --git a/fptcloud/object-storage/datasource_object_storage_bucket_versioning.go b/fptcloud/object-storage/datasource_object_storage_bucket_versioning.go index e9eb8f7..28250f9 100644 --- a/fptcloud/object-storage/datasource_object_storage_bucket_versioning.go +++ b/fptcloud/object-storage/datasource_object_storage_bucket_versioning.go @@ -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) diff --git a/fptcloud/object-storage/datasource_object_storage_sub_user.go b/fptcloud/object-storage/datasource_object_storage_sub_user.go index ad32393..035789a 100644 --- a/fptcloud/object-storage/datasource_object_storage_sub_user.go +++ b/fptcloud/object-storage/datasource_object_storage_sub_user.go @@ -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 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 8befde7..546bf8c 100644 --- a/fptcloud/object-storage/datasource_object_storage_sub_user_detail.go +++ b/fptcloud/object-storage/datasource_object_storage_sub_user_detail.go @@ -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) } diff --git a/fptcloud/object-storage/object_storage_service.go b/fptcloud/object-storage/object_storage_service.go index 984d0c9..966c200 100644 --- a/fptcloud/object-storage/object_storage_service.go +++ b/fptcloud/object-storage/object_storage_service.go @@ -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 diff --git a/fptcloud/object-storage/object_storage_service_test.go b/fptcloud/object-storage/object_storage_service_test.go index 8faf773..55e0bd7 100644 --- a/fptcloud/object-storage/object_storage_service_test.go +++ b/fptcloud/object-storage/object_storage_service_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestCreateResourceAccessKey_ReturnsResourceAccessKeyIDWhenSuccess(t *testing.T) { +func TestCreateResourceAccessKeyReturnsResourceAccessKeyIDWhenSuccess(t *testing.T) { mockResponse := `{ "status": true, "message": "Create resource access key successfully", @@ -34,7 +34,7 @@ func TestCreateResourceAccessKey_ReturnsResourceAccessKeyIDWhenSuccess(t *testin assert.Equal(t, "Create resource access key successfully", resourceAccessKeyID.Message) } -func TestCreateResourceAccessKey_ReturnsErrorWhenFailed(t *testing.T) { +func TestCreateResourceAccessKeyReturnsErrorWhenFailed(t *testing.T) { mockResponse := `{ "status": false, "message": "Failed to create resource access key", @@ -56,7 +56,7 @@ func TestCreateResourceAccessKey_ReturnsErrorWhenFailed(t *testing.T) { assert.Equal(t, "Failed to create resource access key", resourceAccessKeyID.Message) } -func TestDeleteResouurceAccessKey_ReturnOkWhenSuccess(t *testing.T) { +func TestDeleteResouurceAccessKeyReturnOkWhenSuccess(t *testing.T) { mockResponse := `{ "status": true, "message": "Delete resource access key successfully" @@ -75,7 +75,7 @@ func TestDeleteResouurceAccessKey_ReturnOkWhenSuccess(t *testing.T) { assert.Equal(t, "Access key deleted successfully", res.Message) } -func TestListAccessKeys_ReturnAccessKeysWhenSuccess(t *testing.T) { +func TestListAccessKeysReturnAccessKeysWhenSuccess(t *testing.T) { mockResponse := `{ "credentials": [ { @@ -103,7 +103,7 @@ func TestListAccessKeys_ReturnAccessKeysWhenSuccess(t *testing.T) { assert.Equal(t, true, accessKeys.Credentials[0].Credentials[0].Active) } -func TestCreateBucket_ReturnsBucketIDWhenSuccess(t *testing.T) { +func TestCreateBucketReturnsBucketIDWhenSuccess(t *testing.T) { mockResponse := `{ "status": true, "message": "Create bucket successfully" @@ -124,7 +124,7 @@ func TestCreateBucket_ReturnsBucketIDWhenSuccess(t *testing.T) { assert.Equal(t, true, r.Status) } -func TestCreateBucket_ReturnsErrorWhenFailed(t *testing.T) { +func TestCreateBucketReturnsErrorWhenFailed(t *testing.T) { mockResponse := `{ "status": false, "message": "Failed to create bucket", @@ -145,7 +145,7 @@ func TestCreateBucket_ReturnsErrorWhenFailed(t *testing.T) { assert.Equal(t, false, r.Status) } -func TestListBuckets_ReturnsBucketsWhenSuccess(t *testing.T) { +func TestListBucketsReturnsBucketsWhenSuccess(t *testing.T) { mockResponse := `{ "buckets": [ { @@ -176,7 +176,7 @@ func TestListBuckets_ReturnsBucketsWhenSuccess(t *testing.T) { assert.Equal(t, "https://xxxx-xxx.xyz.com", buckets.Buckets[0].Endpoint) } -func TestListBuckets_ReturnsErrorWhenFailed(t *testing.T) { +func TestListBucketsReturnsErrorWhenFailed(t *testing.T) { mockResponse := `{ "buckets": [], "total": 0 @@ -193,7 +193,7 @@ func TestListBuckets_ReturnsErrorWhenFailed(t *testing.T) { assert.Equal(t, 0, buckets.Total) } -func TestDeleteBucket_ReturnsOkWhenSuccess(t *testing.T) { +func TestDeleteBucketReturnsOkWhenSuccess(t *testing.T) { mockResponse := `{ "status": true }` @@ -210,7 +210,7 @@ func TestDeleteBucket_ReturnsOkWhenSuccess(t *testing.T) { assert.Equal(t, true, res.Status) } -func TestCreateSubUser_ReturnsTrueWhenSuccess(t *testing.T) { +func TestCreateSubUserReturnsTrueWhenSuccess(t *testing.T) { mockResponse := `{ "status": true, "message": "Sub-user created successfully" @@ -232,7 +232,7 @@ func TestCreateSubUser_ReturnsTrueWhenSuccess(t *testing.T) { assert.Equal(t, "Sub-user created successfully", r.Message) } -func TestCreateSubUser_ReturnsFalseWhenFailed(t *testing.T) { +func TestCreateSubUserReturnsFalseWhenFailed(t *testing.T) { mockResponse := `{ "status": false }` @@ -252,7 +252,7 @@ func TestCreateSubUser_ReturnsFalseWhenFailed(t *testing.T) { assert.Equal(t, false, r.Status) } -func TestDeleteSubUser_ReturnOkWhenSuccess(t *testing.T) { +func TestDeleteSubUserReturnOkWhenSuccess(t *testing.T) { mockResponse := `{}` mockClient, server, _ := common.NewClientForTesting(map[string]string{ "/v1/vmware/vpc/vpc_id/s3/s3_service_id/sub-users/sub_user_id/delete": mockResponse, @@ -266,7 +266,7 @@ func TestDeleteSubUser_ReturnOkWhenSuccess(t *testing.T) { assert.Nil(t, err) } -func TestListSubUsers_ReturnsSubUsersWhenSuccess(t *testing.T) { +func TestListSubUsersReturnsSubUsersWhenSuccess(t *testing.T) { mockResponse := `{ "sub_users": [ { @@ -295,7 +295,7 @@ func TestListSubUsers_ReturnsSubUsersWhenSuccess(t *testing.T) { assert.Equal(t, "SubUserReadWrite", subUsers.SubUsers[0].Role) } -func TestListSubUsers_ReturnsErrorWhenFailed(t *testing.T) { +func TestListSubUsersReturnsErrorWhenFailed(t *testing.T) { mockResponse := `{ "sub_users": [], "total": 0, @@ -313,7 +313,7 @@ func TestListSubUsers_ReturnsErrorWhenFailed(t *testing.T) { assert.Equal(t, 0, subUsers.Total) } -func TestGetDetailSubUser_ReturnOkWhenSuccess(t *testing.T) { +func TestGetDetailSubUserReturnOkWhenSuccess(t *testing.T) { mockResponse := ` { "user_id": "sgn-replicate123123", @@ -337,7 +337,7 @@ func TestGetDetailSubUser_ReturnOkWhenSuccess(t *testing.T) { assert.Equal(t, "SubUserReadWrite", subUser.Role) } -func TestCreateSubUserAccessKey_ReturnsAccessKeyWhenSuccess(t *testing.T) { +func TestCreateSubUserAccessKeyReturnsAccessKeyWhenSuccess(t *testing.T) { mockResponse := `{ "status": true, "credential": { @@ -360,7 +360,7 @@ func TestCreateSubUserAccessKey_ReturnsAccessKeyWhenSuccess(t *testing.T) { assert.Equal(t, true, accessKey.Status) } -func TestCreateSubUserAccessKey_ReturnsErrorWhenFailed(t *testing.T) { +func TestCreateSubUserAccessKeyReturnsErrorWhenFailed(t *testing.T) { mockResponse := `{ "status": false, }` @@ -379,7 +379,7 @@ func TestCreateSubUserAccessKey_ReturnsErrorWhenFailed(t *testing.T) { assert.Equal(t, false, accessKey.Status) } -func TestDeleteSubUserAccessKey_ReturnOkWhenSuccess(t *testing.T) { +func TestDeleteSubUserAccessKeyReturnOkWhenSuccess(t *testing.T) { mockResponse := `{ "status": true }` @@ -397,7 +397,7 @@ func TestDeleteSubUserAccessKey_ReturnOkWhenSuccess(t *testing.T) { assert.Equal(t, true, res.Status) } -func TestPutBucketPolicy_ReturnOkWhenSuccess(t *testing.T) { +func TestPutBucketPolicyReturnOkWhenSuccess(t *testing.T) { mockResponse := `{ "status": true }` @@ -416,7 +416,7 @@ func TestPutBucketPolicy_ReturnOkWhenSuccess(t *testing.T) { assert.Equal(t, true, res.Status) } -func TestGetBucketPolicy_ReturnsPolicyWhenSuccess(t *testing.T) { +func TestGetBucketPolicyReturnsPolicyWhenSuccess(t *testing.T) { mockResponse := `{ "policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"AllowAllS3Actions\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"s3:*\",\"Resource\":\"arn:aws:s3:::bucket_name/*\"}]}", "status": true @@ -435,7 +435,7 @@ func TestGetBucketPolicy_ReturnsPolicyWhenSuccess(t *testing.T) { assert.Equal(t, true, policy.Status) } -func TestGetBucketPolicy_ReturnsFalseWhenFailed(t *testing.T) { +func TestGetBucketPolicyReturnsFalseWhenFailed(t *testing.T) { mockResponse := `{ "policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"AllowAllS3Actions\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"s3:*\",\"Resource\":\"arn:aws:s3:::bucket_name/*\"}]}", "status": false, @@ -453,7 +453,7 @@ func TestGetBucketPolicy_ReturnsFalseWhenFailed(t *testing.T) { assert.Equal(t, false, policy.Status) } -func TestCreateBucketCors_ReturnOkWhenSuccess(t *testing.T) { +func TestCreateBucketCorsReturnOkWhenSuccess(t *testing.T) { mockResponse := `{ "status": true }` @@ -471,7 +471,7 @@ func TestCreateBucketCors_ReturnOkWhenSuccess(t *testing.T) { assert.Equal(t, true, res.Status) } -func TestUpdateBucketCors_ReturnOkWhenSuccess(t *testing.T) { +func TestUpdateBucketCorsReturnOkWhenSuccess(t *testing.T) { mockResponse := `{ "status": true }` @@ -490,7 +490,7 @@ func TestUpdateBucketCors_ReturnOkWhenSuccess(t *testing.T) { assert.Equal(t, true, res.Status) } -func TestGetBucketCors_ReturnCorsWhenSuccess(t *testing.T) { +func TestGetBucketCorsReturnCorsWhenSuccess(t *testing.T) { mockResponse := `{ "cors_rules": [ { @@ -515,7 +515,7 @@ func TestGetBucketCors_ReturnCorsWhenSuccess(t *testing.T) { assert.Equal(t, "*", cors.CorsRules[0].AllowedHeaders[0]) } -func TestGetBucketCors_ReturnFalseWhenFailed(t *testing.T) { +func TestGetBucketCorsReturnFalseWhenFailed(t *testing.T) { mockResponse := `{ "cors_rules": [], "status": false, @@ -532,7 +532,7 @@ func TestGetBucketCors_ReturnFalseWhenFailed(t *testing.T) { assert.NotNil(t, err) } -func TestPutBucketVersioning_ReturnNilWhenSuccess(t *testing.T) { +func TestPutBucketVersioningReturnNilWhenSuccess(t *testing.T) { mockResponse := `{ "status": true }` @@ -549,7 +549,7 @@ func TestPutBucketVersioning_ReturnNilWhenSuccess(t *testing.T) { assert.Nil(t, res) } -func TestGetBucketVersioning_ReturnBucketVersioning(t *testing.T) { +func TestGetBucketVersioningReturnBucketVersioning(t *testing.T) { mockResponse := `{ "status": true, "config": "Enabled" @@ -565,7 +565,7 @@ func TestGetBucketVersioning_ReturnBucketVersioning(t *testing.T) { assert.Equal(t, true, versioning.Status) } -func TestPutBucketAcl_ReturnAclWhenSuccess(t *testing.T) { +func TestPutBucketAclReturnAclWhenSuccess(t *testing.T) { mockResponse := `{ "status": true, "taskId": "task_id" @@ -586,7 +586,7 @@ func TestPutBucketAcl_ReturnAclWhenSuccess(t *testing.T) { assert.Equal(t, "task_id", res.TaskID) } -func TestGetBucketAcl_ReturnAclWhenSuccess(t *testing.T) { +func TestGetBucketAclReturnAclWhenSuccess(t *testing.T) { mockResponse := `{ "status": true, "Owner": { @@ -622,7 +622,7 @@ func TestGetBucketAcl_ReturnAclWhenSuccess(t *testing.T) { assert.Equal(t, "FULL_CONTROL", acl.Grants[0].Permission) } -func TestGetBucketAcl_ReturnFalseWhenFailed(t *testing.T) { +func TestGetBucketAclReturnFalseWhenFailed(t *testing.T) { mockResponse := `{ "status": false }` @@ -637,7 +637,7 @@ func TestGetBucketAcl_ReturnFalseWhenFailed(t *testing.T) { assert.Equal(t, false, acl.Status) } -func TestGetBucketAcl_ReturnFalseWhenFailedUnmarshalJson(t *testing.T) { +func TestGetBucketAclReturnFalseWhenFailedUnmarshalJson(t *testing.T) { mockResponse := `{ "status": false,,, }` @@ -652,7 +652,7 @@ func TestGetBucketAcl_ReturnFalseWhenFailedUnmarshalJson(t *testing.T) { assert.Equal(t, false, acl.Status) } -func TestPutBucketWebsite_ReturnOkWhenSuccess(t *testing.T) { +func TestPutBucketWebsiteReturnOkWhenSuccess(t *testing.T) { mockResponse := `{ "status": true, }` @@ -672,7 +672,7 @@ func TestPutBucketWebsite_ReturnOkWhenSuccess(t *testing.T) { assert.Equal(t, true, res.Status) } -func TestPutBucketWebsite_ReturnOFalseWhenFailed(t *testing.T) { +func TestPutBucketWebsiteReturnOFalseWhenFailed(t *testing.T) { mockResponse := `{ "status": false, }` @@ -683,7 +683,7 @@ func TestPutBucketWebsite_ReturnOFalseWhenFailed(t *testing.T) { service := fptcloud_object_storage.NewObjectStorageService(mockClient) bucketName := "bucket_name" website := fptcloud_object_storage.BucketWebsiteRequest{ - Key: "index.html", + Key: "example.html", Suffix: "index2.html", Bucket: "bucket_name", } @@ -692,7 +692,7 @@ func TestPutBucketWebsite_ReturnOFalseWhenFailed(t *testing.T) { assert.Equal(t, true, res.Status) } -func TestDeleteBucketStaticWebsite_ReturnTrueWhenSuccess(t *testing.T) { +func TestDeleteBucketStaticWebsiteReturnTrueWhenSuccess(t *testing.T) { mockResponse := `{ "status": true }` @@ -707,7 +707,7 @@ func TestDeleteBucketStaticWebsite_ReturnTrueWhenSuccess(t *testing.T) { assert.Equal(t, true, res.Status) } -func TestDeleteBucketStaticWebsite_ReturnFalseWhenError(t *testing.T) { +func TestDeleteBucketStaticWebsiteReturnFalseWhenError(t *testing.T) { mockResponse := `{ "status": false }` @@ -722,7 +722,7 @@ func TestDeleteBucketStaticWebsite_ReturnFalseWhenError(t *testing.T) { assert.Equal(t, true, res.Status) } -func TestGetBucketWebsite_ReturnWebsiteWhenSuccess(t *testing.T) { +func TestGetBucketWebsiteReturnWebsiteWhenSuccess(t *testing.T) { mockResponse := `{ "status": true, "config": { @@ -741,7 +741,7 @@ func TestGetBucketWebsite_ReturnWebsiteWhenSuccess(t *testing.T) { "RetryAttempts": 0 }, "IndexDocument": { - "Suffix": "index.html" + "Suffix": "some_index.html" }, "ErrorDocument": { "Key": "error.html" @@ -757,11 +757,11 @@ func TestGetBucketWebsite_ReturnWebsiteWhenSuccess(t *testing.T) { website := service.GetBucketWebsite("vpc_id", "s3_service_id", bucketName) assert.NotNil(t, website) assert.Equal(t, true, website.Status) - assert.Equal(t, "index.html", website.Config.IndexDocument.Suffix) + assert.Equal(t, "some_index.html", website.Config.IndexDocument.Suffix) assert.Equal(t, "error.html", website.Config.ErrorDocument.Key) } -func TestGetBucketWebsite_ReturnFalseWhenError(t *testing.T) { +func TestGetBucketWebsiteReturnFalseWhenError(t *testing.T) { mockResponse := `{ "status": false }` @@ -776,7 +776,7 @@ func TestGetBucketWebsite_ReturnFalseWhenError(t *testing.T) { assert.Equal(t, false, website.Status) } -func TestGetBucketLifecycle_ReturnRuleWhenSuccess(t *testing.T) { +func TestGetBucketLifecycleReturnRuleWhenSuccess(t *testing.T) { mockResponse := `{ "status": true, "rules": [ @@ -807,7 +807,7 @@ func TestGetBucketLifecycle_ReturnRuleWhenSuccess(t *testing.T) { assert.Equal(t, 1, lifecycle.Total) } -func TestGetBucketLifecycle_ReturnFalseWhenFailed(t *testing.T) { +func TestGetBucketLifecycleReturnFalseWhenFailed(t *testing.T) { mockResponse := `{ "status": false, "rules": [], @@ -825,22 +825,22 @@ func TestGetBucketLifecycle_ReturnFalseWhenFailed(t *testing.T) { assert.Equal(t, 0, lifecycle.Total) } -func TestPutBucketLifecycle_ReturnOkWhenSuccess(t *testing.T) { +func TestPutBucketLifecycleReturnOkWhenSuccess(t *testing.T) { mockResponse := `{ "status": true }` mockClient, server, _ := common.NewClientForTesting(map[string]string{ - "/v1/vmware/vpc/vpc_id/s3/s3_service_id/bucket/bucket_name/create-bucket-lifecycle-configuration": mockResponse, + "/v1/vmware/vpc/vpc_id/s3/s3_service_id/bucket/bucket_name22/create-bucket-lifecycle-configuration": mockResponse, }) defer server.Close() service := fptcloud_object_storage.NewObjectStorageService(mockClient) - bucketName := "bucket_name" + bucketName := "bucket_name22" rule := map[string]interface{}{ "ID": "rule_id", - "Prefix": "prefix", - "Status": "Enabled", + "Prefix": "prefix2222222", + "Status": "Disabled", "Expiration": map[string]interface{}{ - "Days": 30, + "Days": 8, }, } res := service.PutBucketLifecycle("vpc_id", "s3_service_id", bucketName, rule) @@ -848,30 +848,30 @@ func TestPutBucketLifecycle_ReturnOkWhenSuccess(t *testing.T) { assert.Equal(t, true, res.Status) } -func TestPutBucketLifecycle_ReturnFalseWhenError(t *testing.T) { +func TestPutBucketLifecycleReturnFalseWhenError(t *testing.T) { mockResponse := `{ "status": false }` mockClient, server, _ := common.NewClientForTesting(map[string]string{ - "/v1/vmware/vpc/vpc_id/s3/s3_service_id/bucket/bucket_name/create-bucket-lifecycle-configuration-wrong-endpoint": mockResponse, + "/v1/vmware/vpc/vpc_id/s3/s3_service_id/bucket/bucket_name1111/create-bucket-lifecycle-configuration-wrong-endpoint": mockResponse, }) defer server.Close() service := fptcloud_object_storage.NewObjectStorageService(mockClient) - bucketName := "bucket_name" + bucketName := "bucket_name1111" rule := map[string]interface{}{ - "ID": "rule_id", - "Prefix": "prefix", - "Status": "Enabled", + "ID": "rule_id", "Expiration": map[string]interface{}{ - "Days": 30, + "Days": 90, }, + "Prefix": "filer", + "Status": "Enabled", } res := service.PutBucketLifecycle("vpc_id", "s3_service_id", bucketName, rule) assert.NotNil(t, res) assert.Equal(t, false, res.Status) } -func TestPutBucketLifecycle_ReturnFalseWhenErrorUnmarshalJson(t *testing.T) { +func TestPutBucketLifecycleReturnFalseWhenErrorUnmarshalJson(t *testing.T) { mockResponse := `{ "status": false,,,,@#$@#$234 }` @@ -882,19 +882,19 @@ func TestPutBucketLifecycle_ReturnFalseWhenErrorUnmarshalJson(t *testing.T) { service := fptcloud_object_storage.NewObjectStorageService(mockClient) bucketName := "bucket_name" rule := map[string]interface{}{ - "ID": "rule_id", - "Prefix": "prefix", "Status": "Enabled", + "ID": "rule_id", "Expiration": map[string]interface{}{ "Days": 30, }, + "Prefix": "prefix", } res := service.PutBucketLifecycle("vpc_id", "s3_service_id", bucketName, rule) assert.NotNil(t, res) assert.Equal(t, false, res.Status) } -func TestDeleteBucketLifecycle_ReturnOkWhenSuccess(t *testing.T) { +func TestDeleteBucketLifecycleReturnOkWhenSuccess(t *testing.T) { mockResponse := `{ "status": true }` @@ -905,19 +905,19 @@ func TestDeleteBucketLifecycle_ReturnOkWhenSuccess(t *testing.T) { service := fptcloud_object_storage.NewObjectStorageService(mockClient) bucketName := "bucket_name" rule := map[string]interface{}{ - "ID": "rule_id", - "Prefix": "prefix", - "Status": "Enabled", + "ID": "rule_id", "Expiration": map[string]interface{}{ - "Days": 30, + "Days": 12, }, + "Prefix": "prefix", + "Status": "Enabled", } res := service.DeleteBucketLifecycle("vpc_id", "s3_service_id", bucketName, rule) assert.NotNil(t, res) assert.Equal(t, true, res.Status) } -func TestDeleteBucketLifecycle_ReturnFalseWhenError(t *testing.T) { +func TestDeleteBucketLifecycleReturnFalseWhenError(t *testing.T) { mockResponse := `{ "status": false }` @@ -928,11 +928,13 @@ func TestDeleteBucketLifecycle_ReturnFalseWhenError(t *testing.T) { service := fptcloud_object_storage.NewObjectStorageService(mockClient) bucketName := "bucket_name" rule := map[string]interface{}{ - "ID": "rule_id", - "Prefix": "prefix", - "Status": "Enabled", + "ID": "rule_id", + "Prefix": map[string]interface{}{ + "Filter": "filter", + }, + "Status": "Disabled", "Expiration": map[string]interface{}{ - "Days": 30, + "Days": 12, }, } res := service.DeleteBucketLifecycle("vpc_id", "s3_service_id", bucketName, rule) @@ -940,7 +942,7 @@ func TestDeleteBucketLifecycle_ReturnFalseWhenError(t *testing.T) { assert.Equal(t, false, res.Status) } -func TestDeleteBucketLifecycle_ReturnFalseWhenErrorUnmarshalJson(t *testing.T) { +func TestDeleteBucketLifecycleReturnFalseWhenErrorUnmarshalJson(t *testing.T) { mockResponse := `{ "status": false,,,,@#$@#$234 }` @@ -951,19 +953,19 @@ func TestDeleteBucketLifecycle_ReturnFalseWhenErrorUnmarshalJson(t *testing.T) { service := fptcloud_object_storage.NewObjectStorageService(mockClient) bucketName := "bucket_name" rule := map[string]interface{}{ - "ID": "rule_id", "Prefix": "prefix", - "Status": "Enabled", + "ID": "rule_id9999", "Expiration": map[string]interface{}{ "Days": 30, }, + "Status": "Disabled", } res := service.DeleteBucketLifecycle("vpc_id", "s3_service_id", bucketName, rule) assert.NotNil(t, res) assert.Equal(t, false, res.Status) } -func TestCheckServiceEnable_ReturnServicesWhenSuccess(t *testing.T) { +func TestCheckServiceEnableReturnServicesWhenSuccess(t *testing.T) { mockResponse := `{ "data": [ { @@ -994,7 +996,7 @@ func TestCheckServiceEnable_ReturnServicesWhenSuccess(t *testing.T) { assert.Equal(t, "ceph", services.Data[0].S3Platform) } -func TestCheckServiceEnable_ReturnFalseWhenError(t *testing.T) { +func TestCheckServiceEnableReturnFalseWhenError(t *testing.T) { mockResponse := `{ "total": 0 }` @@ -1010,7 +1012,7 @@ func TestCheckServiceEnable_ReturnFalseWhenError(t *testing.T) { assert.Equal(t, 0, services.Total) } -func TestCheckServiceEnable_ReturnFalseWhenErrorUnmarshal(t *testing.T) { +func TestCheckServiceEnableReturnFalseWhenErrorUnmarshal(t *testing.T) { mockResponse := `{ "total": #$%#$%#$%#$%#$%!@#!23, }` diff --git a/fptcloud/object-storage/resource_access_key.go b/fptcloud/object-storage/resource_access_key.go index 8af42ec..9970256 100644 --- a/fptcloud/object-storage/resource_access_key.go +++ b/fptcloud/object-storage/resource_access_key.go @@ -3,7 +3,6 @@ package fptcloud_object_storage import ( "context" "fmt" - "log" common "terraform-provider-fptcloud/commons" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -66,7 +65,7 @@ func resourceAccessKeyCreate(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)) } resp := service.CreateAccessKey(vpcId, s3ServiceDetail.S3ServiceId) @@ -144,18 +143,15 @@ func resourceAccessKeyDelete(ctx context.Context, d *schema.ResourceData, m inte s3ServiceDetail := getServiceEnableRegion(service, vpcId, regionName) if s3ServiceDetail.S3ServiceId == "" { - log.Printf("[ERROR] Region %s is not enabled for VPC %s", regionName, vpcId) - return diag.FromErr(fmt.Errorf("region %s is not enabled", regionName)) + return diag.FromErr(fmt.Errorf(regionError, regionName)) } if accessKeyId == "" { - log.Printf("[ERROR] access_key_id is empty") return diag.Errorf("access_key_id is required for deletion") } data := service.DeleteAccessKey(vpcId, s3ServiceDetail.S3ServiceId, accessKeyId) if !data.Status { - log.Printf("[ERROR] Failed to delete access key %s: %v", accessKeyId, data.Message) return diag.Errorf("failed to delete access key %s: %s", accessKeyId, data.Message) } if err := d.Set("status", true); err != nil { diff --git a/fptcloud/object-storage/resource_bucket.go b/fptcloud/object-storage/resource_bucket.go index 366abd8..d262235 100644 --- a/fptcloud/object-storage/resource_bucket.go +++ b/fptcloud/object-storage/resource_bucket.go @@ -94,7 +94,7 @@ func resourceBucketCreate(ctx context.Context, d *schema.ResourceData, m interfa } 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))) + return diag.FromErr(fmt.Errorf(regionError, d.Get("region_name").(string))) } bucket := objectStorageService.CreateBucket(req, vpcId, s3ServiceDetail.S3ServiceId) @@ -109,7 +109,7 @@ func resourceBucketRead(_ context.Context, d *schema.ResourceData, m interface{} 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))) + return diag.FromErr(fmt.Errorf(regionError, d.Get("region_name").(string))) } bucket := objectStorageService.ListBuckets(vpcId, s3ServiceDetail.S3ServiceId, 1, 99999) @@ -133,7 +133,7 @@ func resourceBucketDelete(ctx context.Context, d *schema.ResourceData, m interfa bucketName := d.Get("name").(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))) + return diag.FromErr(fmt.Errorf(regionError, d.Get("region_name").(string))) } satus := objectStorageService.DeleteBucket(vpcId, s3ServiceDetail.S3ServiceId, bucketName) diff --git a/fptcloud/object-storage/resource_bucket_acl.go b/fptcloud/object-storage/resource_bucket_acl.go index 3d1c0e9..f569a4e 100644 --- a/fptcloud/object-storage/resource_bucket_acl.go +++ b/fptcloud/object-storage/resource_bucket_acl.go @@ -70,7 +70,7 @@ func resourceBucketAclCreate(ctx context.Context, d *schema.ResourceData, m inte } 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)) } var bucketAclRequest BucketAclRequest bucketAclRequest.CannedAcl = cannedAcl @@ -97,7 +97,7 @@ func resourceBucketAclRead(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)) } r := service.GetBucketAcl(vpcId, s3ServiceDetail.S3ServiceId, bucketName) if !r.Status { diff --git a/fptcloud/object-storage/resource_bucket_cors.go b/fptcloud/object-storage/resource_bucket_cors.go index dca724d..27c198e 100644 --- a/fptcloud/object-storage/resource_bucket_cors.go +++ b/fptcloud/object-storage/resource_bucket_cors.go @@ -18,24 +18,6 @@ func ResourceBucketCors() *schema.Resource { DeleteContext: resourceBucketCorsDelete, ReadContext: resourceBucketCorsRead, Schema: map[string]*schema.Schema{ - "bucket_name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - Description: "Name of the bucket", - }, - "vpc_id": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - Description: "The VPC ID", - }, - "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", - }, "cors_config": { Type: schema.TypeString, Optional: true, @@ -68,6 +50,24 @@ func ResourceBucketCors() *schema.Resource { }, }, }, + "bucket_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "Name of the bucket", + }, + "vpc_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "The VPC ID", + }, + "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", + }, }, } } @@ -80,7 +80,7 @@ func resourceBucketCorsCreate(ctx context.Context, d *schema.ResourceData, m int 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)) } var corsConfigData string @@ -99,10 +99,10 @@ func resourceBucketCorsCreate(ctx context.Context, d *schema.ResourceData, m int return diag.FromErr(err) } payload := map[string]interface{}{ - "ID": jsonMap.ID, "AllowedMethods": jsonMap.AllowedMethods, - "AllowedOrigins": jsonMap.AllowedOrigins, "MaxAgeSeconds": jsonMap.MaxAgeSeconds, + "ID": jsonMap.ID, + "AllowedOrigins": jsonMap.AllowedOrigins, } if len(jsonMap.AllowedHeaders) > 0 { payload["AllowedHeaders"] = jsonMap.AllowedHeaders @@ -131,7 +131,7 @@ func resourceBucketCorsRead(_ 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)) } page := 1 pageSize := 999999 @@ -169,7 +169,7 @@ func resourceBucketCorsDelete(ctx context.Context, d *schema.ResourceData, m int 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)) } var corsConfigData string if v, ok := d.GetOk("cors_config"); ok { @@ -189,9 +189,9 @@ func resourceBucketCorsDelete(ctx context.Context, d *schema.ResourceData, m int var payload []map[string]interface{} for _, corsRule := range jsonMap { payload := map[string]interface{}{ - "ID": corsRule.ID, - "AllowedMethods": corsRule.AllowedMethods, "AllowedOrigins": corsRule.AllowedOrigins, + "AllowedMethods": corsRule.AllowedMethods, + "ID": corsRule.ID, "MaxAgeSeconds": corsRule.MaxAgeSeconds, } if len(corsRule.AllowedHeaders) > 0 { diff --git a/fptcloud/object-storage/resource_bucket_lifecycle.go b/fptcloud/object-storage/resource_bucket_lifecycle.go index 48b6600..bbdc9cb 100644 --- a/fptcloud/object-storage/resource_bucket_lifecycle.go +++ b/fptcloud/object-storage/resource_bucket_lifecycle.go @@ -24,7 +24,23 @@ func ResourceBucketLifeCycle() *schema.Resource { ForceNew: true, Description: "The VPC ID", }, - "bucket_name": { + "state": { + Type: schema.TypeBool, + Computed: true, + Description: "State after bucket lifecycle rule is created", + }, + "rules": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, "bucket_name": { Type: schema.TypeString, Required: true, ForceNew: true, @@ -51,27 +67,17 @@ func ResourceBucketLifeCycle() *schema.Resource { Description: "Path to the JSON file containing the bucket lifecycle rule, support only one rule", ConflictsWith: []string{"life_cycle_rule"}, }, - "state": { - Type: schema.TypeBool, - Computed: true, - Description: "State after bucket lifecycle rule is created", - }, - "rules": { - Type: schema.TypeList, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "id": { - Type: schema.TypeString, - Computed: true, - }, - }, - }, - }, }, } } - +func parseLifeCycleData(lifeCycleData string) (S3BucketLifecycleConfig, error) { + var jsonMap S3BucketLifecycleConfig + err := json.Unmarshal([]byte(lifeCycleData), &jsonMap) + if err != nil { + return S3BucketLifecycleConfig{}, err + } + return jsonMap, nil +} func resourceBucketLifeCycleCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { client := m.(*common.Client) service := NewObjectStorageService(client) @@ -91,18 +97,17 @@ func resourceBucketLifeCycleCreate(ctx context.Context, d *schema.ResourceData, } 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)) } - var jsonMap S3BucketLifecycleConfig - err := json.Unmarshal([]byte(lifecycleRuleContent), &jsonMap) + jsonMap, err := parseLifeCycleData(lifecycleRuleContent) if err != nil { return diag.FromErr(err) } payload := map[string]interface{}{ "ID": jsonMap.ID, - "Filter": map[string]interface{}{"Prefix": jsonMap.Filter.Prefix}, "NoncurrentVersionExpiration": map[string]interface{}{"NoncurrentDays": jsonMap.NoncurrentVersionExpiration.NoncurrentDays}, "AbortIncompleteMultipartUpload": map[string]interface{}{"DaysAfterInitiation": jsonMap.AbortIncompleteMultipartUpload.DaysAfterInitiation}, + "Filter": map[string]interface{}{"Prefix": jsonMap.Filter.Prefix}, } if jsonMap.Expiration.Days != 0 && jsonMap.Expiration.ExpiredObjectDeleteMarker { return diag.FromErr(fmt.Errorf("Expiration.Days and Expiration.ExpiredObjectDeleteMarker cannot be set at the same time")) @@ -137,7 +142,7 @@ func resourceBucketLifeCycleRead(_ context.Context, d *schema.ResourceData, m in 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 := 999999 @@ -175,7 +180,7 @@ func resourceBucketLifeCycleDelete(ctx context.Context, d *schema.ResourceData, 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)) } var lifecycleRuleContent string if v, ok := d.GetOk("life_cycle_rule"); ok { @@ -187,18 +192,18 @@ func resourceBucketLifeCycleDelete(ctx context.Context, d *schema.ResourceData, } else { return diag.FromErr(fmt.Errorf("either 'life_cycle_rule' or 'life_cycle_rule_file' must be specified")) } - var jsonMap S3BucketLifecycleConfig - err := json.Unmarshal([]byte(lifecycleRuleContent), &jsonMap) + jsonMap, err := parseLifeCycleData(lifecycleRuleContent) if err != nil { return diag.FromErr(err) } + payload := map[string]interface{}{ - "ID": jsonMap.ID, - "Filter": map[string]interface{}{"Prefix": jsonMap.Filter.Prefix}, - "NoncurrentVersionExpiration": map[string]interface{}{"NoncurrentDays": jsonMap.NoncurrentVersionExpiration.NoncurrentDays}, "AbortIncompleteMultipartUpload": map[string]interface{}{"DaysAfterInitiation": jsonMap.AbortIncompleteMultipartUpload.DaysAfterInitiation}, "OrgID": jsonMap.ID, "Status": "Enabled", + "ID": jsonMap.ID, + "Filter": map[string]interface{}{"Prefix": jsonMap.Filter.Prefix}, + "NoncurrentVersionExpiration": map[string]interface{}{"NoncurrentDays": jsonMap.NoncurrentVersionExpiration.NoncurrentDays}, } if jsonMap.Expiration.Days != 0 && jsonMap.Expiration.ExpiredObjectDeleteMarker { return diag.FromErr(fmt.Errorf("Expiration.Days and Expiration.ExpiredObjectDeleteMarker cannot be set at the same time")) diff --git a/fptcloud/object-storage/resource_bucket_policy.go b/fptcloud/object-storage/resource_bucket_policy.go index 79cd064..9b0d8ac 100644 --- a/fptcloud/object-storage/resource_bucket_policy.go +++ b/fptcloud/object-storage/resource_bucket_policy.go @@ -18,12 +18,6 @@ func ResourceBucketPolicy() *schema.Resource { DeleteContext: resourceBucketPolicyDelete, ReadContext: dataSourceBucketPolicyRead, Schema: map[string]*schema.Schema{ - "vpc_id": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - Description: "The VPC ID", - }, "region_name": { Type: schema.TypeString, Required: true, @@ -36,6 +30,17 @@ func ResourceBucketPolicy() *schema.Resource { ForceNew: true, Description: "Name of the bucket", }, + "vpc_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "The VPC ID", + }, + "status": { + Type: schema.TypeBool, + Computed: true, + Description: "Status after bucket policy is created", + }, "policy": { Type: schema.TypeString, Optional: true, @@ -51,11 +56,6 @@ func ResourceBucketPolicy() *schema.Resource { Description: "Path to the JSON file containing the bucket policy", ConflictsWith: []string{"policy"}, }, - "status": { - Type: schema.TypeBool, - Computed: true, - Description: "Status after bucket policy is created", - }, }, } } @@ -82,7 +82,7 @@ func resourceBucketPolicyCreate(ctx context.Context, d *schema.ResourceData, m i 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)) } var jsonMap map[string]interface{} err := json.Unmarshal([]byte(policyContent), &jsonMap) @@ -119,7 +119,7 @@ func resourceBucketPolicyDelete(ctx context.Context, d *schema.ResourceData, m i regionName := d.Get("region_name").(string) s3ServiceDetail := getServiceEnableRegion(service, 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, d.Get("region_name").(string))) } resp := service.PutBucketPolicy(vpcId, s3ServiceDetail.S3ServiceId, bucketName, BucketPolicyRequest{ diff --git a/fptcloud/object-storage/resource_bucket_static_website.go b/fptcloud/object-storage/resource_bucket_static_website.go index cbc0c31..15cd1e5 100644 --- a/fptcloud/object-storage/resource_bucket_static_website.go +++ b/fptcloud/object-storage/resource_bucket_static_website.go @@ -73,7 +73,7 @@ func resourceBucketStaticWebsiteCreate(ctx context.Context, d *schema.ResourceDa errorDocument := d.Get("error_document_key").(string) s3ServiceDetail := getServiceEnableRegion(service, 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, d.Get("region_name").(string))) } putBucketWebsite := service.PutBucketWebsite(vpcId, s3ServiceDetail.S3ServiceId, bucketName, BucketWebsiteRequest{ Bucket: bucketName, @@ -104,7 +104,7 @@ func resourceDeleteBucketStaticWebsite(ctx context.Context, d *schema.ResourceDa regionName := d.Get("region_name").(string) s3ServiceDetail := getServiceEnableRegion(service, 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, d.Get("region_name").(string))) } resp := service.DeleteBucketStaticWebsite(vpcId, s3ServiceDetail.S3ServiceId, bucketName) diff --git a/fptcloud/object-storage/resource_bucket_versioning.go b/fptcloud/object-storage/resource_bucket_versioning.go index e744ed4..d10afa3 100644 --- a/fptcloud/object-storage/resource_bucket_versioning.go +++ b/fptcloud/object-storage/resource_bucket_versioning.go @@ -57,7 +57,7 @@ func resourceBucketVersioningCreate(ctx context.Context, d *schema.ResourceData, regionName := d.Get("region_name").(string) s3ServiceDetail := getServiceEnableRegion(service, 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, d.Get("region_name").(string))) } err := service.PutBucketVersioning(vpcId, s3ServiceDetail.S3ServiceId, bucketName, BucketVersioningRequest{ Status: versioningStatus, diff --git a/fptcloud/object-storage/resource_sub_user.go b/fptcloud/object-storage/resource_sub_user.go index 254e21e..29e6f52 100644 --- a/fptcloud/object-storage/resource_sub_user.go +++ b/fptcloud/object-storage/resource_sub_user.go @@ -52,7 +52,7 @@ func resourceSubUserCreate(ctx context.Context, d *schema.ResourceData, m interf 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))) + return diag.FromErr(fmt.Errorf(regionError, d.Get("region_name").(string))) } err := objectStorageService.CreateSubUser(req, vpcId, s3ServiceDetail.S3ServiceId) @@ -76,7 +76,7 @@ func resourceSubUserDelete(ctx context.Context, d *schema.ResourceData, m interf 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))) + return diag.FromErr(fmt.Errorf(regionError, d.Get("region_name").(string))) } err := objectStorageService.DeleteSubUser(d.Id(), vpcId, s3ServiceDetail.S3ServiceId) if err != nil { diff --git a/fptcloud/object-storage/resource_sub_user_keys.go b/fptcloud/object-storage/resource_sub_user_keys.go index 411b707..34a7bd5 100644 --- a/fptcloud/object-storage/resource_sub_user_keys.go +++ b/fptcloud/object-storage/resource_sub_user_keys.go @@ -15,28 +15,28 @@ func ResourceSubUserKeys() *schema.Resource { ReadContext: resourceReadUserDetail, DeleteContext: resourceSubUserAccessKeyDelete, Schema: map[string]*schema.Schema{ - "vpc_id": { + "user_id": { Type: schema.TypeString, Required: true, ForceNew: true, - Description: "The VPC id that the S3 service belongs to", + Description: "The sub user id, can retrieve from data source `fptcloud_object_storage_sub_user`", }, - "region_name": { + "access_key": { 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", + Computed: true, + Description: "The access key of the sub user", }, - "user_id": { + "vpc_id": { Type: schema.TypeString, Required: true, ForceNew: true, - Description: "The sub user id, can retrieve from data source `fptcloud_object_storage_sub_user`", + Description: "The VPC id that the S3 service belongs to", }, - "access_key": { + "region_name": { Type: schema.TypeString, - Computed: true, - Description: "The access key of the sub user", + 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", }, "secret_key": { Type: schema.TypeString, @@ -54,7 +54,7 @@ func resourceSubUserAccessKeyCreate(ctx context.Context, d *schema.ResourceData, subUserId := d.Get("user_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))) + return diag.FromErr(fmt.Errorf(regionError, d.Get("region_name").(string))) } resp := objectStorageService.CreateSubUserAccessKey(vpcId, s3ServiceDetail.S3ServiceId, subUserId) @@ -81,7 +81,7 @@ func resourceReadUserDetail(ctx context.Context, d *schema.ResourceData, m inter 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))) + return diag.FromErr(fmt.Errorf(regionError, d.Get("region_name").(string))) } subUserId := d.Get("user_id").(string) @@ -101,7 +101,7 @@ func resourceSubUserAccessKeyDelete(ctx context.Context, d *schema.ResourceData, 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))) + return diag.FromErr(fmt.Errorf(regionError, d.Get("region_name").(string))) } subUserId := d.Get("user_id").(string) accessKeyToDelete := d.Get("access_key").(string) diff --git a/main.go b/main.go index 1ec6d5f..6db0c26 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,6 @@ func main() { "set to true to run the provider with support for debuggers", ) flag.Parse() - log.Printf("[DEBUG] Configuring provider...") providers := []func() tfprotov5.ProviderServer{ providerserver.NewProtocol5(fptcloud.NewXplatProvider("dev")()),