github.com/dougneal/terraform@v0.6.15-0.20170330092735-b6a3840768a4/builtin/providers/alicloud/resource_alicloud_security_group.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  	"time"
    11  )
    12  
    13  func resourceAliyunSecurityGroup() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceAliyunSecurityGroupCreate,
    16  		Read:   resourceAliyunSecurityGroupRead,
    17  		Update: resourceAliyunSecurityGroupUpdate,
    18  		Delete: resourceAliyunSecurityGroupDelete,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"name": &schema.Schema{
    22  				Type:         schema.TypeString,
    23  				Optional:     true,
    24  				ValidateFunc: validateSecurityGroupName,
    25  			},
    26  
    27  			"description": &schema.Schema{
    28  				Type:         schema.TypeString,
    29  				Optional:     true,
    30  				ValidateFunc: validateSecurityGroupDescription,
    31  			},
    32  
    33  			"vpc_id": &schema.Schema{
    34  				Type:     schema.TypeString,
    35  				Optional: true,
    36  				ForceNew: true,
    37  			},
    38  		},
    39  	}
    40  }
    41  
    42  func resourceAliyunSecurityGroupCreate(d *schema.ResourceData, meta interface{}) error {
    43  	conn := meta.(*AliyunClient).ecsconn
    44  
    45  	args, err := buildAliyunSecurityGroupArgs(d, meta)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	securityGroupID, err := conn.CreateSecurityGroup(args)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	d.SetId(securityGroupID)
    56  
    57  	return resourceAliyunSecurityGroupRead(d, meta)
    58  }
    59  
    60  func resourceAliyunSecurityGroupRead(d *schema.ResourceData, meta interface{}) error {
    61  	conn := meta.(*AliyunClient).ecsconn
    62  
    63  	args := &ecs.DescribeSecurityGroupAttributeArgs{
    64  		SecurityGroupId: d.Id(),
    65  		RegionId:        getRegion(d, meta),
    66  	}
    67  
    68  	sg, err := conn.DescribeSecurityGroupAttribute(args)
    69  	if err != nil {
    70  		if notFoundError(err) {
    71  			d.SetId("")
    72  			return nil
    73  		}
    74  		return fmt.Errorf("Error DescribeSecurityGroupAttribute: %#v", err)
    75  	}
    76  
    77  	d.Set("name", sg.SecurityGroupName)
    78  	d.Set("description", sg.Description)
    79  
    80  	return nil
    81  }
    82  
    83  func resourceAliyunSecurityGroupUpdate(d *schema.ResourceData, meta interface{}) error {
    84  
    85  	conn := meta.(*AliyunClient).ecsconn
    86  
    87  	d.Partial(true)
    88  	attributeUpdate := false
    89  	args := &ecs.ModifySecurityGroupAttributeArgs{
    90  		SecurityGroupId: d.Id(),
    91  		RegionId:        getRegion(d, meta),
    92  	}
    93  
    94  	if d.HasChange("name") {
    95  		d.SetPartial("name")
    96  		args.SecurityGroupName = d.Get("name").(string)
    97  
    98  		attributeUpdate = true
    99  	}
   100  
   101  	if d.HasChange("description") {
   102  		d.SetPartial("description")
   103  		args.Description = d.Get("description").(string)
   104  
   105  		attributeUpdate = true
   106  	}
   107  	if attributeUpdate {
   108  		if err := conn.ModifySecurityGroupAttribute(args); err != nil {
   109  			return err
   110  		}
   111  	}
   112  
   113  	return nil
   114  }
   115  
   116  func resourceAliyunSecurityGroupDelete(d *schema.ResourceData, meta interface{}) error {
   117  
   118  	conn := meta.(*AliyunClient).ecsconn
   119  
   120  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   121  		err := conn.DeleteSecurityGroup(getRegion(d, meta), d.Id())
   122  
   123  		if err != nil {
   124  			e, _ := err.(*common.Error)
   125  			if e.ErrorResponse.Code == SgDependencyViolation {
   126  				return resource.RetryableError(fmt.Errorf("Security group in use - trying again while it is deleted."))
   127  			}
   128  		}
   129  
   130  		sg, err := conn.DescribeSecurityGroupAttribute(&ecs.DescribeSecurityGroupAttributeArgs{
   131  			RegionId:        getRegion(d, meta),
   132  			SecurityGroupId: d.Id(),
   133  		})
   134  
   135  		if err != nil {
   136  			e, _ := err.(*common.Error)
   137  			if e.ErrorResponse.Code == InvalidSecurityGroupIdNotFound {
   138  				return nil
   139  			}
   140  			return resource.NonRetryableError(err)
   141  		} else if sg == nil {
   142  			return nil
   143  		}
   144  
   145  		return resource.RetryableError(fmt.Errorf("Security group in use - trying again while it is deleted."))
   146  	})
   147  
   148  }
   149  
   150  func buildAliyunSecurityGroupArgs(d *schema.ResourceData, meta interface{}) (*ecs.CreateSecurityGroupArgs, error) {
   151  
   152  	args := &ecs.CreateSecurityGroupArgs{
   153  		RegionId: getRegion(d, meta),
   154  	}
   155  
   156  	if v := d.Get("name").(string); v != "" {
   157  		args.SecurityGroupName = v
   158  	}
   159  
   160  	if v := d.Get("description").(string); v != "" {
   161  		args.Description = v
   162  	}
   163  
   164  	if v := d.Get("vpc_id").(string); v != "" {
   165  		args.VpcId = v
   166  	}
   167  
   168  	return args, nil
   169  }