github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  			"project": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Optional: true,
    35  				ForceNew: true,
    36  			},
    37  
    38  			"vpc_id": &schema.Schema{
    39  				Type:     schema.TypeString,
    40  				Required: true,
    41  				ForceNew: true,
    42  			},
    43  		},
    44  	}
    45  }
    46  
    47  func resourceCloudStackNetworkACLCreate(d *schema.ResourceData, meta interface{}) error {
    48  	cs := meta.(*cloudstack.CloudStackClient)
    49  
    50  	name := d.Get("name").(string)
    51  
    52  	// Create a new parameter struct
    53  	p := cs.NetworkACL.NewCreateNetworkACLListParams(name, d.Get("vpc_id").(string))
    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(
    78  		d.Id(),
    79  		cloudstack.WithProject(d.Get("project").(string)),
    80  	)
    81  	if err != nil {
    82  		if count == 0 {
    83  			log.Printf(
    84  				"[DEBUG] Network ACL list %s does no longer exist", d.Get("name").(string))
    85  			d.SetId("")
    86  			return nil
    87  		}
    88  
    89  		return err
    90  	}
    91  
    92  	d.Set("name", f.Name)
    93  	d.Set("description", f.Description)
    94  	d.Set("vpc_id", f.Vpcid)
    95  
    96  	return nil
    97  }
    98  
    99  func resourceCloudStackNetworkACLDelete(d *schema.ResourceData, meta interface{}) error {
   100  	cs := meta.(*cloudstack.CloudStackClient)
   101  
   102  	// Create a new parameter struct
   103  	p := cs.NetworkACL.NewDeleteNetworkACLListParams(d.Id())
   104  
   105  	// Delete the network ACL list
   106  	_, err := Retry(3, func() (interface{}, error) {
   107  		return cs.NetworkACL.DeleteNetworkACLList(p)
   108  	})
   109  	if err != nil {
   110  		// This is a very poor way to be told the ID does no longer exist :(
   111  		if strings.Contains(err.Error(), fmt.Sprintf(
   112  			"Invalid parameter id value=%s due to incorrect long value format, "+
   113  				"or entity does not exist", d.Id())) {
   114  			return nil
   115  		}
   116  
   117  		return fmt.Errorf("Error deleting network ACL list %s: %s", d.Get("name").(string), err)
   118  	}
   119  
   120  	return nil
   121  }