|
| 1 | +package privatedns |
| 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 | + privatednsv20201028 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns/v20201028" |
| 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 ResourceTencentCloudPrivateDnsInboundEndpoint() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + Create: resourceTencentCloudPrivateDnsInboundEndpointCreate, |
| 19 | + Read: resourceTencentCloudPrivateDnsInboundEndpointRead, |
| 20 | + Update: resourceTencentCloudPrivateDnsInboundEndpointUpdate, |
| 21 | + Delete: resourceTencentCloudPrivateDnsInboundEndpointDelete, |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "endpoint_name": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Required: true, |
| 26 | + Description: "Name.", |
| 27 | + }, |
| 28 | + |
| 29 | + "endpoint_region": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + ForceNew: true, |
| 33 | + Description: "Region.", |
| 34 | + }, |
| 35 | + |
| 36 | + "endpoint_vpc": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Required: true, |
| 39 | + ForceNew: true, |
| 40 | + Description: "VPC ID.", |
| 41 | + }, |
| 42 | + |
| 43 | + "subnet_ip": { |
| 44 | + Type: schema.TypeList, |
| 45 | + Required: true, |
| 46 | + ForceNew: true, |
| 47 | + Description: "Subnet information.", |
| 48 | + Elem: &schema.Resource{ |
| 49 | + Schema: map[string]*schema.Schema{ |
| 50 | + "subnet_id": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Required: true, |
| 53 | + ForceNew: true, |
| 54 | + Description: "Subnet ID.", |
| 55 | + }, |
| 56 | + "subnet_vip": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Optional: true, |
| 59 | + ForceNew: true, |
| 60 | + Computed: true, |
| 61 | + Description: "IP address.", |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func resourceTencentCloudPrivateDnsInboundEndpointCreate(d *schema.ResourceData, meta interface{}) error { |
| 71 | + defer tccommon.LogElapsed("resource.tencentcloud_private_dns_inbound_endpoint.create")() |
| 72 | + defer tccommon.InconsistentCheck(d, meta)() |
| 73 | + |
| 74 | + var ( |
| 75 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 76 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 77 | + request = privatednsv20201028.NewCreateInboundEndpointRequest() |
| 78 | + response = privatednsv20201028.NewCreateInboundEndpointResponse() |
| 79 | + endpointId string |
| 80 | + ) |
| 81 | + |
| 82 | + if v, ok := d.GetOk("endpoint_name"); ok { |
| 83 | + request.EndpointName = helper.String(v.(string)) |
| 84 | + } |
| 85 | + |
| 86 | + if v, ok := d.GetOk("endpoint_region"); ok { |
| 87 | + request.EndpointRegion = helper.String(v.(string)) |
| 88 | + } |
| 89 | + |
| 90 | + if v, ok := d.GetOk("endpoint_vpc"); ok { |
| 91 | + request.EndpointVpc = helper.String(v.(string)) |
| 92 | + } |
| 93 | + |
| 94 | + if v, ok := d.GetOk("subnet_ip"); ok { |
| 95 | + for _, item := range v.([]interface{}) { |
| 96 | + subnetIpMap := item.(map[string]interface{}) |
| 97 | + subnetIpInfo := privatednsv20201028.SubnetIpInfo{} |
| 98 | + if v, ok := subnetIpMap["subnet_id"].(string); ok && v != "" { |
| 99 | + subnetIpInfo.SubnetId = helper.String(v) |
| 100 | + } |
| 101 | + |
| 102 | + if v, ok := subnetIpMap["subnet_vip"].(string); ok && v != "" { |
| 103 | + subnetIpInfo.SubnetVip = helper.String(v) |
| 104 | + } |
| 105 | + |
| 106 | + request.SubnetIp = append(request.SubnetIp, &subnetIpInfo) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 111 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePrivatednsV20201028Client().CreateInboundEndpointWithContext(ctx, request) |
| 112 | + if e != nil { |
| 113 | + return tccommon.RetryError(e) |
| 114 | + } else { |
| 115 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 116 | + } |
| 117 | + |
| 118 | + if result == nil || result.Response == nil { |
| 119 | + return resource.NonRetryableError(fmt.Errorf("Create private dns inbound endpoint failed, Response is nil.")) |
| 120 | + } |
| 121 | + |
| 122 | + response = result |
| 123 | + return nil |
| 124 | + }) |
| 125 | + |
| 126 | + if reqErr != nil { |
| 127 | + log.Printf("[CRITAL]%s create private dns inbound endpoint failed, reason:%+v", logId, reqErr) |
| 128 | + return reqErr |
| 129 | + } |
| 130 | + |
| 131 | + if response.Response.EndpointId == nil { |
| 132 | + return fmt.Errorf("EndpointId is nil.") |
| 133 | + } |
| 134 | + |
| 135 | + endpointId = *response.Response.EndpointId |
| 136 | + d.SetId(endpointId) |
| 137 | + return resourceTencentCloudPrivateDnsInboundEndpointRead(d, meta) |
| 138 | +} |
| 139 | + |
| 140 | +func resourceTencentCloudPrivateDnsInboundEndpointRead(d *schema.ResourceData, meta interface{}) error { |
| 141 | + defer tccommon.LogElapsed("resource.tencentcloud_private_dns_inbound_endpoint.read")() |
| 142 | + defer tccommon.InconsistentCheck(d, meta)() |
| 143 | + |
| 144 | + var ( |
| 145 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 146 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 147 | + service = PrivatednsService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 148 | + endpointId = d.Id() |
| 149 | + ) |
| 150 | + |
| 151 | + respData, err := service.DescribePrivateDnsInboundEndpointById(ctx, endpointId) |
| 152 | + if err != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + |
| 156 | + if respData == nil { |
| 157 | + log.Printf("[WARN]%s resource `tencentcloud_private_dns_inbound_endpoint` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 158 | + d.SetId("") |
| 159 | + return nil |
| 160 | + } |
| 161 | + |
| 162 | + if respData.EndPointName != nil { |
| 163 | + _ = d.Set("endpoint_name", respData.EndPointName) |
| 164 | + } |
| 165 | + |
| 166 | + if respData.UniqVpcId != nil { |
| 167 | + _ = d.Set("endpoint_vpc", respData.UniqVpcId) |
| 168 | + } |
| 169 | + |
| 170 | + if respData.EndPointService != nil { |
| 171 | + tmpList := make([]map[string]interface{}, 0, len(respData.EndPointService)) |
| 172 | + for _, item := range respData.EndPointService { |
| 173 | + dMap := make(map[string]interface{}, 0) |
| 174 | + if item.UniqSubnetId != nil { |
| 175 | + dMap["subnet_id"] = item.UniqSubnetId |
| 176 | + } |
| 177 | + |
| 178 | + if item.EndPointVip != nil { |
| 179 | + dMap["subnet_vip"] = item.EndPointVip |
| 180 | + } |
| 181 | + |
| 182 | + tmpList = append(tmpList, dMap) |
| 183 | + } |
| 184 | + |
| 185 | + _ = d.Set("subnet_ip", tmpList) |
| 186 | + } |
| 187 | + |
| 188 | + return nil |
| 189 | +} |
| 190 | + |
| 191 | +func resourceTencentCloudPrivateDnsInboundEndpointUpdate(d *schema.ResourceData, meta interface{}) error { |
| 192 | + defer tccommon.LogElapsed("resource.tencentcloud_private_dns_inbound_endpoint.update")() |
| 193 | + defer tccommon.InconsistentCheck(d, meta)() |
| 194 | + |
| 195 | + var ( |
| 196 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 197 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 198 | + endpointId = d.Id() |
| 199 | + ) |
| 200 | + |
| 201 | + if d.HasChange("endpoint_name") { |
| 202 | + request := privatednsv20201028.NewModifyInboundEndpointRequest() |
| 203 | + if v, ok := d.GetOk("endpoint_name"); ok { |
| 204 | + request.EndpointName = helper.String(v.(string)) |
| 205 | + } |
| 206 | + |
| 207 | + request.EndpointId = &endpointId |
| 208 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 209 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePrivatednsV20201028Client().ModifyInboundEndpointWithContext(ctx, request) |
| 210 | + if e != nil { |
| 211 | + return tccommon.RetryError(e) |
| 212 | + } else { |
| 213 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 214 | + } |
| 215 | + |
| 216 | + return nil |
| 217 | + }) |
| 218 | + |
| 219 | + if reqErr != nil { |
| 220 | + log.Printf("[CRITAL]%s update private dns inbound endpoint failed, reason:%+v", logId, reqErr) |
| 221 | + return reqErr |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + return resourceTencentCloudPrivateDnsInboundEndpointRead(d, meta) |
| 226 | +} |
| 227 | + |
| 228 | +func resourceTencentCloudPrivateDnsInboundEndpointDelete(d *schema.ResourceData, meta interface{}) error { |
| 229 | + defer tccommon.LogElapsed("resource.tencentcloud_private_dns_inbound_endpoint.delete")() |
| 230 | + defer tccommon.InconsistentCheck(d, meta)() |
| 231 | + |
| 232 | + var ( |
| 233 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 234 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 235 | + request = privatednsv20201028.NewDeleteInboundEndpointRequest() |
| 236 | + endpointId = d.Id() |
| 237 | + ) |
| 238 | + |
| 239 | + request.EndpointId = &endpointId |
| 240 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 241 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePrivatednsV20201028Client().DeleteInboundEndpointWithContext(ctx, request) |
| 242 | + if e != nil { |
| 243 | + return tccommon.RetryError(e) |
| 244 | + } else { |
| 245 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 246 | + } |
| 247 | + |
| 248 | + return nil |
| 249 | + }) |
| 250 | + |
| 251 | + if reqErr != nil { |
| 252 | + log.Printf("[CRITAL]%s delete private dns inbound endpoint failed, reason:%+v", logId, reqErr) |
| 253 | + return reqErr |
| 254 | + } |
| 255 | + |
| 256 | + return nil |
| 257 | +} |
0 commit comments