Skip to content

Commit

Permalink
Merge pull request #1 from aleixrm/f-550/data_opennebula_virtual_netw…
Browse files Browse the repository at this point in the history
…ork_address_range

F-550: Adds tests for vnet address range datasources
  • Loading branch information
shurkys authored Nov 18, 2024
2 parents 16dbaac + 9bc5635 commit edd26bd
Show file tree
Hide file tree
Showing 5 changed files with 451 additions and 32 deletions.
165 changes: 165 additions & 0 deletions opennebula/data_opennebula_virtual_network_address_range_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package opennebula

import (
"context"
"strconv"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccDataSourceOpennebulaVirtualNetworkAddressRange(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVirtualNetworkAddressRangeDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVirtualNetworkAddressRangeConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("opennebula_virtual_network.test_vnet", "id"),
resource.TestCheckResourceAttrSet("opennebula_virtual_network_address_range.test_ar_1", "id"),
resource.TestCheckResourceAttrSet("opennebula_virtual_network_address_range.test_ar_2", "id"),
),
},
{
Config: testAccDataSourceVirtualNetworkAddressRangeConfig + testAccDataSourceVirtualNetworkAddressRange1,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(
"data.opennebula_virtual_network_address_range.test_ar_1", "virtual_network_id",
"opennebula_virtual_network.test_vnet", "id",
),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "ar_type", "IP4"),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "size", "16"),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "ip4", "172.16.100.110"),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "ip4_end", "172.16.100.125"),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "global_prefix", ""),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "ula_prefix", ""),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "ip6", ""),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "ip6_end", ""),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "ip6_global", ""),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "ip6_global_end", ""),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "ip6_ula", ""),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "ip6_ula_end", ""),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "mac", "02:00:00:00:00:01"),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "mac_end", "02:00:00:00:00:10"),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "held_ips.#", "1"),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "held_ips.0", "172.16.100.112"),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "custom.%", "2"),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "custom.key1", "value1"),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_1", "custom.key2", "value2"),
),
},
{
Config: testAccDataSourceVirtualNetworkAddressRangeConfig + testAccDataSourceVirtualNetworkAddressRange2,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(
"data.opennebula_virtual_network_address_range.test_ar_2", "virtual_network_id",
"opennebula_virtual_network.test_vnet", "id",
),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_2", "ar_type", "IP6_STATIC"),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_2", "size", "15"),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_2", "ip6", "2001:db8::1"),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_2", "ip6_end", "2001:db8::f"),
resource.TestCheckResourceAttr("data.opennebula_virtual_network_address_range.test_ar_2", "mac", "02:00:00:00:01:01"),
),
},
},
})
}

func testAccCheckVirtualNetworkAddressRangeDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Configuration)
controller := config.Controller

for _, rs := range s.RootModule().Resources {
if rs.Type == "opennebula_virtual_network" && rs.Primary.Attributes["name"] == "test_vnet" {
vnID, _ := strconv.ParseUint(rs.Primary.ID, 10, 0)
vnc := controller.VirtualNetwork(int(vnID))

// Wait for Virtual Network deleted
stateConf := &resource.StateChangeConf{
Pending: []string{"exists"},
Target: []string{"deleted"},
Refresh: func() (any, string, error) {

vn, _ := vnc.Info(false)
if vn == nil {
return vn, "deleted", nil
}

return vn, "exists", nil
},
Timeout: 1 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}

_, err := stateConf.WaitForStateContext(context.Background())

return err
}
}

return nil
}

var testAccDataSourceVirtualNetworkAddressRangeConfig = `
resource "opennebula_virtual_network" "test_vnet" {
name = "test-vnet"
type = "dummy"
bridge = "onebr"
mtu = 1500
gateway = "172.16.100.1"
dns = "172.16.100.1"
network_mask = "255.255.255.0"
network_address = "172.16.100.0"
search_domain = "example.com"
permissions = "642"
group = "oneadmin"
security_groups = [0]
tags = {
env = "dev"
customer = "test"
}
}
resource "opennebula_virtual_network_address_range" "test_ar_1" {
virtual_network_id = opennebula_virtual_network.test_vnet.id
ar_type = "IP4"
size = 16
ip4 = "172.16.100.110"
hold_ips = ["172.16.100.112"]
mac = "02:00:00:00:00:01"
custom = {
key1 = "value1"
key2 = "value2"
}
}
resource "opennebula_virtual_network_address_range" "test_ar_2" {
virtual_network_id = opennebula_virtual_network.test_vnet.id
ar_type = "IP6_STATIC"
size = 15
ip6 = "2001:db8::1"
prefix_length = "64"
mac = "02:00:00:00:01:01"
}
`

var testAccDataSourceVirtualNetworkAddressRange1 = `
data "opennebula_virtual_network_address_range" "test_ar_1" {
virtual_network_id = opennebula_virtual_network.test_vnet.id
id = opennebula_virtual_network_address_range.test_ar_1.id
}
`

var testAccDataSourceVirtualNetworkAddressRange2 = `
data "opennebula_virtual_network_address_range" "test_ar_2" {
virtual_network_id = opennebula_virtual_network.test_vnet.id
id = opennebula_virtual_network_address_range.test_ar_2.id
}
`
89 changes: 78 additions & 11 deletions opennebula/data_opennebula_virtual_network_address_ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"

// "strings"

// vn "github.com/OpenNebula/one/src/oca/go/src/goca/schemas/virtualnetwork"
Expand Down Expand Up @@ -33,17 +34,87 @@ func dataSourceOpennebulaVirtualNetworkAddressRanges() *schema.Resource {
"ar_type": {
Type: schema.TypeString,
Computed: true,
Description: "Type of the Address Range: IP4, IP6.",
Description: "Type of the Address Range: IP4, IP6, IP4_6",
},
"ip4": {
Type: schema.TypeString,
Computed: true,
Description: "Start IPv4 of the range to be allocated.",
Description: "Start IPv4 of the range to be allocated",
},
"ip4_end": {
Type: schema.TypeString,
Computed: true,
Description: "End IPv4 of the range to be allocated",
},
"ip6": {
Type: schema.TypeString,
Computed: true,
Description: "Start IPv6 of the range to be allocated",
},
"ip6_end": {
Type: schema.TypeString,
Computed: true,
Description: "End IPv6 of the range to be allocated",
},
"ip6_global": {
Type: schema.TypeString,
Computed: true,
Description: "Global IPv6 of the range to be allocated",
},
"ip6_global_end": {
Type: schema.TypeString,
Computed: true,
Description: "End Global IPv6 of the range to be allocated",
},
"ip6_ula": {
Type: schema.TypeString,
Computed: true,
Description: "ULA IPv6 of the range to be allocated",
},
"ip6_ula_end": {
Type: schema.TypeString,
Computed: true,
Description: "End ULA IPv6 of the range to be allocated",
},
"size": {
Type: schema.TypeInt,
Computed: true,
Description: "Count of addresses in the IP range.",
Description: "Count of addresses in the IP range",
},
"mac": {
Type: schema.TypeString,
Computed: true,
Description: "Start MAC of the range to be allocated",
},
"mac_end": {
Type: schema.TypeString,
Computed: true,
Description: "End MAC of the range to be allocated",
},
"global_prefix": {
Type: schema.TypeString,
Computed: true,
Description: "Global prefix for IP6 or IP4_6",
},
"ula_prefix": {
Type: schema.TypeString,
Computed: true,
Description: "ULA prefix for IP6 or IP4_6",
},
"held_ips": {
Type: schema.TypeSet,
Computed: true,
Description: "List of IPs held in this address range",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"custom": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
Expand Down Expand Up @@ -75,15 +146,11 @@ func dataSourceOpennebulaVirtualNetworkAddressRangesRead(ctx context.Context, d
// Prepare the results array.
var addressRanges []interface{}

// Iterate over each address range and extract minimal information.
// Iterate over each address range and extract information.
for _, addressRange := range virtualNetworkInfo.ARs {
minimalInfo := map[string]interface{}{
"id": addressRange.ID,
"ar_type": addressRange.Type,
"ip4": addressRange.IP,
"size": addressRange.Size,
}
addressRanges = append(addressRanges, minimalInfo)
// Flatten the address range and append the data to the addressRanges list.
flattenedAddressRange := flattenAddressRange(addressRange)
addressRanges = append(addressRanges, flattenedAddressRange)
}

// Set the result and log success.
Expand Down
Loading

0 comments on commit edd26bd

Please sign in to comment.