|
| 1 | +package vpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" |
| 12 | + |
| 13 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 14 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 15 | +) |
| 16 | + |
| 17 | +func ResourceTencentCloudHaVipInstanceAttachment() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Create: resourceTencentCloudHaVipInstanceAttachmentCreate, |
| 20 | + Read: resourceTencentCloudHaVipInstanceAttachmentRead, |
| 21 | + Delete: resourceTencentCloudHaVipInstanceAttachmentDelete, |
| 22 | + Importer: &schema.ResourceImporter{ |
| 23 | + State: schema.ImportStatePassthrough, |
| 24 | + }, |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "instance_id": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + ForceNew: true, |
| 30 | + Description: "The unique ID of the slave machine or network card to which HaVip is bound.", |
| 31 | + }, |
| 32 | + "ha_vip_id": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Optional: true, |
| 35 | + ForceNew: true, |
| 36 | + Description: "Unique ID of the HaVip instance.", |
| 37 | + }, |
| 38 | + "instance_type": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Optional: true, |
| 41 | + ForceNew: true, |
| 42 | + Description: "The type of HaVip binding. Values:CVM, ENI.", |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func resourceTencentCloudHaVipInstanceAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 49 | + defer tccommon.LogElapsed("resource.tencentcloud_ha_vip_instance_attachment.create")() |
| 50 | + defer tccommon.InconsistentCheck(d, meta)() |
| 51 | + |
| 52 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 53 | + |
| 54 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 55 | + |
| 56 | + var ( |
| 57 | + instanceId string |
| 58 | + haVipId string |
| 59 | + instanceType string |
| 60 | + ) |
| 61 | + var ( |
| 62 | + request = vpc.NewAssociateHaVipInstanceRequest() |
| 63 | + response = vpc.NewAssociateHaVipInstanceResponse() |
| 64 | + ) |
| 65 | + |
| 66 | + instanceId = d.Get("instance_id").(string) |
| 67 | + haVipId = d.Get("ha_vip_id").(string) |
| 68 | + instanceType = d.Get("instance_type").(string) |
| 69 | + |
| 70 | + request.HaVipAssociationSet = []*vpc.HaVipAssociation{ |
| 71 | + { |
| 72 | + HaVipId: helper.String(haVipId), |
| 73 | + InstanceType: helper.String(instanceType), |
| 74 | + InstanceId: helper.String(instanceId), |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 79 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().AssociateHaVipInstanceWithContext(ctx, request) |
| 80 | + if e != nil { |
| 81 | + return tccommon.RetryError(e) |
| 82 | + } else { |
| 83 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 84 | + } |
| 85 | + response = result |
| 86 | + return nil |
| 87 | + }) |
| 88 | + if err != nil { |
| 89 | + log.Printf("[CRITAL]%s create ha vip instance attachment failed, reason:%+v", logId, err) |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + _ = response |
| 94 | + |
| 95 | + d.SetId(strings.Join([]string{haVipId, instanceId}, tccommon.FILED_SP)) |
| 96 | + |
| 97 | + return resourceTencentCloudHaVipInstanceAttachmentRead(d, meta) |
| 98 | +} |
| 99 | + |
| 100 | +func resourceTencentCloudHaVipInstanceAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 101 | + defer tccommon.LogElapsed("resource.tencentcloud_ha_vip_instance_attachment.read")() |
| 102 | + defer tccommon.InconsistentCheck(d, meta)() |
| 103 | + |
| 104 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 105 | + |
| 106 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 107 | + |
| 108 | + service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 109 | + |
| 110 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 111 | + if len(idSplit) != 2 { |
| 112 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 113 | + } |
| 114 | + haVipId := idSplit[0] |
| 115 | + instanceId := idSplit[1] |
| 116 | + |
| 117 | + var haVips []*vpc.HaVip |
| 118 | + filters := map[string]string{ |
| 119 | + "havip-id": haVipId, |
| 120 | + "havip-association.instance-id": instanceId, |
| 121 | + } |
| 122 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 123 | + result, e := service.DescribeHaVipByFilter(ctx, filters) |
| 124 | + if e != nil { |
| 125 | + return tccommon.RetryError(e) |
| 126 | + } |
| 127 | + haVips = result |
| 128 | + return nil |
| 129 | + }) |
| 130 | + if err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + |
| 134 | + if len(haVips) == 0 || len(haVips[0].HaVipAssociationSet) == 0 { |
| 135 | + d.SetId("") |
| 136 | + log.Printf("[WARN]%s resource `ha_vip_instance_attachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 137 | + return nil |
| 138 | + } |
| 139 | + |
| 140 | + _ = d.Set("ha_vip_id", haVips[0].HaVipAssociationSet[0].HaVipId) |
| 141 | + _ = d.Set("instance_type", haVips[0].HaVipAssociationSet[0].InstanceType) |
| 142 | + _ = d.Set("instance_id", haVips[0].HaVipAssociationSet[0].InstanceId) |
| 143 | + |
| 144 | + _ = instanceId |
| 145 | + _ = haVipId |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func resourceTencentCloudHaVipInstanceAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 150 | + defer tccommon.LogElapsed("resource.tencentcloud_ha_vip_instance_attachment.delete")() |
| 151 | + defer tccommon.InconsistentCheck(d, meta)() |
| 152 | + |
| 153 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 154 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 155 | + |
| 156 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 157 | + if len(idSplit) != 2 { |
| 158 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 159 | + } |
| 160 | + haVipId := idSplit[0] |
| 161 | + instanceId := idSplit[1] |
| 162 | + |
| 163 | + var ( |
| 164 | + request = vpc.NewDisassociateHaVipInstanceRequest() |
| 165 | + response = vpc.NewDisassociateHaVipInstanceResponse() |
| 166 | + ) |
| 167 | + |
| 168 | + request.HaVipAssociationSet = []*vpc.HaVipAssociation{ |
| 169 | + { |
| 170 | + HaVipId: helper.String(haVipId), |
| 171 | + InstanceId: helper.String(instanceId), |
| 172 | + }, |
| 173 | + } |
| 174 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 175 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().DisassociateHaVipInstanceWithContext(ctx, request) |
| 176 | + if e != nil { |
| 177 | + return tccommon.RetryError(e) |
| 178 | + } else { |
| 179 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 180 | + } |
| 181 | + response = result |
| 182 | + return nil |
| 183 | + }) |
| 184 | + if err != nil { |
| 185 | + log.Printf("[CRITAL]%s delete ha vip instance attachment failed, reason:%+v", logId, err) |
| 186 | + return err |
| 187 | + } |
| 188 | + |
| 189 | + _ = response |
| 190 | + _ = instanceId |
| 191 | + _ = haVipId |
| 192 | + return nil |
| 193 | +} |
0 commit comments