2017-01-19 15:08:56 +01:00
|
|
|
package alicloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/denverdino/aliyungo/common"
|
2017-03-24 12:04:56 +01:00
|
|
|
"github.com/denverdino/aliyungo/ecs"
|
2017-01-19 15:08:56 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAliyunNatGateway() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAliyunNatGatewayCreate,
|
|
|
|
Read: resourceAliyunNatGatewayRead,
|
|
|
|
Update: resourceAliyunNatGatewayUpdate,
|
|
|
|
Delete: resourceAliyunNatGatewayDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"vpc_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"spec": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"bandwidth_package_ids": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"bandwidth_packages": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"ip_count": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"bandwidth": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"zone": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Required: true,
|
|
|
|
MaxItems: 4,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAliyunNatGatewayCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AliyunClient).vpcconn
|
|
|
|
|
2017-03-24 12:04:56 +01:00
|
|
|
args := &ecs.CreateNatGatewayArgs{
|
2017-01-19 15:08:56 +01:00
|
|
|
RegionId: getRegion(d, meta),
|
|
|
|
VpcId: d.Get("vpc_id").(string),
|
|
|
|
Spec: d.Get("spec").(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
bandwidthPackages := d.Get("bandwidth_packages").([]interface{})
|
|
|
|
|
2017-03-24 12:04:56 +01:00
|
|
|
bandwidthPackageTypes := []ecs.BandwidthPackageType{}
|
2017-01-19 15:08:56 +01:00
|
|
|
|
|
|
|
for _, e := range bandwidthPackages {
|
|
|
|
pack := e.(map[string]interface{})
|
2017-03-24 12:04:56 +01:00
|
|
|
bandwidthPackage := ecs.BandwidthPackageType{
|
2017-01-19 15:08:56 +01:00
|
|
|
IpCount: pack["ip_count"].(int),
|
|
|
|
Bandwidth: pack["bandwidth"].(int),
|
|
|
|
}
|
|
|
|
if pack["zone"].(string) != "" {
|
|
|
|
bandwidthPackage.Zone = pack["zone"].(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
bandwidthPackageTypes = append(bandwidthPackageTypes, bandwidthPackage)
|
|
|
|
}
|
|
|
|
|
|
|
|
args.BandwidthPackage = bandwidthPackageTypes
|
|
|
|
|
|
|
|
var name string
|
|
|
|
if v, ok := d.GetOk("name"); ok {
|
|
|
|
name = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
args.Name = name
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("description"); ok {
|
|
|
|
args.Description = v.(string)
|
|
|
|
}
|
2017-03-24 12:04:56 +01:00
|
|
|
resp, err := conn.CreateNatGateway(args)
|
2017-01-19 15:08:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CreateNatGateway got error: %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(resp.NatGatewayId)
|
|
|
|
|
|
|
|
return resourceAliyunNatGatewayRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAliyunNatGatewayRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
|
|
|
|
client := meta.(*AliyunClient)
|
|
|
|
|
|
|
|
natGateway, err := client.DescribeNatGateway(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
if notFoundError(err) {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("name", natGateway.Name)
|
|
|
|
d.Set("spec", natGateway.Spec)
|
|
|
|
d.Set("bandwidth_package_ids", strings.Join(natGateway.BandwidthPackageIds.BandwidthPackageId, ","))
|
|
|
|
d.Set("description", natGateway.Description)
|
|
|
|
d.Set("vpc_id", natGateway.VpcId)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAliyunNatGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
|
|
|
|
client := meta.(*AliyunClient)
|
2017-03-24 12:04:56 +01:00
|
|
|
conn := client.vpcconn
|
2017-01-19 15:08:56 +01:00
|
|
|
|
|
|
|
natGateway, err := client.DescribeNatGateway(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Partial(true)
|
|
|
|
attributeUpdate := false
|
2017-03-24 12:04:56 +01:00
|
|
|
args := &ecs.ModifyNatGatewayAttributeArgs{
|
2017-01-19 15:08:56 +01:00
|
|
|
RegionId: natGateway.RegionId,
|
|
|
|
NatGatewayId: natGateway.NatGatewayId,
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("name") {
|
|
|
|
d.SetPartial("name")
|
|
|
|
var name string
|
|
|
|
if v, ok := d.GetOk("name"); ok {
|
|
|
|
name = v.(string)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("cann't change name to empty string")
|
|
|
|
}
|
|
|
|
args.Name = name
|
|
|
|
|
|
|
|
attributeUpdate = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("description") {
|
|
|
|
d.SetPartial("description")
|
|
|
|
var description string
|
|
|
|
if v, ok := d.GetOk("description"); ok {
|
|
|
|
description = v.(string)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("can to change description to empty string")
|
|
|
|
}
|
|
|
|
|
|
|
|
args.Description = description
|
|
|
|
|
|
|
|
attributeUpdate = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if attributeUpdate {
|
2017-03-24 12:04:56 +01:00
|
|
|
if err := conn.ModifyNatGatewayAttribute(args); err != nil {
|
2017-01-19 15:08:56 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("spec") {
|
|
|
|
d.SetPartial("spec")
|
2017-03-24 12:04:56 +01:00
|
|
|
var spec ecs.NatGatewaySpec
|
2017-01-19 15:08:56 +01:00
|
|
|
if v, ok := d.GetOk("spec"); ok {
|
2017-03-24 12:04:56 +01:00
|
|
|
spec = ecs.NatGatewaySpec(v.(string))
|
2017-01-19 15:08:56 +01:00
|
|
|
} else {
|
|
|
|
// set default to small spec
|
2017-03-24 12:04:56 +01:00
|
|
|
spec = ecs.NatGatewaySmallSpec
|
2017-01-19 15:08:56 +01:00
|
|
|
}
|
|
|
|
|
2017-03-24 12:04:56 +01:00
|
|
|
args := &ecs.ModifyNatGatewaySpecArgs{
|
2017-01-19 15:08:56 +01:00
|
|
|
RegionId: natGateway.RegionId,
|
|
|
|
NatGatewayId: natGateway.NatGatewayId,
|
|
|
|
Spec: spec,
|
|
|
|
}
|
|
|
|
|
2017-03-24 12:04:56 +01:00
|
|
|
err := conn.ModifyNatGatewaySpec(args)
|
2017-01-19 15:08:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%#v %#v", err, *args)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
d.Partial(false)
|
|
|
|
|
|
|
|
return resourceAliyunNatGatewayRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAliyunNatGatewayDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
|
|
|
|
client := meta.(*AliyunClient)
|
2017-03-24 12:04:56 +01:00
|
|
|
conn := client.vpcconn
|
2017-01-19 15:08:56 +01:00
|
|
|
|
|
|
|
return resource.Retry(5*time.Minute, func() *resource.RetryError {
|
|
|
|
|
2017-03-24 12:04:56 +01:00
|
|
|
packages, err := conn.DescribeBandwidthPackages(&ecs.DescribeBandwidthPackagesArgs{
|
2017-01-19 15:08:56 +01:00
|
|
|
RegionId: getRegion(d, meta),
|
|
|
|
NatGatewayId: d.Id(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] Describe bandwidth package is failed, natGateway Id: %s", d.Id())
|
|
|
|
return resource.NonRetryableError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
retry := false
|
|
|
|
for _, pack := range packages {
|
2017-03-24 12:04:56 +01:00
|
|
|
err = conn.DeleteBandwidthPackage(&ecs.DeleteBandwidthPackageArgs{
|
2017-01-19 15:08:56 +01:00
|
|
|
RegionId: getRegion(d, meta),
|
|
|
|
BandwidthPackageId: pack.BandwidthPackageId,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
er, _ := err.(*common.Error)
|
|
|
|
if er.ErrorResponse.Code == NatGatewayInvalidRegionId {
|
|
|
|
log.Printf("[ERROR] Delete bandwidth package is failed, bandwidthPackageId: %#v", pack.BandwidthPackageId)
|
|
|
|
return resource.NonRetryableError(err)
|
|
|
|
}
|
|
|
|
retry = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if retry {
|
|
|
|
return resource.RetryableError(fmt.Errorf("Bandwidth package in use - trying again while it is deleted."))
|
|
|
|
}
|
|
|
|
|
2017-03-24 12:04:56 +01:00
|
|
|
args := &ecs.DeleteNatGatewayArgs{
|
2017-01-19 15:08:56 +01:00
|
|
|
RegionId: client.Region,
|
|
|
|
NatGatewayId: d.Id(),
|
|
|
|
}
|
|
|
|
|
2017-03-24 12:04:56 +01:00
|
|
|
err = conn.DeleteNatGateway(args)
|
2017-01-19 15:08:56 +01:00
|
|
|
if err != nil {
|
|
|
|
er, _ := err.(*common.Error)
|
|
|
|
if er.ErrorResponse.Code == DependencyViolationBandwidthPackages {
|
|
|
|
return resource.RetryableError(fmt.Errorf("NatGateway in use - trying again while it is deleted."))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 12:04:56 +01:00
|
|
|
describeArgs := &ecs.DescribeNatGatewaysArgs{
|
2017-01-19 15:08:56 +01:00
|
|
|
RegionId: client.Region,
|
|
|
|
NatGatewayId: d.Id(),
|
|
|
|
}
|
2017-03-24 12:04:56 +01:00
|
|
|
gw, _, gwErr := conn.DescribeNatGateways(describeArgs)
|
2017-01-19 15:08:56 +01:00
|
|
|
|
|
|
|
if gwErr != nil {
|
|
|
|
log.Printf("[ERROR] Describe NatGateways failed.")
|
|
|
|
return resource.NonRetryableError(gwErr)
|
|
|
|
} else if gw == nil || len(gw) < 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource.RetryableError(fmt.Errorf("NatGateway in use - trying again while it is deleted."))
|
|
|
|
})
|
|
|
|
}
|