Skip to content

Commit

Permalink
Fix security group not restored
Browse files Browse the repository at this point in the history
Fixed #369
  • Loading branch information
outscale-toa committed Sep 11, 2023
1 parent d4a3bd0 commit 4b55210
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 9 additions & 1 deletion outscale/data_source_outscale_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ func getOAPIVMSecurityGroups(groupSet []oscgo.SecurityGroupLight) []map[string]i
return res
}

func getVMSecurityGroupIds(sgIds []oscgo.SecurityGroupLight) []string {
res := make([]string, len(sgIds))
for k, ids := range sgIds {
res[k] = ids.GetSecurityGroupId()
}
return res
}

func getDataSourceOAPIVMSchemas() map[string]*schema.Schema {
wholeSchema := map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
Expand Down Expand Up @@ -354,7 +362,7 @@ func getOApiVMAttributesSchema() map[string]*schema.Schema {
Computed: true,
},
"security_group_ids": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
Expand Down
11 changes: 8 additions & 3 deletions outscale/resource_outscale_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func resourceOutscaleOApiVM() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
},
"security_group_ids": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
Expand Down Expand Up @@ -667,6 +667,11 @@ func resourceOAPIVMRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("admin_password", adminPassword); err != nil {
return err
}
if len(utils.SetToStringSlice(d.Get("security_group_ids").(*schema.Set))) > 0 {
if err := set("security_group_ids", getVMSecurityGroupIds(vm.GetSecurityGroups())); err != nil {
return err
}
}
d.SetId(vm.GetVmId())
return oapiVMDescriptionAttributes(set, &vm)
}); err != nil {
Expand Down Expand Up @@ -789,7 +794,7 @@ func resourceOAPIVMUpdate(d *schema.ResourceData, meta interface{}) error {
if d.HasChange("security_group_ids") && !d.IsNewResource() {
opts := oscgo.UpdateVmRequest{VmId: id}

opts.SetSecurityGroupIds(utils.InterfaceSliceToStringSlice(d.Get("security_group_ids").([]interface{})))
opts.SetSecurityGroupIds(utils.SetToStringSlice(d.Get("security_group_ids").(*schema.Set)))
if err := updateVmAttr(conn, opts); err != nil {
return err
}
Expand Down Expand Up @@ -976,7 +981,7 @@ func buildCreateVmsRequest(d *schema.ResourceData, meta interface{}) (oscgo.Crea
request.SetPrivateIps(privateIPs)
}

if sgIDs := utils.InterfaceSliceToStringSlice(d.Get("security_group_ids").([]interface{})); len(sgIDs) > 0 {
if sgIDs := utils.SetToStringSlice(d.Get("security_group_ids").(*schema.Set)); len(sgIDs) > 0 {
request.SetSecurityGroupIds(sgIDs)
}

Expand Down

0 comments on commit 4b55210

Please sign in to comment.