github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/alicloud/resource_alicloud_vswitch.go (about)

     1  package alicloud
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/denverdino/aliyungo/common"
     7  	"github.com/denverdino/aliyungo/ecs"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  	"log"
    11  	"time"
    12  )
    13  
    14  func resourceAliyunSubnet() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceAliyunSwitchCreate,
    17  		Read:   resourceAliyunSwitchRead,
    18  		Update: resourceAliyunSwitchUpdate,
    19  		Delete: resourceAliyunSwitchDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"availability_zone": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  			"vpc_id": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Required: true,
    30  				ForceNew: true,
    31  			},
    32  			"cidr_block": &schema.Schema{
    33  				Type:         schema.TypeString,
    34  				Required:     true,
    35  				ForceNew:     true,
    36  				ValidateFunc: validateSwitchCIDRNetworkAddress,
    37  			},
    38  			"name": &schema.Schema{
    39  				Type:     schema.TypeString,
    40  				Optional: true,
    41  			},
    42  			"description": &schema.Schema{
    43  				Type:     schema.TypeString,
    44  				Optional: true,
    45  			},
    46  		},
    47  	}
    48  }
    49  
    50  func resourceAliyunSwitchCreate(d *schema.ResourceData, meta interface{}) error {
    51  
    52  	conn := meta.(*AliyunClient).ecsconn
    53  
    54  	args, err := buildAliyunSwitchArgs(d, meta)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	vswitchID, err := conn.CreateVSwitch(args)
    60  	if err != nil {
    61  		return fmt.Errorf("Create subnet got a error :%s", err)
    62  	}
    63  
    64  	d.SetId(vswitchID)
    65  
    66  	err = conn.WaitForVSwitchAvailable(args.VpcId, vswitchID, 60)
    67  	if err != nil {
    68  		return fmt.Errorf("WaitForVSwitchAvailable got a error: %s", err)
    69  	}
    70  
    71  	return resourceAliyunSwitchRead(d, meta)
    72  }
    73  
    74  func resourceAliyunSwitchRead(d *schema.ResourceData, meta interface{}) error {
    75  
    76  	conn := meta.(*AliyunClient).ecsconn
    77  
    78  	args := &ecs.DescribeVSwitchesArgs{
    79  		VpcId:     d.Get("vpc_id").(string),
    80  		VSwitchId: d.Id(),
    81  	}
    82  
    83  	vswitches, _, err := conn.DescribeVSwitches(args)
    84  
    85  	if err != nil {
    86  		if notFoundError(err) {
    87  			d.SetId("")
    88  			return nil
    89  		}
    90  		return err
    91  	}
    92  
    93  	if len(vswitches) == 0 {
    94  		d.SetId("")
    95  		return nil
    96  	}
    97  
    98  	vswitch := vswitches[0]
    99  
   100  	d.Set("availability_zone", vswitch.ZoneId)
   101  	d.Set("vpc_id", vswitch.VpcId)
   102  	d.Set("cidr_block", vswitch.CidrBlock)
   103  	d.Set("name", vswitch.VSwitchName)
   104  	d.Set("description", vswitch.Description)
   105  
   106  	return nil
   107  }
   108  
   109  func resourceAliyunSwitchUpdate(d *schema.ResourceData, meta interface{}) error {
   110  
   111  	conn := meta.(*AliyunClient).ecsconn
   112  
   113  	d.Partial(true)
   114  
   115  	attributeUpdate := false
   116  	args := &ecs.ModifyVSwitchAttributeArgs{
   117  		VSwitchId: d.Id(),
   118  	}
   119  
   120  	if d.HasChange("name") {
   121  		d.SetPartial("name")
   122  		args.VSwitchName = d.Get("name").(string)
   123  
   124  		attributeUpdate = true
   125  	}
   126  
   127  	if d.HasChange("description") {
   128  		d.SetPartial("description")
   129  		args.Description = d.Get("description").(string)
   130  
   131  		attributeUpdate = true
   132  	}
   133  	if attributeUpdate {
   134  		if err := conn.ModifyVSwitchAttribute(args); err != nil {
   135  			return err
   136  		}
   137  
   138  	}
   139  
   140  	d.Partial(false)
   141  
   142  	return nil
   143  }
   144  
   145  func resourceAliyunSwitchDelete(d *schema.ResourceData, meta interface{}) error {
   146  	conn := meta.(*AliyunClient).ecsconn
   147  
   148  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   149  		err := conn.DeleteVSwitch(d.Id())
   150  
   151  		if err != nil {
   152  			e, _ := err.(*common.Error)
   153  			if e.ErrorResponse.Code == VswitcInvalidRegionId {
   154  				log.Printf("[ERROR] Delete Switch is failed.")
   155  				return resource.NonRetryableError(err)
   156  			}
   157  
   158  			return resource.RetryableError(fmt.Errorf("Switch in use. -- trying again while it is deleted."))
   159  		}
   160  
   161  		vsw, _, vswErr := conn.DescribeVSwitches(&ecs.DescribeVSwitchesArgs{
   162  			VpcId:     d.Get("vpc_id").(string),
   163  			VSwitchId: d.Id(),
   164  		})
   165  
   166  		if vswErr != nil {
   167  			return resource.NonRetryableError(vswErr)
   168  		} else if vsw == nil || len(vsw) < 1 {
   169  			return nil
   170  		}
   171  
   172  		return resource.RetryableError(fmt.Errorf("Switch in use. -- trying again while it is deleted."))
   173  	})
   174  }
   175  
   176  func buildAliyunSwitchArgs(d *schema.ResourceData, meta interface{}) (*ecs.CreateVSwitchArgs, error) {
   177  
   178  	client := meta.(*AliyunClient)
   179  
   180  	vpcID := d.Get("vpc_id").(string)
   181  
   182  	vpc, err := client.DescribeVpc(vpcID)
   183  	if err != nil {
   184  		return nil, err
   185  	}
   186  
   187  	if vpc == nil {
   188  		return nil, fmt.Errorf("vpc_id not found")
   189  	}
   190  
   191  	zoneID := d.Get("availability_zone").(string)
   192  
   193  	zone, err := client.DescribeZone(zoneID)
   194  	if err != nil {
   195  		return nil, err
   196  	}
   197  
   198  	err = client.ResourceAvailable(zone, ecs.ResourceTypeVSwitch)
   199  	if err != nil {
   200  		return nil, err
   201  	}
   202  
   203  	cidrBlock := d.Get("cidr_block").(string)
   204  
   205  	args := &ecs.CreateVSwitchArgs{
   206  		VpcId:     vpcID,
   207  		ZoneId:    zoneID,
   208  		CidrBlock: cidrBlock,
   209  	}
   210  
   211  	if v, ok := d.GetOk("name"); ok && v != "" {
   212  		args.VSwitchName = v.(string)
   213  	}
   214  
   215  	if v, ok := d.GetOk("description"); ok && v != "" {
   216  		args.Description = v.(string)
   217  	}
   218  
   219  	return args, nil
   220  }