github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/resource_alicloud_eip.go (about) 1 package alicloud 2 3 import ( 4 "strconv" 5 6 "fmt" 7 "github.com/denverdino/aliyungo/common" 8 "github.com/denverdino/aliyungo/ecs" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/helper/schema" 11 "time" 12 ) 13 14 func resourceAliyunEip() *schema.Resource { 15 return &schema.Resource{ 16 Create: resourceAliyunEipCreate, 17 Read: resourceAliyunEipRead, 18 Update: resourceAliyunEipUpdate, 19 Delete: resourceAliyunEipDelete, 20 21 Schema: map[string]*schema.Schema{ 22 "bandwidth": &schema.Schema{ 23 Type: schema.TypeInt, 24 Optional: true, 25 Default: 5, 26 }, 27 "internet_charge_type": &schema.Schema{ 28 Type: schema.TypeString, 29 Default: "PayByBandwidth", 30 Optional: true, 31 ForceNew: true, 32 ValidateFunc: validateInternetChargeType, 33 }, 34 35 "ip_address": &schema.Schema{ 36 Type: schema.TypeString, 37 Computed: true, 38 }, 39 40 "status": &schema.Schema{ 41 Type: schema.TypeString, 42 Computed: true, 43 }, 44 45 "instance": &schema.Schema{ 46 Type: schema.TypeString, 47 Optional: true, 48 Computed: true, 49 }, 50 }, 51 } 52 } 53 54 func resourceAliyunEipCreate(d *schema.ResourceData, meta interface{}) error { 55 conn := meta.(*AliyunClient).ecsconn 56 57 args, err := buildAliyunEipArgs(d, meta) 58 if err != nil { 59 return err 60 } 61 62 _, allocationID, err := conn.AllocateEipAddress(args) 63 if err != nil { 64 return err 65 } 66 67 d.SetId(allocationID) 68 69 return resourceAliyunEipRead(d, meta) 70 } 71 72 func resourceAliyunEipRead(d *schema.ResourceData, meta interface{}) error { 73 client := meta.(*AliyunClient) 74 75 eip, err := client.DescribeEipAddress(d.Id()) 76 if err != nil { 77 if notFoundError(err) { 78 d.SetId("") 79 return nil 80 } 81 return fmt.Errorf("Error Describe Eip Attribute: %#v", err) 82 } 83 84 if eip.InstanceId != "" { 85 d.Set("instance", eip.InstanceId) 86 } else { 87 d.Set("instance", "") 88 return nil 89 } 90 91 bandwidth, _ := strconv.Atoi(eip.Bandwidth) 92 d.Set("bandwidth", bandwidth) 93 d.Set("internet_charge_type", eip.InternetChargeType) 94 d.Set("ip_address", eip.IpAddress) 95 d.Set("status", eip.Status) 96 97 return nil 98 } 99 100 func resourceAliyunEipUpdate(d *schema.ResourceData, meta interface{}) error { 101 102 conn := meta.(*AliyunClient).ecsconn 103 104 d.Partial(true) 105 106 if d.HasChange("bandwidth") { 107 err := conn.ModifyEipAddressAttribute(d.Id(), d.Get("bandwidth").(int)) 108 if err != nil { 109 return err 110 } 111 112 d.SetPartial("bandwidth") 113 } 114 115 d.Partial(false) 116 117 return nil 118 } 119 120 func resourceAliyunEipDelete(d *schema.ResourceData, meta interface{}) error { 121 conn := meta.(*AliyunClient).ecsconn 122 123 return resource.Retry(5*time.Minute, func() *resource.RetryError { 124 err := conn.ReleaseEipAddress(d.Id()) 125 126 if err != nil { 127 e, _ := err.(*common.Error) 128 if e.ErrorResponse.Code == EipIncorrectStatus { 129 return resource.RetryableError(fmt.Errorf("EIP in use - trying again while it is deleted.")) 130 } 131 } 132 133 args := &ecs.DescribeEipAddressesArgs{ 134 RegionId: getRegion(d, meta), 135 AllocationId: d.Id(), 136 } 137 138 eips, _, descErr := conn.DescribeEipAddresses(args) 139 if descErr != nil { 140 return resource.NonRetryableError(descErr) 141 } else if eips == nil || len(eips) < 1 { 142 return nil 143 } 144 return resource.RetryableError(fmt.Errorf("EIP in use - trying again while it is deleted.")) 145 }) 146 } 147 148 func buildAliyunEipArgs(d *schema.ResourceData, meta interface{}) (*ecs.AllocateEipAddressArgs, error) { 149 150 args := &ecs.AllocateEipAddressArgs{ 151 RegionId: getRegion(d, meta), 152 Bandwidth: d.Get("bandwidth").(int), 153 InternetChargeType: common.InternetChargeType(d.Get("internet_charge_type").(string)), 154 } 155 156 return args, nil 157 }