|
| 1 | +package vpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" |
| 10 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 11 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 12 | +) |
| 13 | + |
| 14 | +func ResourceTencentCloudEniIpv4Address() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Create: resourceTencentCloudEniIpv4AddressCreate, |
| 17 | + Read: resourceTencentCloudEniIpv4AddressRead, |
| 18 | + Delete: resourceTencentCloudEniIpv4AddressDelete, |
| 19 | + Importer: &schema.ResourceImporter{ |
| 20 | + State: schema.ImportStatePassthrough, |
| 21 | + }, |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "network_interface_id": { |
| 24 | + Required: true, |
| 25 | + ForceNew: true, |
| 26 | + Type: schema.TypeString, |
| 27 | + Description: "The ID of the ENI instance, such as `eni-m6dyj72l`.", |
| 28 | + }, |
| 29 | + |
| 30 | + "private_ip_addresses": { |
| 31 | + Optional: true, |
| 32 | + Type: schema.TypeSet, |
| 33 | + Computed: true, |
| 34 | + ForceNew: true, |
| 35 | + ConflictsWith: []string{"secondary_private_ip_address_count", "qos_level"}, |
| 36 | + Description: "The information on private IP addresses, of which you can specify a maximum of 10 at a time. You should provide either this parameter or SecondaryPrivateIpAddressCount, or both.", |
| 37 | + Elem: &schema.Resource{ |
| 38 | + Schema: map[string]*schema.Schema{ |
| 39 | + "private_ip_address": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Required: true, |
| 42 | + ForceNew: true, |
| 43 | + Description: "Private IP address.", |
| 44 | + }, |
| 45 | + "primary": { |
| 46 | + Type: schema.TypeBool, |
| 47 | + Optional: true, |
| 48 | + ForceNew: true, |
| 49 | + Computed: true, |
| 50 | + Description: "Whether it is a primary IP.", |
| 51 | + }, |
| 52 | + "public_ip_address": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Optional: true, |
| 55 | + ForceNew: true, |
| 56 | + Computed: true, |
| 57 | + Description: "Public IP address.", |
| 58 | + }, |
| 59 | + "address_id": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Optional: true, |
| 62 | + ForceNew: true, |
| 63 | + Computed: true, |
| 64 | + Description: "EIP instance ID, such as `eip-11112222`.", |
| 65 | + }, |
| 66 | + "description": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Optional: true, |
| 69 | + ForceNew: true, |
| 70 | + Computed: true, |
| 71 | + Description: "Private IP description.", |
| 72 | + }, |
| 73 | + "is_wan_ip_blocked": { |
| 74 | + Type: schema.TypeBool, |
| 75 | + Optional: true, |
| 76 | + ForceNew: true, |
| 77 | + Computed: true, |
| 78 | + Description: "Whether the public IP is blocked.", |
| 79 | + }, |
| 80 | + "state": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Optional: true, |
| 83 | + ForceNew: true, |
| 84 | + Computed: true, |
| 85 | + Description: "IP status: `PENDING`: Creating, `MIGRATING`: Migrating, `DELETING`: Deleting, `AVAILABLE`: Available.", |
| 86 | + }, |
| 87 | + "qos_level": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Optional: true, |
| 90 | + ForceNew: true, |
| 91 | + Computed: true, |
| 92 | + Description: "IP service level. Values: PT` (Gold), `AU` (Silver), `AG `(Bronze) and DEFAULT` (Default).", |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + |
| 98 | + "secondary_private_ip_address_count": { |
| 99 | + Optional: true, |
| 100 | + Type: schema.TypeInt, |
| 101 | + Computed: true, |
| 102 | + ForceNew: true, |
| 103 | + ConflictsWith: []string{"private_ip_addresses"}, |
| 104 | + Description: "The number of newly-applied private IP addresses. You should provide either this parameter or PrivateIpAddresses, or both. The total number of private IP addresses cannot exceed the quota.", |
| 105 | + }, |
| 106 | + |
| 107 | + "qos_level": { |
| 108 | + Optional: true, |
| 109 | + Computed: true, |
| 110 | + ForceNew: true, |
| 111 | + ConflictsWith: []string{"private_ip_addresses"}, |
| 112 | + Type: schema.TypeString, |
| 113 | + Description: "IP service level. It is used together with `SecondaryPrivateIpAddressCount`. Values: PT`(Gold), `AU`(Silver), `AG `(Bronze) and DEFAULT (Default).", |
| 114 | + }, |
| 115 | + }, |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func resourceTencentCloudEniIpv4AddressCreate(d *schema.ResourceData, meta interface{}) error { |
| 120 | + defer tccommon.LogElapsed("resource.tencentcloud_eni_ipv4_address.create")() |
| 121 | + defer tccommon.InconsistentCheck(d, meta)() |
| 122 | + |
| 123 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 124 | + |
| 125 | + var ( |
| 126 | + request = vpc.NewAssignPrivateIpAddressesRequest() |
| 127 | + networkInterfaceId string |
| 128 | + ) |
| 129 | + if v, ok := d.GetOk("network_interface_id"); ok { |
| 130 | + networkInterfaceId = v.(string) |
| 131 | + request.NetworkInterfaceId = helper.String(v.(string)) |
| 132 | + } |
| 133 | + |
| 134 | + if v, ok := d.GetOk("private_ip_addresses"); ok { |
| 135 | + for _, item := range v.(*schema.Set).List() { |
| 136 | + dMap := item.(map[string]interface{}) |
| 137 | + privateIpAddressSpecification := vpc.PrivateIpAddressSpecification{} |
| 138 | + if v, ok := dMap["private_ip_address"]; ok { |
| 139 | + privateIpAddressSpecification.PrivateIpAddress = helper.String(v.(string)) |
| 140 | + } |
| 141 | + if v, ok := dMap["primary"]; ok { |
| 142 | + privateIpAddressSpecification.Primary = helper.Bool(v.(bool)) |
| 143 | + } |
| 144 | + if v, ok := dMap["public_ip_address"]; ok && v != "" { |
| 145 | + privateIpAddressSpecification.PublicIpAddress = helper.String(v.(string)) |
| 146 | + } |
| 147 | + if v, ok := dMap["address_id"]; ok && v != "" { |
| 148 | + privateIpAddressSpecification.AddressId = helper.String(v.(string)) |
| 149 | + } |
| 150 | + if v, ok := dMap["description"]; ok && v != "" { |
| 151 | + privateIpAddressSpecification.Description = helper.String(v.(string)) |
| 152 | + } |
| 153 | + if v, ok := dMap["is_wan_ip_blocked"]; ok { |
| 154 | + privateIpAddressSpecification.IsWanIpBlocked = helper.Bool(v.(bool)) |
| 155 | + } |
| 156 | + if v, ok := dMap["state"]; ok && v != "" { |
| 157 | + privateIpAddressSpecification.State = helper.String(v.(string)) |
| 158 | + } |
| 159 | + if v, ok := dMap["qos_level"]; ok && v != "" { |
| 160 | + privateIpAddressSpecification.QosLevel = helper.String(v.(string)) |
| 161 | + } |
| 162 | + request.PrivateIpAddresses = append(request.PrivateIpAddresses, &privateIpAddressSpecification) |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if v, ok := d.GetOkExists("secondary_private_ip_address_count"); ok { |
| 167 | + request.SecondaryPrivateIpAddressCount = helper.IntUint64(v.(int)) |
| 168 | + } |
| 169 | + |
| 170 | + if v, ok := d.GetOk("qos_level"); ok { |
| 171 | + request.QosLevel = helper.String(v.(string)) |
| 172 | + } |
| 173 | + |
| 174 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 175 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().AssignPrivateIpAddresses(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 | + return nil |
| 182 | + }) |
| 183 | + if err != nil { |
| 184 | + log.Printf("[CRITAL]%s create vpc eniIpv4Address failed, reason:%+v", logId, err) |
| 185 | + return err |
| 186 | + } |
| 187 | + |
| 188 | + d.SetId(networkInterfaceId) |
| 189 | + |
| 190 | + return resourceTencentCloudEniIpv4AddressRead(d, meta) |
| 191 | +} |
| 192 | + |
| 193 | +func resourceTencentCloudEniIpv4AddressRead(d *schema.ResourceData, meta interface{}) error { |
| 194 | + defer tccommon.LogElapsed("resource.tencentcloud_eni_ipv4_address.read")() |
| 195 | + defer tccommon.InconsistentCheck(d, meta)() |
| 196 | + |
| 197 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 198 | + |
| 199 | + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 200 | + |
| 201 | + service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 202 | + |
| 203 | + networkInterfaceId := d.Id() |
| 204 | + |
| 205 | + enis, err := service.DescribeEniById(ctx, []string{networkInterfaceId}) |
| 206 | + |
| 207 | + if err != nil { |
| 208 | + return err |
| 209 | + } |
| 210 | + |
| 211 | + if len(enis) < 1 { |
| 212 | + d.SetId("") |
| 213 | + log.Printf("[WARN]%s resource `VpcIpv6EniAddress` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 214 | + return nil |
| 215 | + } |
| 216 | + |
| 217 | + eni := enis[0] |
| 218 | + |
| 219 | + if eni.NetworkInterfaceId != nil { |
| 220 | + _ = d.Set("network_interface_id", eni.NetworkInterfaceId) |
| 221 | + } |
| 222 | + |
| 223 | + ipv4s := make([]map[string]interface{}, 0, len(eni.PrivateIpAddressSet)) |
| 224 | + for _, ipv4 := range eni.PrivateIpAddressSet { |
| 225 | + if !*ipv4.Primary { |
| 226 | + ipv4s = append(ipv4s, map[string]interface{}{ |
| 227 | + "private_ip_address": ipv4.PrivateIpAddress, |
| 228 | + "primary": ipv4.Primary, |
| 229 | + "public_ip_address": ipv4.AddressId, |
| 230 | + "address_id": ipv4.AddressId, |
| 231 | + "description": ipv4.Description, |
| 232 | + "is_wan_ip_blocked": ipv4.IsWanIpBlocked, |
| 233 | + "state": ipv4.State, |
| 234 | + "qos_level": ipv4.QosLevel, |
| 235 | + }) |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + _ = d.Set("network_interface_id", networkInterfaceId) |
| 240 | + _ = d.Set("private_ip_addresses", ipv4s) |
| 241 | + _ = d.Set("secondary_private_ip_address_count", len(ipv4s)) |
| 242 | + if len(eni.PrivateIpAddressSet) > 0 { |
| 243 | + _ = d.Set("qos_level", eni.PrivateIpAddressSet[0].QosLevel) |
| 244 | + } |
| 245 | + |
| 246 | + return nil |
| 247 | +} |
| 248 | + |
| 249 | +func resourceTencentCloudEniIpv4AddressDelete(d *schema.ResourceData, meta interface{}) error { |
| 250 | + defer tccommon.LogElapsed("resource.tencentcloud_eni_ipv6_address.delete")() |
| 251 | + defer tccommon.InconsistentCheck(d, meta)() |
| 252 | + |
| 253 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 254 | + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 255 | + |
| 256 | + service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 257 | + networkInterfaceId := d.Id() |
| 258 | + |
| 259 | + enis, err := service.DescribeEniById(ctx, []string{networkInterfaceId}) |
| 260 | + |
| 261 | + if err != nil { |
| 262 | + return err |
| 263 | + } |
| 264 | + |
| 265 | + if len(enis) < 1 { |
| 266 | + d.SetId("") |
| 267 | + log.Printf("[WARN]%s resource `EniIpv4Address` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 268 | + return nil |
| 269 | + } |
| 270 | + |
| 271 | + eni := enis[0] |
| 272 | + ipv4s := make([]*string, 0, len(eni.PrivateIpAddressSet)) |
| 273 | + for _, ipv4 := range eni.PrivateIpAddressSet { |
| 274 | + if !*ipv4.Primary { |
| 275 | + ipv4s = append(ipv4s, ipv4.PrivateIpAddress) |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + if err := service.DeleteEniIpv4AddressById(ctx, networkInterfaceId, ipv4s); err != nil { |
| 280 | + return err |
| 281 | + } |
| 282 | + |
| 283 | + return nil |
| 284 | +} |
0 commit comments