|
| 1 | +/* |
| 2 | +Provides a resource to create a tcr tag_retention_execution_config |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_tcr_namespace" "my_ns" { |
| 8 | + instance_id = tencentcloud_tcr_instance.mytcr_retention.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 = tencentcloud_tcr_instance.mytcr_retention.id |
| 21 | + namespace_name = tencentcloud_tcr_namespace.my_ns.name |
| 22 | + retention_rule { |
| 23 | + key = "nDaysSinceLastPush" |
| 24 | + value = 2 |
| 25 | + } |
| 26 | + cron_setting = "manual" |
| 27 | + disabled = true |
| 28 | +} |
| 29 | +
|
| 30 | +resource "tencentcloud_tcr_tag_retention_execution_config" "tag_retention_execution_config" { |
| 31 | + registry_id = tencentcloud_tcr_tag_retention_rule.my_rule.registry_id |
| 32 | + retention_id = tencentcloud_tcr_tag_retention_rule.my_rule.retention_id |
| 33 | + dry_run = false |
| 34 | +} |
| 35 | +``` |
| 36 | +*/ |
| 37 | +package tencentcloud |
| 38 | + |
| 39 | +import ( |
| 40 | + "context" |
| 41 | + "fmt" |
| 42 | + "log" |
| 43 | + "strings" |
| 44 | + "time" |
| 45 | + |
| 46 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 47 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 48 | + tcr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr/v20190924" |
| 49 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 50 | +) |
| 51 | + |
| 52 | +func resourceTencentCloudTcrTagRetentionExecutionConfig() *schema.Resource { |
| 53 | + return &schema.Resource{ |
| 54 | + Create: resourceTencentCloudTcrTagRetentionExecutionConfigCreate, |
| 55 | + Read: resourceTencentCloudTcrTagRetentionExecutionConfigRead, |
| 56 | + Update: resourceTencentCloudTcrTagRetentionExecutionConfigUpdate, |
| 57 | + Delete: resourceTencentCloudTcrTagRetentionExecutionConfigDelete, |
| 58 | + Schema: map[string]*schema.Schema{ |
| 59 | + "registry_id": { |
| 60 | + Required: true, |
| 61 | + Type: schema.TypeString, |
| 62 | + Description: "instance id.", |
| 63 | + }, |
| 64 | + |
| 65 | + "retention_id": { |
| 66 | + Required: true, |
| 67 | + Type: schema.TypeInt, |
| 68 | + Description: "retention id.", |
| 69 | + }, |
| 70 | + |
| 71 | + "execution_id": { |
| 72 | + Computed: true, |
| 73 | + Type: schema.TypeInt, |
| 74 | + Description: "execution id.", |
| 75 | + }, |
| 76 | + |
| 77 | + "dry_run": { |
| 78 | + Optional: true, |
| 79 | + Type: schema.TypeBool, |
| 80 | + Description: "Whether to simulate execution, the default value is false, that is, non-simulation execution.", |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func resourceTencentCloudTcrTagRetentionExecutionConfigCreate(d *schema.ResourceData, meta interface{}) error { |
| 87 | + defer logElapsed("resource.tencentcloud_tcr_tag_retention_execution_config.create")() |
| 88 | + defer inconsistentCheck(d, meta)() |
| 89 | + |
| 90 | + var ( |
| 91 | + registryId string |
| 92 | + retentionId string |
| 93 | + ) |
| 94 | + |
| 95 | + if v, ok := d.GetOk("registry_id"); ok { |
| 96 | + registryId = v.(string) |
| 97 | + } |
| 98 | + |
| 99 | + if v, ok := d.GetOk("retention_id"); ok { |
| 100 | + retentionId = helper.IntToStr(v.(int)) |
| 101 | + } |
| 102 | + |
| 103 | + d.SetId(strings.Join([]string{registryId, retentionId}, FILED_SP)) |
| 104 | + |
| 105 | + return resourceTencentCloudTcrTagRetentionExecutionConfigUpdate(d, meta) |
| 106 | +} |
| 107 | + |
| 108 | +func resourceTencentCloudTcrTagRetentionExecutionConfigRead(d *schema.ResourceData, meta interface{}) error { |
| 109 | + defer logElapsed("resource.tencentcloud_tcr_tag_retention_execution_config.read")() |
| 110 | + defer inconsistentCheck(d, meta)() |
| 111 | + |
| 112 | + logId := getLogId(contextNil) |
| 113 | + |
| 114 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 115 | + |
| 116 | + service := TCRService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 117 | + |
| 118 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 119 | + if len(idSplit) != 2 { |
| 120 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 121 | + } |
| 122 | + registryId := idSplit[0] |
| 123 | + retentionId := idSplit[1] |
| 124 | + |
| 125 | + TagRetentionExecutionConfig, err := service.DescribeTcrTagRetentionExecutionConfigById(ctx, registryId, retentionId) |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + if TagRetentionExecutionConfig == nil { |
| 131 | + d.SetId("") |
| 132 | + log.Printf("[WARN]%s resource `TcrTagRetentionExecutionConfig` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 133 | + return nil |
| 134 | + } |
| 135 | + |
| 136 | + _ = d.Set("registry_id", registryId) |
| 137 | + |
| 138 | + if TagRetentionExecutionConfig.RetentionId != nil { |
| 139 | + _ = d.Set("retention_id", TagRetentionExecutionConfig.RetentionId) |
| 140 | + } |
| 141 | + |
| 142 | + if TagRetentionExecutionConfig.ExecutionId != nil { |
| 143 | + _ = d.Set("execution_id", TagRetentionExecutionConfig.ExecutionId) |
| 144 | + } |
| 145 | + |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func resourceTencentCloudTcrTagRetentionExecutionConfigUpdate(d *schema.ResourceData, meta interface{}) error { |
| 150 | + defer logElapsed("resource.tencentcloud_tcr_tag_retention_execution_config.update")() |
| 151 | + defer inconsistentCheck(d, meta)() |
| 152 | + |
| 153 | + logId := getLogId(contextNil) |
| 154 | + |
| 155 | + request := tcr.NewCreateTagRetentionExecutionRequest() |
| 156 | + |
| 157 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 158 | + if len(idSplit) != 2 { |
| 159 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 160 | + } |
| 161 | + registryId := idSplit[0] |
| 162 | + retentionId := idSplit[1] |
| 163 | + |
| 164 | + request.RegistryId = ®istryId |
| 165 | + request.RetentionId = helper.StrToInt64Point(retentionId) |
| 166 | + |
| 167 | + if d.HasChange("dry_run") { |
| 168 | + if v, ok := d.GetOkExists("dry_run"); ok { |
| 169 | + request.DryRun = helper.Bool(v.(bool)) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 174 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseTCRClient().CreateTagRetentionExecution(request) |
| 175 | + if e != nil { |
| 176 | + return retryError(e) |
| 177 | + } else { |
| 178 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 179 | + } |
| 180 | + return nil |
| 181 | + }) |
| 182 | + if err != nil { |
| 183 | + log.Printf("[CRITAL]%s update tcr TagRetentionExecutionConfig failed, reason:%+v", logId, err) |
| 184 | + return err |
| 185 | + } |
| 186 | + |
| 187 | + service := TCRService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 188 | + |
| 189 | + conf := BuildStateChangeConf([]string{}, []string{"Succeed"}, 3*readRetryTimeout, time.Second, service.TcrTagRetentionExecutionConfigStateRefreshFunc(registryId, retentionId, []string{})) |
| 190 | + |
| 191 | + if _, e := conf.WaitForState(); e != nil { |
| 192 | + return e |
| 193 | + } |
| 194 | + |
| 195 | + return resourceTencentCloudTcrTagRetentionExecutionConfigRead(d, meta) |
| 196 | +} |
| 197 | + |
| 198 | +func resourceTencentCloudTcrTagRetentionExecutionConfigDelete(d *schema.ResourceData, meta interface{}) error { |
| 199 | + defer logElapsed("resource.tencentcloud_tcr_tag_retention_execution_config.delete")() |
| 200 | + defer inconsistentCheck(d, meta)() |
| 201 | + |
| 202 | + return nil |
| 203 | +} |
0 commit comments