Skip to content

Commit 36337b8

Browse files
authored
Merge pull request e-breuninger#122 from madnutter56/feature/cluster_type_site_data_source
add data source for cluster type and site
2 parents 1bf95a0 + 12fbb47 commit 36337b8

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package netbox
2+
3+
import (
4+
"errors"
5+
"strconv"
6+
7+
"github.com/fbreckle/go-netbox/netbox/client"
8+
"github.com/fbreckle/go-netbox/netbox/client/virtualization"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
func dataSourceNetboxClusterType() *schema.Resource {
13+
return &schema.Resource{
14+
Read: dataSourceNetboxClusterTypeRead,
15+
Schema: map[string]*schema.Schema{
16+
"cluster_type_id": &schema.Schema{
17+
Type: schema.TypeInt,
18+
Computed: true,
19+
},
20+
"name": &schema.Schema{
21+
Type: schema.TypeString,
22+
Required: true,
23+
},
24+
},
25+
}
26+
}
27+
28+
func dataSourceNetboxClusterTypeRead(d *schema.ResourceData, m interface{}) error {
29+
api := m.(*client.NetBoxAPI)
30+
31+
name := d.Get("name").(string)
32+
params := virtualization.NewVirtualizationClusterTypesListParams()
33+
params.Name = &name
34+
limit := int64(2) // Limit of 2 is enough
35+
params.Limit = &limit
36+
37+
res, err := api.Virtualization.VirtualizationClusterTypesList(params, nil)
38+
if err != nil {
39+
return err
40+
}
41+
42+
if *res.GetPayload().Count > int64(1) {
43+
return errors.New("More than one result. Specify a more narrow filter")
44+
}
45+
if *res.GetPayload().Count == int64(0) {
46+
return errors.New("No result")
47+
}
48+
result := res.GetPayload().Results[0]
49+
d.Set("cluster_type_id", result.ID)
50+
d.SetId(strconv.FormatInt(result.ID, 10))
51+
d.Set("name", result.Name)
52+
return nil
53+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package netbox
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
)
9+
10+
func TestAccNetboxClusterTypeDataSource_basic(t *testing.T) {
11+
12+
testSlug := "clstrtyp_ds_basic"
13+
testName := testAccGetTestName(testSlug)
14+
resource.ParallelTest(t, resource.TestCase{
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: fmt.Sprintf(`
19+
resource "netbox_cluster_type" "test" {
20+
name = "%[1]s"
21+
}
22+
23+
data "netbox_cluster_type" "test" {
24+
depends_on = [netbox_cluster_type.test]
25+
name = "%[1]s"
26+
}`, testName),
27+
Check: resource.ComposeTestCheckFunc(
28+
resource.TestCheckResourceAttrPair("data.netbox_cluster_type.test", "cluster_type_id", "netbox_cluster_type.test", "id"),
29+
resource.TestCheckResourceAttrPair("data.netbox_cluster_type.test", "id", "netbox_cluster_type.test", "id"),
30+
),
31+
},
32+
},
33+
})
34+
}

netbox/data_source_netbox_site.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package netbox
2+
3+
import (
4+
"errors"
5+
"strconv"
6+
7+
"github.com/fbreckle/go-netbox/netbox/client"
8+
"github.com/fbreckle/go-netbox/netbox/client/dcim"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
func dataSourceNetboxSite() *schema.Resource {
13+
return &schema.Resource{
14+
Read: dataSourceNetboxSiteRead,
15+
Schema: map[string]*schema.Schema{
16+
"site_id": &schema.Schema{
17+
Type: schema.TypeInt,
18+
Computed: true,
19+
},
20+
"name": &schema.Schema{
21+
Type: schema.TypeString,
22+
Required: true,
23+
},
24+
},
25+
}
26+
}
27+
28+
func dataSourceNetboxSiteRead(d *schema.ResourceData, m interface{}) error {
29+
api := m.(*client.NetBoxAPI)
30+
31+
name := d.Get("name").(string)
32+
params := dcim.NewDcimSitesListParams()
33+
params.Name = &name
34+
limit := int64(2) // Limit of 2 is enough
35+
params.Limit = &limit
36+
37+
res, err := api.Dcim.DcimSitesList(params, nil)
38+
if err != nil {
39+
return err
40+
}
41+
42+
if *res.GetPayload().Count > int64(1) {
43+
return errors.New("More than one result. Specify a more narrow filter")
44+
}
45+
if *res.GetPayload().Count == int64(0) {
46+
return errors.New("No result")
47+
}
48+
result := res.GetPayload().Results[0]
49+
d.Set("site_id", result.ID)
50+
d.SetId(strconv.FormatInt(result.ID, 10))
51+
d.Set("name", result.Name)
52+
return nil
53+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package netbox
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
)
9+
10+
func TestAccNetboxSiteDataSource_basic(t *testing.T) {
11+
12+
testSlug := "site_ds_basic"
13+
testName := testAccGetTestName(testSlug)
14+
resource.ParallelTest(t, resource.TestCase{
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: fmt.Sprintf(`
19+
resource "netbox_site" "test" {
20+
name = "%[1]s"
21+
status = "active"
22+
}
23+
data "netbox_site" "test" {
24+
depends_on = [netbox_site.test]
25+
name = "%[1]s"
26+
}`, testName),
27+
Check: resource.ComposeTestCheckFunc(
28+
resource.TestCheckResourceAttrPair("data.netbox_site.test", "status", "netbox_site.test", "active"),
29+
resource.TestCheckResourceAttrPair("data.netbox_site.test", "site_id", "netbox_site.test", "id"),
30+
resource.TestCheckResourceAttrPair("data.netbox_site.test", "id", "netbox_site.test", "id"),
31+
),
32+
},
33+
},
34+
})
35+
}

netbox/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ func Provider() *schema.Provider {
4040
DataSourcesMap: map[string]*schema.Resource{
4141
"netbox_cluster": dataSourceNetboxCluster(),
4242
"netbox_cluster_group": dataSourceNetboxClusterGroup(),
43+
"netbox_cluster_type": dataSourceNetboxClusterType(),
4344
"netbox_tenant": dataSourceNetboxTenant(),
4445
"netbox_tenant_group": dataSourceNetboxTenantGroup(),
4546
"netbox_vrf": dataSourceNetboxVrf(),
4647
"netbox_platform": dataSourceNetboxPlatform(),
4748
"netbox_prefix": dataSourceNetboxPrefix(),
4849
"netbox_device_role": dataSourceNetboxDeviceRole(),
50+
"netbox_site": dataSourceNetboxSite(),
4951
"netbox_tag": dataSourceNetboxTag(),
5052
"netbox_virtual_machines": dataSourceNetboxVirtualMachine(),
5153
"netbox_interfaces": dataSourceNetboxInterfaces(),

0 commit comments

Comments
 (0)