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 Feb 27, 2024
1 parent f277537 commit df3d348
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 62 deletions.
44 changes: 30 additions & 14 deletions outscale/data_source_outscale_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,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 +101,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 {
return err
}

if err := set("bsu_optimized", vm.GetBsuOptimized()); err != nil {
return err
}
Expand Down Expand Up @@ -198,18 +204,26 @@ func oapiVMDescriptionAttributes(set AttributeSetter, vm *oscgo.Vm) error {
return set("vm_type", vm.GetVmType())
}

func getOscAPIVMBlockDeviceMapping(blkMappings []oscgo.BlockDeviceMappingCreated) []map[string]interface{} {
res := []map[string]interface{}{}
for _, v := range blkMappings {
blk := map[string]interface{}{
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),
}
if bsu, ok := v.GetBsuOk(); ok {
blk["bsu"] = getOAPIBsuSet(*bsu)
}
res = append(res, blk)
blockDeviceMapping = append(blockDeviceMapping, blockDevice)
}
return res
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 getSecurityGroups(groupSet []oscgo.SecurityGroupLight) []map[string]interface{} {
Expand Down Expand Up @@ -291,7 +305,8 @@ func getOApiVMAttributesSchema() map[string]*schema.Schema {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"bsu": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -311,6 +326,7 @@ func getOApiVMAttributesSchema() map[string]*schema.Schema {
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 df3d348

Please sign in to comment.