Skip to content

Commit

Permalink
Add ASN data source (#285)
Browse files Browse the repository at this point in the history
* Add ASN data source

* Change naming standard for tag variables

* revert index.md change
  • Loading branch information
kyle-burnett authored Nov 4, 2022
1 parent da0c0a7 commit b364e39
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 0 deletions.
44 changes: 44 additions & 0 deletions docs/data-sources/asn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
# generated by https://github.com/fbreckle/terraform-plugin-docs
page_title: "netbox_asn Data Source - terraform-provider-netbox"
subcategory: "IP Address Management (IPAM)"
description: |-
---

# netbox_asn (Data Source)



## Example Usage

```terraform
data "netbox_asn" "asn_1" {
asn = "1111"
tag = "tag-1"
}
data "netbox_asn" "asn_2" {
tag = "tag-1"
tag__n = "tag-2"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `asn` (String) At least one of `asn` or `tag` must be given.
- `tag` (String) Tag to include in the data source filter (must match the tag's slug). At least one of `asn` or `tag` must be given.
- `tag__n` (String) Tag to exclude from the data source filter (must match the tag's slug).
Refer to [Netbox's documentation](https://demo.netbox.dev/static/docs/rest-api/filtering/#lookup-expressions)
for more information on available lookup expressions.

### Read-Only

- `description` (String)
- `id` (Number) The ID of this resource.
- `tags` (Set of String)


9 changes: 9 additions & 0 deletions examples/data-sources/netbox_asn/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data "netbox_asn" "asn_1" {
asn = "1111"
tag = "tag-1"
}

data "netbox_asn" "asn_2" {
tag = "tag-1"
tag__n = "tag-2"
}
83 changes: 83 additions & 0 deletions netbox/data_source_netbox_asn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package netbox

import (
"fmt"
"strconv"

"github.com/fbreckle/go-netbox/netbox/client"
"github.com/fbreckle/go-netbox/netbox/client/ipam"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceNetboxAsn() *schema.Resource {
return &schema.Resource{
Read: dataSourceNetboxAsnRead,
Description: `:meta:subcategory:IP Address Management (IPAM):`,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"asn": {
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: []string{"asn", "tag"},
},
"tag": {
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: []string{"asn", "tag"},
Description: "Tag to include in the data source filter (must match the tag's slug).",
},
"tag__n": {
Type: schema.TypeString,
Optional: true,
Description: `Tag to exclude from the data source filter (must match the tag's slug).
Refer to [Netbox's documentation](https://demo.netbox.dev/static/docs/rest-api/filtering/#lookup-expressions)
for more information on available lookup expressions.`,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchemaRead,
},
}
}

func dataSourceNetboxAsnRead(d *schema.ResourceData, m interface{}) error {
api := m.(*client.NetBoxAPI)

params := ipam.NewIpamAsnsListParams()

limit := int64(2) // Limit of 2 is enough
params.Limit = &limit

if asn, ok := d.Get("asn").(string); ok && asn != "" {
params.Asn = &asn
}

if tag, ok := d.Get("tag").(string); ok && tag != "" {
params.Tag = &tag
}
if tagn, ok := d.Get("tag__n").(string); ok && tagn != "" {
params.Tagn = &tagn
}

res, err := api.Ipam.IpamAsnsList(params, nil)
if err != nil {
return err
}

if count := *res.GetPayload().Count; count != int64(1) {
return fmt.Errorf("expected one ASN, but got %d", count)
}

result := res.GetPayload().Results[0]
d.Set("id", result.ID)
d.Set("asn", strconv.FormatInt(*result.Asn, 10))
d.Set("description", result.Description)
d.Set("tags", getTagListFromNestedTagList(result.Tags))
d.SetId(strconv.FormatInt(result.ID, 10))
return nil
}
78 changes: 78 additions & 0 deletions netbox/data_source_netbox_asn_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package netbox

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func testAccNetboxAsnSetUp(testName string) string {
return fmt.Sprintf(`
resource "netbox_rir" "test" {
name = "%[1]s"
}
resource "netbox_tag" "test" {
name = "%[1]s"
}
resource "netbox_asn" "test" {
asn = "123"
rir_id = netbox_rir.test.id
tags = [netbox_tag.test.slug]
}`, testName)
}

const testAccNetboxAsnNoResult = `
data "netbox_asn" "test" {
asn = "1337"
}`

func testAccNetboxAsnByAsn() string {
return fmt.Sprintf(`
data "netbox_asn" "test" {
asn = "123"
}`)
}

func testAccNetboxAsnByTag(testName string) string {
return fmt.Sprintf(`
data "netbox_asn" "test" {
tag = "%[1]s"
}`, testName)
}

func TestAccNetboxAsnDataSource_basic(t *testing.T) {
testName := testAccGetTestName("asn_ds_basic")
setUp := testAccNetboxAsnSetUp(testName)
resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: setUp,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_asn.test", "asn", "123"),
),
},
{
Config: setUp + testAccNetboxAsnNoResult,
ExpectError: regexp.MustCompile("expected one ASN, but got 0"),
},
{
Config: setUp + testAccNetboxAsnByAsn(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair("data.netbox_asn.test", "id", "netbox_asn.test", "id"),
),
},
{
Config: setUp + testAccNetboxAsnByTag(testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair("data.netbox_asn.test", "id", "netbox_asn.test", "id"),
resource.TestCheckResourceAttr("data.netbox_asn.test", "asn", "123"),
),
},
},
})
}
1 change: 1 addition & 0 deletions netbox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func Provider() *schema.Provider {
"netbox_site_group": resourceNetboxSiteGroup(),
},
DataSourcesMap: map[string]*schema.Resource{
"netbox_asn": dataSourceNetboxAsn(),
"netbox_cluster": dataSourceNetboxCluster(),
"netbox_cluster_group": dataSourceNetboxClusterGroup(),
"netbox_cluster_type": dataSourceNetboxClusterType(),
Expand Down

0 comments on commit b364e39

Please sign in to comment.