Skip to content

Commit

Permalink
Merge pull request #7240 from mcanevet/cloudstack_securitygroupnames
Browse files Browse the repository at this point in the history
provider/cloudstack: add security_group_names
  • Loading branch information
Sander van Harmelen authored Jun 27, 2016
2 parents dd59ceb + 318310c commit 57f21b4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
58 changes: 58 additions & 0 deletions builtin/providers/cloudstack/resource_cloudstack_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ func resourceCloudStackInstance() *schema.Resource {
ConflictsWith: []string{"affinity_group_ids"},
},

"security_group_ids": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
ConflictsWith: []string{"security_group_names"},
},

"security_group_names": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
ConflictsWith: []string{"security_group_ids"},
},

"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -240,6 +258,28 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{})
p.SetAffinitygroupnames(groups)
}

// If there is a security_group_ids supplied, add it to the parameter struct
if sgids := d.Get("security_group_ids").(*schema.Set); sgids.Len() > 0 {
var groups []string

for _, group := range sgids.List() {
groups = append(groups, group.(string))
}

p.SetSecuritygroupids(groups)
}

// If there is a security_group_names supplied, add it to the parameter struct
if sgns := d.Get("security_group_names").(*schema.Set); sgns.Len() > 0 {
var groups []string

for _, group := range sgns.List() {
groups = append(groups, group.(string))
}

p.SetSecuritygroupnames(groups)
}

// If there is a project supplied, we retrieve and set the project id
if err := setProjectid(p, cs, d); err != nil {
return err
Expand Down Expand Up @@ -347,6 +387,24 @@ func resourceCloudStackInstanceRead(d *schema.ResourceData, meta interface{}) er
d.Set("affinity_group_names", agns)
}

sgids := &schema.Set{F: schema.HashString}
for _, group := range vm.Securitygroup {
sgids.Add(group.Id)
}

if sgids.Len() > 0 {
d.Set("security_group_ids", sgids)
}

sgns := &schema.Set{F: schema.HashString}
for _, group := range vm.Securitygroup {
sgns.Add(group.Name)
}

if sgns.Len() > 0 {
d.Set("security_group_names", sgns)
}

return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ The following arguments are supported:
* `affinity_group_names` - (Optional) List of affinity groups to apply to this
instance. Changing this forces a new resource to be created.

* `security_group_ids` - (Optional) List of security groups id to apply to this
isnstance. Changing this forces a new resource to be created.

* `security_group_names` - (Optional) List of security groups to apply to this
isnstance. Changing this forces a new resource to be created.

## Attributes Reference

The following attributes are exported:
Expand Down

0 comments on commit 57f21b4

Please sign in to comment.