From 6285a451eaec3fc7aca0ef5cb08327e5612db20b Mon Sep 17 00:00:00 2001 From: Sander van den Bos Date: Thu, 9 Jun 2022 15:36:19 +0200 Subject: [PATCH 01/11] feat(prefix): add data source to retrieve prefix by description --- ...ata_source_netbox_prefix_by_description.go | 72 +++++++++++++++++++ ...ource_netbox_prefix_by_description_test.go | 36 ++++++++++ netbox/provider.go | 35 ++++----- 3 files changed, 126 insertions(+), 17 deletions(-) create mode 100644 netbox/data_source_netbox_prefix_by_description.go create mode 100644 netbox/data_source_netbox_prefix_by_description_test.go diff --git a/netbox/data_source_netbox_prefix_by_description.go b/netbox/data_source_netbox_prefix_by_description.go new file mode 100644 index 00000000..8170e1dc --- /dev/null +++ b/netbox/data_source_netbox_prefix_by_description.go @@ -0,0 +1,72 @@ +package netbox + +import ( + "errors" + "fmt" + + "github.com/fbreckle/go-netbox/netbox/client" + "github.com/fbreckle/go-netbox/netbox/client/ipam" + "github.com/fbreckle/go-netbox/netbox/models" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceNetboxPrefixByDescription() *schema.Resource { + return &schema.Resource{ + Read: dataSourceNetboxPrefixByDescriptionRead, + Schema: map[string]*schema.Schema{ + "description": { + Type: schema.TypeString, + Required: true, + }, + "cidr": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceNetboxPrefixByDescriptionRead(d *schema.ResourceData, m interface{}) error { + api := m.(*client.NetBoxAPI) + + description := d.Get("description").(string) + + id, prefix, err := dataSourceNetboxPrefixAPICall(api, description) + + if err != nil { + return err + } + + d.Set("cidr", prefix) + d.SetId(id) + return nil +} + +func dataSourceNetboxPrefixAPICall(api *client.NetBoxAPI, description string) (string, string, error) { + + params := ipam.NewIpamPrefixesListParams() + params.Q = &description + + res, err := api.Ipam.IpamPrefixesList(params, nil) + + if err != nil { + return "", "", err + } + + if *res.GetPayload().Count == int64(0) { + return "", "", errors.New(fmt.Sprintf("No result for %s", description)) + } + + var hit *models.Prefix + + for _, result := range res.GetPayload().Results { + if result.Description == description { + hit = result + if hit != nil { + return "", "", errors.New(fmt.Sprintf("Multiple matches found for %s, can't continue.", description)) + } + } + } + + return "", "", errors.New(fmt.Sprintf("No exact match found for %s", description)) +} diff --git a/netbox/data_source_netbox_prefix_by_description_test.go b/netbox/data_source_netbox_prefix_by_description_test.go new file mode 100644 index 00000000..fab8b046 --- /dev/null +++ b/netbox/data_source_netbox_prefix_by_description_test.go @@ -0,0 +1,36 @@ +package netbox + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccNetboxPrefixByDescDataSource_basic(t *testing.T) { + + testPrefix := "10.0.0.0/24" + testDesc := "test-prefix" + resource.ParallelTest(t, resource.TestCase{ + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: fmt.Sprintf(` +resource "netbox_prefix" "test" { + prefix = "%[1]s" + status = "active" + is_pool = true + description = "%[2]s" +} +data "netbox_prefix_by_description" "test" { + depends_on = [netbox_prefix.test] + description = "%[2]s" +}`, testPrefix, testDesc), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair("data.netbox_prefix.test", "id", "netbox_prefix.test", "id"), + ), + ExpectNonEmptyPlan: false, + }, + }, + }) +} diff --git a/netbox/provider.go b/netbox/provider.go index a50acc3c..5e14a534 100644 --- a/netbox/provider.go +++ b/netbox/provider.go @@ -48,23 +48,24 @@ func Provider() *schema.Provider { "netbox_custom_field": resourceCustomField(), }, DataSourcesMap: map[string]*schema.Resource{ - "netbox_cluster": dataSourceNetboxCluster(), - "netbox_cluster_group": dataSourceNetboxClusterGroup(), - "netbox_cluster_type": dataSourceNetboxClusterType(), - "netbox_tenant": dataSourceNetboxTenant(), - "netbox_tenants": dataSourceNetboxTenants(), - "netbox_tenant_group": dataSourceNetboxTenantGroup(), - "netbox_vrf": dataSourceNetboxVrf(), - "netbox_platform": dataSourceNetboxPlatform(), - "netbox_prefix": dataSourceNetboxPrefix(), - "netbox_device_role": dataSourceNetboxDeviceRole(), - "netbox_site": dataSourceNetboxSite(), - "netbox_tag": dataSourceNetboxTag(), - "netbox_virtual_machines": dataSourceNetboxVirtualMachine(), - "netbox_interfaces": dataSourceNetboxInterfaces(), - "netbox_ip_addresses": dataSourceNetboxIpAddresses(), - "netbox_ip_range": dataSourceNetboxIpRange(), - "netbox_region": dataSourceNetboxRegion(), + "netbox_cluster": dataSourceNetboxCluster(), + "netbox_cluster_group": dataSourceNetboxClusterGroup(), + "netbox_cluster_type": dataSourceNetboxClusterType(), + "netbox_tenant": dataSourceNetboxTenant(), + "netbox_tenants": dataSourceNetboxTenants(), + "netbox_tenant_group": dataSourceNetboxTenantGroup(), + "netbox_vrf": dataSourceNetboxVrf(), + "netbox_platform": dataSourceNetboxPlatform(), + "netbox_prefix": dataSourceNetboxPrefix(), + "netbox_prefix_by_description": dataSourceNetboxPrefixByDescription(), + "netbox_device_role": dataSourceNetboxDeviceRole(), + "netbox_site": dataSourceNetboxSite(), + "netbox_tag": dataSourceNetboxTag(), + "netbox_virtual_machines": dataSourceNetboxVirtualMachine(), + "netbox_interfaces": dataSourceNetboxInterfaces(), + "netbox_ip_addresses": dataSourceNetboxIpAddresses(), + "netbox_ip_range": dataSourceNetboxIpRange(), + "netbox_region": dataSourceNetboxRegion(), }, Schema: map[string]*schema.Schema{ "server_url": { From 8c5ab234c50a07ab0bed3874290363c65897c1f9 Mon Sep 17 00:00:00 2001 From: Sander van den Bos Date: Thu, 9 Jun 2022 15:50:10 +0200 Subject: [PATCH 02/11] fix(resource): rename output from cidr to prefix --- netbox/data_source_netbox_prefix_by_description.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/netbox/data_source_netbox_prefix_by_description.go b/netbox/data_source_netbox_prefix_by_description.go index 8170e1dc..6f6100b3 100644 --- a/netbox/data_source_netbox_prefix_by_description.go +++ b/netbox/data_source_netbox_prefix_by_description.go @@ -18,7 +18,7 @@ func dataSourceNetboxPrefixByDescription() *schema.Resource { Type: schema.TypeString, Required: true, }, - "cidr": { + "prefix": { Type: schema.TypeString, Computed: true, }, @@ -37,7 +37,7 @@ func dataSourceNetboxPrefixByDescriptionRead(d *schema.ResourceData, m interface return err } - d.Set("cidr", prefix) + d.Set("prefix", prefix) d.SetId(id) return nil } From 2eb3bc4cf918a7dc1014351950163d6e447d0e3e Mon Sep 17 00:00:00 2001 From: Sander van den Bos Date: Thu, 9 Jun 2022 16:07:16 +0200 Subject: [PATCH 03/11] fix(tests): fix failed test by generating unique prefix --- netbox/data_source_netbox_prefix_by_description_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/netbox/data_source_netbox_prefix_by_description_test.go b/netbox/data_source_netbox_prefix_by_description_test.go index fab8b046..898c0057 100644 --- a/netbox/data_source_netbox_prefix_by_description_test.go +++ b/netbox/data_source_netbox_prefix_by_description_test.go @@ -2,6 +2,7 @@ package netbox import ( "fmt" + "math/rand" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -10,7 +11,7 @@ import ( func TestAccNetboxPrefixByDescDataSource_basic(t *testing.T) { testPrefix := "10.0.0.0/24" - testDesc := "test-prefix" + testDesc := fmt.Sprintf("test-prefix-%d", rand.Intn(100)) resource.ParallelTest(t, resource.TestCase{ Providers: testAccProviders, Steps: []resource.TestStep{ From d68371f398018d3dc56db3b02b2104acbe1babf9 Mon Sep 17 00:00:00 2001 From: Sander van den Bos Date: Thu, 9 Jun 2022 22:01:27 +0200 Subject: [PATCH 04/11] fix(prefix):fix PR comments, add tests, update doc --- docs/data-sources/prefix.md | 17 +++- netbox/data_source_netbox_prefix.go | 66 ++++++++++++- ...ata_source_netbox_prefix_by_description.go | 72 --------------- ...ource_netbox_prefix_by_description_test.go | 37 -------- netbox/data_source_netbox_prefix_test.go | 92 +++++++++++++++++++ netbox/provider.go | 39 ++++---- 6 files changed, 190 insertions(+), 133 deletions(-) delete mode 100644 netbox/data_source_netbox_prefix_by_description.go delete mode 100644 netbox/data_source_netbox_prefix_by_description_test.go diff --git a/docs/data-sources/prefix.md b/docs/data-sources/prefix.md index b0a2041a..57695511 100644 --- a/docs/data-sources/prefix.md +++ b/docs/data-sources/prefix.md @@ -8,16 +8,27 @@ description: |- # netbox_prefix (Data Source) +## Example Usage +```terraform +data "netbox_prefix" "production" { + cidr = "10.0.0.0/18" +} +``` - +```terraform +data "netbox_prefix" "acceptance" { + description = "acc-eu-a" +} +``` ## Schema -### Required +### Optional -- `cidr` (String) +- `cidr` (String) Computed if not given +- `description` (String) Used when no prefix is given ### Read-Only diff --git a/netbox/data_source_netbox_prefix.go b/netbox/data_source_netbox_prefix.go index 389a71de..43fbcbad 100644 --- a/netbox/data_source_netbox_prefix.go +++ b/netbox/data_source_netbox_prefix.go @@ -2,10 +2,12 @@ package netbox import ( "errors" + "fmt" "strconv" "github.com/fbreckle/go-netbox/netbox/client" "github.com/fbreckle/go-netbox/netbox/client/ipam" + "github.com/fbreckle/go-netbox/netbox/models" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) @@ -20,9 +22,15 @@ func dataSourceNetboxPrefix() *schema.Resource { }, "cidr": { Type: schema.TypeString, - Required: true, + Computed: true, + Optional: true, ValidateFunc: validation.IsCIDR, }, + "description": { + Type: schema.TypeString, + Computed: true, + Optional: true, + }, }, } } @@ -31,7 +39,26 @@ func dataSourceNetboxPrefixRead(d *schema.ResourceData, m interface{}) error { api := m.(*client.NetBoxAPI) cidr := d.Get("cidr").(string) + description := d.Get("description").(string) + + if cidr == "" && description == "" { + return errors.New("Either a prefix or a description should be given.") + } else if cidr == "" && description != "" { + err := dataSourceNetBoxPrefixReadByDesc(api, d, description) + if err != nil { + return err + } + } else if cidr != "" { + err := dataSourceNetBoxPrefixReadByCidr(api, d, cidr) + if err != nil { + return err + } + } + + return nil +} +func dataSourceNetBoxPrefixReadByCidr(api *client.NetBoxAPI, d *schema.ResourceData, cidr string) error { params := ipam.NewIpamPrefixesListParams() params.Prefix = &cidr @@ -49,8 +76,45 @@ func dataSourceNetboxPrefixRead(d *schema.ResourceData, m interface{}) error { if *res.GetPayload().Count == int64(0) { return errors.New("No result") } + result := res.GetPayload().Results[0] d.Set("id", result.ID) + d.Set("description", result.Description) d.SetId(strconv.FormatInt(result.ID, 10)) return nil } + +func dataSourceNetBoxPrefixReadByDesc(api *client.NetBoxAPI, d *schema.ResourceData, description string) error { + params := ipam.NewIpamPrefixesListParams().WithDefaults() + params.Q = &description + + res, err := api.Ipam.IpamPrefixesList(params, nil) + if err != nil { + return err + } + + if *res.GetPayload().Count == int64(0) { + return errors.New(fmt.Sprintf("No result for %s", description)) + } + + var hit *models.Prefix + + for _, result := range res.GetPayload().Results { + if result.Description == description { + if hit != nil { + return errors.New(fmt.Sprintf("Multiple matches found for %s, can't continue.", description)) + } + + hit = result + } + } + + if hit == nil { + return errors.New(fmt.Sprintf("No exact match found for %s", description)) + } + + d.Set("id", hit.ID) + d.Set("cidr", hit.Prefix) + d.SetId(strconv.FormatInt(hit.ID, 10)) + return nil +} diff --git a/netbox/data_source_netbox_prefix_by_description.go b/netbox/data_source_netbox_prefix_by_description.go deleted file mode 100644 index 6f6100b3..00000000 --- a/netbox/data_source_netbox_prefix_by_description.go +++ /dev/null @@ -1,72 +0,0 @@ -package netbox - -import ( - "errors" - "fmt" - - "github.com/fbreckle/go-netbox/netbox/client" - "github.com/fbreckle/go-netbox/netbox/client/ipam" - "github.com/fbreckle/go-netbox/netbox/models" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" -) - -func dataSourceNetboxPrefixByDescription() *schema.Resource { - return &schema.Resource{ - Read: dataSourceNetboxPrefixByDescriptionRead, - Schema: map[string]*schema.Schema{ - "description": { - Type: schema.TypeString, - Required: true, - }, - "prefix": { - Type: schema.TypeString, - Computed: true, - }, - }, - } -} - -func dataSourceNetboxPrefixByDescriptionRead(d *schema.ResourceData, m interface{}) error { - api := m.(*client.NetBoxAPI) - - description := d.Get("description").(string) - - id, prefix, err := dataSourceNetboxPrefixAPICall(api, description) - - if err != nil { - return err - } - - d.Set("prefix", prefix) - d.SetId(id) - return nil -} - -func dataSourceNetboxPrefixAPICall(api *client.NetBoxAPI, description string) (string, string, error) { - - params := ipam.NewIpamPrefixesListParams() - params.Q = &description - - res, err := api.Ipam.IpamPrefixesList(params, nil) - - if err != nil { - return "", "", err - } - - if *res.GetPayload().Count == int64(0) { - return "", "", errors.New(fmt.Sprintf("No result for %s", description)) - } - - var hit *models.Prefix - - for _, result := range res.GetPayload().Results { - if result.Description == description { - hit = result - if hit != nil { - return "", "", errors.New(fmt.Sprintf("Multiple matches found for %s, can't continue.", description)) - } - } - } - - return "", "", errors.New(fmt.Sprintf("No exact match found for %s", description)) -} diff --git a/netbox/data_source_netbox_prefix_by_description_test.go b/netbox/data_source_netbox_prefix_by_description_test.go deleted file mode 100644 index 898c0057..00000000 --- a/netbox/data_source_netbox_prefix_by_description_test.go +++ /dev/null @@ -1,37 +0,0 @@ -package netbox - -import ( - "fmt" - "math/rand" - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" -) - -func TestAccNetboxPrefixByDescDataSource_basic(t *testing.T) { - - testPrefix := "10.0.0.0/24" - testDesc := fmt.Sprintf("test-prefix-%d", rand.Intn(100)) - resource.ParallelTest(t, resource.TestCase{ - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: fmt.Sprintf(` -resource "netbox_prefix" "test" { - prefix = "%[1]s" - status = "active" - is_pool = true - description = "%[2]s" -} -data "netbox_prefix_by_description" "test" { - depends_on = [netbox_prefix.test] - description = "%[2]s" -}`, testPrefix, testDesc), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrPair("data.netbox_prefix.test", "id", "netbox_prefix.test", "id"), - ), - ExpectNonEmptyPlan: false, - }, - }, - }) -} diff --git a/netbox/data_source_netbox_prefix_test.go b/netbox/data_source_netbox_prefix_test.go index 7edfd956..c47070c4 100644 --- a/netbox/data_source_netbox_prefix_test.go +++ b/netbox/data_source_netbox_prefix_test.go @@ -2,6 +2,7 @@ package netbox import ( "fmt" + "regexp" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -32,3 +33,94 @@ data "netbox_prefix" "test" { }, }) } + +func TestAccNetboxPrefixDataSource_description_single(t *testing.T) { + + testPrefix := "10.0.0.0/24" + testDesc := "test-description" + resource.ParallelTest(t, resource.TestCase{ + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: fmt.Sprintf(` +resource "netbox_prefix" "test" { + prefix = "%[1]s" + status = "active" + is_pool = true + description = "%[2]s" +} +data "netbox_prefix" "test" { + depends_on = [netbox_prefix.test] + description = "%[2]s" +}`, testPrefix, testDesc), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair("data.netbox_prefix.test", "id", "netbox_prefix.test", "id"), + ), + ExpectNonEmptyPlan: false, + }, + }, + }) +} + +func TestAccNetboxPrefixDataSource_description_multiple_failure(t *testing.T) { + + testPrefix := "10.0.0.0/24" + testPrefix2 := "10.0.64.0/26" + + testDesc := "test-description" + resource.ParallelTest(t, resource.TestCase{ + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: fmt.Sprintf(` +resource "netbox_prefix" "test" { + prefix = "%[1]s" + status = "active" + is_pool = true + description = "%[3]s" +} +resource "netbox_prefix" "test2" { + prefix = "%[2]s" + status = "active" + is_pool = true + description = "%[3]s" + } +data "netbox_prefix" "test" { + depends_on = [netbox_prefix.test] + description = "%[3]s" +}`, testPrefix, testPrefix2, testDesc), + ExpectError: regexp.MustCompile(fmt.Sprintf("Multiple matches found for %[1]s, can't continue.", testDesc)), + }, + }, + }) +} + +func TestAccNetboxPrefixDataSource_description_cidr(t *testing.T) { + + testPrefix := "10.0.0.0/24" + testDesc := "test-description" + resource.ParallelTest(t, resource.TestCase{ + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: fmt.Sprintf(` +resource "netbox_prefix" "test" { + prefix = "%[1]s" + status = "active" + is_pool = true + description = "%[2]s" +} +data "netbox_prefix" "test" { + depends_on = [netbox_prefix.test] + description = "%[2]s" +}`, testPrefix, testDesc), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair("data.netbox_prefix.test", "id", "netbox_prefix.test", "id"), + resource.TestCheckResourceAttrPair("data.netbox_prefix.test", "cidr", "netbox_prefix.test", "cidr"), + resource.TestCheckResourceAttrPair("data.netbox_prefix.test", "description", "netbox_prefix.test", "description"), + ), + ExpectNonEmptyPlan: false, + }, + }, + }) +} diff --git a/netbox/provider.go b/netbox/provider.go index 5f234345..a1d0f7ca 100644 --- a/netbox/provider.go +++ b/netbox/provider.go @@ -52,26 +52,25 @@ func Provider() *schema.Provider { "netbox_custom_field": resourceCustomField(), }, DataSourcesMap: map[string]*schema.Resource{ - "netbox_cluster": dataSourceNetboxCluster(), - "netbox_cluster_group": dataSourceNetboxClusterGroup(), - "netbox_cluster_type": dataSourceNetboxClusterType(), - "netbox_tenant": dataSourceNetboxTenant(), - "netbox_tenants": dataSourceNetboxTenants(), - "netbox_tenant_group": dataSourceNetboxTenantGroup(), - "netbox_vrf": dataSourceNetboxVrf(), - "netbox_platform": dataSourceNetboxPlatform(), - "netbox_prefix": dataSourceNetboxPrefix(), - "netbox_prefix_by_description": dataSourceNetboxPrefixByDescription(), - "netbox_device_role": dataSourceNetboxDeviceRole(), - "netbox_device_type": dataSourceNetboxDeviceType(), - "netbox_site": dataSourceNetboxSite(), - "netbox_tag": dataSourceNetboxTag(), - "netbox_virtual_machines": dataSourceNetboxVirtualMachine(), - "netbox_interfaces": dataSourceNetboxInterfaces(), - "netbox_ip_addresses": dataSourceNetboxIpAddresses(), - "netbox_ip_range": dataSourceNetboxIpRange(), - "netbox_region": dataSourceNetboxRegion(), - "netbox_vlan": dataSourceNetboxVlan(), + "netbox_cluster": dataSourceNetboxCluster(), + "netbox_cluster_group": dataSourceNetboxClusterGroup(), + "netbox_cluster_type": dataSourceNetboxClusterType(), + "netbox_tenant": dataSourceNetboxTenant(), + "netbox_tenants": dataSourceNetboxTenants(), + "netbox_tenant_group": dataSourceNetboxTenantGroup(), + "netbox_vrf": dataSourceNetboxVrf(), + "netbox_platform": dataSourceNetboxPlatform(), + "netbox_prefix": dataSourceNetboxPrefix(), + "netbox_device_role": dataSourceNetboxDeviceRole(), + "netbox_device_type": dataSourceNetboxDeviceType(), + "netbox_site": dataSourceNetboxSite(), + "netbox_tag": dataSourceNetboxTag(), + "netbox_virtual_machines": dataSourceNetboxVirtualMachine(), + "netbox_interfaces": dataSourceNetboxInterfaces(), + "netbox_ip_addresses": dataSourceNetboxIpAddresses(), + "netbox_ip_range": dataSourceNetboxIpRange(), + "netbox_region": dataSourceNetboxRegion(), + "netbox_vlan": dataSourceNetboxVlan(), }, Schema: map[string]*schema.Schema{ "server_url": { From 71813fe88eb85ffcb0c5206e60165c79fbf8f723 Mon Sep 17 00:00:00 2001 From: Sander van den Bos Date: Thu, 9 Jun 2022 22:15:51 +0200 Subject: [PATCH 05/11] fix(prefix): fix docs --- docs/data-sources/prefix.md | 18 ++++++++++-------- .../data-sources/netbox_prefix/data-source.tf | 9 +++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 examples/data-sources/netbox_prefix/data-source.tf diff --git a/docs/data-sources/prefix.md b/docs/data-sources/prefix.md index 57695511..b5657d77 100644 --- a/docs/data-sources/prefix.md +++ b/docs/data-sources/prefix.md @@ -8,17 +8,19 @@ description: |- # netbox_prefix (Data Source) + + ## Example Usage ```terraform -data "netbox_prefix" "production" { - cidr = "10.0.0.0/18" +//Retrieve resource by cidr +resource "netbox_prefix" "cidr" { + cidr = "10.0.0.0/16" } -``` -```terraform -data "netbox_prefix" "acceptance" { - description = "acc-eu-a" +//Retrieve resource by description +resource "netbox_prefix" "description" { + description = "prod-eu-west-1a" } ``` @@ -27,8 +29,8 @@ data "netbox_prefix" "acceptance" { ### Optional -- `cidr` (String) Computed if not given -- `description` (String) Used when no prefix is given +- `cidr` (String) +- `description` (String) ### Read-Only diff --git a/examples/data-sources/netbox_prefix/data-source.tf b/examples/data-sources/netbox_prefix/data-source.tf new file mode 100644 index 00000000..585d9526 --- /dev/null +++ b/examples/data-sources/netbox_prefix/data-source.tf @@ -0,0 +1,9 @@ +//Retrieve resource by cidr +resource "netbox_prefix" "cidr" { + cidr = "10.0.0.0/16" +} + +//Retrieve resource by description +resource "netbox_prefix" "description" { + description = "prod-eu-west-1a" +} \ No newline at end of file From a4cb3107f98f5dabc8030bd2d94b4e9e4b4baf27 Mon Sep 17 00:00:00 2001 From: Sander van den Bos Date: Thu, 9 Jun 2022 22:26:32 +0200 Subject: [PATCH 06/11] fix(prefix): fix test --- netbox/data_source_netbox_prefix_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/netbox/data_source_netbox_prefix_test.go b/netbox/data_source_netbox_prefix_test.go index c47070c4..e04b3a07 100644 --- a/netbox/data_source_netbox_prefix_test.go +++ b/netbox/data_source_netbox_prefix_test.go @@ -98,7 +98,7 @@ data "netbox_prefix" "test" { func TestAccNetboxPrefixDataSource_description_cidr(t *testing.T) { testPrefix := "10.0.0.0/24" - testDesc := "test-description" + testDesc := "test-description-cidr" resource.ParallelTest(t, resource.TestCase{ Providers: testAccProviders, Steps: []resource.TestStep{ @@ -116,7 +116,7 @@ data "netbox_prefix" "test" { }`, testPrefix, testDesc), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair("data.netbox_prefix.test", "id", "netbox_prefix.test", "id"), - resource.TestCheckResourceAttrPair("data.netbox_prefix.test", "cidr", "netbox_prefix.test", "cidr"), + resource.TestCheckResourceAttrPair("data.netbox_prefix.test", "cidr", "netbox_prefix.test", "prefix"), resource.TestCheckResourceAttrPair("data.netbox_prefix.test", "description", "netbox_prefix.test", "description"), ), ExpectNonEmptyPlan: false, From e1ce19597092f2f3e945931fc2b327f4f18dbf0a Mon Sep 17 00:00:00 2001 From: Sander van den Bos Date: Fri, 10 Jun 2022 12:22:19 +0200 Subject: [PATCH 07/11] fix(prefix): adjust tests --- netbox/data_source_netbox_prefix_test.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/netbox/data_source_netbox_prefix_test.go b/netbox/data_source_netbox_prefix_test.go index e04b3a07..4cbe85ab 100644 --- a/netbox/data_source_netbox_prefix_test.go +++ b/netbox/data_source_netbox_prefix_test.go @@ -10,7 +10,7 @@ import ( func TestAccNetboxPrefixDataSource_basic(t *testing.T) { - testPrefix := "10.0.0.0/24" + testPrefix := "3.2.1.0/24" resource.ParallelTest(t, resource.TestCase{ Providers: testAccProviders, Steps: []resource.TestStep{ @@ -36,8 +36,9 @@ data "netbox_prefix" "test" { func TestAccNetboxPrefixDataSource_description_single(t *testing.T) { - testPrefix := "10.0.0.0/24" - testDesc := "test-description" + testPrefix := "1.2.4.0/24" + testSlug := "test-description" + testDesc := testAccGetTestName(testSlug) resource.ParallelTest(t, resource.TestCase{ Providers: testAccProviders, Steps: []resource.TestStep{ @@ -64,10 +65,11 @@ data "netbox_prefix" "test" { func TestAccNetboxPrefixDataSource_description_multiple_failure(t *testing.T) { - testPrefix := "10.0.0.0/24" - testPrefix2 := "10.0.64.0/26" + testPrefix := "1.2.3.2/24" + testPrefix2 := "1.2.64.2/24" - testDesc := "test-description" + testSlug := "test-description" + testDesc := testAccGetTestName(testSlug) resource.ParallelTest(t, resource.TestCase{ Providers: testAccProviders, Steps: []resource.TestStep{ @@ -97,8 +99,9 @@ data "netbox_prefix" "test" { func TestAccNetboxPrefixDataSource_description_cidr(t *testing.T) { - testPrefix := "10.0.0.0/24" - testDesc := "test-description-cidr" + testPrefix := "16.32.64.8/24" + testSlug := "test-description-cidr" + testDesc := testAccGetTestName(testSlug) resource.ParallelTest(t, resource.TestCase{ Providers: testAccProviders, Steps: []resource.TestStep{ From 0b7583a2bff9c4e9f4e6f4821398c5665f1ea62d Mon Sep 17 00:00:00 2001 From: Sander van den Bos Date: Fri, 10 Jun 2022 12:34:50 +0200 Subject: [PATCH 08/11] fix(prefix): add uncommitted prefix change --- netbox/data_source_netbox_prefix_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox/data_source_netbox_prefix_test.go b/netbox/data_source_netbox_prefix_test.go index 4cbe85ab..f518522b 100644 --- a/netbox/data_source_netbox_prefix_test.go +++ b/netbox/data_source_netbox_prefix_test.go @@ -99,7 +99,7 @@ data "netbox_prefix" "test" { func TestAccNetboxPrefixDataSource_description_cidr(t *testing.T) { - testPrefix := "16.32.64.8/24" + testPrefix := "16.32.64.0/24" testSlug := "test-description-cidr" testDesc := testAccGetTestName(testSlug) resource.ParallelTest(t, resource.TestCase{ From eab758edc7f08abf6ab3b3987222901559ee0f3a Mon Sep 17 00:00:00 2001 From: Sander van den Bos Date: Fri, 10 Jun 2022 12:41:38 +0200 Subject: [PATCH 09/11] fix(prefix): adjust more prefixes in tests --- netbox/data_source_netbox_prefix_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/netbox/data_source_netbox_prefix_test.go b/netbox/data_source_netbox_prefix_test.go index f518522b..35e23573 100644 --- a/netbox/data_source_netbox_prefix_test.go +++ b/netbox/data_source_netbox_prefix_test.go @@ -65,8 +65,8 @@ data "netbox_prefix" "test" { func TestAccNetboxPrefixDataSource_description_multiple_failure(t *testing.T) { - testPrefix := "1.2.3.2/24" - testPrefix2 := "1.2.64.2/24" + testPrefix := "1.2.3.0/24" + testPrefix2 := "1.2.64.0/24" testSlug := "test-description" testDesc := testAccGetTestName(testSlug) From 02166d636893368b9975095cc760862d5e8499dc Mon Sep 17 00:00:00 2001 From: Sander van den Bos Date: Fri, 10 Jun 2022 12:51:27 +0200 Subject: [PATCH 10/11] fix(prefix): update test names --- netbox/data_source_netbox_prefix_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/netbox/data_source_netbox_prefix_test.go b/netbox/data_source_netbox_prefix_test.go index 35e23573..bd496eda 100644 --- a/netbox/data_source_netbox_prefix_test.go +++ b/netbox/data_source_netbox_prefix_test.go @@ -37,7 +37,7 @@ data "netbox_prefix" "test" { func TestAccNetboxPrefixDataSource_description_single(t *testing.T) { testPrefix := "1.2.4.0/24" - testSlug := "test-description" + testSlug := "prefix_description_single" testDesc := testAccGetTestName(testSlug) resource.ParallelTest(t, resource.TestCase{ Providers: testAccProviders, @@ -68,7 +68,7 @@ func TestAccNetboxPrefixDataSource_description_multiple_failure(t *testing.T) { testPrefix := "1.2.3.0/24" testPrefix2 := "1.2.64.0/24" - testSlug := "test-description" + testSlug := "prefix_description_multiple_failure" testDesc := testAccGetTestName(testSlug) resource.ParallelTest(t, resource.TestCase{ Providers: testAccProviders, @@ -100,7 +100,7 @@ data "netbox_prefix" "test" { func TestAccNetboxPrefixDataSource_description_cidr(t *testing.T) { testPrefix := "16.32.64.0/24" - testSlug := "test-description-cidr" + testSlug := "tesst_description_cidr" testDesc := testAccGetTestName(testSlug) resource.ParallelTest(t, resource.TestCase{ Providers: testAccProviders, From 029fd690c56dc50d14b4441e48311dc0722eeead Mon Sep 17 00:00:00 2001 From: Sander van den Bos Date: Fri, 10 Jun 2022 14:03:08 +0200 Subject: [PATCH 11/11] fix(prefix): fix another slug --- netbox/data_source_netbox_prefix_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox/data_source_netbox_prefix_test.go b/netbox/data_source_netbox_prefix_test.go index bd496eda..75cce3a8 100644 --- a/netbox/data_source_netbox_prefix_test.go +++ b/netbox/data_source_netbox_prefix_test.go @@ -100,7 +100,7 @@ data "netbox_prefix" "test" { func TestAccNetboxPrefixDataSource_description_cidr(t *testing.T) { testPrefix := "16.32.64.0/24" - testSlug := "tesst_description_cidr" + testSlug := "prefix_description_cidr" testDesc := testAccGetTestName(testSlug) resource.ParallelTest(t, resource.TestCase{ Providers: testAccProviders,