github.com/jrasell/terraform@v0.6.17-0.20160523115548-2652f5232949/builtin/providers/cloudstack/resource_cloudstack_network_acl.go (about) 1 package cloudstack 2 3 import ( 4 "errors" 5 "fmt" 6 "log" 7 "strings" 8 9 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/xanzy/go-cloudstack/cloudstack" 11 ) 12 13 func resourceCloudStackNetworkACL() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceCloudStackNetworkACLCreate, 16 Read: resourceCloudStackNetworkACLRead, 17 Delete: resourceCloudStackNetworkACLDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "name": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 ForceNew: true, 24 }, 25 26 "description": &schema.Schema{ 27 Type: schema.TypeString, 28 Optional: true, 29 Computed: true, 30 ForceNew: true, 31 }, 32 33 "vpc_id": &schema.Schema{ 34 Type: schema.TypeString, 35 Optional: true, 36 Computed: true, 37 ForceNew: true, 38 }, 39 40 "vpc": &schema.Schema{ 41 Type: schema.TypeString, 42 Optional: true, 43 ForceNew: true, 44 Deprecated: "Please use the `vpc_id` field instead", 45 }, 46 }, 47 } 48 } 49 50 func resourceCloudStackNetworkACLCreate(d *schema.ResourceData, meta interface{}) error { 51 cs := meta.(*cloudstack.CloudStackClient) 52 53 name := d.Get("name").(string) 54 55 vpc, ok := d.GetOk("vpc_id") 56 if !ok { 57 vpc, ok = d.GetOk("vpc") 58 } 59 if !ok { 60 return errors.New("Either `vpc_id` or [deprecated] `vpc` must be provided.") 61 } 62 63 // Retrieve the vpc ID 64 vpcid, e := retrieveID(cs, "vpc", vpc.(string)) 65 if e != nil { 66 return e.Error() 67 } 68 69 // Create a new parameter struct 70 p := cs.NetworkACL.NewCreateNetworkACLListParams(name, vpcid) 71 72 // Set the description 73 if description, ok := d.GetOk("description"); ok { 74 p.SetDescription(description.(string)) 75 } else { 76 p.SetDescription(name) 77 } 78 79 // Create the new network ACL list 80 r, err := cs.NetworkACL.CreateNetworkACLList(p) 81 if err != nil { 82 return fmt.Errorf("Error creating network ACL list %s: %s", name, err) 83 } 84 85 d.SetId(r.Id) 86 87 return resourceCloudStackNetworkACLRead(d, meta) 88 } 89 90 func resourceCloudStackNetworkACLRead(d *schema.ResourceData, meta interface{}) error { 91 cs := meta.(*cloudstack.CloudStackClient) 92 93 // Get the network ACL list details 94 f, count, err := cs.NetworkACL.GetNetworkACLListByID(d.Id()) 95 if err != nil { 96 if count == 0 { 97 log.Printf( 98 "[DEBUG] Network ACL list %s does no longer exist", d.Get("name").(string)) 99 d.SetId("") 100 return nil 101 } 102 103 return err 104 } 105 106 d.Set("name", f.Name) 107 d.Set("description", f.Description) 108 d.Set("vpc_id", f.Vpcid) 109 110 return nil 111 } 112 113 func resourceCloudStackNetworkACLDelete(d *schema.ResourceData, meta interface{}) error { 114 cs := meta.(*cloudstack.CloudStackClient) 115 116 // Create a new parameter struct 117 p := cs.NetworkACL.NewDeleteNetworkACLListParams(d.Id()) 118 119 // Delete the network ACL list 120 _, err := cs.NetworkACL.DeleteNetworkACLList(p) 121 if err != nil { 122 // This is a very poor way to be told the ID does no longer exist :( 123 if strings.Contains(err.Error(), fmt.Sprintf( 124 "Invalid parameter id value=%s due to incorrect long value format, "+ 125 "or entity does not exist", d.Id())) { 126 return nil 127 } 128 129 return fmt.Errorf("Error deleting network ACL list %s: %s", d.Get("name").(string), err) 130 } 131 132 return nil 133 }