@@ -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
0 commit comments