Skip to content

Commit

Permalink
feat: Add Cluster data
Browse files Browse the repository at this point in the history
  • Loading branch information
smutel committed Oct 31, 2020
1 parent f340062 commit 5fafa79
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
6 changes: 5 additions & 1 deletion examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ resource "netbox_ipam_ip_addresses" "ip_test" {
}
}

data "netbox_virtualization_cluster" "cluster_test" {
name = "test"
}

resource "netbox_virtualization_vm" "vm_test" {
cluster_id = 1
cluster_id = data.netbox_virtualization_cluster.cluster_test.id
name = "test"
disk = 10
memory = 10
Expand Down
52 changes: 52 additions & 0 deletions netbox/data_netbox_virtualization_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package netbox

import (
"fmt"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
netboxclient "github.com/netbox-community/go-netbox/netbox/client"
"github.com/netbox-community/go-netbox/netbox/client/virtualization"
)

func dataNetboxVirtualizationCluster() *schema.Resource {
return &schema.Resource{
Read: dataNetboxVirtualizationClusterRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 100),
},
},
}
}

func dataNetboxVirtualizationClusterRead(d *schema.ResourceData,
m interface{}) error {
client := m.(*netboxclient.NetBoxAPI)

name := d.Get("name").(string)

resource := virtualization.NewVirtualizationClustersListParams().WithName(
&name)

list, err := client.Virtualization.VirtualizationClustersList(resource, nil)
if err != nil {
return err
}

if *list.Payload.Count < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
} else if *list.Payload.Count > 1 {
return fmt.Errorf("Your query returned more than one result. " +
"Please try a more specific search criteria.")
}

d.SetId(strconv.FormatInt(list.Payload.Results[0].ID, 10))

return nil
}
15 changes: 8 additions & 7 deletions netbox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ func Provider() *schema.Provider {
},
},
DataSourcesMap: map[string]*schema.Resource{
"netbox_dcim_site": dataNetboxDcimSite(),
"netbox_ipam_ip_addresses": dataNetboxIpamIPAddresses(),
"netbox_ipam_role": dataNetboxIpamRole(),
"netbox_ipam_vlan": dataNetboxIpamVlan(),
"netbox_ipam_vlan_group": dataNetboxIpamVlanGroup(),
"netbox_tenancy_tenant": dataNetboxTenancyTenant(),
"netbox_tenancy_tenant_group": dataNetboxTenancyTenantGroup(),
"netbox_dcim_site": dataNetboxDcimSite(),
"netbox_ipam_ip_addresses": dataNetboxIpamIPAddresses(),
"netbox_ipam_role": dataNetboxIpamRole(),
"netbox_ipam_vlan": dataNetboxIpamVlan(),
"netbox_ipam_vlan_group": dataNetboxIpamVlanGroup(),
"netbox_tenancy_tenant": dataNetboxTenancyTenant(),
"netbox_tenancy_tenant_group": dataNetboxTenancyTenantGroup(),
"netbox_virtualization_cluster": dataNetboxVirtualizationCluster(),
},
ResourcesMap: map[string]*schema.Resource{
"netbox_ipam_prefix": resourceNetboxIpamPrefix(),
Expand Down

0 comments on commit 5fafa79

Please sign in to comment.