From a9932dbd2ea7bd4cda38b56fc84b352184610742 Mon Sep 17 00:00:00 2001 From: Samuel Mutel <12967891+smutel@users.noreply.github.com> Date: Wed, 19 Apr 2023 17:15:30 +0200 Subject: [PATCH] feat: Add netbox_dcim_rack_role data --- .../netbox_dcim_rack_role/data-source.tf | 3 + netbox/dcim/data_netbox_dcim_rack_role.go | 66 +++++++++++++++++++ netbox/provider.go | 1 + 3 files changed, 70 insertions(+) create mode 100644 examples/data-sources/netbox_dcim_rack_role/data-source.tf create mode 100644 netbox/dcim/data_netbox_dcim_rack_role.go diff --git a/examples/data-sources/netbox_dcim_rack_role/data-source.tf b/examples/data-sources/netbox_dcim_rack_role/data-source.tf new file mode 100644 index 000000000..cbb28d744 --- /dev/null +++ b/examples/data-sources/netbox_dcim_rack_role/data-source.tf @@ -0,0 +1,3 @@ +data "netbox_dcim_rack_role" "rack_role_test" { + slug = "TestRackRole" +} diff --git a/netbox/dcim/data_netbox_dcim_rack_role.go b/netbox/dcim/data_netbox_dcim_rack_role.go new file mode 100644 index 000000000..157bae7c4 --- /dev/null +++ b/netbox/dcim/data_netbox_dcim_rack_role.go @@ -0,0 +1,66 @@ +package dcim + +import ( + "context" + "regexp" + "strconv" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + netboxclient "github.com/smutel/go-netbox/v3/netbox/client" + "github.com/smutel/go-netbox/v3/netbox/client/dcim" + "github.com/smutel/terraform-provider-netbox/v6/netbox/internal/util" +) + +func DataNetboxDcimRackRole() *schema.Resource { + return &schema.Resource{ + Description: "Get info about rack role (dcim module) from netbox.", + ReadContext: dataNetboxDcimRackRoleRead, + + Schema: map[string]*schema.Schema{ + "content_type": { + Type: schema.TypeString, + Computed: true, + Description: "The content type of this rack role (dcim module).", + }, + "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}$"), + Description: "The slug of the rack role (dcim module).", + }, + }, + } +} + +func dataNetboxDcimRackRoleRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + client := m.(*netboxclient.NetBoxAPI) + + slug := d.Get("slug").(string) + + p := dcim.NewDcimRackRolesListParams().WithSlug(&slug) + + list, err := client.Dcim.DcimRackRolesList(p, nil) + if err != nil { + return diag.FromErr(err) + } + + if *list.Payload.Count < 1 { + return diag.Errorf("Your query returned no results. " + + "Please change your search criteria and try again.") + } else if *list.Payload.Count > 1 { + return diag.Errorf("Your query returned more than one result. " + + "Please try a more specific search criteria.") + } + + r := list.Payload.Results[0] + d.SetId(strconv.FormatInt(r.ID, 10)) + if err = d.Set("content_type", util.ConvertURIContentType(r.URL)); err != nil { + return diag.FromErr(err) + } + + return nil +} diff --git a/netbox/provider.go b/netbox/provider.go index 85684791a..a223dd61d 100644 --- a/netbox/provider.go +++ b/netbox/provider.go @@ -151,6 +151,7 @@ func Provider() *schema.Provider { "netbox_json_wireless_wireless_links_list": json.DataNetboxJSONWirelessWirelessLinksList(), "netbox_dcim_platform": dcim.DataNetboxDcimPlatform(), "netbox_dcim_rack": dcim.DataNetboxDcimRack(), + "netbox_dcim_rack_role": dcim.DataNetboxDcimRackRole(), "netbox_dcim_site": dcim.DataNetboxDcimSite(), "netbox_ipam_aggregate": ipam.DataNetboxIpamAggregate(), "netbox_ipam_ip_addresses": ipam.DataNetboxIpamIPAddresses(),