From d7e7a45ec6ea38026bb6967c5859a76b0e8571f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Can=C3=A9vet?= Date: Mon, 20 Jun 2016 17:12:52 +0200 Subject: [PATCH] provider/cloudstack: add security_group_names --- .../resource_cloudstack_instance.go | 29 +++++++++++++++++++ .../cloudstack/r/instance.html.markdown | 3 ++ 2 files changed, 32 insertions(+) diff --git a/builtin/providers/cloudstack/resource_cloudstack_instance.go b/builtin/providers/cloudstack/resource_cloudstack_instance.go index 255580f27..6f7ccf1f7 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_instance.go +++ b/builtin/providers/cloudstack/resource_cloudstack_instance.go @@ -89,6 +89,15 @@ func resourceCloudStackInstance() *schema.Resource { ConflictsWith: []string{"affinity_group_ids"}, }, + "security_group_names": &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + Computed: true, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, + "project": &schema.Schema{ Type: schema.TypeString, Optional: true, @@ -240,6 +249,17 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{}) p.SetAffinitygroupnames(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 @@ -347,6 +367,15 @@ func resourceCloudStackInstanceRead(d *schema.ResourceData, meta interface{}) er d.Set("affinity_group_names", agns) } + 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 } diff --git a/website/source/docs/providers/cloudstack/r/instance.html.markdown b/website/source/docs/providers/cloudstack/r/instance.html.markdown index fbb22d0df..d198de645 100644 --- a/website/source/docs/providers/cloudstack/r/instance.html.markdown +++ b/website/source/docs/providers/cloudstack/r/instance.html.markdown @@ -73,6 +73,9 @@ 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_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: