Skip to content

Commit 978ece3

Browse files
committed
feat: added data sources for module_type and device_module_bay
1 parent 7b3e9de commit 978ece3

7 files changed

+484
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
# generated by https://github.com/fbreckle/terraform-plugin-docs
3+
page_title: "netbox_device_module_bay Data Source - terraform-provider-netbox"
4+
subcategory: "Data Center Inventory Management (DCIM)"
5+
description: |-
6+
From the official documentation https://docs.netbox.dev/en/stable/models/dcim/modulebay/:
7+
Module bays represent a space or slot within a device in which a field-replaceable module may be installed. A common example is that of a chassis-based switch such as the Cisco Nexus 9000 or Juniper EX9200. Modules in turn hold additional components that become available to the parent device.
8+
---
9+
10+
# netbox_device_module_bay (Data Source)
11+
12+
From the [official documentation](https://docs.netbox.dev/en/stable/models/dcim/modulebay/):
13+
14+
> Module bays represent a space or slot within a device in which a field-replaceable module may be installed. A common example is that of a chassis-based switch such as the Cisco Nexus 9000 or Juniper EX9200. Modules in turn hold additional components that become available to the parent device.
15+
16+
17+
18+
<!-- schema generated by tfplugindocs -->
19+
## Schema
20+
21+
### Required
22+
23+
- `device_id` (Number)
24+
- `name` (String)
25+
26+
### Optional
27+
28+
- `description` (String)
29+
- `label` (String)
30+
- `tags` (Set of String)
31+
32+
### Read-Only
33+
34+
- `id` (Number) The ID of this resource.
35+
36+

docs/data-sources/module_type.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
# generated by https://github.com/fbreckle/terraform-plugin-docs
3+
page_title: "netbox_module_type Data Source - terraform-provider-netbox"
4+
subcategory: "Data Center Inventory Management (DCIM)"
5+
description: |-
6+
From the official documentation https://docs.netbox.dev/en/stable/models/dcim/moduletype/:
7+
A module type represents a specific make and model of hardware component which is installable within a device's module bay and has its own child components. For example, consider a chassis-based switch or router with a number of field-replaceable line cards. Each line card has its own model number and includes a certain set of components such as interfaces. Each module type may have a manufacturer, model number, and part number assigned to it.
8+
---
9+
10+
# netbox_module_type (Data Source)
11+
12+
From the [official documentation](https://docs.netbox.dev/en/stable/models/dcim/moduletype/):
13+
14+
> A module type represents a specific make and model of hardware component which is installable within a device's module bay and has its own child components. For example, consider a chassis-based switch or router with a number of field-replaceable line cards. Each line card has its own model number and includes a certain set of components such as interfaces. Each module type may have a manufacturer, model number, and part number assigned to it.
15+
16+
17+
18+
<!-- schema generated by tfplugindocs -->
19+
## Schema
20+
21+
### Required
22+
23+
- `manufacturer_id` (Number)
24+
- `model` (String)
25+
26+
### Optional
27+
28+
- `comments` (String)
29+
- `description` (String)
30+
- `part_number` (String)
31+
- `tags` (Set of String)
32+
- `weight` (Number)
33+
- `weight_unit` (String) One of [kg, g, lb, oz]. Required when `weight` is set.
34+
35+
### Read-Only
36+
37+
- `id` (Number) The ID of this resource.
38+
39+
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package netbox
2+
3+
import (
4+
"fmt"
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 dataSourceNetboxDeviceModuleBay() *schema.Resource {
13+
return &schema.Resource{
14+
Read: dataSourceNetboxDeviceModuleBayRead,
15+
Description: `:meta:subcategory:Data Center Inventory Management (DCIM):From the [official documentation](https://docs.netbox.dev/en/stable/models/dcim/modulebay/):
16+
17+
> Module bays represent a space or slot within a device in which a field-replaceable module may be installed. A common example is that of a chassis-based switch such as the Cisco Nexus 9000 or Juniper EX9200. Modules in turn hold additional components that become available to the parent device.`,
18+
Schema: map[string]*schema.Schema{
19+
"id": {
20+
Type: schema.TypeInt,
21+
Optional: true,
22+
Computed: true,
23+
},
24+
"device_id": {
25+
Type: schema.TypeInt,
26+
Required: true,
27+
},
28+
"name": {
29+
Type: schema.TypeString,
30+
Required: true,
31+
},
32+
"label": {
33+
Type: schema.TypeString,
34+
Optional: true,
35+
},
36+
//"position": {
37+
// Type: schema.TypeInt,
38+
// Optional: true,
39+
//},
40+
"description": {
41+
Type: schema.TypeString,
42+
Optional: true,
43+
},
44+
tagsKey: tagsSchema,
45+
},
46+
}
47+
}
48+
49+
func dataSourceNetboxDeviceModuleBayRead(d *schema.ResourceData, m interface{}) error {
50+
api := m.(*client.NetBoxAPI)
51+
params := dcim.NewDcimModuleBaysListParams()
52+
53+
params.Limit = int64ToPtr(2)
54+
if deviceId, ok := d.Get("device_id").(int); ok && deviceId != 0 {
55+
deviceId := strconv.Itoa(deviceId)
56+
params.SetDeviceID(&deviceId)
57+
}
58+
if name, ok := d.Get("name").(string); ok && name != "" {
59+
params.SetName(&name)
60+
}
61+
if label, ok := d.Get("label").(string); ok && label != "" {
62+
params.SetLabel(&label)
63+
}
64+
65+
res, err := api.Dcim.DcimModuleBaysList(params, nil)
66+
if err != nil {
67+
return err
68+
}
69+
70+
if count := *res.GetPayload().Count; count != 1 {
71+
return fmt.Errorf("expected one `netbox_device_module_bay`, but got %d", count)
72+
}
73+
74+
result := res.GetPayload().Results[0]
75+
d.SetId(strconv.FormatInt(result.ID, 10))
76+
d.Set("device_id", result.Device.ID)
77+
d.Set("name", result.Name)
78+
d.Set("description", result.Description)
79+
d.Set("label", result.Label)
80+
//d.Set("position", result.Position)
81+
d.Set(tagsKey, getTagListFromNestedTagList(result.Tags))
82+
83+
return nil
84+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package netbox
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
func testAccNetboxDeviceModuleBaySetUp(testName string) string {
12+
return fmt.Sprintf(`
13+
resource "netbox_manufacturer" "test" {
14+
name = "%[1]s"
15+
}
16+
17+
resource "netbox_device_type" "test" {
18+
model = "%[1]s"
19+
manufacturer_id = netbox_manufacturer.test.id
20+
}
21+
22+
resource "netbox_site" "test" {
23+
name = "%[1]s"
24+
status = "active"
25+
}
26+
27+
resource "netbox_device_role" "test" {
28+
name = "%[1]s"
29+
color_hex = "123456"
30+
}
31+
32+
resource "netbox_device" "test" {
33+
name = "%[1]s"
34+
device_type_id = netbox_device_type.test.id
35+
role_id = netbox_device_role.test.id
36+
site_id = netbox_site.test.id
37+
}
38+
39+
resource "netbox_device" "test_2" {
40+
name = "%[1]s_2"
41+
device_type_id = netbox_device_type.test.id
42+
role_id = netbox_device_role.test.id
43+
site_id = netbox_site.test.id
44+
}
45+
46+
resource "netbox_device_module_bay" "test" {
47+
device_id = netbox_device.test.id
48+
name = "%[1]s"
49+
label = "test_label"
50+
#position = "1"
51+
description = "test_description"
52+
}
53+
54+
resource "netbox_device_module_bay" "test_2" {
55+
device_id = netbox_device.test_2.id
56+
name = "%[1]s"
57+
label = "test_label"
58+
#position = "1"
59+
description = "test_description"
60+
}`, testName)
61+
}
62+
63+
const testAccNetboxDeviceModuleBayNoResult = `
64+
data "netbox_device_module_bay" "test" {
65+
device_id = netbox_device.test.id
66+
name = "_does_not_exist_"
67+
}`
68+
69+
func testAccNetboxDeviceModuleBayByName(testName string) string {
70+
return fmt.Sprintf(`
71+
data "netbox_device_module_bay" "test" {
72+
device_id = netbox_device.test.id
73+
name = "%[1]s"
74+
}`, testName)
75+
}
76+
77+
func testAccNetboxDeviceModuleBayByLabel() string {
78+
return `
79+
data "netbox_device_module_bay" "test" {
80+
device_id = netbox_device.test.id
81+
label = "test_label"
82+
}`
83+
}
84+
85+
func TestAccNetboxDeviceModuleBayDataSource_basic(t *testing.T) {
86+
testName := testAccGetTestName("module_bay_ds_basic")
87+
setUp := testAccNetboxDeviceModuleBaySetUp(testName)
88+
resource.Test(t, resource.TestCase{
89+
Providers: testAccProviders,
90+
Steps: []resource.TestStep{
91+
{
92+
Config: setUp + testAccNetboxDeviceModuleBayNoResult,
93+
ExpectError: regexp.MustCompile("expected one"),
94+
},
95+
{
96+
Config: setUp + testAccNetboxDeviceModuleBayByName(testName),
97+
Check: resource.ComposeTestCheckFunc(
98+
resource.TestCheckResourceAttrPair("data.netbox_device_module_bay.test", "id", "netbox_device_module_bay.test", "id"),
99+
resource.TestCheckResourceAttr("data.netbox_device_module_bay.test", "name", testName),
100+
resource.TestCheckResourceAttr("data.netbox_device_module_bay.test", "description", "test_description"),
101+
resource.TestCheckResourceAttr("data.netbox_device_module_bay.test", "label", "test_label"),
102+
//resource.TestCheckResourceAttr("data.netbox_device_module_bay.test", "position", "1"),
103+
),
104+
},
105+
{
106+
Config: setUp + testAccNetboxDeviceModuleBayByLabel(),
107+
Check: resource.ComposeTestCheckFunc(
108+
resource.TestCheckResourceAttrPair("data.netbox_device_module_bay.test", "id", "netbox_device_module_bay.test", "id"),
109+
resource.TestCheckResourceAttr("data.netbox_device_module_bay.test", "name", testName),
110+
resource.TestCheckResourceAttr("data.netbox_device_module_bay.test", "description", "test_description"),
111+
resource.TestCheckResourceAttr("data.netbox_device_module_bay.test", "label", "test_label"),
112+
//resource.TestCheckResourceAttr("data.netbox_device_module_bay.test", "position", "1"),
113+
),
114+
},
115+
},
116+
})
117+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package netbox
2+
3+
import (
4+
"fmt"
5+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
6+
"strconv"
7+
8+
"github.com/fbreckle/go-netbox/netbox/client"
9+
"github.com/fbreckle/go-netbox/netbox/client/dcim"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
)
12+
13+
func dataSourceNetboxModuleType() *schema.Resource {
14+
return &schema.Resource{
15+
Read: dataSourceNetboxModuleTypeRead,
16+
Description: `:meta:subcategory:Data Center Inventory Management (DCIM):From the [official documentation](https://docs.netbox.dev/en/stable/models/dcim/moduletype/):
17+
18+
> A module type represents a specific make and model of hardware component which is installable within a device's module bay and has its own child components. For example, consider a chassis-based switch or router with a number of field-replaceable line cards. Each line card has its own model number and includes a certain set of components such as interfaces. Each module type may have a manufacturer, model number, and part number assigned to it.`,
19+
Schema: map[string]*schema.Schema{
20+
"id": {
21+
Type: schema.TypeInt,
22+
Optional: true,
23+
Computed: true,
24+
},
25+
"manufacturer_id": {
26+
Type: schema.TypeInt,
27+
Required: true,
28+
},
29+
"model": {
30+
Type: schema.TypeString,
31+
Required: true,
32+
},
33+
"part_number": {
34+
Type: schema.TypeString,
35+
Optional: true,
36+
},
37+
"weight": {
38+
Type: schema.TypeFloat,
39+
Optional: true,
40+
},
41+
"weight_unit": {
42+
Type: schema.TypeString,
43+
Optional: true,
44+
RequiredWith: []string{"weight"},
45+
Description: "One of [kg, g, lb, oz]",
46+
ValidateFunc: validation.StringInSlice([]string{"kg", "g", "lb", "oz"}, false),
47+
},
48+
"description": {
49+
Type: schema.TypeString,
50+
Optional: true,
51+
},
52+
"comments": {
53+
Type: schema.TypeString,
54+
Optional: true,
55+
},
56+
tagsKey: tagsSchema,
57+
},
58+
}
59+
}
60+
61+
func dataSourceNetboxModuleTypeRead(d *schema.ResourceData, m interface{}) error {
62+
api := m.(*client.NetBoxAPI)
63+
params := dcim.NewDcimModuleTypesListParams()
64+
65+
params.Limit = int64ToPtr(2)
66+
if manufacturerID, ok := d.Get("manufacturer_id").(int); ok && manufacturerID != 0 {
67+
manufacturerID := strconv.Itoa(manufacturerID)
68+
params.SetManufacturerID(&manufacturerID)
69+
}
70+
if model, ok := d.Get("model").(string); ok && model != "" {
71+
params.SetModel(&model)
72+
}
73+
74+
res, err := api.Dcim.DcimModuleTypesList(params, nil)
75+
if err != nil {
76+
return err
77+
}
78+
79+
if count := *res.GetPayload().Count; count != 1 {
80+
return fmt.Errorf("expected one `netbox_module_type`, but got %d", count)
81+
}
82+
83+
result := res.GetPayload().Results[0]
84+
d.SetId(strconv.FormatInt(result.ID, 10))
85+
d.Set("manufacturer_id", result.Manufacturer.ID)
86+
d.Set("model", result.Model)
87+
d.Set("part_number", result.PartNumber)
88+
d.Set("weight", result.Weight)
89+
90+
if result.WeightUnit != nil {
91+
d.Set("weight_unit", result.WeightUnit.Value)
92+
} else {
93+
d.Set("weight_unit", nil)
94+
}
95+
96+
d.Set("description", result.Description)
97+
d.Set("comments", result.Comments)
98+
d.Set(tagsKey, getTagListFromNestedTagList(result.Tags))
99+
100+
return nil
101+
}

0 commit comments

Comments
 (0)