Skip to content

Commit

Permalink
Enable adding tags on bsu
Browse files Browse the repository at this point in the history
  • Loading branch information
outscale-toa committed Apr 26, 2023
1 parent 72b52a9 commit 37eee32
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 75 deletions.
54 changes: 32 additions & 22 deletions outscale/data_source_outscale_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ func dataSourceOutscaleOAPIVMRead(d *schema.ResourceData, meta interface{}) erro
params.Filters.VmIds = &[]string{instanceID.(string)}
}

log.Printf("[DEBUG] ReadVmsRequest -> %+v\n", params)

var resp oscgo.ReadVmsResponse
err := resource.Retry(30*time.Second, func() *resource.RetryError {
rp, httpResp, err := client.VmApi.ReadVms(context.Background()).ReadVmsRequest(params).Execute()
Expand Down Expand Up @@ -82,6 +80,16 @@ func dataSourceOutscaleOAPIVMRead(d *schema.ResourceData, meta interface{}) erro
// Populate vm attribute fields with the returned vm
return resourceDataAttrSetter(d, func(set AttributeSetter) error {
d.SetId(vm.GetVmId())

booTags, errTags := utils.GetBsuTagsMaps(vm, client)
if errTags != nil {
return errTags
}
if err := d.Set("block_device_mappings_created", getOscAPIVMBlockDeviceMapping(
booTags, vm.GetBlockDeviceMappings())); err != nil {
return err
}

return oapiVMDescriptionAttributes(set, &vm)
})
}
Expand All @@ -91,10 +99,6 @@ func oapiVMDescriptionAttributes(set AttributeSetter, vm *oscgo.Vm) error {
if err := set("architecture", vm.GetArchitecture()); err != nil {
return err
}
if err := set("block_device_mappings_created", getOscAPIVMBlockDeviceMapping(vm.GetBlockDeviceMappings())); err != nil {
log.Printf("[DEBUG] BLOCKING DEVICE MAPPING ERR %+v", err)
return err
}
if err := set("bsu_optimized", vm.GetBsuOptimized()); err != nil {
return err
}
Expand Down Expand Up @@ -195,21 +199,26 @@ func oapiVMDescriptionAttributes(set AttributeSetter, vm *oscgo.Vm) error {
return set("vm_type", vm.GetVmType())
}

func getOscAPIVMBlockDeviceMapping(blockDeviceMappings []oscgo.BlockDeviceMappingCreated) []map[string]interface{} {
blockDeviceMapping := make([]map[string]interface{}, len(blockDeviceMappings))

for k, v := range blockDeviceMappings {
blockDeviceMapping[k] = map[string]interface{}{
"device_name": aws.StringValue(v.DeviceName),
"bsu": map[string]interface{}{
"delete_on_vm_deletion": fmt.Sprintf("%t", aws.BoolValue(v.GetBsu().DeleteOnVmDeletion)),
"volume_id": aws.StringValue(v.GetBsu().VolumeId),
"state": aws.StringValue(v.GetBsu().State),
"link_date": aws.StringValue(v.GetBsu().LinkDate),
},
func getOscAPIVMBlockDeviceMapping(busTagsMaps map[string]interface{}, blockDeviceMappings []oscgo.BlockDeviceMappingCreated) (blockDeviceMapping []map[string]interface{}) {
for _, v := range blockDeviceMappings {
blockDevice := map[string]interface{}{
"device_name": v.GetDeviceName(),
"bsu": getbusToSet(v.GetBsu(), busTagsMaps, *v.DeviceName),
}
blockDeviceMapping = append(blockDeviceMapping, blockDevice)
}
return blockDeviceMapping
return
}

func getbusToSet(bsu oscgo.BsuCreated, busTagsMaps map[string]interface{}, deviceName string) (res []map[string]interface{}) {
res = append(res, map[string]interface{}{
"delete_on_vm_deletion": bsu.GetDeleteOnVmDeletion(),
"volume_id": bsu.GetVolumeId(),
"state": bsu.GetState(),
"link_date": bsu.GetLinkDate(),
"tags": getOscAPITagSet(busTagsMaps[deviceName].([]oscgo.ResourceTag)),
})
return
}

func getOAPIVMSecurityGroups(groupSet []oscgo.SecurityGroupLight) []map[string]interface{} {
Expand Down Expand Up @@ -284,7 +293,7 @@ func getOApiVMAttributesSchema() map[string]*schema.Schema {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"bsu": {
Type: schema.TypeMap,
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Expand All @@ -298,13 +307,14 @@ func getOApiVMAttributesSchema() map[string]*schema.Schema {
Computed: true,
},
"state": {
Type: schema.TypeInt,
Type: schema.TypeString,
Computed: true,
},
"volume_id": {
Type: schema.TypeFloat,
Type: schema.TypeString,
Computed: true,
},
"tags": tagsListOAPISchema(),
},
},
},
Expand Down
6 changes: 4 additions & 2 deletions outscale/data_source_outscale_vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ func dataSourceOutscaleOApiVMSRead(d *schema.ResourceData, meta interface{}) err
}

d.SetId(resource.UniqueId())
return d.Set("vms", dataSourceOAPIVMS(filteredVms))
return d.Set("vms", dataSourceOAPIVMS(filteredVms, client))
}

func dataSourceOAPIVMS(i []oscgo.Vm) []map[string]interface{} {
func dataSourceOAPIVMS(i []oscgo.Vm, conn *oscgo.APIClient) []map[string]interface{} {
vms := make([]map[string]interface{}, len(i))
for index, v := range i {
vm := make(map[string]interface{})
Expand All @@ -130,6 +130,8 @@ func dataSourceOAPIVMS(i []oscgo.Vm) []map[string]interface{} {
if err := oapiVMDescriptionAttributes(setterFunc, &v); err != nil {
log.Fatalf("[DEBUG] oapiVMDescriptionAttributes ERROR %+v", err)
}
mapsTags, _ := utils.GetBsuTagsMaps(v, conn)
vm["block_device_mappings_created"] = getOscAPIVMBlockDeviceMapping(mapsTags, v.GetBlockDeviceMappings())

vm["tags"] = getOscAPITagSet(v.GetTags())
vms[index] = vm
Expand Down
88 changes: 68 additions & 20 deletions outscale/oapi_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,73 @@ import (

func setOSCAPITags(conn *oscgo.APIClient, d *schema.ResourceData) error {

if d.HasChange("tags") {
oraw, nraw := d.GetChange("tags")
o := oraw.(*schema.Set)
n := nraw.(*schema.Set)
create, remove := diffOSCAPITags(tagsFromSliceMap(o), tagsFromSliceMap(n))

// Set tag
if len(remove) > 0 {
oraw, nraw := d.GetChange("tags")
o := oraw.(*schema.Set)
n := nraw.(*schema.Set)
create, remove := diffOSCAPITags(tagsFromSliceMap(o), tagsFromSliceMap(n))
resourceId := d.Id()
// Set tag
if len(remove) > 0 {
err := resource.Retry(60*time.Second, func() *resource.RetryError {
_, httpResp, err := conn.TagApi.DeleteTags(context.Background()).DeleteTagsRequest(oscgo.DeleteTagsRequest{
ResourceIds: []string{resourceId},
Tags: remove,
}).Execute()
if err != nil {
return utils.CheckThrottling(httpResp, err)
}
return nil
})
if err != nil {
return err
}
}
if len(create) > 0 {
err := resource.Retry(60*time.Second, func() *resource.RetryError {
_, httpResp, err := conn.TagApi.CreateTags(context.Background()).CreateTagsRequest(oscgo.CreateTagsRequest{
ResourceIds: []string{resourceId},
Tags: create,
}).Execute()
if err != nil {
return utils.CheckThrottling(httpResp, err)
}
return nil
})
if err != nil {
return err
}
}
return nil
}

func updateBsuTags(conn *oscgo.APIClient, d *schema.ResourceData, addTags map[string]interface{}, delTags map[string]interface{}) error {

var resp oscgo.ReadVmsResponse
err := resource.Retry(60*time.Second, func() *resource.RetryError {
rp, httpResp, err := conn.VmApi.ReadVms(context.Background()).ReadVmsRequest(oscgo.ReadVmsRequest{
Filters: &oscgo.FiltersVm{
VmIds: &[]string{d.Id()},
},
}).Execute()

if err != nil {
return utils.CheckThrottling(httpResp, err)
}
resp = rp
return nil
})
if err != nil {
return err
}

if delTags != nil {
for dName := range delTags {
err := resource.Retry(60*time.Second, func() *resource.RetryError {
_, httpResp, err := conn.TagApi.DeleteTags(context.Background()).DeleteTagsRequest(oscgo.DeleteTagsRequest{
ResourceIds: []string{d.Id()},
Tags: remove,
ResourceIds: []string{utils.GetBsuId(resp.GetVms()[0], dName)},
Tags: tagsFromSliceMap(delTags[dName].(*schema.Set)),
}).Execute()
if err != nil {
if strings.Contains(fmt.Sprint(err), ".NotFound") {
return resource.RetryableError(err) // retry
}
return utils.CheckThrottling(httpResp, err)
}
return nil
Expand All @@ -41,16 +91,15 @@ func setOSCAPITags(conn *oscgo.APIClient, d *schema.ResourceData) error {
return err
}
}
if len(create) > 0 {
}
if addTags != nil {
for dName := range addTags {
err := resource.Retry(60*time.Second, func() *resource.RetryError {
_, httpResp, err := conn.TagApi.CreateTags(context.Background()).CreateTagsRequest(oscgo.CreateTagsRequest{
ResourceIds: []string{d.Id()},
Tags: create,
ResourceIds: []string{utils.GetBsuId(resp.GetVms()[0], dName)},
Tags: tagsFromSliceMap(addTags[dName].(*schema.Set)),
}).Execute()
if err != nil {
if strings.Contains(fmt.Sprint(err), ".NotFound") {
return resource.RetryableError(err) // retry
}
return utils.CheckThrottling(httpResp, err)
}
return nil
Expand All @@ -60,7 +109,6 @@ func setOSCAPITags(conn *oscgo.APIClient, d *schema.ResourceData) error {
}
}
}

return nil
}

Expand Down
Loading

0 comments on commit 37eee32

Please sign in to comment.