Skip to content

Commit d030b62

Browse files
mcanevetSander van Harmelen
authored andcommitted
Cloudstack security group (hashicorp#9103)
* Add cloudstack_security_group resource * Update github.com/xanzy/go-cloudstack/cloudstack * Add support for security_group * Add documentation for cloudstack_security_group
1 parent aaff622 commit d030b62

5 files changed

Lines changed: 390 additions & 7 deletions

File tree

‎builtin/providers/cloudstack/provider.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func Provider() terraform.ResourceProvider {
5454
"cloudstack_nic": resourceCloudStackNIC(),
5555
"cloudstack_port_forward": resourceCloudStackPortForward(),
5656
"cloudstack_secondary_ipaddress": resourceCloudStackSecondaryIPAddress(),
57+
"cloudstack_security_group": resourceCloudStackSecurityGroup(),
5758
"cloudstack_ssh_keypair": resourceCloudStackSSHKeyPair(),
5859
"cloudstack_static_nat": resourceCloudStackStaticNAT(),
5960
"cloudstack_template": resourceCloudStackTemplate(),
Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
package cloudstack
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"strconv"
7+
"strings"
8+
9+
"github.com/hashicorp/terraform/helper/schema"
10+
"github.com/xanzy/go-cloudstack/cloudstack"
11+
)
12+
13+
func resourceCloudStackSecurityGroup() *schema.Resource {
14+
return &schema.Resource{
15+
Create: resourceCloudStackSecurityGroupCreate,
16+
Read: resourceCloudStackSecurityGroupRead,
17+
Delete: resourceCloudStackSecurityGroupDelete,
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+
"project": &schema.Schema{
34+
Type: schema.TypeString,
35+
Optional: true,
36+
ForceNew: true,
37+
},
38+
39+
"rules": &schema.Schema{
40+
Type: schema.TypeList,
41+
ForceNew: true,
42+
Optional: true,
43+
Elem: &schema.Resource{
44+
Schema: map[string]*schema.Schema{
45+
"cidr_list": &schema.Schema{
46+
Type: schema.TypeString,
47+
Optional: true,
48+
ForceNew: true,
49+
},
50+
51+
"security_group": &schema.Schema{
52+
Type: schema.TypeString,
53+
Optional: true,
54+
ForceNew: true,
55+
},
56+
57+
"protocol": &schema.Schema{
58+
Type: schema.TypeString,
59+
Required: true,
60+
ForceNew: true,
61+
},
62+
63+
"ports": &schema.Schema{
64+
Type: schema.TypeString,
65+
Optional: true,
66+
ForceNew: true,
67+
},
68+
69+
"traffic_type": &schema.Schema{
70+
Type: schema.TypeString,
71+
Optional: true,
72+
Default: "ingress",
73+
},
74+
},
75+
},
76+
},
77+
},
78+
}
79+
}
80+
81+
func resourceCloudStackSecurityGroupCreate(d *schema.ResourceData, meta interface{}) error {
82+
cs := meta.(*cloudstack.CloudStackClient)
83+
84+
name := d.Get("name").(string)
85+
86+
// Create a new parameter struct
87+
p := cs.SecurityGroup.NewCreateSecurityGroupParams(name)
88+
89+
// Set the description
90+
if description, ok := d.GetOk("description"); ok {
91+
p.SetDescription(description.(string))
92+
} else {
93+
p.SetDescription(name)
94+
}
95+
96+
// If there is a project supplied, we retrieve and set the project id
97+
if err := setProjectid(p, cs, d); err != nil {
98+
return err
99+
}
100+
101+
log.Printf("[DEBUG] Creating security group %s", name)
102+
r, err := cs.SecurityGroup.CreateSecurityGroup(p)
103+
if err != nil {
104+
return err
105+
}
106+
107+
log.Printf("[DEBUG] Security group %s successfully created with ID: %s", name, r.Id)
108+
d.SetId(r.Id)
109+
110+
// Create Rules
111+
rules := d.Get("rules").([]interface{})
112+
for _, r := range rules {
113+
114+
rule := r.(map[string]interface{})
115+
116+
m := splitPorts.FindStringSubmatch(rule["ports"].(string))
117+
startPort, err := strconv.Atoi(m[1])
118+
if err != nil {
119+
return err
120+
}
121+
122+
endPort := startPort
123+
if m[2] != "" {
124+
endPort, err = strconv.Atoi(m[2])
125+
if err != nil {
126+
return err
127+
}
128+
}
129+
130+
traffic := rule["traffic_type"].(string)
131+
if traffic == "ingress" {
132+
param := cs.SecurityGroup.NewAuthorizeSecurityGroupIngressParams()
133+
134+
param.SetSecuritygroupid(d.Id())
135+
if cidrlist := rule["cidr_list"]; cidrlist != "" {
136+
param.SetCidrlist([]string{cidrlist.(string)})
137+
log.Printf("[DEBUG] cidr = %v", cidrlist)
138+
}
139+
if securitygroup := rule["security_group"]; securitygroup != "" {
140+
// Get the security group details
141+
ag, count, err := cs.SecurityGroup.GetSecurityGroupByName(
142+
securitygroup.(string),
143+
cloudstack.WithProject(d.Get("project").(string)),
144+
)
145+
if err != nil {
146+
if count == 0 {
147+
log.Printf("[DEBUG] Security group %s does not longer exist", d.Get("name").(string))
148+
d.SetId("")
149+
return nil
150+
}
151+
152+
log.Printf("[DEBUG] Found %v groups matching", count)
153+
154+
return err
155+
}
156+
log.Printf("[DEBUG] ag = %v", ag)
157+
m := make(map[string]string)
158+
m[ag.Account] = ag.Name
159+
log.Printf("[DEBUG] m = %v", m)
160+
param.SetUsersecuritygrouplist(m)
161+
}
162+
param.SetStartport(startPort)
163+
param.SetEndport(endPort)
164+
param.SetProtocol(rule["protocol"].(string))
165+
166+
log.Printf("[DEBUG] Authorizing Ingress Rule %#v", param)
167+
_, err := cs.SecurityGroup.AuthorizeSecurityGroupIngress(param)
168+
if err != nil {
169+
return err
170+
}
171+
} else if traffic == "egress" {
172+
param := cs.SecurityGroup.NewAuthorizeSecurityGroupEgressParams()
173+
174+
param.SetSecuritygroupid(d.Id())
175+
if cidrlist := rule["cidr_list"]; cidrlist != "" {
176+
param.SetCidrlist([]string{cidrlist.(string)})
177+
log.Printf("[DEBUG] cidr = %v", cidrlist)
178+
}
179+
if securitygroup := rule["security_group"]; securitygroup != "" {
180+
// Get the security group details
181+
ag, count, err := cs.SecurityGroup.GetSecurityGroupByName(
182+
securitygroup.(string),
183+
cloudstack.WithProject(d.Get("project").(string)),
184+
)
185+
if err != nil {
186+
if count == 0 {
187+
log.Printf("[DEBUG] Security group %s does not longer exist", d.Get("name").(string))
188+
d.SetId("")
189+
return nil
190+
}
191+
192+
log.Printf("[DEBUG] Found %v groups matching", count)
193+
194+
return err
195+
}
196+
log.Printf("[DEBUG] ag = %v", ag)
197+
m := make(map[string]string)
198+
m[ag.Account] = ag.Name
199+
log.Printf("[DEBUG] m = %v", m)
200+
param.SetUsersecuritygrouplist(m)
201+
}
202+
param.SetStartport(startPort)
203+
param.SetEndport(endPort)
204+
param.SetProtocol(rule["protocol"].(string))
205+
206+
log.Printf("[DEBUG] Authorizing Egress Rule %#v", param)
207+
_, err := cs.SecurityGroup.AuthorizeSecurityGroupEgress(param)
208+
if err != nil {
209+
return err
210+
}
211+
} else {
212+
return fmt.Errorf(
213+
"Parameter traffic_type only accepts 'ingress' or 'egress' as values")
214+
}
215+
}
216+
217+
return resourceCloudStackSecurityGroupRead(d, meta)
218+
}
219+
220+
func resourceCloudStackSecurityGroupRead(d *schema.ResourceData, meta interface{}) error {
221+
cs := meta.(*cloudstack.CloudStackClient)
222+
223+
log.Printf("[DEBUG] Retrieving security group %s (ID=%s)", d.Get("name").(string), d.Id())
224+
225+
// Get the security group details
226+
ag, count, err := cs.SecurityGroup.GetSecurityGroupByID(
227+
d.Id(),
228+
cloudstack.WithProject(d.Get("project").(string)),
229+
)
230+
if err != nil {
231+
if count == 0 {
232+
log.Printf("[DEBUG] Security group %s does not longer exist", d.Get("name").(string))
233+
d.SetId("")
234+
return nil
235+
}
236+
237+
log.Printf("[DEBUG] Found %v groups matching", count)
238+
239+
return err
240+
}
241+
242+
// Update the config
243+
d.Set("name", ag.Name)
244+
d.Set("description", ag.Description)
245+
246+
var rules []interface{}
247+
for _, r := range ag.Ingressrule {
248+
var ports string
249+
if r.Startport == r.Endport {
250+
ports = strconv.Itoa(r.Startport)
251+
} else {
252+
ports = fmt.Sprintf("%v-%v", r.Startport, r.Endport)
253+
}
254+
rule := map[string]interface{}{
255+
"cidr_list": r.Cidr,
256+
"ports": ports,
257+
"protocol": r.Protocol,
258+
"traffic_type": "ingress",
259+
"security_group": r.Securitygroupname,
260+
}
261+
rules = append(rules, rule)
262+
}
263+
for _, r := range ag.Egressrule {
264+
var ports string
265+
if r.Startport == r.Endport {
266+
ports = strconv.Itoa(r.Startport)
267+
} else {
268+
ports = fmt.Sprintf("%s-%s", r.Startport, r.Endport)
269+
}
270+
rule := map[string]interface{}{
271+
"cidr_list": r.Cidr,
272+
"ports": ports,
273+
"protocol": r.Protocol,
274+
"traffic_type": "egress",
275+
"security_group": r.Securitygroupname,
276+
}
277+
rules = append(rules, rule)
278+
}
279+
d.Set("rules", rules)
280+
281+
return nil
282+
}
283+
284+
func resourceCloudStackSecurityGroupDelete(d *schema.ResourceData, meta interface{}) error {
285+
cs := meta.(*cloudstack.CloudStackClient)
286+
287+
// Create a new parameter struct
288+
p := cs.SecurityGroup.NewDeleteSecurityGroupParams()
289+
p.SetId(d.Id())
290+
291+
// If there is a project supplied, we retrieve and set the project id
292+
if err := setProjectid(p, cs, d); err != nil {
293+
return err
294+
}
295+
296+
// Delete the security group
297+
_, err := cs.SecurityGroup.DeleteSecurityGroup(p)
298+
if err != nil {
299+
// This is a very poor way to be told the ID does no longer exist :(
300+
if strings.Contains(err.Error(), fmt.Sprintf(
301+
"Invalid parameter id value=%s due to incorrect long value format, "+
302+
"or entity does not exist", d.Id())) {
303+
return nil
304+
}
305+
306+
return fmt.Errorf("Error deleting security group: %s", err)
307+
}
308+
309+
return nil
310+
}

‎vendor/github.com/xanzy/go-cloudstack/cloudstack/SecurityGroupService.go‎

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/vendor.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,11 +2137,11 @@
21372137
"revision": "9051bd6b44125d9472e0c148b5965692ab283d4a"
21382138
},
21392139
{
2140-
"checksumSHA1": "CT9vuv0kaEnwEDcNywB/wD6eA54=",
2140+
"checksumSHA1": "s6bz9U3gi638R0Ln7v14h5+e2Wg=",
21412141
"comment": "v2.1.3",
21422142
"path": "github.com/xanzy/go-cloudstack/cloudstack",
2143-
"revision": "b33e8849d135b99cb8236237524d10b41f597cb3",
2144-
"revisionTime": "2016-08-31T14:38:59Z"
2143+
"revision": "ff444b1afdd1961f8118bad30db9fa27cb989ced",
2144+
"revisionTime": "2016-10-24T10:36:39Z"
21452145
},
21462146
{
21472147
"path": "github.com/xanzy/ssh-agent",

0 commit comments

Comments
 (0)