diff --git a/docs/data-sources/vrfs.md b/docs/data-sources/vrfs.md new file mode 100644 index 00000000..0dedef01 --- /dev/null +++ b/docs/data-sources/vrfs.md @@ -0,0 +1,48 @@ +--- +# generated by https://github.com/fbreckle/terraform-plugin-docs +page_title: "netbox_vrfs Data Source - terraform-provider-netbox" +subcategory: "IP Address Management (IPAM)" +description: |- + +--- + +# netbox_vrfs (Data Source) + + + + + + +## Schema + +### Optional + +- `filter` (Block Set) (see [below for nested schema](#nestedblock--filter)) +- `limit` (Number) Defaults to `0`. + +### Read-Only + +- `id` (String) The ID of this resource. +- `vrfs` (List of Object) (see [below for nested schema](#nestedatt--vrfs)) + + +### Nested Schema for `filter` + +Required: + +- `name` (String) +- `value` (String) + + + +### Nested Schema for `vrfs` + +Read-Only: + +- `description` (String) +- `id` (Number) +- `name` (String) +- `rd` (String) +- `tenant` (Number) + + diff --git a/netbox/data_source_netbox_vrfs.go b/netbox/data_source_netbox_vrfs.go new file mode 100644 index 00000000..f0acf309 --- /dev/null +++ b/netbox/data_source_netbox_vrfs.go @@ -0,0 +1,149 @@ +package netbox + +import ( + "errors" + "fmt" + + "github.com/fbreckle/go-netbox/netbox/client" + "github.com/fbreckle/go-netbox/netbox/client/ipam" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/id" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" +) + +func dataSourceNetboxVrfs() *schema.Resource { + return &schema.Resource{ + Read: dataSourceNetboxVrfsRead, + Description: `:meta:subcategory:IP Address Management (IPAM):`, + Schema: map[string]*schema.Schema{ + "filter": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "value": { + Type: schema.TypeString, + Required: true, + }, + }, + }, + }, + "limit": { + Type: schema.TypeInt, + Optional: true, + ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtLeast(1)), + Default: 0, + }, + "vrfs": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeInt, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "description": { + Type: schema.TypeString, + Computed: true, + }, + "rd": { + Type: schema.TypeString, + Computed: true, + }, + "tenant": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func dataSourceNetboxVrfsRead(d *schema.ResourceData, m interface{}) error { + api := m.(*client.NetBoxAPI) + + params := ipam.NewIpamVrfsListParams() + + if limitValue, ok := d.GetOk("limit"); ok { + params.Limit = int64ToPtr(int64(limitValue.(int))) + } + + if filter, ok := d.GetOk("filter"); ok { + var filterParams = filter.(*schema.Set) + for _, f := range filterParams.List() { + k := f.(map[string]interface{})["name"] + v := f.(map[string]interface{})["value"] + vString := v.(string) + switch k { + case "id": + params.ID = &vString + case "name": + params.Name = &vString + case "description": + params.Description = &vString + case "rd": + params.Rd = &vString + case "tenant": + params.Tenant = &vString + case "tenant__n": + params.Tenantn = &vString + case "tenant_group": + params.TenantGroup = &vString + case "tenant_group__n": + params.TenantGroupn = &vString + case "tenant_group_id": + params.TenantGroupID = &vString + case "tenant_group_id__n": + params.TenantGroupIDn = &vString + case "tenant_id": + params.TenantID = &vString + case "tenant_id__n": + params.TenantIDn = &vString + default: + return fmt.Errorf("'%s' is not a supported filter parameter", k) + } + } + } + + res, err := api.Ipam.IpamVrfsList(params, nil) + if err != nil { + return err + } + + if *res.GetPayload().Count == int64(0) { + return errors.New("no result") + } + + filteredVrfs := res.GetPayload().Results + + var s []map[string]interface{} + for _, v := range filteredVrfs { + var mapping = make(map[string]interface{}) + + mapping["id"] = v.ID + mapping["name"] = v.Name + mapping["description"] = v.Description + if v.Rd != nil { + mapping["rd"] = v.Rd + } + if v.Tenant != nil { + mapping["tenant"] = v.Tenant.ID + } + + s = append(s, mapping) + } + + d.SetId(id.UniqueId()) + return d.Set("vrfs", s) +} diff --git a/netbox/data_source_netbox_vrfs_test.go b/netbox/data_source_netbox_vrfs_test.go new file mode 100644 index 00000000..37ebaaa6 --- /dev/null +++ b/netbox/data_source_netbox_vrfs_test.go @@ -0,0 +1,56 @@ +package netbox + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func testAccNetboxVrfsSetUp() string { + return ` +resource "netbox_vrf" "test_1" { + name = "VRF1" +} + +resource "netbox_vrf" "test_2" { + name = "VRF2" +} + +resource "netbox_vrf" "test_3" { + name = "VRF3" +}` +} + +func testAccNetboxVrfsByName() string { + return ` +data "netbox_vrfs" "test" { + filter { + name = "name" + value = "VRF1" + } +}` +} + +func TestAccNetboxVrfsDataSource_basic(t *testing.T) { + setUp := testAccNetboxVrfsSetUp() + resource.Test(t, resource.TestCase{ + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: setUp, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("netbox_vrf.test_1", "name", "VRF1"), + resource.TestCheckResourceAttr("netbox_vrf.test_2", "name", "VRF2"), + resource.TestCheckResourceAttr("netbox_vrf.test_3", "name", "VRF3"), + ), + }, + { + Config: setUp + testAccNetboxVrfsByName(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.netbox_vrfs.test", "vrfs.#", "1"), + resource.TestCheckResourceAttrPair("data.netbox_vrfs.test", "vrfs.0.name", "netbox_vrf.test_1", "name"), + ), + }, + }, + }) +} diff --git a/netbox/provider.go b/netbox/provider.go index 12c2b277..b73ad5c2 100644 --- a/netbox/provider.go +++ b/netbox/provider.go @@ -136,6 +136,7 @@ func Provider() *schema.Provider { "netbox_tenants": dataSourceNetboxTenants(), "netbox_tenant_group": dataSourceNetboxTenantGroup(), "netbox_vrf": dataSourceNetboxVrf(), + "netbox_vrfs": dataSourceNetboxVrfs(), "netbox_platform": dataSourceNetboxPlatform(), "netbox_prefix": dataSourceNetboxPrefix(), "netbox_prefixes": dataSourceNetboxPrefixes(),