|
| 1 | +/* |
| 2 | +Provides a resource to create a dnspod custom_line |
| 3 | +
|
| 4 | +~> **NOTE:** Terraform uses the combined id of doamin and name when importing. When the name changes, the combined id will also change. |
| 5 | +
|
| 6 | +Example Usage |
| 7 | +
|
| 8 | +```hcl |
| 9 | +resource "tencentcloud_dnspod_custom_line" "custom_line" { |
| 10 | + domain = "dnspod.com" |
| 11 | + name = "testline8" |
| 12 | + area = "6.6.6.1-6.6.6.2" |
| 13 | +} |
| 14 | +``` |
| 15 | +
|
| 16 | +Import |
| 17 | +
|
| 18 | +dnspod custom_line can be imported using the id, e.g. |
| 19 | +
|
| 20 | +``` |
| 21 | +terraform import tencentcloud_dnspod_custom_line.custom_line domain#name |
| 22 | +``` |
| 23 | +*/ |
| 24 | +package tencentcloud |
| 25 | + |
| 26 | +import ( |
| 27 | + "context" |
| 28 | + "fmt" |
| 29 | + "log" |
| 30 | + "strings" |
| 31 | + |
| 32 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 33 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 34 | + dnspod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod/v20210323" |
| 35 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 36 | +) |
| 37 | + |
| 38 | +func resourceTencentCloudDnspodCustomLine() *schema.Resource { |
| 39 | + return &schema.Resource{ |
| 40 | + Create: resourceTencentCloudDnspodCustomLineCreate, |
| 41 | + Read: resourceTencentCloudDnspodCustomLineRead, |
| 42 | + Update: resourceTencentCloudDnspodCustomLineUpdate, |
| 43 | + Delete: resourceTencentCloudDnspodCustomLineDelete, |
| 44 | + Importer: &schema.ResourceImporter{ |
| 45 | + State: schema.ImportStatePassthrough, |
| 46 | + }, |
| 47 | + Schema: map[string]*schema.Schema{ |
| 48 | + "domain": { |
| 49 | + Required: true, |
| 50 | + ForceNew: true, |
| 51 | + Type: schema.TypeString, |
| 52 | + Description: "Domain.", |
| 53 | + }, |
| 54 | + |
| 55 | + "name": { |
| 56 | + Required: true, |
| 57 | + Type: schema.TypeString, |
| 58 | + Description: "The Name of custom line.", |
| 59 | + }, |
| 60 | + |
| 61 | + "area": { |
| 62 | + Required: true, |
| 63 | + Type: schema.TypeString, |
| 64 | + Description: "The IP segment of custom line, split with `-`.", |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func resourceTencentCloudDnspodCustomLineCreate(d *schema.ResourceData, meta interface{}) error { |
| 71 | + defer logElapsed("resource.tencentcloud_dnspod_custom_line.create")() |
| 72 | + defer inconsistentCheck(d, meta)() |
| 73 | + |
| 74 | + logId := getLogId(contextNil) |
| 75 | + |
| 76 | + var ( |
| 77 | + request = dnspod.NewCreateDomainCustomLineRequest() |
| 78 | + domain string |
| 79 | + name string |
| 80 | + ) |
| 81 | + if v, ok := d.GetOk("domain"); ok { |
| 82 | + domain = v.(string) |
| 83 | + request.Domain = helper.String(v.(string)) |
| 84 | + } |
| 85 | + |
| 86 | + if v, ok := d.GetOk("name"); ok { |
| 87 | + name = v.(string) |
| 88 | + request.Name = helper.String(v.(string)) |
| 89 | + } |
| 90 | + |
| 91 | + if v, ok := d.GetOk("area"); ok { |
| 92 | + request.Area = helper.String(v.(string)) |
| 93 | + } |
| 94 | + |
| 95 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 96 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseDnsPodClient().CreateDomainCustomLine(request) |
| 97 | + if e != nil { |
| 98 | + return retryError(e) |
| 99 | + } else { |
| 100 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 101 | + } |
| 102 | + return nil |
| 103 | + }) |
| 104 | + if err != nil { |
| 105 | + log.Printf("[CRITAL]%s create dnspod custom_line failed, reason:%+v", logId, err) |
| 106 | + return err |
| 107 | + } |
| 108 | + |
| 109 | + d.SetId(strings.Join([]string{domain, name}, FILED_SP)) |
| 110 | + |
| 111 | + return resourceTencentCloudDnspodCustomLineRead(d, meta) |
| 112 | +} |
| 113 | + |
| 114 | +func resourceTencentCloudDnspodCustomLineRead(d *schema.ResourceData, meta interface{}) error { |
| 115 | + defer logElapsed("resource.tencentcloud_dnspod_custom_line.read")() |
| 116 | + defer inconsistentCheck(d, meta)() |
| 117 | + |
| 118 | + logId := getLogId(contextNil) |
| 119 | + |
| 120 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 121 | + |
| 122 | + service := DnspodService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 123 | + |
| 124 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 125 | + if len(idSplit) != 2 { |
| 126 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 127 | + } |
| 128 | + domain := idSplit[0] |
| 129 | + name := idSplit[1] |
| 130 | + |
| 131 | + customLineInfo, err := service.DescribeDnspodCustomLineById(ctx, domain, name) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + if customLineInfo == nil { |
| 137 | + d.SetId("") |
| 138 | + log.Printf("[WARN]%s resource `DnspodCustom_line` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 139 | + return nil |
| 140 | + } |
| 141 | + |
| 142 | + _ = d.Set("domain", domain) |
| 143 | + _ = d.Set("name", name) |
| 144 | + |
| 145 | + if customLineInfo.Area != nil { |
| 146 | + _ = d.Set("area", customLineInfo.Area) |
| 147 | + } |
| 148 | + |
| 149 | + return nil |
| 150 | +} |
| 151 | + |
| 152 | +func resourceTencentCloudDnspodCustomLineUpdate(d *schema.ResourceData, meta interface{}) error { |
| 153 | + defer logElapsed("resource.tencentcloud_dnspod_custom_line.update")() |
| 154 | + defer inconsistentCheck(d, meta)() |
| 155 | + |
| 156 | + logId := getLogId(contextNil) |
| 157 | + |
| 158 | + request := dnspod.NewModifyDomainCustomLineRequest() |
| 159 | + |
| 160 | + var ( |
| 161 | + newName string |
| 162 | + ) |
| 163 | + |
| 164 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 165 | + if len(idSplit) != 2 { |
| 166 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 167 | + } |
| 168 | + domain := idSplit[0] |
| 169 | + name := idSplit[1] |
| 170 | + |
| 171 | + request.Domain = &domain |
| 172 | + request.Name = &name |
| 173 | + |
| 174 | + if v, ok := d.GetOk("name"); ok { |
| 175 | + newName = v.(string) |
| 176 | + request.PreName = helper.String(name) |
| 177 | + request.Name = helper.String(v.(string)) |
| 178 | + } |
| 179 | + |
| 180 | + if v, ok := d.GetOk("area"); ok { |
| 181 | + request.Area = helper.String(v.(string)) |
| 182 | + } |
| 183 | + |
| 184 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 185 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseDnsPodClient().ModifyDomainCustomLine(request) |
| 186 | + if e != nil { |
| 187 | + return retryError(e) |
| 188 | + } else { |
| 189 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 190 | + } |
| 191 | + return nil |
| 192 | + }) |
| 193 | + if err != nil { |
| 194 | + log.Printf("[CRITAL]%s update dnspod custom_line failed, reason:%+v", logId, err) |
| 195 | + return err |
| 196 | + } |
| 197 | + |
| 198 | + if d.HasChange("name") { |
| 199 | + d.SetId(strings.Join([]string{domain, newName}, FILED_SP)) |
| 200 | + } |
| 201 | + |
| 202 | + return resourceTencentCloudDnspodCustomLineRead(d, meta) |
| 203 | +} |
| 204 | + |
| 205 | +func resourceTencentCloudDnspodCustomLineDelete(d *schema.ResourceData, meta interface{}) error { |
| 206 | + defer logElapsed("resource.tencentcloud_dnspod_custom_line.delete")() |
| 207 | + defer inconsistentCheck(d, meta)() |
| 208 | + |
| 209 | + logId := getLogId(contextNil) |
| 210 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 211 | + |
| 212 | + service := DnspodService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 213 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 214 | + if len(idSplit) != 2 { |
| 215 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 216 | + } |
| 217 | + domain := idSplit[0] |
| 218 | + name := idSplit[1] |
| 219 | + |
| 220 | + if err := service.DeleteDnspodCustomLineById(ctx, domain, name); err != nil { |
| 221 | + return err |
| 222 | + } |
| 223 | + |
| 224 | + return nil |
| 225 | +} |
0 commit comments