|
| 1 | +/* |
| 2 | +Provide a resource to create a Private Dns Record. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_private_dns_record" "foo" { |
| 8 | + zone_id = "zone-rqndjnki" |
| 9 | + record_type = "A" |
| 10 | + record_value = "192.168.1.2" |
| 11 | + sub_domain = "www" |
| 12 | + ttl = 300 |
| 13 | + weight = 1 |
| 14 | + mx = 0 |
| 15 | +} |
| 16 | +``` |
| 17 | +
|
| 18 | +Import |
| 19 | +
|
| 20 | +Private Dns Record can be imported, e.g. |
| 21 | +
|
| 22 | +``` |
| 23 | +$ terraform import tencentcloud_private_dns_zone.foo zone_id#record_id |
| 24 | +``` |
| 25 | +*/ |
| 26 | +package tencentcloud |
| 27 | + |
| 28 | +import ( |
| 29 | + "context" |
| 30 | + "fmt" |
| 31 | + "log" |
| 32 | + "strings" |
| 33 | + |
| 34 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 35 | + |
| 36 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 37 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 38 | + privatedns "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns/v20201028" |
| 39 | +) |
| 40 | + |
| 41 | +func resourceTencentCloudPrivateDnsRecord() *schema.Resource { |
| 42 | + return &schema.Resource{ |
| 43 | + Create: resourceTencentCloudDPrivateDnsRecordCreate, |
| 44 | + Read: resourceTencentCloudDPrivateDnsRecordRead, |
| 45 | + Update: resourceTencentCloudDPrivateDnsRecordUpdate, |
| 46 | + Delete: resourceTencentCloudDPrivateDnsRecordDelete, |
| 47 | + Importer: &schema.ResourceImporter{ |
| 48 | + State: schema.ImportStatePassthrough, |
| 49 | + }, |
| 50 | + |
| 51 | + Schema: map[string]*schema.Schema{ |
| 52 | + "zone_id": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Required: true, |
| 55 | + Description: "Private domain ID.", |
| 56 | + }, |
| 57 | + "record_type": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Required: true, |
| 60 | + Description: "Record type. Valid values: \"A\", \"AAAA\", \"CNAME\", \"MX\", \"TXT\", \"PTR\".", |
| 61 | + }, |
| 62 | + "sub_domain": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Required: true, |
| 65 | + Description: "Subdomain, such as \"www\", \"m\", and \"@\".", |
| 66 | + }, |
| 67 | + "record_value": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Required: true, |
| 70 | + Description: "Record value, such as IP: 192.168.10.2," + |
| 71 | + " CNAME: cname.qcloud.com, and MX: mail.qcloud.com..", |
| 72 | + }, |
| 73 | + "weight": { |
| 74 | + Type: schema.TypeInt, |
| 75 | + Optional: true, |
| 76 | + Description: "Record weight. Value range: 1~100.", |
| 77 | + }, |
| 78 | + "mx": { |
| 79 | + Type: schema.TypeInt, |
| 80 | + Optional: true, |
| 81 | + Description: "MX priority, which is required when the record type is MX." + |
| 82 | + " Valid values: 5, 10, 15, 20, 30, 40, 50.", |
| 83 | + }, |
| 84 | + "ttl": { |
| 85 | + Type: schema.TypeInt, |
| 86 | + Optional: true, |
| 87 | + Description: "Record cache time. The smaller the value, the faster the record will take effect." + |
| 88 | + " Value range: 1~86400s.", |
| 89 | + }, |
| 90 | + }, |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func resourceTencentCloudDPrivateDnsRecordCreate(d *schema.ResourceData, meta interface{}) error { |
| 95 | + defer logElapsed("resource.tencentcloud_private_dns_record.create")() |
| 96 | + |
| 97 | + logId := getLogId(contextNil) |
| 98 | + |
| 99 | + request := privatedns.NewCreatePrivateZoneRecordRequest() |
| 100 | + |
| 101 | + zoneId := d.Get("zone_id").(string) |
| 102 | + request.ZoneId = &zoneId |
| 103 | + |
| 104 | + recordType := d.Get("record_type").(string) |
| 105 | + request.RecordType = &recordType |
| 106 | + |
| 107 | + subDomain := d.Get("sub_domain").(string) |
| 108 | + request.SubDomain = &subDomain |
| 109 | + |
| 110 | + recordValue := d.Get("record_value").(string) |
| 111 | + request.RecordValue = &recordValue |
| 112 | + |
| 113 | + if v, ok := d.GetOk("weight"); ok { |
| 114 | + request.Weight = helper.Int64(int64(v.(int))) |
| 115 | + } |
| 116 | + |
| 117 | + if v, ok := d.GetOk("mx"); ok { |
| 118 | + request.MX = helper.Int64(int64(v.(int))) |
| 119 | + } |
| 120 | + if v, ok := d.GetOk("ttl"); ok { |
| 121 | + request.TTL = helper.Int64(int64(v.(int))) |
| 122 | + } |
| 123 | + |
| 124 | + result, err := meta.(*TencentCloudClient).apiV3Conn.UsePrivateDnsClient().CreatePrivateZoneRecord(request) |
| 125 | + |
| 126 | + if err != nil { |
| 127 | + log.Printf("[CRITAL]%s create PrivateDns record failed, reason:%s\n", logId, err.Error()) |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + var response *privatedns.CreatePrivateZoneRecordResponse |
| 132 | + response = result |
| 133 | + |
| 134 | + recordId := *response.Response.RecordId |
| 135 | + d.SetId(strings.Join([]string{zoneId, recordId}, FILED_SP)) |
| 136 | + |
| 137 | + return resourceTencentCloudDPrivateDnsRecordRead(d, meta) |
| 138 | +} |
| 139 | + |
| 140 | +func resourceTencentCloudDPrivateDnsRecordRead(d *schema.ResourceData, meta interface{}) error { |
| 141 | + defer logElapsed("resource.tencentcloud_private_dns_zone.read")() |
| 142 | + defer inconsistentCheck(d, meta)() |
| 143 | + |
| 144 | + logId := getLogId(contextNil) |
| 145 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 146 | + |
| 147 | + service := PrivateDnsService{ |
| 148 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 149 | + } |
| 150 | + |
| 151 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 152 | + if len(idSplit) != 2 { |
| 153 | + return fmt.Errorf("record id strategy is can't read, id is borken, id is %s", d.Id()) |
| 154 | + } |
| 155 | + zoneId := idSplit[0] |
| 156 | + recordId := idSplit[1] |
| 157 | + |
| 158 | + records, err := service.DescribePrivateDnsRecordByFilter(ctx, zoneId, "") |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + |
| 163 | + if len(records) < 1 { |
| 164 | + return fmt.Errorf("private dns record not exists.") |
| 165 | + } |
| 166 | + |
| 167 | + var record *privatedns.PrivateZoneRecord |
| 168 | + for _, item := range records { |
| 169 | + if *item.RecordId == recordId { |
| 170 | + record = item |
| 171 | + } |
| 172 | + } |
| 173 | + _ = d.Set("zone_id", record.ZoneId) |
| 174 | + _ = d.Set("record_type", record.RecordType) |
| 175 | + _ = d.Set("sub_domain", record.SubDomain) |
| 176 | + _ = d.Set("record_value", record.RecordValue) |
| 177 | + _ = d.Set("weight", record.Weight) |
| 178 | + _ = d.Set("mx", record.MX) |
| 179 | + _ = d.Set("ttl", record.TTL) |
| 180 | + |
| 181 | + return nil |
| 182 | +} |
| 183 | + |
| 184 | +func resourceTencentCloudDPrivateDnsRecordUpdate(d *schema.ResourceData, meta interface{}) error { |
| 185 | + defer logElapsed("resource.tencentcloud_private_dns_record.update")() |
| 186 | + |
| 187 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 188 | + if len(idSplit) != 2 { |
| 189 | + return fmt.Errorf("record id strategy is can't read, id is borken, id is %s", d.Id()) |
| 190 | + } |
| 191 | + logId := getLogId(contextNil) |
| 192 | + zoneId := idSplit[0] |
| 193 | + recordId := idSplit[1] |
| 194 | + |
| 195 | + request := privatedns.NewModifyPrivateZoneRecordRequest() |
| 196 | + request.ZoneId = helper.String(zoneId) |
| 197 | + request.RecordId = helper.String(recordId) |
| 198 | + |
| 199 | + needModify := false |
| 200 | + if d.HasChange("record_type") { |
| 201 | + needModify = true |
| 202 | + } |
| 203 | + |
| 204 | + if d.HasChange("sub_domain") { |
| 205 | + needModify = true |
| 206 | + } |
| 207 | + |
| 208 | + if d.HasChange("record_value") { |
| 209 | + needModify = true |
| 210 | + } |
| 211 | + |
| 212 | + if d.HasChange("weight") { |
| 213 | + needModify = true |
| 214 | + if v, ok := d.GetOk("weight"); ok { |
| 215 | + request.Weight = helper.Int64(int64(v.(int))) |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + if d.HasChange("mx") { |
| 220 | + needModify = true |
| 221 | + if v, ok := d.GetOk("mx"); ok { |
| 222 | + request.MX = helper.Int64(int64(v.(int))) |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + if d.HasChange("ttl") { |
| 227 | + needModify = true |
| 228 | + if v, ok := d.GetOk("ttl"); ok { |
| 229 | + request.TTL = helper.Int64(int64(v.(int))) |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + if needModify { |
| 234 | + if v, ok := d.GetOk("record_type"); ok { |
| 235 | + request.RecordType = helper.String(v.(string)) |
| 236 | + } |
| 237 | + if v, ok := d.GetOk("sub_domain"); ok { |
| 238 | + request.SubDomain = helper.String(v.(string)) |
| 239 | + } |
| 240 | + if v, ok := d.GetOk("record_value"); ok { |
| 241 | + request.RecordValue = helper.String(v.(string)) |
| 242 | + } |
| 243 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 244 | + _, e := meta.(*TencentCloudClient).apiV3Conn.UsePrivateDnsClient().ModifyPrivateZoneRecord(request) |
| 245 | + if e != nil { |
| 246 | + return retryError(e) |
| 247 | + } |
| 248 | + return nil |
| 249 | + }) |
| 250 | + if err != nil { |
| 251 | + log.Printf("[CRITAL]%s modify privateDns record info failed, reason:%s\n", logId, err.Error()) |
| 252 | + return err |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + return resourceTencentCloudDPrivateDnsRecordRead(d, meta) |
| 257 | +} |
| 258 | + |
| 259 | +func resourceTencentCloudDPrivateDnsRecordDelete(d *schema.ResourceData, meta interface{}) error { |
| 260 | + defer logElapsed("resource.tencentcloud_private_dns_record.delete")() |
| 261 | + |
| 262 | + logId := getLogId(contextNil) |
| 263 | + |
| 264 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 265 | + if len(idSplit) != 2 { |
| 266 | + return fmt.Errorf("record id strategy is can't read, id is borken, id is %s", d.Id()) |
| 267 | + } |
| 268 | + zoneId := idSplit[0] |
| 269 | + recordId := idSplit[1] |
| 270 | + |
| 271 | + request := privatedns.NewDeletePrivateZoneRecordRequest() |
| 272 | + request.ZoneId = helper.String(zoneId) |
| 273 | + request.RecordId = helper.String(recordId) |
| 274 | + |
| 275 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 276 | + _, e := meta.(*TencentCloudClient).apiV3Conn.UsePrivateDnsClient().DeletePrivateZoneRecord(request) |
| 277 | + if e != nil { |
| 278 | + return retryError(e) |
| 279 | + } |
| 280 | + return nil |
| 281 | + }) |
| 282 | + if err != nil { |
| 283 | + log.Printf("[CRITAL]%s delete privateDns record failed, reason:%s\n", logId, err.Error()) |
| 284 | + return err |
| 285 | + } |
| 286 | + return nil |
| 287 | +} |
0 commit comments