|
| 1 | +/* |
| 2 | +Provides a resource to create a ipv6_address_bandwidth |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_ipv6_address_bandwidth" "ipv6_address_bandwidth" { |
| 8 | + ipv6_address = "2402:4e00:1019:9400:0:9905:a90b:2ef0" |
| 9 | + internet_max_bandwidth_out = 6 |
| 10 | + internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR" |
| 11 | +# bandwidth_package_id = "bwp-34rfgt56" |
| 12 | +} |
| 13 | +``` |
| 14 | +
|
| 15 | +*/ |
| 16 | +package tencentcloud |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "log" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 25 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 26 | + vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" |
| 27 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 28 | +) |
| 29 | + |
| 30 | +func resourceTencentCloudIpv6AddressBandwidth() *schema.Resource { |
| 31 | + return &schema.Resource{ |
| 32 | + Create: resourceTencentCloudIpv6AddressBandwidthCreate, |
| 33 | + Read: resourceTencentCloudIpv6AddressBandwidthRead, |
| 34 | + Update: resourceTencentCloudIpv6AddressBandwidthUpdate, |
| 35 | + Delete: resourceTencentCloudIpv6AddressBandwidthDelete, |
| 36 | + // it can support import because |
| 37 | + //Importer: &schema.ResourceImporter{ |
| 38 | + // State: schema.ImportStatePassthrough, |
| 39 | + //}, |
| 40 | + Schema: map[string]*schema.Schema{ |
| 41 | + "ipv6_address": { |
| 42 | + Required: true, |
| 43 | + Type: schema.TypeString, |
| 44 | + ForceNew: true, |
| 45 | + Description: "IPV6 address that needs to be enabled for public network access.", |
| 46 | + }, |
| 47 | + |
| 48 | + "internet_max_bandwidth_out": { |
| 49 | + Optional: true, |
| 50 | + Type: schema.TypeInt, |
| 51 | + Default: 1, |
| 52 | + Description: "Bandwidth, in Mbps. The default is 1Mbps.", |
| 53 | + }, |
| 54 | + |
| 55 | + "internet_charge_type": { |
| 56 | + Optional: true, |
| 57 | + Type: schema.TypeString, |
| 58 | + Description: "Network billing mode. IPV6 currently supports: `TRAFFIC_POSTPAID_BY_HOUR`, for standard account types; `BANDWIDTH_PACKAGE`, for traditional account types. The default network billing mode is: `TRAFFIC_POSTPAID_BY_HOUR`.", |
| 59 | + }, |
| 60 | + |
| 61 | + "bandwidth_package_id": { |
| 62 | + Optional: true, |
| 63 | + Type: schema.TypeString, |
| 64 | + Description: "The bandwidth package id, the Legacy account and the ipv6 address to apply for the bandwidth package charge type need to be passed in.", |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func resourceTencentCloudIpv6AddressBandwidthCreate(d *schema.ResourceData, meta interface{}) error { |
| 71 | + defer logElapsed("resource.tencentcloud_ipv6_address_bandwidth.create")() |
| 72 | + defer inconsistentCheck(d, meta)() |
| 73 | + |
| 74 | + logId := getLogId(contextNil) |
| 75 | + |
| 76 | + var ( |
| 77 | + request = vpc.NewAllocateIp6AddressesBandwidthRequest() |
| 78 | + response = vpc.NewAllocateIp6AddressesBandwidthResponse() |
| 79 | + ip6AddressId string |
| 80 | + ) |
| 81 | + if v, ok := d.GetOk("ipv6_address"); ok { |
| 82 | + ip6AddressId := helper.String(v.(string)) |
| 83 | + request.Ip6Addresses = []*string{ip6AddressId} |
| 84 | + } |
| 85 | + |
| 86 | + if v, ok := d.GetOkExists("internet_max_bandwidth_out"); ok { |
| 87 | + request.InternetMaxBandwidthOut = helper.IntInt64(v.(int)) |
| 88 | + } |
| 89 | + |
| 90 | + if v, ok := d.GetOk("internet_charge_type"); ok { |
| 91 | + request.InternetChargeType = helper.String(v.(string)) |
| 92 | + } |
| 93 | + |
| 94 | + if v, ok := d.GetOk("bandwidth_package_id"); ok { |
| 95 | + request.BandwidthPackageId = helper.String(v.(string)) |
| 96 | + } |
| 97 | + |
| 98 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 99 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().AllocateIp6AddressesBandwidth(request) |
| 100 | + if e != nil { |
| 101 | + return retryError(e) |
| 102 | + } else { |
| 103 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 104 | + } |
| 105 | + response = result |
| 106 | + return nil |
| 107 | + }) |
| 108 | + if err != nil { |
| 109 | + log.Printf("[CRITAL]%s create vpc ipv6Address failed, reason:%+v", logId, err) |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + taskId := *response.Response.TaskId |
| 114 | + d.SetId(ip6AddressId) |
| 115 | + |
| 116 | + ip6AddressId = *response.Response.AddressSet[0] |
| 117 | + d.SetId(ip6AddressId) |
| 118 | + |
| 119 | + service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 120 | + |
| 121 | + conf := BuildStateChangeConf([]string{}, []string{"SUCCESS"}, 1*readRetryTimeout, time.Second, service.VpcIpv6AddressStateRefreshFunc(taskId, []string{})) |
| 122 | + |
| 123 | + if _, e := conf.WaitForState(); e != nil { |
| 124 | + return e |
| 125 | + } |
| 126 | + |
| 127 | + return resourceTencentCloudIpv6AddressBandwidthRead(d, meta) |
| 128 | +} |
| 129 | + |
| 130 | +func resourceTencentCloudIpv6AddressBandwidthRead(d *schema.ResourceData, meta interface{}) error { |
| 131 | + defer logElapsed("resource.tencentcloud_ipv6_address_bandwidth.read")() |
| 132 | + defer inconsistentCheck(d, meta)() |
| 133 | + |
| 134 | + logId := getLogId(contextNil) |
| 135 | + |
| 136 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 137 | + |
| 138 | + service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 139 | + |
| 140 | + ipv6AddressId := d.Id() |
| 141 | + |
| 142 | + ipv6Address, err := service.DescribeVpcIpv6AddressById(ctx, ipv6AddressId) |
| 143 | + if err != nil { |
| 144 | + return err |
| 145 | + } |
| 146 | + |
| 147 | + if ipv6Address == nil { |
| 148 | + d.SetId("") |
| 149 | + log.Printf("[WARN]%s resource `VpcIpv6Address` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 150 | + return nil |
| 151 | + } |
| 152 | + |
| 153 | + _ = d.Set("ipv6_address", ipv6Address.AddressIp) |
| 154 | + |
| 155 | + if ipv6Address.Bandwidth != nil { |
| 156 | + _ = d.Set("internet_max_bandwidth_out", ipv6Address.Bandwidth) |
| 157 | + } |
| 158 | + |
| 159 | + if ipv6Address.InternetChargeType != nil { |
| 160 | + _ = d.Set("internet_charge_type", ipv6Address.InternetChargeType) |
| 161 | + } |
| 162 | + |
| 163 | + //if ipv6Address.BandwidthPackageId != nil { |
| 164 | + // _ = d.Set("bandwidth_package_id", ipv6Address.BandwidthPackageId) |
| 165 | + //} |
| 166 | + |
| 167 | + return nil |
| 168 | +} |
| 169 | + |
| 170 | +func resourceTencentCloudIpv6AddressBandwidthUpdate(d *schema.ResourceData, meta interface{}) error { |
| 171 | + defer logElapsed("resource.tencentcloud_ipv6_address_bandwidth.update")() |
| 172 | + defer inconsistentCheck(d, meta)() |
| 173 | + |
| 174 | + logId := getLogId(contextNil) |
| 175 | + |
| 176 | + request := vpc.NewModifyIp6AddressesBandwidthRequest() |
| 177 | + |
| 178 | + ipv6AddressId := d.Id() |
| 179 | + |
| 180 | + request.Ip6AddressIds = []*string{&ipv6AddressId} |
| 181 | + |
| 182 | + immutableArgs := []string{"internet_charge_type", "bandwidth_package_id"} |
| 183 | + |
| 184 | + for _, v := range immutableArgs { |
| 185 | + if d.HasChange(v) { |
| 186 | + return fmt.Errorf("argument `%s` cannot be changed", v) |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + needChange := false |
| 191 | + |
| 192 | + if d.HasChange("internet_max_bandwidth_out") { |
| 193 | + needChange = true |
| 194 | + if v, ok := d.GetOkExists("internet_max_bandwidth_out"); ok { |
| 195 | + request.InternetMaxBandwidthOut = helper.IntInt64(v.(int)) |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + if needChange { |
| 200 | + var taskId string |
| 201 | + |
| 202 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 203 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().ModifyIp6AddressesBandwidth(request) |
| 204 | + if e != nil { |
| 205 | + return retryError(e) |
| 206 | + } else { |
| 207 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 208 | + } |
| 209 | + taskId = *result.Response.TaskId |
| 210 | + return nil |
| 211 | + }) |
| 212 | + if err != nil { |
| 213 | + log.Printf("[CRITAL]%s update vpc ipv6Address failed, reason:%+v", logId, err) |
| 214 | + return err |
| 215 | + } |
| 216 | + |
| 217 | + service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 218 | + |
| 219 | + conf := BuildStateChangeConf([]string{}, []string{"SUCCESS"}, 1*readRetryTimeout, time.Second, service.VpcIpv6AddressStateRefreshFunc(taskId, []string{})) |
| 220 | + |
| 221 | + if _, e := conf.WaitForState(); e != nil { |
| 222 | + return e |
| 223 | + } |
| 224 | + |
| 225 | + } |
| 226 | + |
| 227 | + return resourceTencentCloudIpv6AddressBandwidthRead(d, meta) |
| 228 | +} |
| 229 | + |
| 230 | +func resourceTencentCloudIpv6AddressBandwidthDelete(d *schema.ResourceData, meta interface{}) error { |
| 231 | + defer logElapsed("resource.tencentcloud_ipv6_address_bandwidth.delete")() |
| 232 | + defer inconsistentCheck(d, meta)() |
| 233 | + |
| 234 | + logId := getLogId(contextNil) |
| 235 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 236 | + |
| 237 | + service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 238 | + ipv6AddressId := d.Id() |
| 239 | + |
| 240 | + if err := service.DeleteVpcIpv6AddressById(ctx, ipv6AddressId); err != nil { |
| 241 | + return err |
| 242 | + } |
| 243 | + |
| 244 | + return nil |
| 245 | +} |
0 commit comments