|
| 1 | +/* |
| 2 | +Provides a resource to create a gaap global domain |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_gaap_global_domain" "global_domain" { |
| 8 | + project_id = 0 |
| 9 | + default_value = "xxxxxx.com" |
| 10 | + alias = "demo" |
| 11 | + tags={ |
| 12 | + key = "value" |
| 13 | + } |
| 14 | +} |
| 15 | +``` |
| 16 | +
|
| 17 | +Import |
| 18 | +
|
| 19 | +gaap global_domain can be imported using the id, e.g. |
| 20 | +
|
| 21 | +``` |
| 22 | +terraform import tencentcloud_gaap_global_domain.global_domain ${projectId}#${domainId} |
| 23 | +``` |
| 24 | +*/ |
| 25 | +package tencentcloud |
| 26 | + |
| 27 | +import ( |
| 28 | + "context" |
| 29 | + "fmt" |
| 30 | + "log" |
| 31 | + "strconv" |
| 32 | + "strings" |
| 33 | + "time" |
| 34 | + |
| 35 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 36 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 37 | + gaap "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap/v20180529" |
| 38 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 39 | +) |
| 40 | + |
| 41 | +func resourceTencentCloudGaapGlobalDomain() *schema.Resource { |
| 42 | + return &schema.Resource{ |
| 43 | + Create: resourceTencentCloudGaapGlobalDomainCreate, |
| 44 | + Read: resourceTencentCloudGaapGlobalDomainRead, |
| 45 | + Update: resourceTencentCloudGaapGlobalDomainUpdate, |
| 46 | + Delete: resourceTencentCloudGaapGlobalDomainDelete, |
| 47 | + Importer: &schema.ResourceImporter{ |
| 48 | + State: schema.ImportStatePassthrough, |
| 49 | + }, |
| 50 | + Schema: map[string]*schema.Schema{ |
| 51 | + "project_id": { |
| 52 | + Required: true, |
| 53 | + Type: schema.TypeInt, |
| 54 | + Description: "Domain Name Project ID.", |
| 55 | + }, |
| 56 | + |
| 57 | + "default_value": { |
| 58 | + Required: true, |
| 59 | + Type: schema.TypeString, |
| 60 | + Description: "Domain name default entry.", |
| 61 | + }, |
| 62 | + |
| 63 | + "alias": { |
| 64 | + Optional: true, |
| 65 | + Type: schema.TypeString, |
| 66 | + Description: "alias.", |
| 67 | + }, |
| 68 | + |
| 69 | + "tags": { |
| 70 | + Type: schema.TypeMap, |
| 71 | + Optional: true, |
| 72 | + Description: "Instance tags.", |
| 73 | + }, |
| 74 | + }, |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func resourceTencentCloudGaapGlobalDomainCreate(d *schema.ResourceData, meta interface{}) error { |
| 79 | + defer logElapsed("resource.tencentcloud_gaap_global_domain.create")() |
| 80 | + defer inconsistentCheck(d, meta)() |
| 81 | + |
| 82 | + logId := getLogId(contextNil) |
| 83 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 84 | + |
| 85 | + var ( |
| 86 | + request = gaap.NewCreateGlobalDomainRequest() |
| 87 | + response = gaap.NewCreateGlobalDomainResponse() |
| 88 | + domainId string |
| 89 | + projectId int |
| 90 | + ) |
| 91 | + if v, ok := d.GetOkExists("project_id"); ok { |
| 92 | + projectId = v.(int) |
| 93 | + request.ProjectId = helper.IntInt64(projectId) |
| 94 | + } |
| 95 | + |
| 96 | + if v, ok := d.GetOk("default_value"); ok { |
| 97 | + request.DefaultValue = helper.String(v.(string)) |
| 98 | + } |
| 99 | + |
| 100 | + if v, ok := d.GetOk("alias"); ok { |
| 101 | + request.Alias = helper.String(v.(string)) |
| 102 | + } |
| 103 | + |
| 104 | + if v, ok := d.GetOk("tag_set"); ok { |
| 105 | + for _, item := range v.([]interface{}) { |
| 106 | + dMap := item.(map[string]interface{}) |
| 107 | + tagPair := gaap.TagPair{} |
| 108 | + if v, ok := dMap["tag_key"]; ok { |
| 109 | + tagPair.TagKey = helper.String(v.(string)) |
| 110 | + } |
| 111 | + if v, ok := dMap["tag_value"]; ok { |
| 112 | + tagPair.TagValue = helper.String(v.(string)) |
| 113 | + } |
| 114 | + request.TagSet = append(request.TagSet, &tagPair) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 119 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseGaapClient().CreateGlobalDomain(request) |
| 120 | + if e != nil { |
| 121 | + return retryError(e) |
| 122 | + } else { |
| 123 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 124 | + } |
| 125 | + response = result |
| 126 | + return nil |
| 127 | + }) |
| 128 | + if err != nil { |
| 129 | + log.Printf("[CRITAL]%s create gaap globalDomain failed, reason:%+v", logId, err) |
| 130 | + return err |
| 131 | + } |
| 132 | + |
| 133 | + domainId = *response.Response.DomainId |
| 134 | + |
| 135 | + d.SetId(strconv.Itoa(projectId) + FILED_SP + domainId) |
| 136 | + |
| 137 | + if tags := helper.GetTags(d, "tags"); len(tags) > 0 { |
| 138 | + tcClient := meta.(*TencentCloudClient).apiV3Conn |
| 139 | + tagService := &TagService{client: tcClient} |
| 140 | + resourceName := BuildTagResourceName("gaap", "domain", tcClient.Region, d.Id()) |
| 141 | + if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return resourceTencentCloudGaapGlobalDomainRead(d, meta) |
| 147 | +} |
| 148 | + |
| 149 | +func resourceTencentCloudGaapGlobalDomainRead(d *schema.ResourceData, meta interface{}) error { |
| 150 | + defer logElapsed("resource.tencentcloud_gaap_global_domain.read")() |
| 151 | + defer inconsistentCheck(d, meta)() |
| 152 | + |
| 153 | + logId := getLogId(contextNil) |
| 154 | + |
| 155 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 156 | + |
| 157 | + service := GaapService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 158 | + |
| 159 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 160 | + if len(idSplit) != 2 { |
| 161 | + return fmt.Errorf("id is broken,%s", idSplit) |
| 162 | + } |
| 163 | + |
| 164 | + projectId, err := strconv.Atoi(idSplit[0]) |
| 165 | + if err != nil { |
| 166 | + return err |
| 167 | + } |
| 168 | + domainId := idSplit[1] |
| 169 | + |
| 170 | + globalDomain, err := service.DescribeGaapGlobalDomainById(ctx, domainId, projectId) |
| 171 | + if err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + |
| 175 | + if globalDomain == nil { |
| 176 | + d.SetId("") |
| 177 | + log.Printf("[WARN]%s resource `GaapGlobalDomain` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 178 | + return nil |
| 179 | + } |
| 180 | + |
| 181 | + if globalDomain.ProjectId != nil { |
| 182 | + _ = d.Set("project_id", globalDomain.ProjectId) |
| 183 | + } |
| 184 | + |
| 185 | + if globalDomain.DefaultValue != nil { |
| 186 | + _ = d.Set("default_value", globalDomain.DefaultValue) |
| 187 | + } |
| 188 | + |
| 189 | + if globalDomain.Alias != nil { |
| 190 | + _ = d.Set("alias", globalDomain.Alias) |
| 191 | + } |
| 192 | + |
| 193 | + tcClient := meta.(*TencentCloudClient).apiV3Conn |
| 194 | + tagService := &TagService{client: tcClient} |
| 195 | + tags, err := tagService.DescribeResourceTags(ctx, "gaap", "domain", tcClient.Region, d.Id()) |
| 196 | + if err != nil { |
| 197 | + return err |
| 198 | + } |
| 199 | + |
| 200 | + _ = d.Set("tags", tags) |
| 201 | + |
| 202 | + return nil |
| 203 | +} |
| 204 | + |
| 205 | +func resourceTencentCloudGaapGlobalDomainUpdate(d *schema.ResourceData, meta interface{}) error { |
| 206 | + defer logElapsed("resource.tencentcloud_gaap_global_domain.update")() |
| 207 | + defer inconsistentCheck(d, meta)() |
| 208 | + |
| 209 | + logId := getLogId(contextNil) |
| 210 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 211 | + |
| 212 | + request := gaap.NewModifyGlobalDomainAttributeRequest() |
| 213 | + |
| 214 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 215 | + if len(idSplit) != 2 { |
| 216 | + return fmt.Errorf("id is broken,%s", idSplit) |
| 217 | + } |
| 218 | + |
| 219 | + projectId, err := strconv.Atoi(idSplit[0]) |
| 220 | + if err != nil { |
| 221 | + return err |
| 222 | + } |
| 223 | + domainId := idSplit[1] |
| 224 | + |
| 225 | + request.DomainId = &domainId |
| 226 | + request.ProjectId = helper.IntUint64(projectId) |
| 227 | + immutableArgs := []string{"project_id"} |
| 228 | + |
| 229 | + for _, v := range immutableArgs { |
| 230 | + if d.HasChange(v) { |
| 231 | + return fmt.Errorf("argument `%s` cannot be changed", v) |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + if d.HasChange("default_value") { |
| 236 | + if v, ok := d.GetOk("default_value"); ok { |
| 237 | + request.DefaultValue = helper.String(v.(string)) |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + if d.HasChange("alias") { |
| 242 | + if v, ok := d.GetOk("alias"); ok { |
| 243 | + request.Alias = helper.String(v.(string)) |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + err = resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 248 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseGaapClient().ModifyGlobalDomainAttribute(request) |
| 249 | + if e != nil { |
| 250 | + return retryError(e) |
| 251 | + } else { |
| 252 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 253 | + } |
| 254 | + return nil |
| 255 | + }) |
| 256 | + if err != nil { |
| 257 | + log.Printf("[CRITAL]%s update gaap globalDomain failed, reason:%+v", logId, err) |
| 258 | + return err |
| 259 | + } |
| 260 | + |
| 261 | + if d.HasChange("tags") { |
| 262 | + |
| 263 | + oldValue, newValue := d.GetChange("tags") |
| 264 | + replaceTags, deleteTags := diffTags(oldValue.(map[string]interface{}), newValue.(map[string]interface{})) |
| 265 | + |
| 266 | + tcClient := meta.(*TencentCloudClient).apiV3Conn |
| 267 | + tagService := &TagService{client: tcClient} |
| 268 | + resourceName := BuildTagResourceName("gaap", "domain", tcClient.Region, d.Id()) |
| 269 | + err := tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags) |
| 270 | + if err != nil { |
| 271 | + return err |
| 272 | + } |
| 273 | + |
| 274 | + } |
| 275 | + |
| 276 | + return resourceTencentCloudGaapGlobalDomainRead(d, meta) |
| 277 | +} |
| 278 | + |
| 279 | +func resourceTencentCloudGaapGlobalDomainDelete(d *schema.ResourceData, meta interface{}) error { |
| 280 | + defer logElapsed("resource.tencentcloud_gaap_global_domain.delete")() |
| 281 | + defer inconsistentCheck(d, meta)() |
| 282 | + |
| 283 | + logId := getLogId(contextNil) |
| 284 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 285 | + |
| 286 | + service := GaapService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 287 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 288 | + if len(idSplit) != 2 { |
| 289 | + return fmt.Errorf("id is broken,%s", idSplit) |
| 290 | + } |
| 291 | + projectId, err := strconv.Atoi(idSplit[0]) |
| 292 | + if err != nil { |
| 293 | + return err |
| 294 | + } |
| 295 | + domainId := idSplit[1] |
| 296 | + |
| 297 | + if err := service.DisableGlobalDomain(ctx, domainId); err != nil { |
| 298 | + return err |
| 299 | + } |
| 300 | + |
| 301 | + conf := BuildStateChangeConf([]string{}, []string{"1"}, 1*readRetryTimeout, time.Second, service.DomainInstanceStateRefreshFunc(domainId, projectId, []string{})) |
| 302 | + if _, e := conf.WaitForState(); e != nil { |
| 303 | + return e |
| 304 | + } |
| 305 | + if err := service.DeleteGaapGlobalDomainById(ctx, domainId); err != nil { |
| 306 | + return err |
| 307 | + } |
| 308 | + |
| 309 | + return nil |
| 310 | +} |
0 commit comments