Skip to content

Commit

Permalink
VNET: Add new hold_ips parameter and deprecated previous ones
Browse files Browse the repository at this point in the history
This commit changes the behaviour and also fix #67 about holding IPs for
a VNET.
A proper way to use has been implemented. Now to hold IPs from AR the
administrator only needs to provide the list of IPs from all ranges.

`hold_size` and `ip_start` are now deprecated
  • Loading branch information
jaypif committed Oct 20, 2020
1 parent aeca41b commit 10a5012
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 19 deletions.
97 changes: 78 additions & 19 deletions opennebula/resource_opennebula_virtual_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,33 +244,45 @@ func resourceOpennebulaVirtualNetwork() *schema.Resource {
},
},
},
"hold_ips": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: "List of IPs to be held the VNET",
ConflictsWith: []string{"reservation_vnet", "reservation_size"},
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"hold_size": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "Carve a network reservation of this size from the reservation starting from `ip_hold`",
ConflictsWith: []string{"reservation_vnet", "reservation_size"},
Deprecated: "use 'hold_ips' instead",
},
"ip_hold": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Start IP of the range to be held",
ConflictsWith: []string{"reservation_vnet", "reservation_size"},
Deprecated: "use 'hold_ips' instead",
},
"reservation_vnet": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "Create a reservation from this VNET ID",
ConflictsWith: []string{"bridge", "physical_device", "ar", "hold_size", "ip_hold", "type", "vlan_id", "automatic_vlan_id", "mtu", "clusters", "dns", "gateway", "network_mask"},
ConflictsWith: []string{"bridge", "physical_device", "ar", "hold_ips", "hold_size", "ip_hold", "type", "vlan_id", "automatic_vlan_id", "mtu", "clusters", "dns", "gateway", "network_mask"},
},
"reservation_size": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "Reserve this many IPs from reservation_vnet",
ConflictsWith: []string{"bridge", "physical_device", "ar", "hold_size", "ip_hold", "type", "vlan_id", "automatic_vlan_id", "mtu", "clusters", "dns", "gateway", "network_mask"},
ConflictsWith: []string{"bridge", "physical_device", "ar", "hold_ips", "hold_size", "ip_hold", "type", "vlan_id", "automatic_vlan_id", "mtu", "clusters", "dns", "gateway", "network_mask"},
},
"security_groups": {
Type: schema.TypeList,
Expand Down Expand Up @@ -405,22 +417,6 @@ func resourceOpennebulaVirtualNetworkCreate(d *schema.ResourceData, meta interfa

d.SetId(fmt.Sprintf("%v", vnetID))

if d.Get("hold_size").(int) > 0 {
// add address range and reservations
ip := net.ParseIP(d.Get("ip_start").(string))
ip = ip.To4()

for i := 0; i < d.Get("hold_size").(int); i++ {
var address_reservation_string = `LEASES=[IP=%s]`
r_err := vnc.Hold(fmt.Sprintf(address_reservation_string, ip))
if r_err != nil {
return r_err
}

ip[3]++
}
}

// Call API once
update, err := generateVnTemplate(d)
if err != nil {
Expand Down Expand Up @@ -450,6 +446,34 @@ func resourceOpennebulaVirtualNetworkCreate(d *schema.ResourceData, meta interfa
return err
}
}

// Deprecated
if d.Get("hold_size").(int) > 0 {
// add address range and reservations
ip := net.ParseIP(d.Get("ip_hold").(string))
ip = ip.To4()

for i := 0; i < d.Get("hold_size").(int); i++ {
var address_reservation_string = `LEASES = [ IP = %s]`
r_err := vnc.Hold(fmt.Sprintf(address_reservation_string, ip))
if r_err != nil {
return r_err
}

ip[3]++
}
}

if hold_ips_list, ok := d.GetOk("hold_ips"); ok {
for _, ip := range hold_ips_list.([]interface{}) {
var address_reservation_string = `LEASES = [ IP = %s]`
r_err := vnc.Hold(fmt.Sprintf(address_reservation_string, ip.(string)))
if r_err != nil {
return r_err
}
}
}

}

// Set Security Groups
Expand Down Expand Up @@ -891,6 +915,18 @@ func resourceOpennebulaVirtualNetworkUpdate(d *schema.ResourceData, meta interfa
log.Printf("[INFO] Successfully updated group for Vnet %s\n", vn.Name)
}

if d.HasChange("hold_ips") {
// Release all old Held IPs
o_hold_ips_list, _ := d.GetChange("hold_ips")
for _, ip := range o_hold_ips_list.([]interface{}) {
var address_reservation_string = `LEASES = [ IP = %s]`
r_err := vnc.Release(fmt.Sprintf(address_reservation_string, ip.(string)))
if r_err != nil {
return r_err
}
}
}

// TODO: fix it after 5.10 release
// Force the "decrypt" bool to false to keep ONE 5.8 behavior
vn, err = vnc.Info(false)
Expand Down Expand Up @@ -929,6 +965,18 @@ func resourceOpennebulaVirtualNetworkUpdate(d *schema.ResourceData, meta interfa

}

if d.HasChange("hold_ips") {
_, n_hold_ips_list := d.GetChange("hold_ips")
// Hold only requested IPs
for _, ip := range n_hold_ips_list.([]interface{}) {
var address_reservation_string = `LEASES = [ IP = %s]`
r_err := vnc.Hold(fmt.Sprintf(address_reservation_string, ip.(string)))
if r_err != nil {
return r_err
}
}
}

return nil
}

Expand All @@ -938,6 +986,7 @@ func resourceOpennebulaVirtualNetworkDelete(d *schema.ResourceData, meta interfa
return err
}

// Deprecated
if d.Get("hold_size").(int) > 0 {
// add address range and reservations
ip := net.ParseIP(d.Get("ip_hold").(string))
Expand All @@ -953,9 +1002,19 @@ func resourceOpennebulaVirtualNetworkDelete(d *schema.ResourceData, meta interfa

ip[3]++
}
log.Printf("[INFO] Successfully released reservered IP addresses.")
}

if hold_ips_list, ok := d.GetOk("hold_ips"); ok {
for _, ip := range hold_ips_list.([]interface{}) {
var address_reservation_string = `LEASES = [ IP = %s]`
r_err := vnc.Release(fmt.Sprintf(address_reservation_string, ip.(string)))
if r_err != nil {
return r_err
}
}
}
log.Printf("[INFO] Successfully released reservered IP addresses.")

err = vnc.Delete()
if err != nil {
return err
Expand Down
9 changes: 9 additions & 0 deletions opennebula/resource_opennebula_virtual_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func TestAccVirtualNetwork(t *testing.T) {
resource.TestCheckResourceAttr("opennebula_virtual_network.test", "ar.1.ar_type", "IP4"),
resource.TestCheckResourceAttr("opennebula_virtual_network.test", "ar.1.size", "12"),
resource.TestCheckResourceAttr("opennebula_virtual_network.test", "ar.1.ip4", "172.16.100.130"),
resource.TestCheckResourceAttr("opennebula_virtual_network.test", "hold_ips.#", "2"),
resource.TestCheckResourceAttr("opennebula_virtual_network.test", "hold_ips.0", "172.16.100.112"),
resource.TestCheckResourceAttr("opennebula_virtual_network.test", "hold_ips.1", "172.16.100.131"),
resource.TestCheckResourceAttr("opennebula_virtual_network.test", "tags.%", "2"),
resource.TestCheckResourceAttr("opennebula_virtual_network.test", "tags.env", "prod"),
resource.TestCheckResourceAttr("opennebula_virtual_network.test", "tags.customer", "test"),
Expand Down Expand Up @@ -82,6 +85,9 @@ func TestAccVirtualNetwork(t *testing.T) {
resource.TestCheckResourceAttr("opennebula_virtual_network.test", "ar.2.ar_type", "IP6"),
resource.TestCheckResourceAttr("opennebula_virtual_network.test", "ar.2.size", "2"),
resource.TestCheckResourceAttr("opennebula_virtual_network.test", "ar.2.ip6", "2001:db8:0:85a3::ac1f:8001"),
resource.TestCheckResourceAttr("opennebula_virtual_network.test", "hold_ips.#", "2"),
resource.TestCheckResourceAttr("opennebula_virtual_network.test", "hold_ips.0", "172.16.100.112"),
resource.TestCheckResourceAttr("opennebula_virtual_network.test", "hold_ips.1", "172.16.100.141"),
resource.TestCheckResourceAttr("opennebula_virtual_network.test", "tags.%", "3"),
resource.TestCheckResourceAttr("opennebula_virtual_network.test", "tags.env", "dev"),
resource.TestCheckResourceAttr("opennebula_virtual_network.test", "tags.customer", "test"),
Expand Down Expand Up @@ -223,6 +229,7 @@ resource "opennebula_virtual_network" "test" {
size = 12
ip4 = "172.16.100.130"
}
hold_ips = ["172.16.100.112", "172.16.100.131"]
permissions = "642"
group = "oneadmin"
security_groups = [0]
Expand Down Expand Up @@ -259,6 +266,7 @@ resource "opennebula_virtual_network" "test" {
size = 2
ip6 = "2001:db8:0:85a3::ac1f:8001"
}
hold_ips = ["172.16.100.112", "172.16.100.141"]
security_groups = [0]
clusters = [0]
permissions = "660"
Expand Down Expand Up @@ -296,6 +304,7 @@ resource "opennebula_virtual_network" "test" {
size = 2
ip6 = "2001:db8:0:85a3::ac1f:8001"
}
hold_ips = ["172.16.100.112", "172.16.100.141"]
security_groups = [0]
clusters = [0]
permissions = "660"
Expand Down

0 comments on commit 10a5012

Please sign in to comment.