diff --git a/examples/data-sources/netbox_dcim_device_role/data-source.tf b/examples/data-sources/netbox_dcim_device_role/data-source.tf new file mode 100644 index 000000000..a65645946 --- /dev/null +++ b/examples/data-sources/netbox_dcim_device_role/data-source.tf @@ -0,0 +1,3 @@ +data "netbox_dcim_device_role" "device_role_test" { + slug = "TestDeviceRole" +} diff --git a/examples/data-sources/netbox_dcim_manufacturer/data-source.tf b/examples/data-sources/netbox_dcim_manufacturer/data-source.tf new file mode 100644 index 000000000..0cf4667d0 --- /dev/null +++ b/examples/data-sources/netbox_dcim_manufacturer/data-source.tf @@ -0,0 +1,3 @@ +data "netbox_dcim_manufacturer" "manufacturer_test" { + slug = "TestManufacturer" +} diff --git a/examples/data-sources/netbox_ipam_asn/data-source.tf b/examples/data-sources/netbox_ipam_asn/data-source.tf new file mode 100644 index 000000000..f2debe25e --- /dev/null +++ b/examples/data-sources/netbox_ipam_asn/data-source.tf @@ -0,0 +1,4 @@ +data "netbox_ipam_asn" "asn_test" { + asn = 15 + rir_id = 15 +} diff --git a/examples/data-sources/netbox_ipam_ip_range/data-source.tf b/examples/data-sources/netbox_ipam_ip_range/data-source.tf new file mode 100644 index 000000000..f5065cf57 --- /dev/null +++ b/examples/data-sources/netbox_ipam_ip_range/data-source.tf @@ -0,0 +1,4 @@ +data "netbox_ipam_ip_range" "iprange_test" { + start_address = "192.168.56.1/24" + end_address = "192.168.56.254/24" +} diff --git a/examples/data-sources/netbox_ipam_prefix/data-source.tf b/examples/data-sources/netbox_ipam_prefix/data-source.tf new file mode 100644 index 000000000..1192bb1a4 --- /dev/null +++ b/examples/data-sources/netbox_ipam_prefix/data-source.tf @@ -0,0 +1,3 @@ +data "netbox_ipam_prefix" "prefix_test" { + prefix = "192.168.56.0/24" +} diff --git a/examples/data-sources/netbox_ipam_rir/data-source.tf b/examples/data-sources/netbox_ipam_rir/data-source.tf new file mode 100644 index 000000000..716e2effe --- /dev/null +++ b/examples/data-sources/netbox_ipam_rir/data-source.tf @@ -0,0 +1,3 @@ +data "netbox_ipam_rir" "rir_test" { + slug = "test-rir" +} diff --git a/examples/data-sources/netbox_ipam_route_targets/data-source.tf b/examples/data-sources/netbox_ipam_route_targets/data-source.tf new file mode 100644 index 000000000..3aaaa60bf --- /dev/null +++ b/examples/data-sources/netbox_ipam_route_targets/data-source.tf @@ -0,0 +1,3 @@ +data "netbox_ipam_route_targets" "rt_test" { + name = "rt-test" +} diff --git a/netbox/dcim/data_netbox_dcim_device_role.go b/netbox/dcim/data_netbox_dcim_device_role.go new file mode 100644 index 000000000..41bf4be44 --- /dev/null +++ b/netbox/dcim/data_netbox_dcim_device_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/v7/netbox/internal/util" +) + +func DataNetboxDcimDeviceRole() *schema.Resource { + return &schema.Resource{ + Description: "Get info about rack role (dcim module) from netbox.", + ReadContext: dataNetboxDcimDeviceRoleRead, + + 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 dataNetboxDcimDeviceRoleRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + client := m.(*netboxclient.NetBoxAPI) + + slug := d.Get("slug").(string) + + p := dcim.NewDcimDeviceRolesListParams().WithSlug(&slug) + + list, err := client.Dcim.DcimDeviceRolesList(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/dcim/data_netbox_dcim_manufacturer.go b/netbox/dcim/data_netbox_dcim_manufacturer.go new file mode 100644 index 000000000..6b4c531b9 --- /dev/null +++ b/netbox/dcim/data_netbox_dcim_manufacturer.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/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 +} diff --git a/netbox/ipam/data_netbox_ipam_asn.go b/netbox/ipam/data_netbox_ipam_asn.go new file mode 100644 index 000000000..e98b3d8f3 --- /dev/null +++ b/netbox/ipam/data_netbox_ipam_asn.go @@ -0,0 +1,72 @@ +package ipam + +import ( + "context" + "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/ipam" + "github.com/smutel/terraform-provider-netbox/v7/netbox/internal/util" +) + +func DataNetboxIpamAsn() *schema.Resource { + return &schema.Resource{ + Description: "Get info about aggregate (ipam module) from Netbox.", + ReadContext: dataNetboxIpamAsnRead, + + Schema: map[string]*schema.Schema{ + "asn": { + Type: schema.TypeInt, + Required: true, + ValidateFunc: validation.IntAtLeast(1), + Description: "The asn number of this asn (ipam module).", + }, + "content_type": { + Type: schema.TypeString, + Computed: true, + Description: "The content type of this aggregate (ipam module).", + }, + "rir_id": { + Type: schema.TypeInt, + Required: true, + Description: "The rir for this asn (ipam module).", + }, + }, + } +} + +func dataNetboxIpamAsnRead(ctx context.Context, d *schema.ResourceData, + m interface{}) diag.Diagnostics { + client := m.(*netboxclient.NetBoxAPI) + + asn := d.Get("asn").(int) + asnStr := strconv.Itoa(asn) + rirID := d.Get("rir_id").(int) + rirIDStr := strconv.Itoa(rirID) + + p := ipam.NewIpamAsnsListParams().WithAsn(&asnStr).WithRirID(&rirIDStr) + + list, err := client.Ipam.IpamAsnsList(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/ipam/data_netbox_ipam_ip_range.go b/netbox/ipam/data_netbox_ipam_ip_range.go new file mode 100644 index 000000000..8267ad8b8 --- /dev/null +++ b/netbox/ipam/data_netbox_ipam_ip_range.go @@ -0,0 +1,71 @@ +package ipam + +import ( + "context" + "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/ipam" + "github.com/smutel/terraform-provider-netbox/v7/netbox/internal/util" +) + +func DataNetboxIpamIPRange() *schema.Resource { + return &schema.Resource{ + Description: "Get info about IP addresses (ipam module) from netbox.", + ReadContext: dataNetboxIpamIPRangeRead, + + Schema: map[string]*schema.Schema{ + "content_type": { + Type: schema.TypeString, + Computed: true, + Description: "The content type of this IP range (ipam module).", + }, + "start_address": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.IsCIDR, + Description: "The first address of this IP range", + }, + "end_address": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.IsCIDR, + Description: "The last address of this IP range (ipam module)", + }, + }, + } +} + +func dataNetboxIpamIPRangeRead(ctx context.Context, d *schema.ResourceData, + m interface{}) diag.Diagnostics { + client := m.(*netboxclient.NetBoxAPI) + + startAddress := d.Get("start_address").(string) + endAddress := d.Get("end_address").(string) + + p := ipam.NewIpamIPRangesListParams().WithStartAddress(&startAddress).WithEndAddress(&endAddress) + + list, err := client.Ipam.IpamIPRangesList(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/ipam/data_netbox_ipam_prefix.go b/netbox/ipam/data_netbox_ipam_prefix.go new file mode 100644 index 000000000..7bed8a04c --- /dev/null +++ b/netbox/ipam/data_netbox_ipam_prefix.go @@ -0,0 +1,64 @@ +package ipam + +import ( + "context" + "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/ipam" + "github.com/smutel/terraform-provider-netbox/v7/netbox/internal/util" +) + +func DataNetboxIpamPrefix() *schema.Resource { + return &schema.Resource{ + Description: "Get info about prefix (ipam module) from netbox.", + ReadContext: dataNetboxIpamPrefixRead, + + Schema: map[string]*schema.Schema{ + "content_type": { + Type: schema.TypeString, + Computed: true, + Description: "The content type of this prefix (ipam module).", + }, + "prefix": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ValidateFunc: validation.IsCIDRNetwork(0, 256), + Description: "The prefix (IP address/mask) used for this prefix (ipam module).", + }, + }, + } +} + +func dataNetboxIpamPrefixRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + client := m.(*netboxclient.NetBoxAPI) + + prefix := d.Get("prefix").(string) + + p := ipam.NewIpamPrefixesListParams().WithPrefix(&prefix) + + list, err := client.Ipam.IpamPrefixesList(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/ipam/data_netbox_ipam_rir.go b/netbox/ipam/data_netbox_ipam_rir.go new file mode 100644 index 000000000..7c1aaee68 --- /dev/null +++ b/netbox/ipam/data_netbox_ipam_rir.go @@ -0,0 +1,63 @@ +package ipam + +import ( + "context" + "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/ipam" + "github.com/smutel/terraform-provider-netbox/v7/netbox/internal/util" +) + +func DataNetboxIpamRir() *schema.Resource { + return &schema.Resource{ + Description: "Get info about prefix (ipam module) from netbox.", + ReadContext: dataNetboxIpamRirRead, + + Schema: map[string]*schema.Schema{ + "content_type": { + Type: schema.TypeString, + Computed: true, + Description: "The content type of this rir (ipam module).", + }, + "slug": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringLenBetween(1, 100), + Description: "The slug of this rir (ipam module).", + }, + }, + } +} + +func dataNetboxIpamRirRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + client := m.(*netboxclient.NetBoxAPI) + + slug := d.Get("slug").(string) + + p := ipam.NewIpamRirsListParams().WithSlug(&slug) + + list, err := client.Ipam.IpamRirsList(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/ipam/data_netbox_ipam_route_targets.go b/netbox/ipam/data_netbox_ipam_route_targets.go new file mode 100644 index 000000000..8746eb882 --- /dev/null +++ b/netbox/ipam/data_netbox_ipam_route_targets.go @@ -0,0 +1,63 @@ +package ipam + +import ( + "context" + "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/ipam" + "github.com/smutel/terraform-provider-netbox/v7/netbox/internal/util" +) + +func DataNetboxIpamRouteTargets() *schema.Resource { + return &schema.Resource{ + Description: "Get info about vrf (ipam module) from netbox.", + ReadContext: dataNetboxIpamRouteTargetsRead, + + Schema: map[string]*schema.Schema{ + "content_type": { + Type: schema.TypeString, + Computed: true, + Description: "The content type of this vrf (ipam module).", + }, + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringLenBetween(1, 21), + Description: "The name of this Route Targets (ipam module).", + }, + }, + } +} + +func dataNetboxIpamRouteTargetsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + client := m.(*netboxclient.NetBoxAPI) + + name := d.Get("name").(string) + + p := ipam.NewIpamRouteTargetsListParams().WithName(&name) + + list, err := client.Ipam.IpamRouteTargetsList(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 44f1597c4..6c31dda7b 100644 --- a/netbox/provider.go +++ b/netbox/provider.go @@ -150,15 +150,22 @@ func Provider() *schema.Provider { "netbox_json_wireless_wireless_lan_groups_list": json.DataNetboxJSONWirelessWirelessLanGroupsList(), "netbox_json_wireless_wireless_lans_list": json.DataNetboxJSONWirelessWirelessLansList(), "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(), "netbox_dcim_region": dcim.DataNetboxDcimRegion(), "netbox_dcim_site": dcim.DataNetboxDcimSite(), "netbox_ipam_aggregate": ipam.DataNetboxIpamAggregate(), + "netbox_ipam_asn": ipam.DataNetboxIpamAsn(), "netbox_ipam_ip_addresses": ipam.DataNetboxIpamIPAddresses(), + "netbox_ipam_ip_range": ipam.DataNetboxIpamIPRange(), + "netbox_ipam_prefix": ipam.DataNetboxIpamPrefix(), + "netbox_ipam_rir": ipam.DataNetboxIpamRir(), "netbox_ipam_role": ipam.DataNetboxIpamRole(), + "netbox_ipam_route_targets": ipam.DataNetboxIpamRouteTargets(), "netbox_ipam_service": ipam.DataNetboxIpamService(), "netbox_ipam_vlan": ipam.DataNetboxIpamVlan(), "netbox_ipam_vlan_group": ipam.DataNetboxIpamVlanGroup(), diff --git a/netbox/tenancy/data_netbox_tenancy_tenant_role.go b/netbox/tenancy/data_netbox_tenancy_contact_role.go similarity index 100% rename from netbox/tenancy/data_netbox_tenancy_tenant_role.go rename to netbox/tenancy/data_netbox_tenancy_contact_role.go