github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/cloudstack/resource_cloudstack_security_group.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"github.com/xanzy/go-cloudstack/cloudstack"
    10  )
    11  
    12  func resourceCloudStackSecurityGroup() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceCloudStackSecurityGroupCreate,
    15  		Read:   resourceCloudStackSecurityGroupRead,
    16  		Delete: resourceCloudStackSecurityGroupDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"name": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"description": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Optional: true,
    28  				Computed: true,
    29  				ForceNew: true,
    30  			},
    31  
    32  			"project": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Optional: true,
    35  				Computed: true,
    36  				ForceNew: true,
    37  			},
    38  		},
    39  	}
    40  }
    41  
    42  func resourceCloudStackSecurityGroupCreate(d *schema.ResourceData, meta interface{}) error {
    43  	cs := meta.(*cloudstack.CloudStackClient)
    44  
    45  	name := d.Get("name").(string)
    46  
    47  	// Create a new parameter struct
    48  	p := cs.SecurityGroup.NewCreateSecurityGroupParams(name)
    49  
    50  	// Set the description
    51  	if description, ok := d.GetOk("description"); ok {
    52  		p.SetDescription(description.(string))
    53  	} else {
    54  		p.SetDescription(name)
    55  	}
    56  
    57  	// If there is a project supplied, we retrieve and set the project id
    58  	if err := setProjectid(p, cs, d); err != nil {
    59  		return err
    60  	}
    61  
    62  	r, err := cs.SecurityGroup.CreateSecurityGroup(p)
    63  	if err != nil {
    64  		return fmt.Errorf("Error creating security group %s: %s", name, err)
    65  	}
    66  
    67  	d.SetId(r.Id)
    68  
    69  	return resourceCloudStackSecurityGroupRead(d, meta)
    70  }
    71  
    72  func resourceCloudStackSecurityGroupRead(d *schema.ResourceData, meta interface{}) error {
    73  	cs := meta.(*cloudstack.CloudStackClient)
    74  
    75  	// Get the security group details
    76  	sg, count, err := cs.SecurityGroup.GetSecurityGroupByID(
    77  		d.Id(),
    78  		cloudstack.WithProject(d.Get("project").(string)),
    79  	)
    80  	if err != nil {
    81  		if count == 0 {
    82  			log.Printf("[DEBUG] Security group %s does not longer exist", d.Get("name").(string))
    83  			d.SetId("")
    84  			return nil
    85  		}
    86  
    87  		return err
    88  	}
    89  
    90  	// Update the config
    91  	d.Set("name", sg.Name)
    92  	d.Set("description", sg.Description)
    93  
    94  	setValueOrID(d, "project", sg.Project, sg.Projectid)
    95  
    96  	return nil
    97  }
    98  
    99  func resourceCloudStackSecurityGroupDelete(d *schema.ResourceData, meta interface{}) error {
   100  	cs := meta.(*cloudstack.CloudStackClient)
   101  
   102  	// Create a new parameter struct
   103  	p := cs.SecurityGroup.NewDeleteSecurityGroupParams()
   104  	p.SetId(d.Id())
   105  
   106  	// If there is a project supplied, we retrieve and set the project id
   107  	if err := setProjectid(p, cs, d); err != nil {
   108  		return err
   109  	}
   110  
   111  	// Delete the security group
   112  	_, err := cs.SecurityGroup.DeleteSecurityGroup(p)
   113  	if err != nil {
   114  		// This is a very poor way to be told the ID does no longer exist :(
   115  		if strings.Contains(err.Error(), fmt.Sprintf(
   116  			"Invalid parameter id value=%s due to incorrect long value format, "+
   117  				"or entity does not exist", d.Id())) {
   118  			return nil
   119  		}
   120  
   121  		return fmt.Errorf("Error deleting security group: %s", err)
   122  	}
   123  
   124  	return nil
   125  }