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