Skip to content

Commit

Permalink
Update: region name validate, fix bucket config
Browse files Browse the repository at this point in the history
  • Loading branch information
hoanglm committed Nov 13, 2024
1 parent 9dbeb86 commit 5197692
Show file tree
Hide file tree
Showing 20 changed files with 145 additions and 59 deletions.
26 changes: 26 additions & 0 deletions commons/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -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"
}

Original file line number Diff line number Diff line change
@@ -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"
}
17 changes: 9 additions & 8 deletions fptcloud/object-storage/datasource_object_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand All @@ -77,8 +76,7 @@ func dataSourceSubUserDetailRead(ctx context.Context, d *schema.ResourceData, m
if subUser.Arn != nil {
d.Set("arn", subUser.Arn)

Check failure on line 77 in fptcloud/object-storage/datasource_object_storage_sub_user_detail.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Error return value of `d.Set` is not checked (errcheck)
}
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
}
Expand All @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion fptcloud/object-storage/resource_access_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion fptcloud/object-storage/resource_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion fptcloud/object-storage/resource_bucket_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion fptcloud/object-storage/resource_bucket_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 3 additions & 5 deletions fptcloud/object-storage/resource_bucket_static_website.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion fptcloud/object-storage/resource_bucket_versioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
36 changes: 28 additions & 8 deletions fptcloud/object-storage/resource_sub_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
}
Expand All @@ -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 {

Check failure on line 69 in fptcloud/object-storage/resource_sub_user.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

`readDetailSubUserOnly` - `ctx` is unused (unparam)
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)
Expand Down
Loading

0 comments on commit 5197692

Please sign in to comment.