Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add missing data objects #223

Merged
merged 8 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/data-sources/netbox_dcim_device_role/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "netbox_dcim_device_role" "device_role_test" {
slug = "TestDeviceRole"
}
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"
}
4 changes: 4 additions & 0 deletions examples/data-sources/netbox_ipam_asn/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "netbox_ipam_asn" "asn_test" {
asn = 15
rir_id = 15
}
4 changes: 4 additions & 0 deletions examples/data-sources/netbox_ipam_ip_range/data-source.tf
Original file line number Diff line number Diff line change
@@ -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"
}
3 changes: 3 additions & 0 deletions examples/data-sources/netbox_ipam_prefix/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "netbox_ipam_prefix" "prefix_test" {
prefix = "192.168.56.0/24"
}
3 changes: 3 additions & 0 deletions examples/data-sources/netbox_ipam_rir/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "netbox_ipam_rir" "rir_test" {
slug = "test-rir"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "netbox_ipam_route_targets" "rt_test" {
name = "rt-test"
}
66 changes: 66 additions & 0 deletions netbox/dcim/data_netbox_dcim_device_role.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 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
}
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
}
72 changes: 72 additions & 0 deletions netbox/ipam/data_netbox_ipam_asn.go
Original file line number Diff line number Diff line change
@@ -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
}
71 changes: 71 additions & 0 deletions netbox/ipam/data_netbox_ipam_ip_range.go
Original file line number Diff line number Diff line change
@@ -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
}
64 changes: 64 additions & 0 deletions netbox/ipam/data_netbox_ipam_prefix.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading