Skip to content

Commit

Permalink
Update: region name validate, fix bucket config
Browse files Browse the repository at this point in the history
[Object Storage] update: change payload list sub user

[Object Storage] update: fix lifecycle, acl, access keys data source

[Object Storage] update: fix config bucket lifecycle cannot create

[Object Storage] fix: fix payload bucket lifecycle

[WIP] [Object Storage] fix: bucket cors cannot delete

Update: cors example
  • Loading branch information
hoanglm committed Nov 18, 2024
1 parent d4c536e commit 92b2df3
Show file tree
Hide file tree
Showing 46 changed files with 1,570 additions and 621 deletions.
26 changes: 13 additions & 13 deletions commons/api_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,25 @@ var ApiPath = struct {
GetBucketVersioning func(vpcId, s3ServiceId, bucketName string) string
PutBucketVersioning func(vpcId, s3ServiceId, bucketName string) string
// Bucket Lifecycle
GetBucketLifecycle func(vpcId, s3ServiceId, bucketName, page, pageSize string) string
GetBucketLifecycle func(vpcId, s3ServiceId, bucketName string, page, pageSize int) string
PutBucketLifecycle func(vpcId, s3ServiceId, bucketName string) string
DeleteBucketLifecycle func(vpcId, s3ServiceId, bucketName string) string
// Bucket CORS
GetBucketCORS func(vpcId, s3ServiceId, bucketName string) string
GetBucketCORS func(vpcId, s3ServiceId, bucketName string, page, pageSize int) string
PutBucketCORS func(vpcId, s3ServiceId, bucketName string) string
CreateBucketCors func(vpcId, s3ServiceId, bucketName string) string
// Bucket ACL
GetBucketAcl func(vpcId, s3ServiceId, bucketName string) string
PutBucketAcl func(vpcId, s3ServiceId, bucketName string) string

// Sub-user
ListSubUsers func(vpcId, s3ServiceId string) string
ListSubUsers func(vpcId, s3ServiceId string, page, pageSize int) 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
DeleteSubUserAccessKey func(vpcId, s3ServiceId, subUserId string) string
// Access Key
ListAccessKeys func(vpcId, s3ServiceId string) string
CreateAccessKey func(vpcId, s3ServiceId string) string
Expand Down Expand Up @@ -308,18 +308,18 @@ var ApiPath = struct {
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/%s/bucket/%s/delete-config", vpcId, s3ServiceId, bucketName)
},
// Bucket Lifecycle
GetBucketLifecycle: func(vpcId, s3ServiceId, bucketName, page, pageSize string) string {
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/%s/bucket/%s/lifecycles?page=%s&page_size=%s", vpcId, s3ServiceId, bucketName, page, pageSize)
GetBucketLifecycle: func(vpcId, s3ServiceId, bucketName string, page, pageSize int) string {
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/%s/bucket/%s/lifecycles?page=%d&page_size=%d", vpcId, s3ServiceId, bucketName, page, pageSize)
},
PutBucketLifecycle: func(vpcId, s3ServiceId, bucketName string) string {
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/%s/bucket/%s/create-bucket-lifecycle-configuration`", vpcId, s3ServiceId, bucketName)
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/%s/bucket/%s/create-bucket-lifecycle-configuration", vpcId, s3ServiceId, bucketName)
},
DeleteBucketLifecycle: func(vpcId, s3ServiceId, bucketName string) string {
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/%s/bucket/%s/delete-bucket-lifecycle-configuration", vpcId, s3ServiceId, bucketName)
},
// Bucket CORS
GetBucketCORS: func(vpcId, s3ServiceId, bucketName string) string {
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/%s/bucket/%s/cors", vpcId, s3ServiceId, bucketName)
GetBucketCORS: func(vpcId, s3ServiceId, bucketName string, page, pageSize int) string {
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/%s/bucket/%s/cors?page=%d&page_size=%d", vpcId, s3ServiceId, bucketName, page, pageSize)
},
PutBucketCORS: func(vpcId, s3ServiceId, bucketName string) string {
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/%s/bucket/%s/put-bucket-cors", vpcId, s3ServiceId, bucketName)
Expand All @@ -335,8 +335,8 @@ var ApiPath = struct {
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/%s/bucket/%s/acl", vpcId, s3ServiceId, bucketName)
},
// Sub-user
ListSubUsers: func(vpcId, serviceId string) string {
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/%s/sub-users/list", vpcId, serviceId)
ListSubUsers: func(vpcId, serviceId string, page, pageSize int) string {
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/%s/sub-users/list?page=%d&page_size=%d", vpcId, serviceId, page, pageSize)
},
CreateSubUser: func(vpcId, s3ServiceId string) string {
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/%s/sub-users/create", vpcId, s3ServiceId)
Expand All @@ -354,8 +354,8 @@ var ApiPath = struct {
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)
DeleteSubUserAccessKey: func(vpcId, s3ServiceId, subUserId string) string {
return fmt.Sprintf("/v1/vmware/vpc/%s/s3/%s/sub-users/%s/credentials/delete", vpcId, s3ServiceId, subUserId)
},

// Access Key
Expand Down
8 changes: 3 additions & 5 deletions commons/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -112,6 +113,7 @@ func (c *Client) SendRequest(req *http.Request) ([]byte, error) {

body, err := io.ReadAll(resp.Body)
c.LastJSONResponse = string(body)
log.Printf("[DEBUG] Response: %s - URL: %s", c.LastJSONResponse, resp.Request.URL.String())

if resp.StatusCode >= 300 {
return nil, HTTPError{Code: resp.StatusCode, Status: resp.Status, Reason: string(body)}
Expand Down Expand Up @@ -166,7 +168,6 @@ func (c *Client) SendDeleteRequest(requestURL string) ([]byte, error) {
if err != nil {
return nil, err
}

return c.SendRequest(req)
}

Expand All @@ -175,10 +176,7 @@ func (c *Client) SendDeleteRequestWithBody(requestURL string, params interface{}
u := c.PrepareClientURL(requestURL)

// we create a new buffer and encode everything to json to send it in the request
jsonValue, err := json.Marshal(params)
if err != nil {
return nil, err
}
jsonValue, _ := json.Marshal(params)

req, err := http.NewRequest("DELETE", u.String(), bytes.NewBuffer(jsonValue))
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
data "fptcloud_object_storage_access_key" "keys" {
vpc_id = "your_vpc_id"
region_name = "your_region_name"
}
// for raw data and all access keys from region_name will be listed
output "access_key" {
value = data.fptcloud_object_storage_access_key.keys
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data "fptcloud_object_storage_bucket_acl" "example_bucket_acl" {
vpc_id = "your_vpc_id"
region_name = "your_region_name"
bucket_name = "your_bucket_name"
}

output "bucket_acl" {
value = data.fptcloud_object_storage_bucket_acl.example_bucket_acl
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
data "fptcloud_object_storage_bucket_cors" "example_bucket_cors" {
vpc_id = "1b413c55-b752-4183-abad-06c4b5aca6ad"
region_name = "HCM-02"
bucket_name = "hoanglm3-test-terraform-static-website"
page = 1
page_size = 100
}

output "bucket_cors" {
value = data.fptcloud_object_storage_bucket_cors.example_bucket_cors
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
data "fptcloud_object_storage_bucket_lifecycle" "example_bucket_lifecycle" {
vpc_id = "your_vpc_id"
region_name = "your_region_name"
bucket_name = "your_bucket_name"
page = 1
page_size = 100
}

output "bucket_lifecycle" {
value = data.fptcloud_object_storage_bucket_lifecycle.example_bucket_lifecycle
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data "fptcloud_object_storage_bucket_policy" "example_bucket_policy" {
vpc_id = "your_vpc_id"
region_name = "your_region_name"
bucket_name = "your_bucket_name"
}

output "bucket_policy" {
value = data.fptcloud_object_storage_bucket_policy.example_bucket_policy.policy
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data "fptcloud_object_storage_bucket_static_website" "example_bucket_static_website" {
vpc_id = "your_vpc_id"
region_name = "your_region_name"
bucket_name = "your_bucket_name"
}

output "bucket_static_website" {
value = data.fptcloud_object_storage_bucket_static_website.example_bucket_static_website
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data "fptcloud_object_storage_bucket_versioning" "example_bucket_versioning" {
vpc_id = "your_vpc_id"
region_name = "your_region_name"
bucket_name = "your_bucket_name"
}

output "bucket_versioning" {
value = data.fptcloud_object_storage_bucket_versioning.example_bucket_versioning
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
data "fptcloud_object_storage_bucket" "hoanglm32" {
vpc_id = "your_vpc_id"
page = 1
page_size = 100000
region_name = "your_region_name"
}
// for raw data and all buckets will be listed
output "name" {
value = data.fptcloud_object_storage_bucket.hoanglm32.list_bucket_result
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
data "fptcloud_s3_service_enable" "hoanglm32" {
vpc_id = "your_vpc_id"
}
// All regions formatted
output "all_regions_formatted" {
value = {
for region in data.fptcloud_s3_service_enable.hoanglm32.s3_enable_services :
region.s3_service_name => {
id = region.s3_service_id
platform = region.s3_platform
region_name = region.s3_service_name
}
}
}
// Region name only, * for all if you want specific index, use [0], [1], ...
output "region_name" {
value = data.fptcloud_s3_service_enable.hoanglm32.s3_enable_services[*].s3_service_name
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
data "fptcloud_object_storage_sub_user" "hoanglm32" {
vpc_id = "your_vpc_id"
page = 1
page_size = 100000
region_name = "your_region_name"
}
// for raw data and all sub users will be listed
output "list_sub_user" {
value = data.fptcloud_object_storage_sub_user.hoanglm32.list_sub_user
}
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,23 @@
resource "fptcloud_object_storage_bucket_cors" "example_bucket_cors" {
vpc_id = "1b413c55-b752-4183-abad-06c4b5aca6ad"
region_name = "HCM-02"
bucket_name = "a-hoanglm32-test"

# Option 1: Load cors config from file
cors_config_file = file("${path.module}/your_bucket_cors_config.json")

# Option 2: Inline cors_config
# cors_config = jsonencode({
# {
# "ID": "a9099",
# "AllowedOrigins": ["http://www.example.com", "http://www.example2.com"],
# "AllowedMethods": ["GET", "PUT", "DELETE"],
# "MaxAgeSeconds": 3000,
# "ExposeHeaders": ["Etag", "x-amz"],
# "AllowedHeaders": ["*", "demo"]
# }
# })
}
output "bucket_cors" {
value = fptcloud_object_storage_bucket_cors.example_bucket_cors.status
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "fptcloud_object_storage_bucket_lifecycle" "example_bucket_lifecycle" {
bucket_name = "your_bucket_name"
region_name = "your_region_name"
vpc_id = "your_vpc_id"

# Option 1: Load policy from file
life_cycle_rule_file = file("${path.module}/your_bucket_lifecycle.json")
}

output "bucket_lifecycle" {
value = fptcloud_object_storage_bucket_lifecycle.example_bucket_lifecycle
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
resource "fptcloud_object_storage_bucket_policy" "example_bucket_policy" {
vpc_id = "your_vpc_id"
region_name = "your_region_name"
bucket_name = "your_bucket_name"

// Option 1: Load policy from file
policy_file = file("${path.module}/your_bucket_policy_json_content.json")

// Option 2: Inline policy
// policy = jsonencode({
// Version = "2012-10-17"
// Statement = [
// {
// Sid = "PublicReadGetObject"
// Effect = "Allow"
// Principal = "*"
// Action = "s3:GetObject"
// Resource = "arn:aws:s3:::example-bucket/*"
// }
// ]
// })
}
// NOTE: In case wanna delete bucket policy, just ignore policy_file and policy fields
output "bucket_policy" {
value = fptcloud_object_storage_bucket_policy.example_bucket_policy.status
}
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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "fptcloud_object_storage_user_key" "test_key" {
vpc_id = "your_vpc_id"
region_name = "your_region_name"
user_id = "your_user_id"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "fptcloud_object_storage_access_key" "test_key" {
vpc_id = "your_vpc_id"
region_name = "your_region_name"
}
Loading

0 comments on commit 92b2df3

Please sign in to comment.