Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable adding tags on bsu #340

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions outscale/data_source_outscale_net_peerings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ const testAccDataSourceOutscaleOAPILinPeeringsConnectionConfig = `
}

resource "outscale_net_peering" "outscale_net_peering" {
accepter_net_id = "${outscale_net.outscale_net.net_id}"
source_net_id = "${outscale_net.outscale_net2.net_id}"
accepter_net_id = outscale_net.outscale_net.net_id
source_net_id = outscale_net.outscale_net2.net_id
tags {
key = "okht"
value = "testacc-peerings-ds"
Expand All @@ -50,14 +50,14 @@ const testAccDataSourceOutscaleOAPILinPeeringsConnectionConfig = `
}

resource "outscale_net_peering" "outscale_net_peering2" {
accepter_net_id = "${outscale_net.outscale_net.net_id}"
source_net_id = "${outscale_net.outscale_net2.net_id}"
accepter_net_id = outscale_net.outscale_net.net_id
source_net_id = outscale_net.outscale_net2.net_id
}

data "outscale_net_peerings" "outscale_net_peerings" {
filter {
name = "net_peering_ids"
values = ["${outscale_net_peering.outscale_net_peering.net_peering_id}"]
values = [outscale_net_peering.outscale_net_peering.net_peering_id]
}
}
`
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
Loading