Skip to content

Commit

Permalink
[Object Storage] update: fix lifecycle, acl, access keys data source
Browse files Browse the repository at this point in the history
  • Loading branch information
hoanglm committed Nov 13, 2024
1 parent e7b7bef commit 0305e99
Show file tree
Hide file tree
Showing 19 changed files with 539 additions and 278 deletions.
12 changes: 6 additions & 6 deletions commons/api_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ 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
Expand Down Expand Up @@ -308,8 +308,8 @@ 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)
Expand All @@ -318,8 +318,8 @@ var ApiPath = struct {
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 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_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_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_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_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
}
159 changes: 159 additions & 0 deletions fptcloud/object-storage/dataqsource_object_storage_bucket_acl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
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 DataSourceBucketAcl() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBucketAclRead,
Schema: map[string]*schema.Schema{
"vpc_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The VPC ID",
},
"bucket_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the bucket to config the ACL",
},
"region_name": {
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",
},
"canned_acl": {
Type: schema.TypeString,
Computed: true,
Description: "The Access Control List (ACL) status of the bucket which can be one of the following values: private, public-read, default is private",
},
"status": {
Type: schema.TypeBool,
Computed: true,
Description: "The status after configuring the bucket ACL",
},
"bucket_acl": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"owner": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"display_name": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"grants": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"grantee": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"display_name": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"permission": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
},
}
}

func dataSourceBucketAclRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*common.Client)
service := NewObjectStorageService(client)
vpcId := d.Get("vpc_id").(string)
bucketName := d.Get("bucket_name").(string)
regionName := d.Get("region_name").(string)
s3ServiceDetail := getServiceEnableRegion(service, vpcId, regionName)
if s3ServiceDetail.S3ServiceId == "" {
return diag.FromErr(fmt.Errorf("region %s is not enabled", regionName))
}
r := service.GetBucketAcl(vpcId, s3ServiceDetail.S3ServiceId, bucketName)
if !r.Status {
return diag.Errorf("failed to get bucket ACL for bucket %s", bucketName)
}
bucketAcl := []interface{}{
map[string]interface{}{
"owner": []interface{}{
map[string]interface{}{
"display_name": r.Owner.DisplayName,
"id": r.Owner.ID,
},
},
"grants": func() []interface{} {
grants := make([]interface{}, len(r.Grants))
for i, grant := range r.Grants {
grants[i] = map[string]interface{}{
"grantee": []interface{}{
map[string]interface{}{
"display_name": grant.Grantee.DisplayName,
"id": grant.Grantee.ID,
"type": grant.Grantee.Type,
},
},
"permission": grant.Permission,
}
}
return grants
}(),
},
}
d.SetId(bucketName)
if err := d.Set("bucket_acl", bucketAcl); err != nil {
d.SetId("")
return diag.FromErr(err)
}
if err := d.Set("canned_acl", r.CannedACL); err != nil {
d.SetId("")
return diag.FromErr(err)
}
if err := d.Set("status", r.Status); err != nil {
d.SetId("")
return diag.FromErr(err)
}

return nil
}
Loading

0 comments on commit 0305e99

Please sign in to comment.