Skip to content

Commit

Permalink
[Object Storage] update: update missing code about action sub user
Browse files Browse the repository at this point in the history
  • Loading branch information
hoanglm committed Oct 29, 2024
1 parent 97c6db0 commit 782994e
Show file tree
Hide file tree
Showing 10 changed files with 494 additions and 24 deletions.
22 changes: 17 additions & 5 deletions commons/api_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ var ApiPath = struct {
PutBucketAcl func(vpcId, s3ServiceId, bucketName string) string

// Sub-user
ListSubUsers func(vpcId, s3ServiceId string) string
CreateSubUser func(vpcId, s3ServiceId string) string
UpdateSubUser func(vpcId, s3ServiceId, subUserId string) string
DeleteSubUser func(vpcId, s3ServiceId, subUserId string) string

ListSubUsers func(vpcId, s3ServiceId string) string
CreateSubUser func(vpcId, s3ServiceId string) string
UpdateSubUser func(vpcId, s3ServiceId, subUserId string) string
DeleteSubUser func(vpcId, s3ServiceId, subUserId string) string
DetailSubUser func(vpcId, s3ServiceId, subUserId string) string
CreateSubUserAccessKey func(vpcId, s3ServiceId, subUserId string) string
DeleteSubUserAccessKey func(vpcId, s3ServiceId, subUserId, accessKeyId string) string
// Access Key
ListAccessKeys func(vpcId string) string
CreateAccessKey func(vpcId, s3ServiceId string) string
Expand Down Expand Up @@ -349,6 +351,16 @@ var ApiPath = struct {
DeleteSubUser: func(vpcId, s3ServiceId, subUserId string) string {
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/%s/sub-users/%s/delete", vpcId, s3ServiceId, subUserId)
},
CreateSubUserAccessKey: func(vpcId, s3ServiceId, subUserId string) string {
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/%s/sub-users/%s/credentials/create", vpcId, s3ServiceId, subUserId)
},
DeleteSubUserAccessKey: func(vpcId, s3ServiceId, subUserId, accessKeyId string) string {
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/%s/sub-users/%s/credentials/%s/delete", vpcId, s3ServiceId, subUserId, accessKeyId)
},
DetailSubUser: func(vpcId, s3ServiceId, subUserId string) string {
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/%s/sub-users/%s/detail", vpcId, s3ServiceId, subUserId)
},

// Access Key
ListAccessKeys: func(vpcId string) string {
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/user/credentials", vpcId)
Expand Down
51 changes: 50 additions & 1 deletion fptcloud/object-storage/datasource_object_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type AccessKey struct {
}
type CreateAccessKeyResponse struct {
Status bool `json:"status"`
Message string `json:"message"`
Message string `json:"message,omitempty"`
Credential struct {
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
Expand Down Expand Up @@ -170,6 +170,15 @@ type BucketLifecycleResponse struct {
Total int `json:"total"`
}

type DetailSubUser struct {
UserID string `json:"user_id"`
Arn interface{} `json:"arn"`
Active bool `json:"active"`
Role string `json:"role"`
CreatedAt interface{} `json:"created_at"`
AccessKeys []string `json:"access_keys"`
}

// ObjectStorageService defines the interface for object storage operations
type ObjectStorageService interface {
CheckServiceEnable(vpcId string) S3ServiceEnableResponse
Expand All @@ -188,6 +197,9 @@ type ObjectStorageService interface {
CreateSubUser(req SubUser, vpcId, s3ServiceId string) (*SubUser, error)
DeleteSubUser(vpcId, s3ServiceId, subUserId string) error
ListSubUsers(vpcId, s3ServiceId string) ([]SubUser, error)
DetailSubUser(vpcId, s3ServiceId, subUserId string) *DetailSubUser
CreateSubUserAccessKey(vpcId, s3ServiceId, subUserId string) *CreateAccessKeyResponse
DeleteSubUserAccessKey(vpcId, s3ServiceId, subUserId, accessKeyId string) CommonResponse

// bucket configuration
PutBucketPolicy(vpcId, s3ServiceId, bucketName string, policy BucketPolicyRequest) CommonResponse
Expand All @@ -209,6 +221,7 @@ type ObjectStorageService interface {
// Static website configuration
PutBucketWebsite(vpcId, s3ServiceId, bucketName string, website BucketWebsiteRequest) CommonResponse
GetBucketWebsite(vpcId, s3ServiceId, bucketName string) *BucketWebsiteResponse

// Lifecycle configuration
GetBucketLifecycle(vpcId, s3ServiceId, bucketName, page, pageSize string) (*BucketLifecycleResponse, error)
PutBucketLifecycle(vpcId, s3ServiceId, bucketName string, lifecycle interface{}) (*BucketLifecycleResponse, error)
Expand Down Expand Up @@ -529,3 +542,39 @@ func (s *ObjectStorageServiceImpl) DeleteBucketLifecycle(vpcId, s3ServiceId, buc
}
return &bucketLifecycle, nil
}

func (s *ObjectStorageServiceImpl) CreateSubUserAccessKey(vpcId, s3ServiceId, subUserId string) *CreateAccessKeyResponse {
apiPath := common.ApiPath.CreateSubUserAccessKey(vpcId, s3ServiceId, subUserId)
resp, err := s.client.SendPostRequest(apiPath, nil)
if err != nil {
return nil
}

var accessKey CreateAccessKeyResponse
if err := json.Unmarshal(resp, &accessKey); err != nil {
return nil
}
return &accessKey
}

func (s *ObjectStorageServiceImpl) DeleteSubUserAccessKey(vpcId, s3ServiceId, subUserId, accessKeyId string) CommonResponse {
apiPath := common.ApiPath.DeleteSubUserAccessKey(vpcId, s3ServiceId, subUserId, accessKeyId)
if _, err := s.client.SendDeleteRequest(apiPath); err != nil {
return CommonResponse{Status: false}
}
return CommonResponse{Status: true}
}

func (s *ObjectStorageServiceImpl) DetailSubUser(vpcId, s3ServiceId, subUserId string) *DetailSubUser {
apiPath := common.ApiPath.DetailSubUser(vpcId, s3ServiceId, subUserId)
resp, err := s.client.SendGetRequest(apiPath)
if err != nil {
return nil
}

var detail DetailSubUser
if err := json.Unmarshal(resp, &detail); err != nil {
return nil
}
return &detail
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func DataSourceBucketPolicy() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBucketCorsRead,
ReadContext: dataSourceBucketPolicyRead,
Schema: map[string]*schema.Schema{
"vpc_id": {
Type: schema.TypeString,
Expand Down
75 changes: 75 additions & 0 deletions fptcloud/object-storage/datasource_object_storage_lifecycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package fptcloud_object_storage

import (
"context"
"fmt"
common "terraform-provider-fptcloud/commons"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DataSourceBucketLifecycle() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBucketLifecycle,
Schema: map[string]*schema.Schema{
"vpc_id": {
Type: schema.TypeString,
Required: true,
Description: "The VPC ID",
},
"bucket_name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the bucket to fetch policy for",
},
"policy": {
Type: schema.TypeString,
Computed: true,
Description: "The bucket policy in JSON format",
},
"region_name": {
Type: schema.TypeString,
Required: false,
Default: "HCM-02",
Optional: true,
Description: "The region name of the bucket",
},
"page_size": {
Type: schema.TypeString,
Optional: true,
Default: "25",
Description: "The number of items to return in each page",
},
"page": {
Type: schema.TypeString,
Optional: true,
Default: "1",
Description: "The page number",
},
},
}
}

func dataSourceBucketLifecycle(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)
s3ServiceDetail := getServiceEnableRegion(service, vpcId, d.Get("region_name").(string))
page := d.Get("page").(string)
pageSize := d.Get("page_size").(string)

lifeCycleResponse, err := service.GetBucketLifecycle(vpcId, s3ServiceDetail.S3ServiceId, bucketName, page, pageSize)
if err != nil {
return diag.FromErr(err)
}

d.SetId(fmt.Sprintf("%s-%s", vpcId, bucketName))
if err := d.Set("policy", lifeCycleResponse.Rules); err != nil {
return diag.FromErr(err)
}

return nil
}
142 changes: 142 additions & 0 deletions fptcloud/object-storage/datasource_object_storage_static_website.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package fptcloud_object_storage

import (
"context"
common "terraform-provider-fptcloud/commons"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DataSourceBucketStaticWebsite() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBucketStaticWebsite,
Schema: map[string]*schema.Schema{
"vpc_id": {
Type: schema.TypeString,
Required: true,
Description: "The VPC ID",
},
"bucket_name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the bucket to fetch policy for",
},
"region_name": {
Type: schema.TypeString,
Required: false,
Default: "HCM-02",
Optional: true,
Description: "The region name of the bucket",
},
"status": {
Type: schema.TypeBool,
Computed: true,
Description: "Status of the bucket website configuration",
},
"request_id": {
Type: schema.TypeString,
Computed: true,
Description: "Request ID of the operation",
},
"host_id": {
Type: schema.TypeString,
Computed: true,
Description: "Host ID of the operation",
},
"http_status_code": {
Type: schema.TypeInt,
Computed: true,
Description: "HTTP status code of the operation",
},
"http_headers": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "HTTP headers of the response",
},
"retry_attempts": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of retry attempts",
},
"index_document": {
Type: schema.TypeString,
Computed: true,
Description: "Suffix for index document",
},
"error_document": {
Type: schema.TypeString,
Computed: true,
Description: "Key for error document",
},
},
}
}

func dataSourceBucketStaticWebsite(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)
s3ServiceDetail := getServiceEnableRegion(service, vpcId, d.Get("region_name").(string))

staticWebsiteResponse := service.GetBucketWebsite(vpcId, bucketName, s3ServiceDetail.S3ServiceId)
if !staticWebsiteResponse.Status {
return diag.Errorf("failed to get bucket policy for bucket %s", bucketName)
}

d.SetId(bucketName)

// Set the computed values
if err := d.Set("status", staticWebsiteResponse.Status); err != nil {
return diag.FromErr(err)
}

if staticWebsiteResponse.Config.ResponseMetadata.RequestID != "" {
if err := d.Set("request_id", staticWebsiteResponse.Config.ResponseMetadata.RequestID); err != nil {
return diag.FromErr(err)
}
}

if staticWebsiteResponse.Config.ResponseMetadata.HostID != "" {
if err := d.Set("host_id", staticWebsiteResponse.Config.ResponseMetadata.HostID); err != nil {
return diag.FromErr(err)
}
}

if err := d.Set("http_status_code", staticWebsiteResponse.Config.ResponseMetadata.HTTPStatusCode); err != nil {
return diag.FromErr(err)
}

headers := map[string]string{
"x-amz-request-id": staticWebsiteResponse.Config.ResponseMetadata.HTTPHeaders.XAmzRequestID,
"content-type": staticWebsiteResponse.Config.ResponseMetadata.HTTPHeaders.ContentType,
"content-length": staticWebsiteResponse.Config.ResponseMetadata.HTTPHeaders.ContentLength,
"date": staticWebsiteResponse.Config.ResponseMetadata.HTTPHeaders.Date,
}
if err := d.Set("http_headers", headers); err != nil {
return diag.FromErr(err)
}

if err := d.Set("retry_attempts", staticWebsiteResponse.Config.ResponseMetadata.RetryAttempts); err != nil {
return diag.FromErr(err)
}

if staticWebsiteResponse.Config.IndexDocument.Suffix != "" {
if err := d.Set("index_document", staticWebsiteResponse.Config.IndexDocument.Suffix); err != nil {
return diag.FromErr(err)
}
}

if staticWebsiteResponse.Config.ErrorDocument.Key != "" {
if err := d.Set("error_document", staticWebsiteResponse.Config.ErrorDocument.Key); err != nil {
return diag.FromErr(err)
}
}

return nil
}
Loading

0 comments on commit 782994e

Please sign in to comment.