github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/cloudstack/resource_cloudstack_network_acl.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 resourceCloudStackNetworkACL() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceCloudStackNetworkACLCreate,
    15  		Read:   resourceCloudStackNetworkACLRead,
    16  		Delete: resourceCloudStackNetworkACLDelete,
    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  			"vpc": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Required: true,
    35  				ForceNew: true,
    36  			},
    37  		},
    38  	}
    39  }
    40  
    41  func resourceCloudStackNetworkACLCreate(d *schema.ResourceData, meta interface{}) error {
    42  	cs := meta.(*cloudstack.CloudStackClient)
    43  
    44  	name := d.Get("name").(string)
    45  
    46  	// Retrieve the vpc ID
    47  	vpcid, e := retrieveID(cs, "vpc", d.Get("vpc").(string))
    48  	if e != nil {
    49  		return e.Error()
    50  	}
    51  
    52  	// Create a new parameter struct
    53  	p := cs.NetworkACL.NewCreateNetworkACLListParams(name, vpcid)
    54  
    55  	// Set the description
    56  	if description, ok := d.GetOk("description"); ok {
    57  		p.SetDescription(description.(string))
    58  	} else {
    59  		p.SetDescription(name)
    60  	}
    61  
    62  	// Create the new network ACL list
    63  	r, err := cs.NetworkACL.CreateNetworkACLList(p)
    64  	if err != nil {
    65  		return fmt.Errorf("Error creating network ACL list %s: %s", name, err)
    66  	}
    67  
    68  	d.SetId(r.Id)
    69  
    70  	return resourceCloudStackNetworkACLRead(d, meta)
    71  }
    72  
    73  func resourceCloudStackNetworkACLRead(d *schema.ResourceData, meta interface{}) error {
    74  	cs := meta.(*cloudstack.CloudStackClient)
    75  
    76  	// Get the network ACL list details
    77  	f, count, err := cs.NetworkACL.GetNetworkACLListByID(d.Id())
    78  	if err != nil {
    79  		if count == 0 {
    80  			log.Printf(
    81  				"[DEBUG] Network ACL list %s does no longer exist", d.Get("name").(string))
    82  			d.SetId("")
    83  			return nil
    84  		}
    85  
    86  		return err
    87  	}
    88  
    89  	d.Set("name", f.Name)
    90  	d.Set("description", f.Description)
    91  
    92  	// Get the VPC details
    93  	v, _, err := cs.VPC.GetVPCByID(f.Vpcid)
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	setValueOrID(d, "vpc", v.Name, v.Id)
    99  
   100  	return nil
   101  }
   102  
   103  func resourceCloudStackNetworkACLDelete(d *schema.ResourceData, meta interface{}) error {
   104  	cs := meta.(*cloudstack.CloudStackClient)
   105  
   106  	// Create a new parameter struct
   107  	p := cs.NetworkACL.NewDeleteNetworkACLListParams(d.Id())
   108  
   109  	// Delete the network ACL list
   110  	_, err := cs.NetworkACL.DeleteNetworkACLList(p)
   111  	if err != nil {
   112  		// This is a very poor way to be told the ID does no longer exist :(
   113  		if strings.Contains(err.Error(), fmt.Sprintf(
   114  			"Invalid parameter id value=%s due to incorrect long value format, "+
   115  				"or entity does not exist", d.Id())) {
   116  			return nil
   117  		}
   118  
   119  		return fmt.Errorf("Error deleting network ACL list %s: %s", d.Get("name").(string), err)
   120  	}
   121  
   122  	return nil
   123  }