Skip to content

Commit

Permalink
feat: Add Platform data
Browse files Browse the repository at this point in the history
  • Loading branch information
smutel committed Oct 31, 2020
1 parent 5fafa79 commit 7eb4c91
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
6 changes: 5 additions & 1 deletion examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,16 @@ data "netbox_virtualization_cluster" "cluster_test" {
name = "test"
}

data "netbox_dcim_platform" "platform_test" {
slug = "Debian_10"
}

resource "netbox_virtualization_vm" "vm_test" {
cluster_id = data.netbox_virtualization_cluster.cluster_test.id
name = "test"
disk = 10
memory = 10
platform_id = 1
platform_id = data.netbox_dcim_platform.platform_test.id
tenant_id = netbox_tenancy_tenant.tenant_test.id
role_id = 1

Expand Down
53 changes: 53 additions & 0 deletions netbox/data_netbox_dcim_platform.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package netbox

import (
"fmt"
"regexp"
"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/dcim"
)

func dataNetboxDcimPlatform() *schema.Resource {
return &schema.Resource{
Read: dataNetboxDcimPlatformRead,

Schema: map[string]*schema.Schema{
"slug": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile("^[-a-zA-Z0-9_]{1,50}$"),
"Must be like ^[-a-zA-Z0-9_]{1,50}$"),
},
},
}
}

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

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

resource := dcim.NewDcimPlatformsListParams().WithSlug(&slug)

list, err := client.Dcim.DcimPlatformsList(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
}
1 change: 1 addition & 0 deletions netbox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func Provider() *schema.Provider {
},
DataSourcesMap: map[string]*schema.Resource{
"netbox_dcim_site": dataNetboxDcimSite(),
"netbox_dcim_platform": dataNetboxDcimPlatform(),
"netbox_ipam_ip_addresses": dataNetboxIpamIPAddresses(),
"netbox_ipam_role": dataNetboxIpamRole(),
"netbox_ipam_vlan": dataNetboxIpamVlan(),
Expand Down

0 comments on commit 7eb4c91

Please sign in to comment.