From 51976928f4f1dde1452c92105f31055cb4c26e84 Mon Sep 17 00:00:00 2001 From: hoanglm Date: Wed, 13 Nov 2024 10:07:33 +0700 Subject: [PATCH] Update: region name validate, fix bucket config --- commons/client.go | 26 ++++++++ .../resource_bucket_acl.tf | 6 ++ .../resource_bucket_static_website.tf | 7 +++ .../resource_bucket_versioning.tf | 7 +++ .../resource_sub_user.tf | 6 ++ .../datasource_object_storage.go | 17 ++--- .../datasource_object_storage_access_key.go | 2 +- .../datasource_object_storage_lifecycle.go | 2 +- ...atasource_object_storage_static_website.go | 2 +- .../datasource_object_storage_sub_user.go | 2 +- ...tasource_object_storage_sub_user_detail.go | 6 +- .../datasource_object_storage_versioning.go | 4 +- .../object-storage/resource_access_key.go | 2 +- fptcloud/object-storage/resource_bucket.go | 2 +- .../object-storage/resource_bucket_acl.go | 2 +- .../object-storage/resource_bucket_policy.go | 2 +- .../resource_bucket_static_website.go | 8 +-- .../resource_bucket_versioning.go | 2 +- fptcloud/object-storage/resource_sub_user.go | 36 ++++++++--- .../object-storage/resource_sub_user_keys.go | 63 ++++++++++++------- 20 files changed, 145 insertions(+), 59 deletions(-) create mode 100644 examples/resources/fptcloud_object_storage/resource_bucket_acl.tf create mode 100644 examples/resources/fptcloud_object_storage/resource_bucket_static_website.tf create mode 100644 examples/resources/fptcloud_object_storage/resource_bucket_versioning.tf create mode 100644 examples/resources/fptcloud_object_storage/resource_sub_user.tf diff --git a/commons/client.go b/commons/client.go index 415e88a..1372cfc 100644 --- a/commons/client.go +++ b/commons/client.go @@ -88,6 +88,17 @@ func (c *Client) PrepareClientURL(requestURL string) *url.URL { return u } +type SubUserCreateKeyResponse2 struct { + Status bool `json:"status"` + Message string `json:"message,omitempty"` + Credential struct { + AccessKey string `json:"accessKey,omitempty"` + SecretKey string `json:"secretKey,omitempty"` + Active interface{} `json:"active,omitempty"` + CreatedDate interface{} `json:"createdDate,omitempty"` + } `json:"credential,omitempty"` +} + func (c *Client) SendRequest(req *http.Request) ([]byte, error) { req.Header.Set("Accept", "application/json") req.Header.Set("User-Agent", c.UserAgent) @@ -113,6 +124,21 @@ func (c *Client) SendRequest(req *http.Request) ([]byte, error) { body, err := io.ReadAll(resp.Body) c.LastJSONResponse = string(body) + var data SubUserCreateKeyResponse2 + fmt.Println("-----------------") + fmt.Println("RESPONSE: ", resp.Request.URL.Path) + fmt.Println(strings.Contains(resp.Request.URL.Path, "sub-users")) + fmt.Println("-----------------") + if strings.Contains(resp.Request.URL.Path, "sub-users") { + err = json.Unmarshal(body, &data) + if err != nil { + return nil, err + } + fmt.Println("-----------------") + fmt.Println("RESPONSE data: ", data) + fmt.Println("-----------------") + } + if resp.StatusCode >= 300 { return nil, HTTPError{Code: resp.StatusCode, Status: resp.Status, Reason: string(body)} } diff --git a/examples/resources/fptcloud_object_storage/resource_bucket_acl.tf b/examples/resources/fptcloud_object_storage/resource_bucket_acl.tf new file mode 100644 index 0000000..6f8f5bf --- /dev/null +++ b/examples/resources/fptcloud_object_storage/resource_bucket_acl.tf @@ -0,0 +1,6 @@ +resource "fptcloud_object_storage_bucket_acl" "bucket_acl" { + vpc_id = "your_vpc_id" + region_name = "your_bucket_region" + bucket_name = "your_bucket_name" + canned_acl = "private" +} diff --git a/examples/resources/fptcloud_object_storage/resource_bucket_static_website.tf b/examples/resources/fptcloud_object_storage/resource_bucket_static_website.tf new file mode 100644 index 0000000..e453d72 --- /dev/null +++ b/examples/resources/fptcloud_object_storage/resource_bucket_static_website.tf @@ -0,0 +1,7 @@ +resource "fptcloud_object_storage_bucket_static_website" "example_static_website" { + vpc_id = "your_vpc_id" + region_name = "your_region" + bucket_name = "your_bucket_name" + index_document_suffix = "your_index_document_suffix" + error_document_key = "your_error_document_suffix" +} diff --git a/examples/resources/fptcloud_object_storage/resource_bucket_versioning.tf b/examples/resources/fptcloud_object_storage/resource_bucket_versioning.tf new file mode 100644 index 0000000..d55d0ab --- /dev/null +++ b/examples/resources/fptcloud_object_storage/resource_bucket_versioning.tf @@ -0,0 +1,7 @@ +resource "fptcloud_object_storage_bucket_versioning" "versioning" { + vpc_id = "your_vpc_id" + region_name = "your_bucket_region" + bucket_name = "your_bucket_name" + versioning_status = "Suspended" // or "Enabled" +} + diff --git a/examples/resources/fptcloud_object_storage/resource_sub_user.tf b/examples/resources/fptcloud_object_storage/resource_sub_user.tf new file mode 100644 index 0000000..15f3f26 --- /dev/null +++ b/examples/resources/fptcloud_object_storage/resource_sub_user.tf @@ -0,0 +1,6 @@ +resource "fptcloud_object_storage_sub_user" "example" { + vpc_id = "your_vpc_id" + region_name = "your_region_name" + user_id = "your_user_id" + role = "your_role" +} diff --git a/fptcloud/object-storage/datasource_object_storage.go b/fptcloud/object-storage/datasource_object_storage.go index f813054..d05f373 100644 --- a/fptcloud/object-storage/datasource_object_storage.go +++ b/fptcloud/object-storage/datasource_object_storage.go @@ -35,13 +35,14 @@ type CreateAccessKeyResponse struct { } `json:"credential,omitempty"` } type SubUserCreateKeyResponse struct { - Status bool `json:"status"` + Status bool `json:"status"` + Message string `json:"message,omitempty"` Credential struct { - AccessKey string `json:"accessKey"` - SecretKey string `json:"secretKey"` - Active interface{} `json:"active"` - CreatedDate interface{} `json:"createdDate"` - } `json:"credential"` + AccessKey string `json:"accessKey,omitempty"` + SecretKey string `json:"secretKey,omitempty"` + Active interface{} `json:"active,omitempty"` + CreatedDate interface{} `json:"createdDate,omitempty"` + } `json:"credential,omitempty"` } type SubUser struct { @@ -592,12 +593,12 @@ func (s *ObjectStorageServiceImpl) CreateSubUserAccessKey(vpcId, s3ServiceId, su apiPath := common.ApiPath.CreateSubUserAccessKey(vpcId, s3ServiceId, subUserId) resp, err := s.client.SendPostRequest(apiPath, nil) if err != nil { - return nil + return &SubUserCreateKeyResponse{Status: false, Message: err.Error()} } var subUserKeys SubUserCreateKeyResponse if err := json.Unmarshal(resp, &subUserKeys); err != nil { - return nil + return &SubUserCreateKeyResponse{Status: false, Message: err.Error()} } return &subUserKeys } diff --git a/fptcloud/object-storage/datasource_object_storage_access_key.go b/fptcloud/object-storage/datasource_object_storage_access_key.go index 275f01b..0876ed7 100644 --- a/fptcloud/object-storage/datasource_object_storage_access_key.go +++ b/fptcloud/object-storage/datasource_object_storage_access_key.go @@ -21,7 +21,7 @@ func DataSourceAccessKey() *schema.Resource { "region_name": { Type: schema.TypeString, Required: true, - Description: "The region name to create the access key", + 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", }, "access_keys": { Type: schema.TypeList, diff --git a/fptcloud/object-storage/datasource_object_storage_lifecycle.go b/fptcloud/object-storage/datasource_object_storage_lifecycle.go index 34a883c..7da8df9 100644 --- a/fptcloud/object-storage/datasource_object_storage_lifecycle.go +++ b/fptcloud/object-storage/datasource_object_storage_lifecycle.go @@ -31,7 +31,7 @@ func DataSourceBucketLifecycle() *schema.Resource { "region_name": { Type: schema.TypeString, Required: true, - Description: "The region name of the bucket", + 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", }, "page_size": { Type: schema.TypeString, diff --git a/fptcloud/object-storage/datasource_object_storage_static_website.go b/fptcloud/object-storage/datasource_object_storage_static_website.go index d227fa5..ece980a 100644 --- a/fptcloud/object-storage/datasource_object_storage_static_website.go +++ b/fptcloud/object-storage/datasource_object_storage_static_website.go @@ -26,7 +26,7 @@ func DataSourceBucketStaticWebsite() *schema.Resource { "region_name": { Type: schema.TypeString, Required: true, - Description: "The region name of the bucket", + 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", }, "status": { Type: schema.TypeBool, diff --git a/fptcloud/object-storage/datasource_object_storage_sub_user.go b/fptcloud/object-storage/datasource_object_storage_sub_user.go index 88560ec..fe0ac09 100644 --- a/fptcloud/object-storage/datasource_object_storage_sub_user.go +++ b/fptcloud/object-storage/datasource_object_storage_sub_user.go @@ -34,7 +34,7 @@ func DataSourceSubUser() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - Description: "The region name of sub-user", + 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", }, "list_sub_user": { Type: schema.TypeList, 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 9a223f2..9660399 100644 --- a/fptcloud/object-storage/datasource_object_storage_sub_user_detail.go +++ b/fptcloud/object-storage/datasource_object_storage_sub_user_detail.go @@ -3,7 +3,6 @@ package fptcloud_object_storage import ( "context" "fmt" - "reflect" common "terraform-provider-fptcloud/commons" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -68,7 +67,7 @@ func dataSourceSubUserDetailRead(ctx context.Context, d *schema.ResourceData, m subUserId := d.Get("user_id").(string) subUser := objectStorageService.DetailSubUser(vpcId, s3ServiceDetail.S3ServiceId, subUserId) - if subUser == nil { + if subUser.UserID == "" { return diag.Errorf("sub-user with ID %s not found", subUserId) } @@ -77,8 +76,7 @@ func dataSourceSubUserDetailRead(ctx context.Context, d *schema.ResourceData, m if subUser.Arn != nil { d.Set("arn", subUser.Arn) } - fmt.Println("subUser active is: ", subUser.Active) - fmt.Println("reflect subUser active is: ", reflect.TypeOf(subUser.Active)) + d.Set("active", subUser.Active) d.Set("role", subUser.Role) if subUser.CreatedAt != nil { d.Set("created_at", subUser.CreatedAt) diff --git a/fptcloud/object-storage/datasource_object_storage_versioning.go b/fptcloud/object-storage/datasource_object_storage_versioning.go index 1f73478..a81c5fc 100644 --- a/fptcloud/object-storage/datasource_object_storage_versioning.go +++ b/fptcloud/object-storage/datasource_object_storage_versioning.go @@ -34,7 +34,7 @@ func DataSourceBucketVersioning() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - Description: "The region name to create the access key", + 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", }, }, } @@ -52,7 +52,7 @@ func dataSourceBucketVersioningRead(ctx context.Context, d *schema.ResourceData, bucketName := d.Get("bucket_name").(string) versioning := service.GetBucketVersioning(vpcId, bucketName, s3ServiceDetail.S3ServiceId) - if versioning == nil { + if versioning.Status != "" { return diag.Errorf("failed to get bucket versioning for bucket %s", bucketName) } diff --git a/fptcloud/object-storage/resource_access_key.go b/fptcloud/object-storage/resource_access_key.go index eb5ab4d..aa424ca 100644 --- a/fptcloud/object-storage/resource_access_key.go +++ b/fptcloud/object-storage/resource_access_key.go @@ -39,7 +39,7 @@ func ResourceAccessKey() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - Description: "The region name to create the access key", + 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", }, "status": { Type: schema.TypeBool, diff --git a/fptcloud/object-storage/resource_bucket.go b/fptcloud/object-storage/resource_bucket.go index 3527e6e..20d9695 100644 --- a/fptcloud/object-storage/resource_bucket.go +++ b/fptcloud/object-storage/resource_bucket.go @@ -38,7 +38,7 @@ func ResourceBucket() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - Description: "The region name that's are the same with the region name in the S3 service.", + 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", }, "acl": { Type: schema.TypeString, diff --git a/fptcloud/object-storage/resource_bucket_acl.go b/fptcloud/object-storage/resource_bucket_acl.go index e6f3cc4..1ca06ee 100644 --- a/fptcloud/object-storage/resource_bucket_acl.go +++ b/fptcloud/object-storage/resource_bucket_acl.go @@ -32,7 +32,7 @@ func ResourceBucketAcl() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - Description: "The region name where the bucket is located, e.g., HCM-02, can be retrieved when creating the bucket", + 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", }, "canned_acl": { Type: schema.TypeString, diff --git a/fptcloud/object-storage/resource_bucket_policy.go b/fptcloud/object-storage/resource_bucket_policy.go index 0755538..8a86664 100644 --- a/fptcloud/object-storage/resource_bucket_policy.go +++ b/fptcloud/object-storage/resource_bucket_policy.go @@ -31,7 +31,7 @@ func ResourceBucketPolicy() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - Description: "The region name of the bucket", + 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", }, "vpc_id": { Type: schema.TypeString, diff --git a/fptcloud/object-storage/resource_bucket_static_website.go b/fptcloud/object-storage/resource_bucket_static_website.go index c64953a..6636c9f 100644 --- a/fptcloud/object-storage/resource_bucket_static_website.go +++ b/fptcloud/object-storage/resource_bucket_static_website.go @@ -25,7 +25,7 @@ func ResourceBucketStaticWebsite() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - Description: "The region name of the bucket", + 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", }, "vpc_id": { Type: schema.TypeString, @@ -80,9 +80,6 @@ func resourceBucketStaticWebsiteCreate(ctx context.Context, d *schema.ResourceDa Suffix: indexDocument, Key: errorDocument, }) - fmt.Println("--------------------------------------- \n:") - fmt.Println("--------------------------------------- \n: ", putBucketWebsite) - fmt.Println("--------------------------------------- \n: ") if !putBucketWebsite.Status { diag.Errorf("failed to create bucket website for bucket %s", bucketName) @@ -91,7 +88,7 @@ func resourceBucketStaticWebsiteCreate(ctx context.Context, d *schema.ResourceDa } d.Set("status", true) d.SetId(bucketName) - return dataSourceBucketStaticWebsite(ctx, d, m) + return nil } func resourceDeleteBucketStaticWebsite(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { @@ -110,6 +107,7 @@ func resourceDeleteBucketStaticWebsite(ctx context.Context, d *schema.ResourceDa if !resp.Status { return diag.Errorf("failed to delete bucket website for bucket %s", bucketName) } + d.SetId("") return nil } diff --git a/fptcloud/object-storage/resource_bucket_versioning.go b/fptcloud/object-storage/resource_bucket_versioning.go index d388099..dd671e4 100644 --- a/fptcloud/object-storage/resource_bucket_versioning.go +++ b/fptcloud/object-storage/resource_bucket_versioning.go @@ -31,7 +31,7 @@ func ResourceBucketVersioning() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - Description: "The region name of the bucket", + 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", }, "vpc_id": { Type: schema.TypeString, diff --git a/fptcloud/object-storage/resource_sub_user.go b/fptcloud/object-storage/resource_sub_user.go index d4e5cfc..d2231d1 100644 --- a/fptcloud/object-storage/resource_sub_user.go +++ b/fptcloud/object-storage/resource_sub_user.go @@ -31,9 +31,10 @@ func ResourceSubUser() *schema.Resource { ForceNew: true, }, "region_name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, + 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", }, }, } @@ -48,20 +49,39 @@ func resourceSubUserCreate(ctx context.Context, d *schema.ResourceData, m interf Role: d.Get("role").(string), UserId: subUserId, } + 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.CreateSubUser(req, vpcId, s3ServiceDetail.S3ServiceId) - if !subUser.Status { - return diag.FromErr(fmt.Errorf(subUser.Message)) + err := objectStorageService.CreateSubUser(req, vpcId, s3ServiceDetail.S3ServiceId) + if !err.Status { + return diag.FromErr(fmt.Errorf("error creating sub-user: %s", err.Message)) } + // Set the resource ID after successful creation d.SetId(subUserId) - return dataSourceSubUserDetailRead(ctx, d, m) -} + 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 { + d.SetId("") + return nil + } + 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 7e157e7..010422a 100644 --- a/fptcloud/object-storage/resource_sub_user_keys.go +++ b/fptcloud/object-storage/resource_sub_user_keys.go @@ -2,6 +2,7 @@ package fptcloud_object_storage import ( "context" + "fmt" common "terraform-provider-fptcloud/commons" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -14,20 +15,33 @@ func ResourceSubUserKeys() *schema.Resource { ReadContext: dataSourceSubUserRead, DeleteContext: resourceSubUserAccessKeyDelete, Schema: map[string]*schema.Schema{ - "user_id": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, "vpc_id": { - Type: schema.TypeString, - Required: true, - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "The VPC id that the S3 service belongs to", }, "region_name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, + 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", + }, + "sub_user_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "The sub user id, can retrieve from data source `fptcloud_object_storage_sub_user`", + }, + "access_key": { + Type: schema.TypeString, + Computed: true, + Description: "The access key of the sub user", + }, + "secret_key": { + Type: schema.TypeString, + Computed: true, + Description: "The secret key of the sub user", }, }, } @@ -37,33 +51,36 @@ func resourceSubUserAccessKeyCreate(ctx context.Context, d *schema.ResourceData, objectStorageService := NewObjectStorageService(client) vpcId := d.Get("vpc_id").(string) - s3ServiceId := d.Get("s3_service_id").(string) subUserId := d.Get("sub_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))) + } + resp := objectStorageService.CreateSubUserAccessKey(vpcId, s3ServiceDetail.S3ServiceId, subUserId) - accessKey := objectStorageService.CreateSubUserAccessKey(vpcId, s3ServiceId, subUserId) - if accessKey == nil { - return diag.Errorf("failed to create sub-user access key") + if !resp.Status { + return diag.FromErr(fmt.Errorf("error creating sub-user access key: %s", resp.Message)) } - d.SetId(accessKey.Credential.AccessKey) - d.Set("access_key", accessKey.Credential.AccessKey) - d.Set("secret_key", accessKey.Credential.SecretKey) + d.SetId(subUserId) + d.Set("access_key", resp.Credential.AccessKey) + d.Set("secret_key", resp.Credential.SecretKey) - return dataSourceSubUserRead(ctx, d, m) + return nil } -func resourceSubUserAccessKeyDelete(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { +func resourceSubUserAccessKeyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { client := m.(*common.Client) objectStorageService := NewObjectStorageService(client) vpcId := d.Get("vpc_id").(string) s3ServiceId := d.Get("s3_service_id").(string) subUserId := d.Get("sub_user_id").(string) - accessKeyId := d.Id() + accessKeyToDelete := d.Id() - resp := objectStorageService.DeleteSubUserAccessKey(vpcId, s3ServiceId, subUserId, accessKeyId) + resp := objectStorageService.DeleteSubUserAccessKey(vpcId, s3ServiceId, subUserId, accessKeyToDelete) if !resp.Status { - return diag.Errorf("failed to delete sub-user access key") + return diag.Errorf("failed to delete sub-user access key: %s", resp.Message) } return nil