github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 err
    82  	}
    83  
    84  	bandwidth, _ := strconv.Atoi(eip.Bandwidth)
    85  	d.Set("bandwidth", bandwidth)
    86  	d.Set("internet_charge_type", eip.InternetChargeType)
    87  	d.Set("ip_address", eip.IpAddress)
    88  	d.Set("status", eip.Status)
    89  
    90  	if eip.InstanceId != "" {
    91  		d.Set("instance", eip.InstanceId)
    92  	} else {
    93  		d.Set("instance", "")
    94  	}
    95  
    96  	return nil
    97  }
    98  
    99  func resourceAliyunEipUpdate(d *schema.ResourceData, meta interface{}) error {
   100  
   101  	conn := meta.(*AliyunClient).ecsconn
   102  
   103  	d.Partial(true)
   104  
   105  	if d.HasChange("bandwidth") {
   106  		err := conn.ModifyEipAddressAttribute(d.Id(), d.Get("bandwidth").(int))
   107  		if err != nil {
   108  			return err
   109  		}
   110  
   111  		d.SetPartial("bandwidth")
   112  	}
   113  
   114  	d.Partial(false)
   115  
   116  	return nil
   117  }
   118  
   119  func resourceAliyunEipDelete(d *schema.ResourceData, meta interface{}) error {
   120  	conn := meta.(*AliyunClient).ecsconn
   121  
   122  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   123  		err := conn.ReleaseEipAddress(d.Id())
   124  
   125  		if err != nil {
   126  			e, _ := err.(*common.Error)
   127  			if e.ErrorResponse.Code == EipIncorrectStatus {
   128  				return resource.RetryableError(fmt.Errorf("EIP in use - trying again while it is deleted."))
   129  			}
   130  		}
   131  
   132  		args := &ecs.DescribeEipAddressesArgs{
   133  			RegionId:     getRegion(d, meta),
   134  			AllocationId: d.Id(),
   135  		}
   136  
   137  		eips, _, descErr := conn.DescribeEipAddresses(args)
   138  		if descErr != nil {
   139  			return resource.NonRetryableError(descErr)
   140  		} else if eips == nil || len(eips) < 1 {
   141  			return nil
   142  		}
   143  		return resource.RetryableError(fmt.Errorf("EIP in use - trying again while it is deleted."))
   144  	})
   145  }
   146  
   147  func buildAliyunEipArgs(d *schema.ResourceData, meta interface{}) (*ecs.AllocateEipAddressArgs, error) {
   148  
   149  	args := &ecs.AllocateEipAddressArgs{
   150  		RegionId:           getRegion(d, meta),
   151  		Bandwidth:          d.Get("bandwidth").(int),
   152  		InternetChargeType: common.InternetChargeType(d.Get("internet_charge_type").(string)),
   153  	}
   154  
   155  	return args, nil
   156  }