|
| 1 | +package teo |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + teov20220901 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo/v20220901" |
| 11 | + |
| 12 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 13 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 14 | +) |
| 15 | + |
| 16 | +func ResourceTencentCloudTeoContentIdentifier() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + Create: resourceTencentCloudTeoContentIdentifierCreate, |
| 19 | + Read: resourceTencentCloudTeoContentIdentifierRead, |
| 20 | + Update: resourceTencentCloudTeoContentIdentifierUpdate, |
| 21 | + Delete: resourceTencentCloudTeoContentIdentifierDelete, |
| 22 | + Importer: &schema.ResourceImporter{ |
| 23 | + State: schema.ImportStatePassthrough, |
| 24 | + }, |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "description": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + Description: "Description of the content identifier, length limit of up to 20 characters.", |
| 30 | + }, |
| 31 | + |
| 32 | + "plan_id": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Required: true, |
| 35 | + Description: "Target plan id to be bound, available only for the enterprise edition. <li>if there is already a plan under your account, go to [plan management](https://console.cloud.tencent.com/edgeone/package) to get the plan id and directly bind the content identifier to the plan;</li><li>if you do not have a plan to bind, please purchase an enterprise edition plan first.</li>.", |
| 36 | + }, |
| 37 | + |
| 38 | + "tags": { |
| 39 | + Type: schema.TypeList, |
| 40 | + Optional: true, |
| 41 | + Description: "Tags of the content identifier. this parameter is used for authority control. to create tags, go to the [tag console](https://console.cloud.tencent.com/tag/taglist).", |
| 42 | + Elem: &schema.Resource{ |
| 43 | + Schema: map[string]*schema.Schema{ |
| 44 | + "tag_key": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Required: true, |
| 47 | + Description: "The tag key.\nNote: This field may return null, indicating that no valid values can be obtained.", |
| 48 | + }, |
| 49 | + "tag_value": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Required: true, |
| 52 | + Description: "The tag value.\nNote: This field may return null, indicating that no valid values can be obtained.", |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + |
| 58 | + // computed |
| 59 | + "content_id": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Computed: true, |
| 62 | + Description: "Content identifier ID.", |
| 63 | + }, |
| 64 | + |
| 65 | + "created_on": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Computed: true, |
| 68 | + Description: "Creation time, which is in Coordinated Universal Time (UTC) and follows the ISO 8601 date and time format..", |
| 69 | + }, |
| 70 | + |
| 71 | + "modified_on": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Computed: true, |
| 74 | + Description: "The time of the latest update, in Coordinated Universal Time (UTC), following the ISO 8601 date and time format..", |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func resourceTencentCloudTeoContentIdentifierCreate(d *schema.ResourceData, meta interface{}) error { |
| 81 | + defer tccommon.LogElapsed("resource.tencentcloud_teo_content_identifier.create")() |
| 82 | + defer tccommon.InconsistentCheck(d, meta)() |
| 83 | + |
| 84 | + var ( |
| 85 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 86 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 87 | + request = teov20220901.NewCreateContentIdentifierRequest() |
| 88 | + response = teov20220901.NewCreateContentIdentifierResponse() |
| 89 | + ) |
| 90 | + |
| 91 | + if v, ok := d.GetOk("description"); ok { |
| 92 | + request.Description = helper.String(v.(string)) |
| 93 | + } |
| 94 | + |
| 95 | + if v, ok := d.GetOk("plan_id"); ok { |
| 96 | + request.PlanId = helper.String(v.(string)) |
| 97 | + } |
| 98 | + |
| 99 | + if v, ok := d.GetOk("tags"); ok { |
| 100 | + for _, item := range v.([]interface{}) { |
| 101 | + tagsMap := item.(map[string]interface{}) |
| 102 | + tag := teov20220901.Tag{} |
| 103 | + if v, ok := tagsMap["tag_key"].(string); ok && v != "" { |
| 104 | + tag.TagKey = helper.String(v) |
| 105 | + } |
| 106 | + |
| 107 | + if v, ok := tagsMap["tag_value"].(string); ok && v != "" { |
| 108 | + tag.TagValue = helper.String(v) |
| 109 | + } |
| 110 | + |
| 111 | + request.Tags = append(request.Tags, &tag) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 116 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().CreateContentIdentifierWithContext(ctx, request) |
| 117 | + if e != nil { |
| 118 | + return tccommon.RetryError(e) |
| 119 | + } else { |
| 120 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 121 | + } |
| 122 | + |
| 123 | + if result == nil || result.Response == nil { |
| 124 | + return resource.NonRetryableError(fmt.Errorf("Create teo content identifier failed, Response is nil.")) |
| 125 | + } |
| 126 | + |
| 127 | + response = result |
| 128 | + return nil |
| 129 | + }) |
| 130 | + |
| 131 | + if reqErr != nil { |
| 132 | + log.Printf("[CRITAL]%s create teo content identifier failed, reason:%+v", logId, reqErr) |
| 133 | + return reqErr |
| 134 | + } |
| 135 | + |
| 136 | + if response.Response.ContentId == nil { |
| 137 | + return fmt.Errorf("ContentId is nil.") |
| 138 | + } |
| 139 | + |
| 140 | + d.SetId(*response.Response.ContentId) |
| 141 | + return resourceTencentCloudTeoContentIdentifierRead(d, meta) |
| 142 | +} |
| 143 | + |
| 144 | +func resourceTencentCloudTeoContentIdentifierRead(d *schema.ResourceData, meta interface{}) error { |
| 145 | + defer tccommon.LogElapsed("resource.tencentcloud_teo_content_identifier.read")() |
| 146 | + defer tccommon.InconsistentCheck(d, meta)() |
| 147 | + |
| 148 | + var ( |
| 149 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 150 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 151 | + service = TeoService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 152 | + contentId = d.Id() |
| 153 | + ) |
| 154 | + |
| 155 | + respData, err := service.DescribeTeoContentIdentifierById(ctx, contentId) |
| 156 | + if err != nil { |
| 157 | + return err |
| 158 | + } |
| 159 | + |
| 160 | + if respData == nil { |
| 161 | + log.Printf("[WARN]%s resource `teo_content_identifier` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 162 | + d.SetId("") |
| 163 | + return nil |
| 164 | + } |
| 165 | + |
| 166 | + if respData.Description != nil { |
| 167 | + _ = d.Set("description", respData.Description) |
| 168 | + } |
| 169 | + |
| 170 | + if respData.PlanId != nil { |
| 171 | + _ = d.Set("plan_id", respData.PlanId) |
| 172 | + } |
| 173 | + |
| 174 | + if respData.Tags != nil { |
| 175 | + tagsList := make([]map[string]interface{}, 0, len(respData.Tags)) |
| 176 | + for _, tags := range respData.Tags { |
| 177 | + tagsMap := map[string]interface{}{} |
| 178 | + if tags.TagKey != nil { |
| 179 | + tagsMap["tag_key"] = tags.TagKey |
| 180 | + } |
| 181 | + |
| 182 | + if tags.TagValue != nil { |
| 183 | + tagsMap["tag_value"] = tags.TagValue |
| 184 | + } |
| 185 | + |
| 186 | + tagsList = append(tagsList, tagsMap) |
| 187 | + } |
| 188 | + |
| 189 | + _ = d.Set("tags", tagsList) |
| 190 | + } |
| 191 | + |
| 192 | + if respData.ContentId != nil { |
| 193 | + _ = d.Set("content_id", respData.ContentId) |
| 194 | + } |
| 195 | + |
| 196 | + if respData.CreatedOn != nil { |
| 197 | + _ = d.Set("created_on", respData.CreatedOn) |
| 198 | + } |
| 199 | + |
| 200 | + if respData.ModifiedOn != nil { |
| 201 | + _ = d.Set("modified_on", respData.ModifiedOn) |
| 202 | + } |
| 203 | + |
| 204 | + return nil |
| 205 | +} |
| 206 | + |
| 207 | +func resourceTencentCloudTeoContentIdentifierUpdate(d *schema.ResourceData, meta interface{}) error { |
| 208 | + defer tccommon.LogElapsed("resource.tencentcloud_teo_content_identifier.update")() |
| 209 | + defer tccommon.InconsistentCheck(d, meta)() |
| 210 | + |
| 211 | + var ( |
| 212 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 213 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 214 | + contentId = d.Id() |
| 215 | + ) |
| 216 | + |
| 217 | + immutableArgs := []string{"plan_id", "tags"} |
| 218 | + for _, v := range immutableArgs { |
| 219 | + if d.HasChange(v) { |
| 220 | + return fmt.Errorf("argument `%s` cannot be changed", v) |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + if d.HasChange("description") { |
| 225 | + request := teov20220901.NewModifyContentIdentifierRequest() |
| 226 | + if v, ok := d.GetOk("description"); ok { |
| 227 | + request.Description = helper.String(v.(string)) |
| 228 | + } |
| 229 | + |
| 230 | + request.ContentId = &contentId |
| 231 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 232 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().ModifyContentIdentifierWithContext(ctx, request) |
| 233 | + if e != nil { |
| 234 | + return tccommon.RetryError(e) |
| 235 | + } else { |
| 236 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 237 | + } |
| 238 | + |
| 239 | + return nil |
| 240 | + }) |
| 241 | + |
| 242 | + if reqErr != nil { |
| 243 | + log.Printf("[CRITAL]%s update teo content identifier failed, reason:%+v", logId, reqErr) |
| 244 | + return reqErr |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + return resourceTencentCloudTeoContentIdentifierRead(d, meta) |
| 249 | +} |
| 250 | + |
| 251 | +func resourceTencentCloudTeoContentIdentifierDelete(d *schema.ResourceData, meta interface{}) error { |
| 252 | + defer tccommon.LogElapsed("resource.tencentcloud_teo_content_identifier.delete")() |
| 253 | + defer tccommon.InconsistentCheck(d, meta)() |
| 254 | + |
| 255 | + var ( |
| 256 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 257 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 258 | + request = teov20220901.NewDeleteContentIdentifierRequest() |
| 259 | + contentId = d.Id() |
| 260 | + ) |
| 261 | + |
| 262 | + request.ContentId = &contentId |
| 263 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 264 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().DeleteContentIdentifierWithContext(ctx, request) |
| 265 | + if e != nil { |
| 266 | + return tccommon.RetryError(e) |
| 267 | + } else { |
| 268 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 269 | + } |
| 270 | + |
| 271 | + return nil |
| 272 | + }) |
| 273 | + |
| 274 | + if reqErr != nil { |
| 275 | + log.Printf("[CRITAL]%s delete teo content identifier failed, reason:%+v", logId, reqErr) |
| 276 | + return reqErr |
| 277 | + } |
| 278 | + |
| 279 | + return nil |
| 280 | +} |
0 commit comments