github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/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_id": &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  	// Create a new parameter struct
    47  	p := cs.NetworkACL.NewCreateNetworkACLListParams(name, d.Get("vpc_id").(string))
    48  
    49  	// Set the description
    50  	if description, ok := d.GetOk("description"); ok {
    51  		p.SetDescription(description.(string))
    52  	} else {
    53  		p.SetDescription(name)
    54  	}
    55  
    56  	// Create the new network ACL list
    57  	r, err := cs.NetworkACL.CreateNetworkACLList(p)
    58  	if err != nil {
    59  		return fmt.Errorf("Error creating network ACL list %s: %s", name, err)
    60  	}
    61  
    62  	d.SetId(r.Id)
    63  
    64  	return resourceCloudStackNetworkACLRead(d, meta)
    65  }
    66  
    67  func resourceCloudStackNetworkACLRead(d *schema.ResourceData, meta interface{}) error {
    68  	cs := meta.(*cloudstack.CloudStackClient)
    69  
    70  	// Get the network ACL list details
    71  	f, count, err := cs.NetworkACL.GetNetworkACLListByID(
    72  		d.Id(),
    73  		cloudstack.WithVPCID(d.Get("vpc_id").(string)),
    74  	)
    75  	if err != nil {
    76  		if count == 0 {
    77  			log.Printf(
    78  				"[DEBUG] Network ACL list %s does no longer exist", d.Get("name").(string))
    79  			d.SetId("")
    80  			return nil
    81  		}
    82  
    83  		return err
    84  	}
    85  
    86  	d.Set("name", f.Name)
    87  	d.Set("description", f.Description)
    88  	d.Set("vpc_id", f.Vpcid)
    89  
    90  	return nil
    91  }
    92  
    93  func resourceCloudStackNetworkACLDelete(d *schema.ResourceData, meta interface{}) error {
    94  	cs := meta.(*cloudstack.CloudStackClient)
    95  
    96  	// Create a new parameter struct
    97  	p := cs.NetworkACL.NewDeleteNetworkACLListParams(d.Id())
    98  
    99  	// Delete the network ACL list
   100  	_, err := Retry(3, func() (interface{}, error) {
   101  		return cs.NetworkACL.DeleteNetworkACLList(p)
   102  	})
   103  	if err != nil {
   104  		// This is a very poor way to be told the ID does no longer exist :(
   105  		if strings.Contains(err.Error(), fmt.Sprintf(
   106  			"Invalid parameter id value=%s due to incorrect long value format, "+
   107  				"or entity does not exist", d.Id())) {
   108  			return nil
   109  		}
   110  
   111  		return fmt.Errorf("Error deleting network ACL list %s: %s", d.Get("name").(string), err)
   112  	}
   113  
   114  	return nil
   115  }