|
| 1 | +/* |
| 2 | +Provides a COS resource to create a COS bucket policy and set its attributes. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_cos_bucket_policy" "cos_policy" { |
| 8 | + bucket = "mycos-1258798060" |
| 9 | +
|
| 10 | + policy = <<EOF |
| 11 | +{ |
| 12 | + "version": "2.0", |
| 13 | + "Statement": [ |
| 14 | + { |
| 15 | + "Principal": { |
| 16 | + "qcs": [ |
| 17 | + "qcs::cam::uin/<your-account-id>:uin/<your-account-id>" |
| 18 | + ] |
| 19 | + }, |
| 20 | + "Action": [ |
| 21 | + "name/cos:DeleteBucket", |
| 22 | + "name/cos:PutBucketACL" |
| 23 | + ], |
| 24 | + "Effect": "allow", |
| 25 | + "Resource": [ |
| 26 | + "qcs::cos:<bucket region>:uid/<your-account-id>:<bucket name>/*" |
| 27 | + ] |
| 28 | + } |
| 29 | + ] |
| 30 | +} |
| 31 | +EOF |
| 32 | +} |
| 33 | +``` |
| 34 | +
|
| 35 | +Import |
| 36 | +
|
| 37 | +COS bucket policy can be imported, e.g. |
| 38 | +
|
| 39 | +``` |
| 40 | +$ terraform import tencentcloud_cos_bucket_policy.bucket bucket-name |
| 41 | +``` |
| 42 | +*/ |
| 43 | +package tencentcloud |
| 44 | + |
| 45 | +import ( |
| 46 | + "context" |
| 47 | + "encoding/json" |
| 48 | + "log" |
| 49 | + "reflect" |
| 50 | + "time" |
| 51 | + |
| 52 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 53 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 54 | +) |
| 55 | + |
| 56 | +func resourceTencentCloudCosBucketPolicy() *schema.Resource { |
| 57 | + return &schema.Resource{ |
| 58 | + Create: resourceTencentCloudCosBucketPolicyCreate, |
| 59 | + Read: resourceTencentCloudCosBucketPolicyRead, |
| 60 | + Update: resourceTencentCloudCosBucketPolicyUpdate, |
| 61 | + Delete: resourceTencentCloudCosBucketPolicyDelete, |
| 62 | + Importer: &schema.ResourceImporter{ |
| 63 | + State: schema.ImportStatePassthrough, |
| 64 | + }, |
| 65 | + |
| 66 | + Schema: map[string]*schema.Schema{ |
| 67 | + "bucket": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Required: true, |
| 70 | + ForceNew: true, |
| 71 | + ValidateFunc: validateCosBucketName, |
| 72 | + Description: "The name of a bucket to be created. Bucket format should be [custom name]-[appid], for example `mycos-1258798060`.", |
| 73 | + }, |
| 74 | + "policy": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Required: true, |
| 77 | + DiffSuppressFunc: func(k, olds, news string, d *schema.ResourceData) bool { |
| 78 | + var oldJson interface{} |
| 79 | + err := json.Unmarshal([]byte(olds), &oldJson) |
| 80 | + if err != nil { |
| 81 | + return olds == news |
| 82 | + } |
| 83 | + var newJson interface{} |
| 84 | + err = json.Unmarshal([]byte(news), &newJson) |
| 85 | + if err != nil { |
| 86 | + return olds == news |
| 87 | + } |
| 88 | + flag := reflect.DeepEqual(oldJson, newJson) |
| 89 | + return flag |
| 90 | + }, |
| 91 | + Description: "The text of the policy. this field is required. the syntax refers to https://cloud.tencent.com/document/product/436/18023.", |
| 92 | + }, |
| 93 | + }, |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func resourceTencentCloudCosBucketPolicyCreate(d *schema.ResourceData, meta interface{}) error { |
| 98 | + defer logElapsed("resource.tencentcloud_cos_bucket_policy.create")() |
| 99 | + |
| 100 | + logId := getLogId(contextNil) |
| 101 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 102 | + bucket := d.Get("bucket").(string) |
| 103 | + policy := d.Get("policy").(string) |
| 104 | + |
| 105 | + cosService := CosService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 106 | + |
| 107 | + err := cosService.PutBucketPolicy(ctx, bucket, policy) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + d.SetId(bucket) |
| 112 | + |
| 113 | + return resourceTencentCloudCosBucketPolicyRead(d, meta) |
| 114 | +} |
| 115 | + |
| 116 | +func resourceTencentCloudCosBucketPolicyRead(d *schema.ResourceData, meta interface{}) error { |
| 117 | + defer logElapsed("resource.tencentcloud_cos_bucket_policy.read")() |
| 118 | + |
| 119 | + logId := getLogId(contextNil) |
| 120 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 121 | + |
| 122 | + bucket := d.Id() |
| 123 | + cosService := CosService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 124 | + |
| 125 | + var result string |
| 126 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 127 | + policy, e := cosService.DescribePolicyByBucket(ctx, bucket) |
| 128 | + if e != nil { |
| 129 | + return retryError(e) |
| 130 | + } |
| 131 | + result = policy |
| 132 | + return nil |
| 133 | + }) |
| 134 | + if err != nil { |
| 135 | + log.Printf("[CRITAL]%s read cos bucket policy failed, reason:%s\n", logId, err.Error()) |
| 136 | + return err |
| 137 | + } |
| 138 | + result, err = removeSid(result) |
| 139 | + if err != nil { |
| 140 | + log.Printf("[CRITAL]%s read cos bucket policy failed, reason:%s\n", logId, err.Error()) |
| 141 | + return err |
| 142 | + } |
| 143 | + if result == "" { |
| 144 | + d.SetId("") |
| 145 | + return nil |
| 146 | + } |
| 147 | + _ = d.Set("policy", result) |
| 148 | + _ = d.Set("bucket", bucket) |
| 149 | + |
| 150 | + return nil |
| 151 | +} |
| 152 | + |
| 153 | +func resourceTencentCloudCosBucketPolicyUpdate(d *schema.ResourceData, meta interface{}) error { |
| 154 | + defer logElapsed("resource.tencentcloud_cos_bucket_policy.update")() |
| 155 | + |
| 156 | + logId := getLogId(contextNil) |
| 157 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 158 | + cosService := CosService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 159 | + bucket := d.Id() |
| 160 | + |
| 161 | + if d.HasChange("policy") { |
| 162 | + policy := d.Get("policy").(string) |
| 163 | + err := cosService.PutBucketPolicy(ctx, bucket, policy) |
| 164 | + if err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // wait for update cache |
| 170 | + // if not, the data may be outdated. |
| 171 | + time.Sleep(3 * time.Second) |
| 172 | + |
| 173 | + return resourceTencentCloudCosBucketPolicyRead(d, meta) |
| 174 | +} |
| 175 | + |
| 176 | +func resourceTencentCloudCosBucketPolicyDelete(d *schema.ResourceData, meta interface{}) error { |
| 177 | + defer logElapsed("resource.tencentcloud_cos_bucket_policy.delete")() |
| 178 | + |
| 179 | + logId := getLogId(contextNil) |
| 180 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 181 | + |
| 182 | + bucket := d.Id() |
| 183 | + cosService := CosService{ |
| 184 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 185 | + } |
| 186 | + err := cosService.DeleteBucketPolicy(ctx, bucket) |
| 187 | + if err != nil { |
| 188 | + return err |
| 189 | + } |
| 190 | + |
| 191 | + // wait for update cache |
| 192 | + // if not, head bucket may be successful |
| 193 | + time.Sleep(3 * time.Second) |
| 194 | + |
| 195 | + return nil |
| 196 | +} |
| 197 | + |
| 198 | +//In the returned JSON, the SDK automatically adds the Sid, which needs to be removed |
| 199 | +func removeSid(v string) (result string, err error) { |
| 200 | + m := make(map[string]interface{}) |
| 201 | + err = json.Unmarshal([]byte(v), &m) |
| 202 | + if err != nil { |
| 203 | + return |
| 204 | + } |
| 205 | + var stateMend []interface{} |
| 206 | + if v, ok := m["Statement"]; ok { |
| 207 | + stateMend = v.([]interface{}) |
| 208 | + } else if v, ok := m["statement"]; ok { |
| 209 | + stateMend = v.([]interface{}) |
| 210 | + } |
| 211 | + for index, v := range stateMend { |
| 212 | + mp := v.(map[string]interface{}) |
| 213 | + delete(mp, "Sid") |
| 214 | + stateMend[index] = mp |
| 215 | + } |
| 216 | + if _, ok := m["Statement"]; ok { |
| 217 | + m["Statement"] = stateMend |
| 218 | + } else if _, ok := m["statement"]; ok { |
| 219 | + m["statement"] = stateMend |
| 220 | + } |
| 221 | + s, err := json.Marshal(m) |
| 222 | + return string(s), err |
| 223 | +} |
0 commit comments