Skip to content

Commit

Permalink
Merge pull request #3 from Thiryn/vrouter-nic-add-ip
Browse files Browse the repository at this point in the history
allow to add ips to vrouter nics
  • Loading branch information
Thiryn authored Aug 26, 2024
2 parents bb09d2d + fbcf514 commit 9783dbb
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 77 deletions.
7 changes: 7 additions & 0 deletions opennebula/helpers_vr.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ func vrNICAttach(ctx context.Context, timeout time.Duration, controller *goca.Co
updatedNICsLoop:
for i, nic := range updatedNICs {

// For VRouter NICs, floating IPs are set using the "IP" field, but it is represented in the ON API
// as the "VROUTER_IP" field.
if vrouterIP, err := nic.GetStr("VROUTER_IP"); err == nil {
if _, err = nic.GetStr("IP"); err != nil {
nic.Add("IP", vrouterIP)
}
}
for _, pair := range nicTpl.Pairs {

value, err := nic.GetStr(pair.Key())
Expand Down
21 changes: 11 additions & 10 deletions opennebula/resource_opennebula_virtual_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -1694,15 +1694,16 @@ func resourceOpennebulaVirtualNetworkDelete(ctx context.Context, d *schema.Resou
}
log.Printf("[INFO] Successfully released reservered IP addresses.")

err = vnc.Delete()
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failed to delete",
Detail: fmt.Sprintf("virtual network (ID: %s): %s", d.Id(), err),
})
return diags
}
err = resource.RetryContext(ctx, d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
err = vnc.Delete()
if err != nil {
if strings.Contains(err.Error(), "Can not remove a virtual network with leases in use") {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})

timeout := d.Timeout(schema.TimeoutDelete)
transient := []string{vn.Init.String(), vn.Ready.String()}
Expand All @@ -1716,7 +1717,7 @@ func resourceOpennebulaVirtualNetworkDelete(ctx context.Context, d *schema.Resou
return diags
}

log.Printf("[INFO] Successfully deleted Vnet\n")
log.Printf("[INFO] Successfully deleted Vnet (ID: %s)\n", d.Id())
return nil
}

Expand Down
18 changes: 18 additions & 0 deletions opennebula/resource_opennebula_virtual_router_nic.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ func resourceOpennebulaVirtualRouterNIC() *schema.Resource {
Default: false,
ForceNew: true,
},
"ip": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}
Expand Down Expand Up @@ -137,6 +143,10 @@ func resourceOpennebulaVirtualRouterNICCreate(ctx context.Context, d *schema.Res
}
}

if v, ok := d.GetOk("ip"); ok {
nicTpl.Add("IP", v.(string))
}

// wait before checking NIC
nicID, err := vrNICAttach(ctx, d.Timeout(schema.TimeoutCreate), controller, vRouterID, nicTpl)
if err != nil {
Expand Down Expand Up @@ -209,6 +219,13 @@ func resourceOpennebulaVirtualRouterNICRead(ctx context.Context, d *schema.Resou

floatingIP, _ := nic.GetStr("FLOATING_IP")
floatingOnly, _ := nic.GetStr("FLOATING_ONLY")
ip, _ := nic.Get("IP")

// For VRouter NICs, floating IPs are set using the "IP" field, but it is represented in the ON API
// as the "VROUTER_IP" field.
if strings.ToUpper(floatingIP) == "YES" {
ip, _ = nic.Get("VROUTER_IP")
}

d.Set("network_id", networkID)
d.Set("virtual_router_id", vr.ID)
Expand All @@ -219,6 +236,7 @@ func resourceOpennebulaVirtualRouterNICRead(ctx context.Context, d *schema.Resou
d.Set("security_groups", sg)
d.Set("floating_ip", strings.ToUpper(floatingIP) == "YES")
d.Set("floating_only", strings.ToUpper(floatingOnly) == "YES")
d.Set("ip", ip)

return nil
}
Expand Down
Loading

0 comments on commit 9783dbb

Please sign in to comment.