Skip to content

Commit 57f21b4

Browse files
author
Sander van Harmelen
authored
Merge pull request hashicorp#7240 from mcanevet/cloudstack_securitygroupnames
provider/cloudstack: add security_group_names
2 parents dd59ceb + 318310c commit 57f21b4

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

builtin/providers/cloudstack/resource_cloudstack_instance.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ func resourceCloudStackInstance() *schema.Resource {
8989
ConflictsWith: []string{"affinity_group_ids"},
9090
},
9191

92+
"security_group_ids": &schema.Schema{
93+
Type: schema.TypeSet,
94+
Optional: true,
95+
ForceNew: true,
96+
Elem: &schema.Schema{Type: schema.TypeString},
97+
Set: schema.HashString,
98+
ConflictsWith: []string{"security_group_names"},
99+
},
100+
101+
"security_group_names": &schema.Schema{
102+
Type: schema.TypeSet,
103+
Optional: true,
104+
ForceNew: true,
105+
Elem: &schema.Schema{Type: schema.TypeString},
106+
Set: schema.HashString,
107+
ConflictsWith: []string{"security_group_ids"},
108+
},
109+
92110
"project": &schema.Schema{
93111
Type: schema.TypeString,
94112
Optional: true,
@@ -240,6 +258,28 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{})
240258
p.SetAffinitygroupnames(groups)
241259
}
242260

261+
// If there is a security_group_ids supplied, add it to the parameter struct
262+
if sgids := d.Get("security_group_ids").(*schema.Set); sgids.Len() > 0 {
263+
var groups []string
264+
265+
for _, group := range sgids.List() {
266+
groups = append(groups, group.(string))
267+
}
268+
269+
p.SetSecuritygroupids(groups)
270+
}
271+
272+
// If there is a security_group_names supplied, add it to the parameter struct
273+
if sgns := d.Get("security_group_names").(*schema.Set); sgns.Len() > 0 {
274+
var groups []string
275+
276+
for _, group := range sgns.List() {
277+
groups = append(groups, group.(string))
278+
}
279+
280+
p.SetSecuritygroupnames(groups)
281+
}
282+
243283
// If there is a project supplied, we retrieve and set the project id
244284
if err := setProjectid(p, cs, d); err != nil {
245285
return err
@@ -347,6 +387,24 @@ func resourceCloudStackInstanceRead(d *schema.ResourceData, meta interface{}) er
347387
d.Set("affinity_group_names", agns)
348388
}
349389

390+
sgids := &schema.Set{F: schema.HashString}
391+
for _, group := range vm.Securitygroup {
392+
sgids.Add(group.Id)
393+
}
394+
395+
if sgids.Len() > 0 {
396+
d.Set("security_group_ids", sgids)
397+
}
398+
399+
sgns := &schema.Set{F: schema.HashString}
400+
for _, group := range vm.Securitygroup {
401+
sgns.Add(group.Name)
402+
}
403+
404+
if sgns.Len() > 0 {
405+
d.Set("security_group_names", sgns)
406+
}
407+
350408
return nil
351409
}
352410

website/source/docs/providers/cloudstack/r/instance.html.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ The following arguments are supported:
7373
* `affinity_group_names` - (Optional) List of affinity groups to apply to this
7474
instance. Changing this forces a new resource to be created.
7575

76+
* `security_group_ids` - (Optional) List of security groups id to apply to this
77+
isnstance. Changing this forces a new resource to be created.
78+
79+
* `security_group_names` - (Optional) List of security groups to apply to this
80+
isnstance. Changing this forces a new resource to be created.
81+
7682
## Attributes Reference
7783

7884
The following attributes are exported:

0 commit comments

Comments
 (0)