Skip to content

Commit

Permalink
feat: Add netbox_dcim_manufacturer data
Browse files Browse the repository at this point in the history
  • Loading branch information
smutel committed Aug 31, 2023
1 parent 92c401b commit 5d5c20f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/data-sources/netbox_dcim_manufacturer/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "netbox_dcim_manufacturer" "manufacturer_test" {
slug = "TestManufacturer"
}
66 changes: 66 additions & 0 deletions netbox/dcim/data_netbox_dcim_manufacturer.go
Original file line number Diff line number Diff line change
@@ -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/v7/netbox/internal/util"
)

func DataNetboxDcimManufacturer() *schema.Resource {
return &schema.Resource{
Description: "Get info about manufacturer (dcim module) from netbox.",
ReadContext: dataNetboxDcimManufacturerRead,

Schema: map[string]*schema.Schema{
"content_type": {
Type: schema.TypeString,
Computed: true,
Description: "The content type of this manufacturer (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 manufacturer (dcim module).",
},
},
}
}

func dataNetboxDcimManufacturerRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*netboxclient.NetBoxAPI)

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

p := dcim.NewDcimManufacturersListParams().WithSlug(&slug)

list, err := client.Dcim.DcimManufacturersList(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
}
1 change: 1 addition & 0 deletions netbox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func Provider() *schema.Provider {
"netbox_json_wireless_wireless_links_list": json.DataNetboxJSONWirelessWirelessLinksList(),
"netbox_dcim_device_role": dcim.DataNetboxDcimDeviceRole(),
"netbox_dcim_location": dcim.DataNetboxDcimLocation(),
"netbox_dcim_manufacturer": dcim.DataNetboxDcimManufacturer(),
"netbox_dcim_platform": dcim.DataNetboxDcimPlatform(),
"netbox_dcim_rack": dcim.DataNetboxDcimRack(),
"netbox_dcim_rack_role": dcim.DataNetboxDcimRackRole(),
Expand Down

0 comments on commit 5d5c20f

Please sign in to comment.