generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ASN data source * Change naming standard for tag variables * revert index.md change
- Loading branch information
1 parent
da0c0a7
commit b364e39
Showing
5 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters