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

Add network datasource #1

Merged
merged 2 commits into from
Oct 4, 2018
Merged
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
41 changes: 41 additions & 0 deletions website/docs/d/network.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
title: "xenserver_network"
---

Provides information about the Networks of a XenServer host.

## Example Usage

```hcl
data "xenserver_network" "vlan-100" {
name_label = "VLAN_100"
}

resource "xenserver_vm" "web" {
name_label = "web"
base_template_name = "<desired template>"
static_mem_min = 2147483648 # 2GB
static_mem_max = 2147483648
dynamic_mem_min = 2147483648
dynamic_mem_max = 2147483648
boot_order = "cdn"
network_interface {
network_uuid = "<uuid>"
mac = "<desired-mac>"
mtu = 1500
device = 0
}
network_interface {
network_uuid = "${data.xenserver_network.vlan-100.id}"
mtu = 1500
device = 1
}
vcpus = 2
cdrom {
vdi_uuid = "<iso uuid>"
}
hard_drive {
vdi_uuid = "<desired vdi>"
}
}
```
52 changes: 52 additions & 0 deletions xenserver/data_source_network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package xenserver

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceXenServerNetwork() *schema.Resource {
return &schema.Resource{
Read: dataSourceXenServerNetworkRead,

Schema: map[string]*schema.Schema{
"name_label": &schema.Schema{
Type: schema.TypeString,
Description: "The human readable name of the Network",
Required: true,
},
},
}
}

func dataSourceXenServerNetworkRead(d *schema.ResourceData, meta interface{}) error {
c := meta.(*Connection)

nameLabel, nameLabelOk := d.GetOk("name_label")

if !nameLabelOk {
return fmt.Errorf("name_label must be provided")
}

if srs, err := c.client.Network.GetByNameLabel(c.session, nameLabel.(string)); err == nil {
found := false
for _, sr := range srs {
record, err := c.client.Network.GetRecord(c.session, sr)
if err != nil {
break
}

d.SetId(record.UUID)

found = true
break
}

if !found {
return fmt.Errorf("Matching Network not found")
}
}

return nil
}
7 changes: 4 additions & 3 deletions xenserver/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ func Provider() terraform.ResourceProvider {
},

DataSourcesMap: map[string]*schema.Resource{
"xenserver_pif": dataSourceXenServerPif(),
"xenserver_pifs": dataSourceXenServerPifs(),
"xenserver_sr": dataSourceXenServerSR(),
"xenserver_pif": dataSourceXenServerPif(),
"xenserver_pifs": dataSourceXenServerPifs(),
"xenserver_sr": dataSourceXenServerSR(),
"xenserver_network": dataSourceXenServerNetwork(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down