Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.199
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.199
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199
github.com/tencentyun/cos-go-sdk-v5 v0.7.31-0.20210902132439-360bc9b1be6b
github.com/tencentyun/cos-go-sdk-v5 v0.7.33
github.com/yangwenmai/ratelimit v0.0.0-20180104140304-44221c2292e1
github.com/zclconf/go-cty v1.4.2 // indirect
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199 h1:hMBLtiJ
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199/go.mod h1:nnY91/H3j/Gu7V/oCA6Zeg8T5D3q36EUdBh4EjmHwqY=
github.com/tencentyun/cos-go-sdk-v5 v0.7.31-0.20210902132439-360bc9b1be6b h1:rLl5sAeLt382023Kd3X4TaOEaT2hdgXWwTGyKiy16Zo=
github.com/tencentyun/cos-go-sdk-v5 v0.7.31-0.20210902132439-360bc9b1be6b/go.mod h1:4E4+bQ2gBVJcgEC9Cufwylio4mXOct2iu05WjgEBx1o=
github.com/tencentyun/cos-go-sdk-v5 v0.7.33 h1:5jmJU7U/1nf/7ZPDkrUL8KlF1oDUzTHsdtLNY6x0hq4=
github.com/tencentyun/cos-go-sdk-v5 v0.7.33/go.mod h1:4E4+bQ2gBVJcgEC9Cufwylio4mXOct2iu05WjgEBx1o=
github.com/tetafro/godot v0.3.7 h1:+mecr7RKrUKB5UQ1gwqEMn13sDKTyDR8KNIquB9mm+8=
github.com/tetafro/godot v0.3.7/go.mod h1:/7NLHhv08H1+8DNj0MElpAACw1ajsCuf3TKNQxA5S+0=
github.com/timakin/bodyclose v0.0.0-20190930140734-f7f2e9bca95e h1:RumXZ56IrCj4CL+g1b9OL/oH0QnsF976bC8xQFYUD5Q=
Expand Down
1 change: 1 addition & 0 deletions tencentcloud/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ the following must be changed to your resource id.
*/

var appid string = os.Getenv("TENCENTCLOUD_APPID")
var ownerUin string = os.Getenv("TENCENTCLOUD_OWNER_UIN")

const (
defaultRegion = "ap-guangzhou"
Expand Down
5 changes: 3 additions & 2 deletions tencentcloud/connectivity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,12 @@ func (me *TencentCloudClient) UseCosClient() *s3.S3 {

// UseTencentCosClient tencent cloud own client for service instead of aws
func (me *TencentCloudClient) UseTencentCosClient(bucket string) *cos.Client {
if me.tencentCosConn != nil {
u, _ := url.Parse(fmt.Sprintf("https://%s.cos.%s.myqcloud.com", bucket, me.Region))

if me.tencentCosConn != nil && me.tencentCosConn.BaseURL.BucketURL == u {
return me.tencentCosConn
}

u, _ := url.Parse(fmt.Sprintf("https://%s.cos.%s.myqcloud.com", bucket, me.Region))
baseUrl := &cos.BaseURL{
BucketURL: u,
}
Expand Down
191 changes: 185 additions & 6 deletions tencentcloud/resource_tc_cos_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,28 @@ resource "tencentcloud_cos_bucket" "with_origin" {
}
```

Using replication
```hcl
resource "tencentcloud_cos_bucket" "replica1" {
bucket = "tf-replica-foo-1234567890"
acl = "private"
versioning_enable = true
}

resource "tencentcloud_cos_bucket" "with_replication" {
bucket = "tf-bucket-replica-1234567890"
acl = "private"
versioning_enable = true
replica_role = "qcs::cam::uin/100000000001:uin/100000000001"
replica_rules {
id = "test-rep1"
status = "Enabled"
prefix = "dist"
destination_bucket = "qcs::cos:%s::${tencentcloud_cos_bucket.replica1.bucket}"
}
}
```

Setting log status

```hcl
Expand Down Expand Up @@ -385,6 +407,47 @@ func resourceTencentCloudCosBucket() *schema.Resource {
Default: false,
Description: "Enable bucket versioning.",
},
"replica_role": {
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"replica_rules", "versioning_enable"},
Description: "Request initiator identifier, format: `qcs::cam::uin/<owneruin>:uin/<subuin>`. NOTE: only `versioning_enable` is true can configure this argument.",
},
"replica_rules": {
Type: schema.TypeList,
Optional: true,
Description: "List of replica rule. NOTE: only `versioning_enable` is true and `replica_role` set can configure this argument.",
RequiredWith: []string{"replica_role", "versioning_enable"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Description: "Name of a specific rule.",
},
"status": {
Type: schema.TypeString,
Required: true,
Description: "Status identifier, available values: `Enabled`, `Disabled`.",
},
"prefix": {
Type: schema.TypeString,
Optional: true,
Description: "Prefix matching policy. Policies cannot overlap; otherwise, an error will be returned. To match the root directory, leave this parameter empty.",
},
"destination_bucket": {
Type: schema.TypeString,
Required: true,
Description: "Destination bucket identifier, format: `qcs::cos:<region>::<bucketname-appid>`. NOTE: destination bucket must enable versioning.",
},
"destination_storage_class": {
Type: schema.TypeString,
Optional: true,
Description: "Storage class of destination, available values: `STANDARD`, `INTELLIGENT_TIERING`, `STANDARD_IA`. default is following current class of destination.",
},
},
},
},
"cors_rules": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -569,18 +632,18 @@ func resourceTencentCloudCosBucket() *schema.Resource {
Computed: true,
Description: "The prefix log name which saves the access log of this bucket per 5 minutes. Eg. `MyLogPrefix/`. The log access file format is `log_target_bucket`/`log_prefix`{YYYY}/{MM}/{DD}/{time}_{random}_{index}.gz. Only valid when `log_enable` is `true`.",
},
//computed
"cos_bucket_url": {
Type: schema.TypeString,
Computed: true,
Description: "The URL of this cos bucket.",
},
"multi_az": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: "Indicates whether to create a bucket of multi available zone.",
},
//computed
"cos_bucket_url": {
Type: schema.TypeString,
Computed: true,
Description: "The URL of this cos bucket.",
},
},
}
}
Expand All @@ -595,6 +658,17 @@ func resourceTencentCloudCosBucketCreate(d *schema.ResourceData, meta interface{

bucket := d.Get("bucket").(string)
acl := d.Get("acl").(string)
role, roleOk := d.GetOk("replica_role")
rule, ruleOk := d.GetOk("replica_rules")
versioning, versioningOk := d.GetOk("versioning_enable")

if v := versioning.(bool); !versioningOk || !v {
if roleOk || role.(string) != "" {
return fmt.Errorf("cannot configure role unless versioning enable")
} else if ruleOk || len(rule.([]interface{})) > 0 {
return fmt.Errorf("cannot configure replica rule unless versioning enable")
}
}

cosService := CosService{client: meta.(*TencentCloudClient).apiV3Conn}

Expand Down Expand Up @@ -713,6 +787,18 @@ func resourceTencentCloudCosBucketRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("setting versioning_enable error: %v", err)
}

replicaResult, err := cosService.GetBucketReplication(ctx, bucket)
if err != nil {
return err
}

if replicaResult != nil {
err := setBucketReplication(d, *replicaResult)
if err != nil {
return err
}
}

//read the log
logEnable, logTargetBucket, logPrefix, err := cosService.GetBucketLogStatus(ctx, bucket)
if err != nil {
Expand Down Expand Up @@ -823,6 +909,14 @@ func resourceTencentCloudCosBucketUpdate(d *schema.ResourceData, meta interface{
d.SetPartial("versioning_enable")
}

if d.HasChange("replica_role") || d.HasChange("replica_rules") {
err := resourceTencentCloudCosBucketReplicaUpdate(ctx, cosService, d)

if err != nil {
return err
}
}

if d.HasChange("tags") {
bucket := d.Id()

Expand Down Expand Up @@ -949,6 +1043,40 @@ func resourceTencentCloudCosBucketVersioningUpdate(ctx context.Context, client *
return nil
}

func resourceTencentCloudCosBucketReplicaUpdate(ctx context.Context, service CosService, d *schema.ResourceData) error {
bucket := d.Get("bucket").(string)
oldRole, newRole := d.GetChange("replica_role")
oldRules, newRules := d.GetChange("replica_rules")
oldRuleLength := len(oldRules.([]interface{}))
newRuleLength := len(newRules.([]interface{}))

// check if remove
if oldRole.(string) != "" && newRole.(string) == "" || oldRuleLength > 0 && newRuleLength == 0 {
result, err := service.GetBucketReplication(ctx, bucket)
if err != nil {
return err
}

if result != nil {
err := service.DeleteBucketReplication(ctx, d.Get("bucket").(string))
if err != nil {
return err
}
}
} else if newRole.(string) != "" || newRuleLength > 0 {
role, rules, _ := getBucketReplications(d)
err := service.PutBucketReplication(ctx, d.Get("bucket").(string), role, rules)
if err != nil {
return err
}
}

d.SetPartial("replica_role")
d.SetPartial("replica_rules")

return nil
}

func resourceTencentCloudCosBucketAclUpdate(ctx context.Context, client *s3.S3, d *schema.ResourceData) error {
logId := getLogId(ctx)

Expand Down Expand Up @@ -1471,3 +1599,54 @@ func transitionHash(v interface{}) int {
}
return hashcode.String(buf.String())
}

func getBucketReplications(d *schema.ResourceData) (role string, rules []cos.BucketReplicationRule, err error) {
role = d.Get("replica_role").(string)
replicaRules := d.Get("replica_rules").([]interface{})
for i := range replicaRules {
item := replicaRules[i].(map[string]interface{})
rule := cos.BucketReplicationRule{
Status: item["status"].(string),
Destination: &cos.ReplicationDestination{
Bucket: item["destination_bucket"].(string),
},
}
if v, ok := item["prefix"].(string); ok {
rule.Prefix = v
}
if v, ok := item["id"].(string); ok {
rule.ID = v
}
if v, ok := item["destination_storage_class"].(string); ok {
rule.Destination.StorageClass = v
}
rules = append(rules, rule)
}
return
}

func setBucketReplication(d *schema.ResourceData, result cos.GetBucketReplicationResult) (err error) {
if result.Role != "" {
err = d.Set("replica_role", result.Role)
}
rules := make([]map[string]interface{}, 0)
if len(result.Rule) > 0 {
for i := range result.Rule {
item := result.Rule[i]
rule := map[string]interface{}{
"status": item.Status,
"destination_bucket": item.Destination.Bucket,
"destination_storage_class": item.Destination.StorageClass,
}
if item.ID != "" {
rule["id"] = item.ID
}
if item.Prefix != "" {
rule["prefix"] = item.Prefix
}
rules = append(rules, rule)
}
}
err = d.Set("replica_rules", rules)
return
}
Loading