github.com/ottenhoff/terraform@v0.7.0-rc1.0.20160607213102-ac2d195cc560/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 vpc, ok := d.GetOk("vpc_id") 94 if !ok { 95 vpc, ok = d.GetOk("vpc") 96 } 97 if !ok { 98 return errors.New("Either `vpc_id` or [deprecated] `vpc` must be provided.") 99 } 100 101 // Retrieve the vpc ID 102 vpcid, e := retrieveID(cs, "vpc", vpc.(string)) 103 if e != nil { 104 return e.Error() 105 } 106 107 // Get the network ACL list details 108 f, count, err := cs.NetworkACL.GetNetworkACLListByID(d.Id(), cloudstack.WithVPCID(vpcid)) 109 if err != nil { 110 if count == 0 { 111 log.Printf( 112 "[DEBUG] Network ACL list %s does no longer exist", d.Get("name").(string)) 113 d.SetId("") 114 return nil 115 } 116 117 return err 118 } 119 120 d.Set("name", f.Name) 121 d.Set("description", f.Description) 122 d.Set("vpc_id", f.Vpcid) 123 124 return nil 125 } 126 127 func resourceCloudStackNetworkACLDelete(d *schema.ResourceData, meta interface{}) error { 128 cs := meta.(*cloudstack.CloudStackClient) 129 130 // Create a new parameter struct 131 p := cs.NetworkACL.NewDeleteNetworkACLListParams(d.Id()) 132 133 // Delete the network ACL list 134 _, err := Retry(3, func() (interface{}, error) { 135 return cs.NetworkACL.DeleteNetworkACLList(p) 136 }) 137 if err != nil { 138 // This is a very poor way to be told the ID does no longer exist :( 139 if strings.Contains(err.Error(), fmt.Sprintf( 140 "Invalid parameter id value=%s due to incorrect long value format, "+ 141 "or entity does not exist", d.Id())) { 142 return nil 143 } 144 145 return fmt.Errorf("Error deleting network ACL list %s: %s", d.Get("name").(string), err) 146 } 147 148 return nil 149 }