Skip to content

Commit

Permalink
Feature: wait_for_ip reports nic name and its ip address (#458)
Browse files Browse the repository at this point in the history
* feature: wait_for_ip reports nic and its address

* use camlCase instead of snake_case

* skipped GlobalUnicast checking

* added statement about network interfaces

* removed ip count checking
  • Loading branch information
goodbaikin authored Jul 22, 2022
1 parent 29f35af commit 79f71a7
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 8 deletions.
10 changes: 10 additions & 0 deletions docs/data-sources/wait_for_ip.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,15 @@ resource "ovirt_wait_for_ip" "test" {
### Read-Only

- `id` (String) The ID of this resource.
- `interfaces` (Set of Object) (see [below for nested schema](#nestedatt--interfaces))

<a id="nestedatt--interfaces"></a>
### Nested Schema for `interfaces`

Read-Only:

- `ipv4_addresses` (Set of String)
- `ipv6_addresses` (Set of String)
- `name` (String)


60 changes: 58 additions & 2 deletions internal/ovirt/data_source_ovirt_wait_for_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,35 @@ func (p *provider) waitForIPDataSource() *schema.Resource {
Description: "ID of the oVirt VM.",
Required: true,
},
"interfaces": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the interface.",
},
"ipv4_addresses": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "IP v4 addresses of the interface.",
},
"ipv6_addresses": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "IP v6 addresses of the interface.",
},
},
},
},
},
Description: `This data source will wait for the VM to report an IP address.`,
}
Expand All @@ -28,12 +57,39 @@ func (p *provider) waitForIPDataSourceRead(
_ interface{},
) diag.Diagnostics {
client := p.client.WithContext(ctx)
vmID := data.Get("vm_id").(string)

var vmID = data.Get("vm_id").(string)
_, err := client.WaitForNonLocalVMIPAddress(ovirtclient.VMID(vmID))
result, err := client.WaitForNonLocalVMIPAddress(ovirtclient.VMID(vmID))
if err != nil {
return errorToDiags("waiting for IP", err)
}

ifaces := make([]map[string]interface{}, 0)
for ifname, ips := range result {
iface := make(map[string]interface{}, 0)
iface["name"] = ifname

ipv4Addresses := make([]string, 0)
ipv6Addresses := make([]string, 0)
for _, ip := range ips {
ipv4 := ip.To4()
if ipv4 != nil {
ipv4Addresses = append(ipv4Addresses, ip.String())
} else {
ipv6Addresses = append(ipv6Addresses, ip.String())
}
}

iface["ipv4_addresses"] = ipv4Addresses
iface["ipv6_addresses"] = ipv6Addresses

ifaces = append(ifaces, iface)
}

if err := data.Set("interfaces", ifaces); err != nil {
return errorToDiags("set interfaces", err)
}
data.SetId(vmID)

return nil
}
19 changes: 13 additions & 6 deletions internal/ovirt/data_source_ovirt_wait_for_ip_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package ovirt

import (
"context"
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
ovirtclient "github.com/ovirt/go-ovirt-client/v2"
)

func TestOvirtWaitForIP(t *testing.T) {
Expand All @@ -21,7 +19,6 @@ func TestOvirtWaitForIP(t *testing.T) {

p := newProvider(newTestLogger(t))
helper := p.getTestHelper()
client := helper.GetClient().WithContext(context.Background())
clusterID := helper.GetClusterID()
templateID := helper.GetBlankTemplateID()
vnicProfileID := helper.GetVNICProfileID()
Expand Down Expand Up @@ -67,6 +64,14 @@ resource "ovirt_vm_start" "foo" {
data "ovirt_wait_for_ip" "test" {
vm_id = ovirt_vm_start.foo.vm_id
}
output "ipv4" {
value = data.ovirt_wait_for_ip.test.interfaces.*.ipv4_addresses
}
output "ipv6" {
value = data.ovirt_wait_for_ip.test.interfaces.*.ipv6_addresses
}
`,
clusterID,
templateID,
Expand All @@ -80,12 +85,14 @@ data "ovirt_wait_for_ip" "test" {
{
Config: config,
Check: func(state *terraform.State) error {
vmID := ovirtclient.VMID(state.RootModule().Resources["ovirt_vm.foo"].Primary.ID)
vmIPs, err := client.GetVMNonLocalIPAddresses(vmID)
ipv4 := state.RootModule().Outputs["ipv4"].Value.([]interface{})
ipv6 := state.RootModule().Outputs["ipv6"].Value.([]interface{})

if err != nil {
return err
}
if len(vmIPs) == 0 {

if len(ipv4) == 0 && len(ipv6) == 0 {
return fmt.Errorf("no non-local IP addresses found")
}
return nil
Expand Down

0 comments on commit 79f71a7

Please sign in to comment.