|
| 1 | +/* |
| 2 | +Provides a resource to create a PrivateDns zone_vpc_attachment |
| 3 | +
|
| 4 | +~> **NOTE:** If you need to bind account A to account B's VPC resources, you need to first grant role authorization to account A. |
| 5 | +
|
| 6 | +Example Usage |
| 7 | +
|
| 8 | +Append VPC associated with private dns zone |
| 9 | +
|
| 10 | +```hcl |
| 11 | +resource "tencentcloud_private_dns_zone" "example" { |
| 12 | + domain = "domain.com" |
| 13 | + remark = "remark." |
| 14 | +
|
| 15 | + dns_forward_status = "DISABLED" |
| 16 | + cname_speedup_status = "ENABLED" |
| 17 | +
|
| 18 | + tags = { |
| 19 | + createdBy : "terraform" |
| 20 | + } |
| 21 | +} |
| 22 | +
|
| 23 | +resource "tencentcloud_vpc" "vpc" { |
| 24 | + name = "vpc-example" |
| 25 | + cidr_block = "10.0.0.0/16" |
| 26 | +} |
| 27 | +
|
| 28 | +resource "tencentcloud_private_dns_zone_vpc_attachment" "example" { |
| 29 | + zone_id = tencentcloud_private_dns_zone.example.id |
| 30 | +
|
| 31 | + vpc_set { |
| 32 | + uniq_vpc_id = tencentcloud_vpc.vpc.id |
| 33 | + region = "ap-guangzhou" |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | +
|
| 38 | +Add VPC information for associated accounts in the private dns zone |
| 39 | +
|
| 40 | +```hcl |
| 41 | +resource "tencentcloud_private_dns_zone_vpc_attachment" "example" { |
| 42 | + zone_id = tencentcloud_private_dns_zone.example.id |
| 43 | +
|
| 44 | + account_vpc_set { |
| 45 | + uniq_vpc_id = "vpc-82znjzn3" |
| 46 | + region = "ap-guangzhou" |
| 47 | + uin = "100017155920" |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | +
|
| 52 | +Import |
| 53 | +
|
| 54 | +PrivateDns zone_vpc_attachment can be imported using the id, e.g. |
| 55 | +
|
| 56 | +``` |
| 57 | +terraform import tencentcloud_private_dns_zone_vpc_attachment.example zone-6t11lof0#vpc-jdx11z0t |
| 58 | +``` |
| 59 | +*/ |
| 60 | +package tencentcloud |
| 61 | + |
| 62 | +import ( |
| 63 | + "context" |
| 64 | + "fmt" |
| 65 | + "log" |
| 66 | + "strings" |
| 67 | + |
| 68 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 69 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 70 | + privatedns "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns/v20201028" |
| 71 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 72 | +) |
| 73 | + |
| 74 | +func resourceTencentCloudPrivateDnsZoneVpcAttachment() *schema.Resource { |
| 75 | + return &schema.Resource{ |
| 76 | + Create: resourceTencentCloudPrivateDnsZoneVpcAttachmentCreate, |
| 77 | + Read: resourceTencentCloudPrivateDnsZoneVpcAttachmentRead, |
| 78 | + Delete: resourceTencentCloudPrivateDnsZoneVpcAttachmentDelete, |
| 79 | + Importer: &schema.ResourceImporter{ |
| 80 | + State: schema.ImportStatePassthrough, |
| 81 | + }, |
| 82 | + Schema: map[string]*schema.Schema{ |
| 83 | + "zone_id": { |
| 84 | + Required: true, |
| 85 | + ForceNew: true, |
| 86 | + Type: schema.TypeString, |
| 87 | + Description: "PrivateZone ID.", |
| 88 | + }, |
| 89 | + "vpc_set": { |
| 90 | + Optional: true, |
| 91 | + ForceNew: true, |
| 92 | + MaxItems: 1, |
| 93 | + ExactlyOneOf: []string{"account_vpc_set"}, |
| 94 | + Type: schema.TypeList, |
| 95 | + Description: "New add vpc info.", |
| 96 | + Elem: &schema.Resource{ |
| 97 | + Schema: map[string]*schema.Schema{ |
| 98 | + "uniq_vpc_id": { |
| 99 | + Type: schema.TypeString, |
| 100 | + Required: true, |
| 101 | + Description: "Uniq Vpc Id.", |
| 102 | + }, |
| 103 | + "region": { |
| 104 | + Type: schema.TypeString, |
| 105 | + Required: true, |
| 106 | + Description: "Vpc region.", |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + "account_vpc_set": { |
| 112 | + Optional: true, |
| 113 | + ForceNew: true, |
| 114 | + MaxItems: 1, |
| 115 | + ExactlyOneOf: []string{"vpc_set"}, |
| 116 | + Type: schema.TypeList, |
| 117 | + Description: "New add account vpc info.", |
| 118 | + Elem: &schema.Resource{ |
| 119 | + Schema: map[string]*schema.Schema{ |
| 120 | + "uniq_vpc_id": { |
| 121 | + Type: schema.TypeString, |
| 122 | + Required: true, |
| 123 | + Description: "Uniq Vpc Id.", |
| 124 | + }, |
| 125 | + "region": { |
| 126 | + Type: schema.TypeString, |
| 127 | + Required: true, |
| 128 | + Description: "Vpc region.", |
| 129 | + }, |
| 130 | + "uin": { |
| 131 | + Type: schema.TypeString, |
| 132 | + Required: true, |
| 133 | + Description: "Vpc owner uin. To grant role authorization to this account.", |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func resourceTencentCloudPrivateDnsZoneVpcAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 143 | + defer logElapsed("resource.tencentcloud_private_dns_zone_vpc_attachment.create")() |
| 144 | + defer inconsistentCheck(d, meta)() |
| 145 | + |
| 146 | + var ( |
| 147 | + logId = getLogId(contextNil) |
| 148 | + request = privatedns.NewAddSpecifyPrivateZoneVpcRequest() |
| 149 | + zoneId string |
| 150 | + uniqVpcId string |
| 151 | + ) |
| 152 | + |
| 153 | + if v, ok := d.GetOk("zone_id"); ok { |
| 154 | + request.ZoneId = helper.String(v.(string)) |
| 155 | + zoneId = v.(string) |
| 156 | + } |
| 157 | + |
| 158 | + if v, ok := d.GetOk("vpc_set"); ok { |
| 159 | + for _, item := range v.([]interface{}) { |
| 160 | + dMap := item.(map[string]interface{}) |
| 161 | + vpcInfo := new(privatedns.VpcInfo) |
| 162 | + if v, ok := dMap["uniq_vpc_id"]; ok { |
| 163 | + vpcInfo.UniqVpcId = helper.String(v.(string)) |
| 164 | + uniqVpcId = v.(string) |
| 165 | + } |
| 166 | + |
| 167 | + if v, ok := dMap["region"]; ok { |
| 168 | + vpcInfo.Region = helper.String(v.(string)) |
| 169 | + } |
| 170 | + |
| 171 | + request.VpcSet = append(request.VpcSet, vpcInfo) |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + if v, ok := d.GetOk("account_vpc_set"); ok { |
| 176 | + for _, item := range v.([]interface{}) { |
| 177 | + dMap := item.(map[string]interface{}) |
| 178 | + accountVpcInfo := new(privatedns.AccountVpcInfo) |
| 179 | + if v, ok := dMap["uniq_vpc_id"]; ok { |
| 180 | + accountVpcInfo.UniqVpcId = helper.String(v.(string)) |
| 181 | + uniqVpcId = v.(string) |
| 182 | + } |
| 183 | + |
| 184 | + if v, ok := dMap["region"]; ok { |
| 185 | + accountVpcInfo.Region = helper.String(v.(string)) |
| 186 | + } |
| 187 | + |
| 188 | + if v, ok := dMap["uin"]; ok { |
| 189 | + accountVpcInfo.Uin = helper.String(v.(string)) |
| 190 | + } |
| 191 | + |
| 192 | + request.AccountVpcSet = append(request.AccountVpcSet, accountVpcInfo) |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 197 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UsePrivateDnsClient().AddSpecifyPrivateZoneVpc(request) |
| 198 | + if e != nil { |
| 199 | + return retryError(e) |
| 200 | + } else { |
| 201 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 202 | + } |
| 203 | + |
| 204 | + return nil |
| 205 | + }) |
| 206 | + |
| 207 | + if err != nil { |
| 208 | + log.Printf("[CRITAL]%s create PrivateDns ZoneVpcAttachment failed, reason:%+v", logId, err) |
| 209 | + return err |
| 210 | + } |
| 211 | + |
| 212 | + d.SetId(strings.Join([]string{zoneId, uniqVpcId}, FILED_SP)) |
| 213 | + |
| 214 | + return resourceTencentCloudPrivateDnsZoneVpcAttachmentRead(d, meta) |
| 215 | +} |
| 216 | + |
| 217 | +func resourceTencentCloudPrivateDnsZoneVpcAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 218 | + defer logElapsed("resource.tencentcloud_private_dns_zone_vpc_attachment.read")() |
| 219 | + defer inconsistentCheck(d, meta)() |
| 220 | + |
| 221 | + var ( |
| 222 | + logId = getLogId(contextNil) |
| 223 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 224 | + service = PrivateDnsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 225 | + ) |
| 226 | + |
| 227 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 228 | + if len(idSplit) != 2 { |
| 229 | + return fmt.Errorf("id is broken,%s", idSplit) |
| 230 | + } |
| 231 | + |
| 232 | + zoneId := idSplit[0] |
| 233 | + uniqVpcId := idSplit[1] |
| 234 | + |
| 235 | + ZoneVpcAttachment, err := service.DescribePrivateDnsZoneVpcAttachmentById(ctx, zoneId) |
| 236 | + if err != nil { |
| 237 | + return err |
| 238 | + } |
| 239 | + |
| 240 | + if ZoneVpcAttachment == nil { |
| 241 | + d.SetId("") |
| 242 | + log.Printf("[WARN]%s resource `PrivateDnsZoneVpcAttachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 243 | + return nil |
| 244 | + } |
| 245 | + |
| 246 | + if ZoneVpcAttachment.ZoneId != nil { |
| 247 | + _ = d.Set("zone_id", ZoneVpcAttachment.ZoneId) |
| 248 | + } |
| 249 | + |
| 250 | + if ZoneVpcAttachment.VpcSet != nil { |
| 251 | + vpcSetList := []interface{}{} |
| 252 | + for _, vpcSet := range ZoneVpcAttachment.VpcSet { |
| 253 | + vpcSetMap := map[string]interface{}{} |
| 254 | + |
| 255 | + if *vpcSet.UniqVpcId == uniqVpcId { |
| 256 | + vpcSetMap["uniq_vpc_id"] = *vpcSet.UniqVpcId |
| 257 | + vpcSetMap["region"] = *vpcSet.Region |
| 258 | + vpcSetList = append(vpcSetList, vpcSetMap) |
| 259 | + break |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + _ = d.Set("vpc_set", vpcSetList) |
| 264 | + } |
| 265 | + |
| 266 | + if ZoneVpcAttachment.AccountVpcSet != nil { |
| 267 | + accountVpcSetList := []interface{}{} |
| 268 | + for _, accountVpcSet := range ZoneVpcAttachment.AccountVpcSet { |
| 269 | + accountVpcSetMap := map[string]interface{}{} |
| 270 | + |
| 271 | + if *accountVpcSet.UniqVpcId == uniqVpcId { |
| 272 | + accountVpcSetMap["uniq_vpc_id"] = *accountVpcSet.UniqVpcId |
| 273 | + accountVpcSetMap["region"] = *accountVpcSet.Region |
| 274 | + accountVpcSetMap["uin"] = *accountVpcSet.Uin |
| 275 | + accountVpcSetList = append(accountVpcSetList, accountVpcSetMap) |
| 276 | + break |
| 277 | + } |
| 278 | + } |
| 279 | + |
| 280 | + _ = d.Set("account_vpc_set", accountVpcSetList) |
| 281 | + } |
| 282 | + |
| 283 | + return nil |
| 284 | +} |
| 285 | + |
| 286 | +func resourceTencentCloudPrivateDnsZoneVpcAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 287 | + defer logElapsed("resource.tencentcloud_private_dns_zone_vpc_attachment.delete")() |
| 288 | + defer inconsistentCheck(d, meta)() |
| 289 | + |
| 290 | + var ( |
| 291 | + logId = getLogId(contextNil) |
| 292 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 293 | + service = PrivateDnsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 294 | + region string |
| 295 | + uin string |
| 296 | + ) |
| 297 | + |
| 298 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 299 | + if len(idSplit) != 2 { |
| 300 | + return fmt.Errorf("id is broken,%s", idSplit) |
| 301 | + } |
| 302 | + |
| 303 | + zoneId := idSplit[0] |
| 304 | + uniqVpcId := idSplit[1] |
| 305 | + |
| 306 | + // get vpc detail |
| 307 | + ZoneVpcAttachment, err := service.DescribePrivateDnsZoneVpcAttachmentById(ctx, zoneId) |
| 308 | + if err != nil { |
| 309 | + return err |
| 310 | + } |
| 311 | + |
| 312 | + if ZoneVpcAttachment == nil { |
| 313 | + d.SetId("") |
| 314 | + log.Printf("[WARN]%s resource `PrivateDnsZoneVpcAttachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 315 | + return nil |
| 316 | + } |
| 317 | + |
| 318 | + if ZoneVpcAttachment.VpcSet != nil { |
| 319 | + for _, vpcSet := range ZoneVpcAttachment.VpcSet { |
| 320 | + if *vpcSet.UniqVpcId == uniqVpcId { |
| 321 | + region = *vpcSet.Region |
| 322 | + break |
| 323 | + } |
| 324 | + } |
| 325 | + } |
| 326 | + |
| 327 | + if ZoneVpcAttachment.AccountVpcSet != nil { |
| 328 | + for _, accountVpcSet := range ZoneVpcAttachment.AccountVpcSet { |
| 329 | + if *accountVpcSet.UniqVpcId == uniqVpcId { |
| 330 | + region = *accountVpcSet.Region |
| 331 | + uin = *accountVpcSet.Uin |
| 332 | + break |
| 333 | + } |
| 334 | + } |
| 335 | + } |
| 336 | + |
| 337 | + if err = service.DeletePrivateDnsZoneVpcAttachmentById(ctx, zoneId, uniqVpcId, region, uin); err != nil { |
| 338 | + return err |
| 339 | + } |
| 340 | + |
| 341 | + return nil |
| 342 | +} |
0 commit comments