|
| 1 | +/* |
| 2 | +Provides a resource to create a tcr tag_retention_rule |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_tcr_namespace" "my_ns" { |
| 8 | + instance_id = local.tcr_id |
| 9 | + name = "tf_test_ns_retention" |
| 10 | + is_public = true |
| 11 | + is_auto_scan = true |
| 12 | + is_prevent_vul = true |
| 13 | + severity = "medium" |
| 14 | + cve_whitelist_items { |
| 15 | + cve_id = "cve-xxxxx" |
| 16 | + } |
| 17 | +} |
| 18 | +
|
| 19 | +resource "tencentcloud_tcr_tag_retention_rule" "my_rule" { |
| 20 | + registry_id = local.tcr_id |
| 21 | + namespace_name = tencentcloud_tcr_namespace.my_ns.name |
| 22 | + retention_rule { |
| 23 | + key = "nDaysSinceLastPush" |
| 24 | + value = 2 |
| 25 | + } |
| 26 | + cron_setting = "daily" |
| 27 | + disabled = true |
| 28 | +} |
| 29 | +``` |
| 30 | +
|
| 31 | +*/ |
| 32 | +package tencentcloud |
| 33 | + |
| 34 | +import ( |
| 35 | + "context" |
| 36 | + "fmt" |
| 37 | + "log" |
| 38 | + "strings" |
| 39 | + |
| 40 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 41 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 42 | + tcr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr/v20190924" |
| 43 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 44 | +) |
| 45 | + |
| 46 | +func resourceTencentCloudTcrTagRetentionRule() *schema.Resource { |
| 47 | + return &schema.Resource{ |
| 48 | + Create: resourceTencentCloudTcrTagRetentionRuleCreate, |
| 49 | + Read: resourceTencentCloudTcrTagRetentionRuleRead, |
| 50 | + Update: resourceTencentCloudTcrTagRetentionRuleUpdate, |
| 51 | + Delete: resourceTencentCloudTcrTagRetentionRuleDelete, |
| 52 | + Importer: &schema.ResourceImporter{ |
| 53 | + State: schema.ImportStatePassthrough, |
| 54 | + }, |
| 55 | + Schema: map[string]*schema.Schema{ |
| 56 | + "registry_id": { |
| 57 | + Required: true, |
| 58 | + Type: schema.TypeString, |
| 59 | + Description: "The main instance ID.", |
| 60 | + }, |
| 61 | + |
| 62 | + "namespace_name": { |
| 63 | + Required: true, |
| 64 | + Type: schema.TypeString, |
| 65 | + Description: "The Name of the namespace.", |
| 66 | + }, |
| 67 | + |
| 68 | + "retention_rule": { |
| 69 | + Required: true, |
| 70 | + Type: schema.TypeList, |
| 71 | + MaxItems: 1, |
| 72 | + Description: "Retention Policy.", |
| 73 | + Elem: &schema.Resource{ |
| 74 | + Schema: map[string]*schema.Schema{ |
| 75 | + "key": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Required: true, |
| 78 | + Description: "The supported policies are latestPushedK (retain the latest `k` pushed versions) and nDaysSinceLastPush (retain pushed versions within the last `n` days).", |
| 79 | + }, |
| 80 | + "value": { |
| 81 | + Type: schema.TypeInt, |
| 82 | + Required: true, |
| 83 | + Description: "corresponding values for rule settings.", |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + |
| 89 | + "cron_setting": { |
| 90 | + Required: true, |
| 91 | + Type: schema.TypeString, |
| 92 | + Description: "Execution cycle, currently only available selections are: manual; daily; weekly; monthly.", |
| 93 | + }, |
| 94 | + |
| 95 | + "disabled": { |
| 96 | + Optional: true, |
| 97 | + Type: schema.TypeBool, |
| 98 | + Description: "Whether to disable the rule, with the default value of false.", |
| 99 | + }, |
| 100 | + }, |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func resourceTencentCloudTcrTagRetentionRuleCreate(d *schema.ResourceData, meta interface{}) error { |
| 105 | + defer logElapsed("resource.tencentcloud_tcr_tag_retention_rule.create")() |
| 106 | + defer inconsistentCheck(d, meta)() |
| 107 | + |
| 108 | + var ( |
| 109 | + logId = getLogId(contextNil) |
| 110 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 111 | + request = tcr.NewCreateTagRetentionRuleRequest() |
| 112 | + registryId string |
| 113 | + namespaceName string |
| 114 | + tcrService = TCRService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 115 | + ) |
| 116 | + if v, ok := d.GetOk("registry_id"); ok { |
| 117 | + registryId = v.(string) |
| 118 | + request.RegistryId = helper.String(v.(string)) |
| 119 | + } |
| 120 | + |
| 121 | + if v, ok := d.GetOkExists("namespace_id"); ok { |
| 122 | + request.NamespaceId = helper.IntInt64(v.(int)) |
| 123 | + } |
| 124 | + |
| 125 | + if v, ok := d.GetOk("namespace_name"); ok { |
| 126 | + namespaceName = v.(string) |
| 127 | + namespace, has, err := tcrService.DescribeTCRNameSpaceById(ctx, registryId, namespaceName) |
| 128 | + if !has || namespace == nil { |
| 129 | + return fmt.Errorf("TCR namespace not found.") |
| 130 | + } |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + request.NamespaceId = namespace.NamespaceId |
| 135 | + } |
| 136 | + |
| 137 | + if dMap, ok := helper.InterfacesHeadMap(d, "retention_rule"); ok { |
| 138 | + retentionRule := tcr.RetentionRule{} |
| 139 | + if v, ok := dMap["key"]; ok { |
| 140 | + retentionRule.Key = helper.String(v.(string)) |
| 141 | + } |
| 142 | + if v, ok := dMap["value"]; ok { |
| 143 | + retentionRule.Value = helper.IntInt64(v.(int)) |
| 144 | + } |
| 145 | + request.RetentionRule = &retentionRule |
| 146 | + } |
| 147 | + |
| 148 | + if v, ok := d.GetOk("cron_setting"); ok { |
| 149 | + request.CronSetting = helper.String(v.(string)) |
| 150 | + } |
| 151 | + |
| 152 | + if v, ok := d.GetOkExists("disabled"); ok { |
| 153 | + request.Disabled = helper.Bool(v.(bool)) |
| 154 | + } |
| 155 | + |
| 156 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 157 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseTCRClient().CreateTagRetentionRule(request) |
| 158 | + if e != nil { |
| 159 | + return retryError(e) |
| 160 | + } else { |
| 161 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 162 | + } |
| 163 | + return nil |
| 164 | + }) |
| 165 | + if err != nil { |
| 166 | + log.Printf("[CRITAL]%s create tcr TagRetentionRule failed, reason:%+v", logId, err) |
| 167 | + return err |
| 168 | + } |
| 169 | + |
| 170 | + TagRetentionRule, err := tcrService.DescribeTcrTagRetentionRuleById(ctx, registryId, namespaceName, nil) |
| 171 | + if err != nil { |
| 172 | + return fmt.Errorf("Query retention rule by id failed, reason:[%s]", err.Error()) |
| 173 | + } |
| 174 | + |
| 175 | + if TagRetentionRule != nil { |
| 176 | + retentionId := helper.Int64ToStr(*TagRetentionRule.RetentionId) |
| 177 | + d.SetId(strings.Join([]string{registryId, namespaceName, retentionId}, FILED_SP)) |
| 178 | + } else { |
| 179 | + log.Printf("[CRITAL]%s TagRetentionRule is nil! Set unique id as empty.", logId) |
| 180 | + d.SetId("") |
| 181 | + } |
| 182 | + |
| 183 | + return resourceTencentCloudTcrTagRetentionRuleRead(d, meta) |
| 184 | +} |
| 185 | + |
| 186 | +func resourceTencentCloudTcrTagRetentionRuleRead(d *schema.ResourceData, meta interface{}) error { |
| 187 | + defer logElapsed("resource.tencentcloud_tcr_tag_retention_rule.read")() |
| 188 | + defer inconsistentCheck(d, meta)() |
| 189 | + |
| 190 | + logId := getLogId(contextNil) |
| 191 | + |
| 192 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 193 | + |
| 194 | + service := TCRService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 195 | + |
| 196 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 197 | + if len(idSplit) != 3 { |
| 198 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 199 | + } |
| 200 | + registryId := idSplit[0] |
| 201 | + namespaceName := idSplit[1] |
| 202 | + retentionId := idSplit[2] |
| 203 | + |
| 204 | + TagRetentionRule, err := service.DescribeTcrTagRetentionRuleById(ctx, registryId, namespaceName, &retentionId) |
| 205 | + if err != nil { |
| 206 | + return err |
| 207 | + } |
| 208 | + |
| 209 | + if TagRetentionRule == nil { |
| 210 | + d.SetId("") |
| 211 | + log.Printf("[WARN]%s resource `TcrTagRetentionRule` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 212 | + return nil |
| 213 | + } |
| 214 | + |
| 215 | + _ = d.Set("registry_id", registryId) |
| 216 | + |
| 217 | + if TagRetentionRule.NamespaceName != nil { |
| 218 | + _ = d.Set("namespace_name", TagRetentionRule.NamespaceName) |
| 219 | + } |
| 220 | + |
| 221 | + if len(TagRetentionRule.RetentionRuleList) > 0 { |
| 222 | + retentionRuleMap := map[string]interface{}{} |
| 223 | + retentionRule := TagRetentionRule.RetentionRuleList[0] |
| 224 | + |
| 225 | + if retentionRule.Key != nil { |
| 226 | + retentionRuleMap["key"] = retentionRule.Key |
| 227 | + } |
| 228 | + |
| 229 | + if retentionRule.Value != nil { |
| 230 | + retentionRuleMap["value"] = retentionRule.Value |
| 231 | + } |
| 232 | + |
| 233 | + _ = d.Set("retention_rule", []interface{}{retentionRuleMap}) |
| 234 | + } |
| 235 | + |
| 236 | + if TagRetentionRule.CronSetting != nil { |
| 237 | + _ = d.Set("cron_setting", TagRetentionRule.CronSetting) |
| 238 | + } |
| 239 | + |
| 240 | + if TagRetentionRule.Disabled != nil { |
| 241 | + _ = d.Set("disabled", TagRetentionRule.Disabled) |
| 242 | + } |
| 243 | + |
| 244 | + return nil |
| 245 | +} |
| 246 | + |
| 247 | +func resourceTencentCloudTcrTagRetentionRuleUpdate(d *schema.ResourceData, meta interface{}) error { |
| 248 | + defer logElapsed("resource.tencentcloud_tcr_tag_retention_rule.update")() |
| 249 | + defer inconsistentCheck(d, meta)() |
| 250 | + |
| 251 | + logId := getLogId(contextNil) |
| 252 | + |
| 253 | + request := tcr.NewModifyTagRetentionRuleRequest() |
| 254 | + tcrService := TCRService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 255 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 256 | + |
| 257 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 258 | + if len(idSplit) != 3 { |
| 259 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 260 | + } |
| 261 | + registryId := idSplit[0] |
| 262 | + namespaceName := idSplit[1] |
| 263 | + retentionId := idSplit[2] |
| 264 | + |
| 265 | + namespace, has, err := tcrService.DescribeTCRNameSpaceById(ctx, registryId, namespaceName) |
| 266 | + if !has || namespace == nil { |
| 267 | + return fmt.Errorf("TCR namespace not found.") |
| 268 | + } |
| 269 | + if err != nil { |
| 270 | + return err |
| 271 | + } |
| 272 | + |
| 273 | + request.RegistryId = ®istryId |
| 274 | + request.NamespaceId = namespace.NamespaceId |
| 275 | + request.RetentionId = helper.StrToInt64Point(retentionId) |
| 276 | + if v, ok := d.GetOkExists("cron_setting"); ok { |
| 277 | + request.CronSetting = helper.String(v.(string)) |
| 278 | + } |
| 279 | + |
| 280 | + immutableArgs := []string{"registry_id", "namespace_name", "cron_setting"} |
| 281 | + |
| 282 | + for _, v := range immutableArgs { |
| 283 | + if d.HasChange(v) { |
| 284 | + return fmt.Errorf("argument `%s` cannot be changed", v) |
| 285 | + } |
| 286 | + } |
| 287 | + |
| 288 | + if d.HasChange("retention_rule") { |
| 289 | + if dMap, ok := helper.InterfacesHeadMap(d, "retention_rule"); ok { |
| 290 | + retentionRule := tcr.RetentionRule{} |
| 291 | + if v, ok := dMap["key"]; ok { |
| 292 | + retentionRule.Key = helper.String(v.(string)) |
| 293 | + } |
| 294 | + if v, ok := dMap["value"]; ok { |
| 295 | + retentionRule.Value = helper.IntInt64(v.(int)) |
| 296 | + } |
| 297 | + request.RetentionRule = &retentionRule |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + if d.HasChange("disabled") { |
| 302 | + if v, ok := d.GetOkExists("disabled"); ok { |
| 303 | + request.Disabled = helper.Bool(v.(bool)) |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + err = resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 308 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseTCRClient().ModifyTagRetentionRule(request) |
| 309 | + if e != nil { |
| 310 | + return retryError(e) |
| 311 | + } else { |
| 312 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 313 | + } |
| 314 | + return nil |
| 315 | + }) |
| 316 | + |
| 317 | + if err != nil { |
| 318 | + log.Printf("[CRITAL]%s update tcr TagRetentionRule failed, reason:%+v", logId, err) |
| 319 | + return err |
| 320 | + } |
| 321 | + |
| 322 | + return resourceTencentCloudTcrTagRetentionRuleRead(d, meta) |
| 323 | +} |
| 324 | + |
| 325 | +func resourceTencentCloudTcrTagRetentionRuleDelete(d *schema.ResourceData, meta interface{}) error { |
| 326 | + defer logElapsed("resource.tencentcloud_tcr_tag_retention_rule.delete")() |
| 327 | + defer inconsistentCheck(d, meta)() |
| 328 | + |
| 329 | + logId := getLogId(contextNil) |
| 330 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 331 | + |
| 332 | + service := TCRService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 333 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 334 | + if len(idSplit) != 3 { |
| 335 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 336 | + } |
| 337 | + registryId := idSplit[0] |
| 338 | + retentionId := idSplit[2] |
| 339 | + |
| 340 | + if err := service.DeleteTcrTagRetentionRuleById(ctx, registryId, retentionId); err != nil { |
| 341 | + return err |
| 342 | + } |
| 343 | + |
| 344 | + return nil |
| 345 | +} |
0 commit comments