Skip to content

Commit

Permalink
[Object Storage] feat: implement crud bucket,access key,sub user
Browse files Browse the repository at this point in the history
  • Loading branch information
hoanglm committed Oct 22, 2024
1 parent 5b4367f commit db02692
Show file tree
Hide file tree
Showing 5 changed files with 412 additions and 25 deletions.
190 changes: 190 additions & 0 deletions fptcloud/object-storage/datasource_object_storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package fptcloud_object_storage

import (
"encoding/json"
"fmt"
common "terraform-provider-fptcloud/commons"
)

// BucketCreateRequest represents the request body for creating a bucket
type BucketCreateRequest struct {
Name string `json:"name"`
Region string `json:"region"`
StorageClass string `json:"storage_class"`
Versioning bool `json:"versioning"`
Tags map[string]string `json:"tags,omitempty"`
}

// SubUserCreateRequest represents the request body for creating a sub-user
type SubUserCreateRequest struct {
Username string `json:"username"`
DisplayName string `json:"display_name"`
Email string `json:"email"`
Permissions []string `json:"permissions"`
}
type AccessKey struct {
Credentials []struct {
ID string `json:"id"`
Credentials []struct {
AccessKey string `json:"accessKey"`
Active bool `json:"active"`
CreatedDate interface{} `json:"createdDate"`
} `json:"credentials"`
} `json:"credentials"`
}
type CreateAccessKeyResponse struct {
Status bool `json:"status"`
Message string `json:"message"`
Credential struct {
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
Active interface{} `json:"active"`
CreatedDate interface{} `json:"createdDate"`
} `json:"credential"`
}
type SubUser struct {
Role string `json:"role"`
UserId string `json:"user_id,omitempty"`
}
type CorsRule struct {
ID string `json:"ID"`
AllowedOrigins []string `json:"AllowedOrigins"`
AllowedMethods []string `json:"AllowedMethods"`
MaxAgeSeconds int `json:"MaxAgeSeconds,omitempty"`
ExposeHeaders []string `json:"ExposeHeaders,omitempty"`
AllowedHeaders []string `json:"AllowedHeaders,omitempty"`
}
type BucketCors struct {
Status bool `json:"status"`
CorsRules []CorsRule `json:"cors_rules"`
Total int `json:"total"`
}

// ObjectStorageService defines the interface for object storage operations
type ObjectStorageService interface {
CreateBucket(req BucketCreateRequest) (*Bucket, error)
CreateSubUser(req SubUser) (*SubUser, error)
CreateAccessKey() (*CreateAccessKeyResponse, error)
ListBuckets() ([]Bucket, error)
ListSubUsers() ([]SubUser, error)
ListAccessKeys() ([]AccessKey, error)
}

// ObjectStorageServiceImpl is the implementation of ObjectStorageService
type ObjectStorageServiceImpl struct {
client *common.Client
}

// NewObjectStorageService creates a new instance of ObjectStorageService
func NewObjectStorageService(client *common.Client) ObjectStorageService {
return &ObjectStorageServiceImpl{client: client}
}

// Bucket represents the response structure for a created bucket
type Bucket struct {
ID string `json:"id"`
Name string `json:"name"`
Region string `json:"region"`
StorageClass string `json:"storage_class"`
Versioning bool `json:"versioning"`
Tags map[string]string `json:"tags,omitempty"`
CreationDate string `json:"creation_date"`
}

// CreateBucket creates a new bucket
func (s *ObjectStorageServiceImpl) CreateBucket(req BucketCreateRequest) (*Bucket, error) {
apiPath := "/v1/object-storage/buckets"
resp, err := s.client.SendPostRequest(apiPath, req)
if err != nil {
return nil, fmt.Errorf("failed to create bucket: %v", err)
}

var bucket Bucket
err = json.Unmarshal(resp, &bucket)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal bucket response: %v", err)
}

return &bucket, nil
}

// CreateSubUser creates a new sub-user
func (s *ObjectStorageServiceImpl) CreateSubUser(req SubUser) (*SubUser, error) {
apiPath := "/v1/object-storage/sub-users"
resp, err := s.client.SendPostRequest(apiPath, req)
if err != nil {
return nil, fmt.Errorf("failed to create sub-user: %v", err)
}

var subUser SubUser
err = json.Unmarshal(resp, &subUser)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal sub-user response: %v", err)
}

return &subUser, nil
}

func (s *ObjectStorageServiceImpl) CreateAccessKey() (*CreateAccessKeyResponse, error) {
apiPath := "/v1/object-storage/access-keys"
resp, err := s.client.SendPostRequest(apiPath, nil)
if err != nil {
return nil, fmt.Errorf("failed to create access key: %v", err)
}

var accessKey CreateAccessKeyResponse
err = json.Unmarshal(resp, &accessKey)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal access key response: %v", err)
}

return &accessKey, nil
}

func (s *ObjectStorageServiceImpl) ListBuckets() ([]Bucket, error) {
apiPath := "/v1/object-storage/buckets"
resp, err := s.client.SendGetRequest(apiPath)
if err != nil {
return nil, fmt.Errorf("failed to list buckets: %v", err)
}

var buckets []Bucket
err = json.Unmarshal(resp, &buckets)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal bucket list response: %v", err)
}

return buckets, nil
}

func (s *ObjectStorageServiceImpl) ListSubUsers() ([]SubUser, error) {
apiPath := "/v1/object-storage/sub-users"
resp, err := s.client.SendGetRequest(apiPath)
if err != nil {
return nil, fmt.Errorf("failed to list sub-users: %v", err)
}

var subUsers []SubUser
err = json.Unmarshal(resp, &subUsers)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal sub-user list response: %v", err)
}

return subUsers, nil
}

func (s *ObjectStorageServiceImpl) ListAccessKeys() ([]AccessKey, error) {
apiPath := "/v1/object-storage/access-keys"
resp, err := s.client.SendGetRequest(apiPath)
if err != nil {
return nil, fmt.Errorf("failed to list access keys: %v", err)
}

var accessKeys []AccessKey
err = json.Unmarshal(resp, &accessKeys)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal access key list response: %v", err)
}

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

import (
"context"

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

func ResourceAccessKey() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAccessKeyCreate,
ReadContext: resourceAccessKeyRead,
DeleteContext: resourceAccessKeyDelete,
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"access_key": {
Type: schema.TypeString,
Computed: true,
},
"secret_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
}
}

func resourceAccessKeyCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return nil
}

func resourceAccessKeyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
// client := m.(*common.Client)
// objectStorageService := NewObjectStorageService(client)
// fmt.Println("Reading Access Key")

return nil
}

func resourceAccessKeyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return nil
}
80 changes: 80 additions & 0 deletions fptcloud/object-storage/resource_bucket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package fptcloud_object_storage

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
common "terraform-provider-fptcloud/commons"
)

func ResourceBucket() *schema.Resource {
return &schema.Resource{
CreateContext: resourceBucketCreate,
ReadContext: resourceBucketRead,
UpdateContext: resourceBucketUpdate,
DeleteContext: resourceBucketDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"region": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"storage_class": {
Type: schema.TypeString,
Optional: true,
Default: "STANDARD",
},
"versioning": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"tags": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

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

req := BucketCreateRequest{
Name: d.Get("name").(string),
Region: d.Get("region").(string),
StorageClass: d.Get("storage_class").(string),
Versioning: d.Get("versioning").(bool),
Tags: d.Get("tags").(map[string]string),
}

bucket, err := objectStorageService.CreateBucket(req)
if err != nil {
return diag.FromErr(err)
}

d.SetId(bucket.ID)
return resourceBucketRead(ctx, d, m)
}

func resourceBucketRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
// Implement the read logic
return nil
}

func resourceBucketUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
// Implement the update logic
return resourceBucketRead(ctx, d, m)
}

func resourceBucketDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
// Implement the delete logic
return nil
}
63 changes: 63 additions & 0 deletions fptcloud/object-storage/resource_sub_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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 ResourceSubUser() *schema.Resource {
return &schema.Resource{
CreateContext: resourceSubUserCreate,
ReadContext: resourceSubUserRead,
UpdateContext: resourceSubUserUpdate,
DeleteContext: resourceSubUserDelete,
Schema: map[string]*schema.Schema{
"role": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"user_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

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

req := SubUser{
Role: d.Get("role").(string),
UserId: d.Get("user_id").(string),
}

subUser, err := objectStorageService.CreateSubUser(req)
if err != nil {
return diag.FromErr(err)
}

d.SetId(subUser.UserId)
d.Set("role", subUser.Role)

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

View workflow job for this annotation

GitHub Actions / test (macos-latest)

Error return value of `d.Set` is not checked (errcheck)
return resourceSubUserRead(ctx, d, m)
}

func resourceSubUserRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
// Implement the read logic
return nil
}

func resourceSubUserUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
// Implement the update logic
return resourceSubUserRead(ctx, d, m)
}

func resourceSubUserDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
// Implement the delete logic
return nil
}
Loading

0 comments on commit db02692

Please sign in to comment.