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

Fix tag settings for reservations #590

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions opennebula/resource_opennebula_virtual_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,38 @@ func resourceOpennebulaVirtualNetworkCreate(ctx context.Context, d *schema.Resou
return diags
}

update := false
vnTemplate := vnet.Template
if tags, ok := d.Get("tags").(map[string]interface{}); ok {
for k, v := range tags {
update = true
key := strings.ToUpper(k)
vnTemplate.AddPair(key, v)
}
}
// add default tags if they aren't overriden
if len(config.defaultTags) > 0 {
for k, v := range config.defaultTags {
update = true
key := strings.ToUpper(k)
p, _ := vnTemplate.GetPair(key)
if p != nil {
continue
}
vnTemplate.AddPair(key, v)
}
}
if update {
err := vnc.Update(vnTemplate.String(), parameters.Replace)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failed to update content",
Detail: fmt.Sprintf("virtual network (ID: %s): %s", d.Id(), err),
})
return diags
}
}
d.SetId(fmt.Sprintf("%v", vnet.ID))

log.Printf("[DEBUG] New VNET reservation ID: %d", vnet.ID)
Expand Down
4 changes: 4 additions & 0 deletions opennebula/resource_opennebula_virtual_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func TestAccVirtualNetwork(t *testing.T) {
resource.TestCheckResourceAttr("opennebula_virtual_network.reservation1", "reservation_size", "5"),
resource.TestCheckResourceAttr("opennebula_virtual_network.reservation1", "reservation_first_ip", "172.16.100.115"),
resource.TestCheckResourceAttr("opennebula_virtual_network.reservation1", "permissions", "660"),
resource.TestCheckResourceAttr("opennebula_virtual_network.reservation1", "tags.MY_TAG", "tag_value"),
resource.TestCheckResourceAttrSet("opennebula_virtual_network.reservation1", "uid"),
resource.TestCheckResourceAttrSet("opennebula_virtual_network.reservation1", "gid"),
resource.TestCheckResourceAttrSet("opennebula_virtual_network.reservation1", "uname"),
Expand Down Expand Up @@ -575,6 +576,9 @@ resource "opennebula_virtual_network" "reservation1" {
reservation_first_ip = "172.16.100.115"
security_groups = [0]
permissions = 660
tags = {
MY_TAG = "tag_value"
}
}

resource "opennebula_virtual_network" "reservation2" {
Expand Down
Loading