-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Object Storage] update: fix lifecycle, acl, access keys data source
- Loading branch information
hoanglm
committed
Nov 13, 2024
1 parent
e7b7bef
commit 0305e99
Showing
19 changed files
with
539 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
examples/data-sources/fptcloud_object_storage/datasource_list_access_keys.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
9 changes: 9 additions & 0 deletions
9
examples/data-sources/fptcloud_object_storage/datasource_list_bucket_acl.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
11 changes: 11 additions & 0 deletions
11
examples/data-sources/fptcloud_object_storage/datasource_list_bucket_cors.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
11 changes: 11 additions & 0 deletions
11
examples/data-sources/fptcloud_object_storage/datasource_list_bucket_lifecycle.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
9 changes: 9 additions & 0 deletions
9
examples/data-sources/fptcloud_object_storage/datasource_list_bucket_policy.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
9 changes: 9 additions & 0 deletions
9
examples/data-sources/fptcloud_object_storage/datasource_list_bucket_static_website.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
9 changes: 9 additions & 0 deletions
9
examples/data-sources/fptcloud_object_storage/datasource_list_bucket_versioning.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
159
fptcloud/object-storage/dataqsource_object_storage_bucket_acl.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.