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 7, 2023
1 parent 8720a14 commit 40d2f1e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
13 changes: 12 additions & 1 deletion outscale/data_source_outscale_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ func oapiVMDescriptionAttributes(set AttributeSetter, vm *oscgo.Vm) error {
if err := set("root_device_type", vm.GetRootDeviceType()); err != nil {
return err
}
if err := set("security_group_ids", getVMSecurityGroupIds(vm.GetSecurityGroups())); err != nil {
return err
}
if err := set("security_groups", getOAPIVMSecurityGroups(vm.GetSecurityGroups())); err != nil {
log.Printf("[DEBUG] SECURITY GROUPS ERR %+v", err)
return err
Expand Down Expand Up @@ -225,6 +228,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 +365,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
6 changes: 3 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 @@ -789,7 +789,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 +976,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 40d2f1e

Please sign in to comment.