diff --git a/examples/main.tf b/examples/main.tf index c3c2a8507..562b22e00 100644 --- a/examples/main.tf +++ b/examples/main.tf @@ -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 diff --git a/netbox/data_netbox_virtualization_cluster.go b/netbox/data_netbox_virtualization_cluster.go new file mode 100644 index 000000000..897598a7e --- /dev/null +++ b/netbox/data_netbox_virtualization_cluster.go @@ -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 +} diff --git a/netbox/provider.go b/netbox/provider.go index a95e5fb67..eddc4f5d4 100644 --- a/netbox/provider.go +++ b/netbox/provider.go @@ -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(),