From 995d47e89883f8f6ad39668d96c9f1ca71314d15 Mon Sep 17 00:00:00 2001 From: vaneuk Date: Fri, 8 Dec 2023 22:52:20 +0000 Subject: [PATCH 1/6] feat: added data sources for module_type and device_module_bay --- docs/data-sources/device_module_bay.md | 36 ++++++ docs/data-sources/module_type.md | 39 ++++++ .../data_source_netbox_device_module_bay.go | 84 +++++++++++++ ...ta_source_netbox_device_module_bay_test.go | 117 ++++++++++++++++++ netbox/data_source_netbox_module_type.go | 101 +++++++++++++++ netbox/data_source_netbox_module_type_test.go | 105 ++++++++++++++++ netbox/provider.go | 2 + 7 files changed, 484 insertions(+) create mode 100644 docs/data-sources/device_module_bay.md create mode 100644 docs/data-sources/module_type.md create mode 100644 netbox/data_source_netbox_device_module_bay.go create mode 100644 netbox/data_source_netbox_device_module_bay_test.go create mode 100644 netbox/data_source_netbox_module_type.go create mode 100644 netbox/data_source_netbox_module_type_test.go diff --git a/docs/data-sources/device_module_bay.md b/docs/data-sources/device_module_bay.md new file mode 100644 index 00000000..baa059b1 --- /dev/null +++ b/docs/data-sources/device_module_bay.md @@ -0,0 +1,36 @@ +--- +# generated by https://github.com/fbreckle/terraform-plugin-docs +page_title: "netbox_device_module_bay Data Source - terraform-provider-netbox" +subcategory: "Data Center Inventory Management (DCIM)" +description: |- + From the official documentation https://docs.netbox.dev/en/stable/models/dcim/modulebay/: + 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. +--- + +# netbox_device_module_bay (Data Source) + +From the [official documentation](https://docs.netbox.dev/en/stable/models/dcim/modulebay/): + +> 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. + + + + +## Schema + +### Required + +- `device_id` (Number) +- `name` (String) + +### Optional + +- `description` (String) +- `label` (String) +- `tags` (Set of String) + +### Read-Only + +- `id` (Number) The ID of this resource. + + diff --git a/docs/data-sources/module_type.md b/docs/data-sources/module_type.md new file mode 100644 index 00000000..e4030b18 --- /dev/null +++ b/docs/data-sources/module_type.md @@ -0,0 +1,39 @@ +--- +# generated by https://github.com/fbreckle/terraform-plugin-docs +page_title: "netbox_module_type Data Source - terraform-provider-netbox" +subcategory: "Data Center Inventory Management (DCIM)" +description: |- + From the official documentation https://docs.netbox.dev/en/stable/models/dcim/moduletype/: + 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. +--- + +# netbox_module_type (Data Source) + +From the [official documentation](https://docs.netbox.dev/en/stable/models/dcim/moduletype/): + +> 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. + + + + +## Schema + +### Required + +- `manufacturer_id` (Number) +- `model` (String) + +### Optional + +- `comments` (String) +- `description` (String) +- `part_number` (String) +- `tags` (Set of String) +- `weight` (Number) +- `weight_unit` (String) One of [kg, g, lb, oz]. Required when `weight` is set. + +### Read-Only + +- `id` (Number) The ID of this resource. + + diff --git a/netbox/data_source_netbox_device_module_bay.go b/netbox/data_source_netbox_device_module_bay.go new file mode 100644 index 00000000..e8062581 --- /dev/null +++ b/netbox/data_source_netbox_device_module_bay.go @@ -0,0 +1,84 @@ +package netbox + +import ( + "fmt" + "strconv" + + "github.com/fbreckle/go-netbox/netbox/client" + "github.com/fbreckle/go-netbox/netbox/client/dcim" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceNetboxDeviceModuleBay() *schema.Resource { + return &schema.Resource{ + Read: dataSourceNetboxDeviceModuleBayRead, + Description: `:meta:subcategory:Data Center Inventory Management (DCIM):From the [official documentation](https://docs.netbox.dev/en/stable/models/dcim/modulebay/): + +> 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.`, + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeInt, + Optional: true, + Computed: true, + }, + "device_id": { + Type: schema.TypeInt, + Required: true, + }, + "name": { + Type: schema.TypeString, + Required: true, + }, + "label": { + Type: schema.TypeString, + Optional: true, + }, + //"position": { + // Type: schema.TypeInt, + // Optional: true, + //}, + "description": { + Type: schema.TypeString, + Optional: true, + }, + tagsKey: tagsSchema, + }, + } +} + +func dataSourceNetboxDeviceModuleBayRead(d *schema.ResourceData, m interface{}) error { + api := m.(*client.NetBoxAPI) + params := dcim.NewDcimModuleBaysListParams() + + params.Limit = int64ToPtr(2) + if deviceId, ok := d.Get("device_id").(int); ok && deviceId != 0 { + deviceId := strconv.Itoa(deviceId) + params.SetDeviceID(&deviceId) + } + if name, ok := d.Get("name").(string); ok && name != "" { + params.SetName(&name) + } + if label, ok := d.Get("label").(string); ok && label != "" { + params.SetLabel(&label) + } + + res, err := api.Dcim.DcimModuleBaysList(params, nil) + if err != nil { + return err + } + + if count := *res.GetPayload().Count; count != 1 { + return fmt.Errorf("expected one `netbox_device_module_bay`, but got %d", count) + } + + result := res.GetPayload().Results[0] + d.SetId(strconv.FormatInt(result.ID, 10)) + d.Set("device_id", result.Device.ID) + d.Set("name", result.Name) + d.Set("description", result.Description) + d.Set("label", result.Label) + //d.Set("position", result.Position) + d.Set(tagsKey, getTagListFromNestedTagList(result.Tags)) + + return nil +} diff --git a/netbox/data_source_netbox_device_module_bay_test.go b/netbox/data_source_netbox_device_module_bay_test.go new file mode 100644 index 00000000..e20b2ae5 --- /dev/null +++ b/netbox/data_source_netbox_device_module_bay_test.go @@ -0,0 +1,117 @@ +package netbox + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func testAccNetboxDeviceModuleBaySetUp(testName string) string { + return fmt.Sprintf(` +resource "netbox_manufacturer" "test" { + name = "%[1]s" +} + +resource "netbox_device_type" "test" { + model = "%[1]s" + manufacturer_id = netbox_manufacturer.test.id +} + +resource "netbox_site" "test" { + name = "%[1]s" + status = "active" +} + +resource "netbox_device_role" "test" { + name = "%[1]s" + color_hex = "123456" +} + +resource "netbox_device" "test" { + name = "%[1]s" + device_type_id = netbox_device_type.test.id + role_id = netbox_device_role.test.id + site_id = netbox_site.test.id +} + +resource "netbox_device" "test_2" { + name = "%[1]s_2" + device_type_id = netbox_device_type.test.id + role_id = netbox_device_role.test.id + site_id = netbox_site.test.id +} + +resource "netbox_device_module_bay" "test" { + device_id = netbox_device.test.id + name = "%[1]s" + label = "test_label" + #position = "1" + description = "test_description" +} + +resource "netbox_device_module_bay" "test_2" { + device_id = netbox_device.test_2.id + name = "%[1]s" + label = "test_label" + #position = "1" + description = "test_description" +}`, testName) +} + +const testAccNetboxDeviceModuleBayNoResult = ` +data "netbox_device_module_bay" "test" { + device_id = netbox_device.test.id + name = "_does_not_exist_" +}` + +func testAccNetboxDeviceModuleBayByName(testName string) string { + return fmt.Sprintf(` +data "netbox_device_module_bay" "test" { + device_id = netbox_device.test.id + name = "%[1]s" +}`, testName) +} + +func testAccNetboxDeviceModuleBayByLabel() string { + return ` +data "netbox_device_module_bay" "test" { + device_id = netbox_device.test.id + label = "test_label" +}` +} + +func TestAccNetboxDeviceModuleBayDataSource_basic(t *testing.T) { + testName := testAccGetTestName("module_bay_ds_basic") + setUp := testAccNetboxDeviceModuleBaySetUp(testName) + resource.Test(t, resource.TestCase{ + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: setUp + testAccNetboxDeviceModuleBayNoResult, + ExpectError: regexp.MustCompile("expected one"), + }, + { + Config: setUp + testAccNetboxDeviceModuleBayByName(testName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair("data.netbox_device_module_bay.test", "id", "netbox_device_module_bay.test", "id"), + resource.TestCheckResourceAttr("data.netbox_device_module_bay.test", "name", testName), + resource.TestCheckResourceAttr("data.netbox_device_module_bay.test", "description", "test_description"), + resource.TestCheckResourceAttr("data.netbox_device_module_bay.test", "label", "test_label"), + //resource.TestCheckResourceAttr("data.netbox_device_module_bay.test", "position", "1"), + ), + }, + { + Config: setUp + testAccNetboxDeviceModuleBayByLabel(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair("data.netbox_device_module_bay.test", "id", "netbox_device_module_bay.test", "id"), + resource.TestCheckResourceAttr("data.netbox_device_module_bay.test", "name", testName), + resource.TestCheckResourceAttr("data.netbox_device_module_bay.test", "description", "test_description"), + resource.TestCheckResourceAttr("data.netbox_device_module_bay.test", "label", "test_label"), + //resource.TestCheckResourceAttr("data.netbox_device_module_bay.test", "position", "1"), + ), + }, + }, + }) +} diff --git a/netbox/data_source_netbox_module_type.go b/netbox/data_source_netbox_module_type.go new file mode 100644 index 00000000..238a8878 --- /dev/null +++ b/netbox/data_source_netbox_module_type.go @@ -0,0 +1,101 @@ +package netbox + +import ( + "fmt" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "strconv" + + "github.com/fbreckle/go-netbox/netbox/client" + "github.com/fbreckle/go-netbox/netbox/client/dcim" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceNetboxModuleType() *schema.Resource { + return &schema.Resource{ + Read: dataSourceNetboxModuleTypeRead, + Description: `:meta:subcategory:Data Center Inventory Management (DCIM):From the [official documentation](https://docs.netbox.dev/en/stable/models/dcim/moduletype/): + +> 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.`, + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeInt, + Optional: true, + Computed: true, + }, + "manufacturer_id": { + Type: schema.TypeInt, + Required: true, + }, + "model": { + Type: schema.TypeString, + Required: true, + }, + "part_number": { + Type: schema.TypeString, + Optional: true, + }, + "weight": { + Type: schema.TypeFloat, + Optional: true, + }, + "weight_unit": { + Type: schema.TypeString, + Optional: true, + RequiredWith: []string{"weight"}, + Description: "One of [kg, g, lb, oz]", + ValidateFunc: validation.StringInSlice([]string{"kg", "g", "lb", "oz"}, false), + }, + "description": { + Type: schema.TypeString, + Optional: true, + }, + "comments": { + Type: schema.TypeString, + Optional: true, + }, + tagsKey: tagsSchema, + }, + } +} + +func dataSourceNetboxModuleTypeRead(d *schema.ResourceData, m interface{}) error { + api := m.(*client.NetBoxAPI) + params := dcim.NewDcimModuleTypesListParams() + + params.Limit = int64ToPtr(2) + if manufacturerID, ok := d.Get("manufacturer_id").(int); ok && manufacturerID != 0 { + manufacturerID := strconv.Itoa(manufacturerID) + params.SetManufacturerID(&manufacturerID) + } + if model, ok := d.Get("model").(string); ok && model != "" { + params.SetModel(&model) + } + + res, err := api.Dcim.DcimModuleTypesList(params, nil) + if err != nil { + return err + } + + if count := *res.GetPayload().Count; count != 1 { + return fmt.Errorf("expected one `netbox_module_type`, but got %d", count) + } + + result := res.GetPayload().Results[0] + d.SetId(strconv.FormatInt(result.ID, 10)) + d.Set("manufacturer_id", result.Manufacturer.ID) + d.Set("model", result.Model) + d.Set("part_number", result.PartNumber) + d.Set("weight", result.Weight) + + if result.WeightUnit != nil { + d.Set("weight_unit", result.WeightUnit.Value) + } else { + d.Set("weight_unit", nil) + } + + d.Set("description", result.Description) + d.Set("comments", result.Comments) + d.Set(tagsKey, getTagListFromNestedTagList(result.Tags)) + + return nil +} diff --git a/netbox/data_source_netbox_module_type_test.go b/netbox/data_source_netbox_module_type_test.go new file mode 100644 index 00000000..9f0aacde --- /dev/null +++ b/netbox/data_source_netbox_module_type_test.go @@ -0,0 +1,105 @@ +package netbox + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func testAccNetboxModuleTypeSetUp(testName string) string { + return fmt.Sprintf(` +resource "netbox_manufacturer" "test" { + name = "%[1]s" +} + +resource "netbox_tag" "test" { + name = "%[1]sa" +} + +resource "netbox_module_type" "test" { + manufacturer_id = netbox_manufacturer.test.id + model = "%[1]s" + part_number = "test_pn" + description = "test_description" + comments = "test_comments" + + weight = 1 + weight_unit = "kg" + tags = ["%[1]sa"] +} + +resource "netbox_module_type" "test_2" { + manufacturer_id = netbox_manufacturer.test.id + model = "%[1]s_2" +}`, testName) +} + +const testAccNetboxModuleTypeNoResult = ` +data "netbox_module_type" "test" { + manufacturer_id = netbox_manufacturer.test.id + model = "_does_not_exist_" +}` + +func testAccNetboxModuleType(testName string) string { + return fmt.Sprintf(` +data "netbox_module_type" "test" { + manufacturer_id = netbox_manufacturer.test.id + model = "%[1]s" + depends_on = [ + netbox_module_type.test + ] +}`, testName) +} + +func testAccNetboxModuleType2(testName string) string { + return fmt.Sprintf(` +data "netbox_module_type" "test_2" { + manufacturer_id = netbox_manufacturer.test.id + model = "%[1]s_2" + depends_on = [ + netbox_module_type.test_2 + ] +}`, testName) +} + +func TestAccNetboxModuleTypeDataSource_basic(t *testing.T) { + testName := testAccGetTestName("module_type_ds_basic") + setUp := testAccNetboxModuleTypeSetUp(testName) + resource.ParallelTest(t, resource.TestCase{ + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: setUp + testAccNetboxModuleTypeNoResult, + ExpectError: regexp.MustCompile("expected one"), + }, + { + Config: setUp + testAccNetboxModuleType(testName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair("data.netbox_module_type.test", "id", "netbox_module_type.test", "id"), + resource.TestCheckResourceAttrPair("data.netbox_module_type.test", "manufacturer_id", "netbox_manufacturer.test", "id"), + resource.TestCheckResourceAttr("data.netbox_module_type.test", "model", testName), + resource.TestCheckResourceAttr("data.netbox_module_type.test", "part_number", "test_pn"), + resource.TestCheckResourceAttr("data.netbox_module_type.test", "description", "test_description"), + resource.TestCheckResourceAttr("data.netbox_module_type.test", "comments", "test_comments"), + resource.TestCheckResourceAttr("data.netbox_module_type.test", "weight", "1"), + resource.TestCheckResourceAttr("data.netbox_module_type.test", "weight_unit", "kg"), + ), + }, + { + Config: setUp + testAccNetboxModuleType2(testName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair("data.netbox_module_type.test_2", "id", "netbox_module_type.test_2", "id"), + resource.TestCheckResourceAttrPair("data.netbox_module_type.test_2", "manufacturer_id", "netbox_manufacturer.test", "id"), + resource.TestCheckResourceAttr("data.netbox_module_type.test_2", "model", fmt.Sprintf("%s_2", testName)), + resource.TestCheckResourceAttr("data.netbox_module_type.test_2", "part_number", ""), + resource.TestCheckResourceAttr("data.netbox_module_type.test_2", "description", ""), + resource.TestCheckResourceAttr("data.netbox_module_type.test_2", "comments", ""), + resource.TestCheckResourceAttr("data.netbox_module_type.test_2", "weight", "0"), + resource.TestCheckResourceAttr("data.netbox_module_type.test_2", "weight_unit", ""), + ), + }, + }, + }) +} diff --git a/netbox/provider.go b/netbox/provider.go index fc8fcd76..792fdd13 100644 --- a/netbox/provider.go +++ b/netbox/provider.go @@ -159,11 +159,13 @@ func Provider() *schema.Provider { "netbox_prefix": dataSourceNetboxPrefix(), "netbox_prefixes": dataSourceNetboxPrefixes(), "netbox_devices": dataSourceNetboxDevices(), + "netbox_device_module_bay": dataSourceNetboxDeviceModuleBay(), "netbox_device_role": dataSourceNetboxDeviceRole(), "netbox_device_type": dataSourceNetboxDeviceType(), "netbox_site": dataSourceNetboxSite(), "netbox_location": dataSourceNetboxLocation(), "netbox_locations": dataSourceNetboxLocations(), + "netbox_module_type": dataSourceNetboxModuleType(), "netbox_tag": dataSourceNetboxTag(), "netbox_tags": dataSourceNetboxTags(), "netbox_virtual_machines": dataSourceNetboxVirtualMachine(), From d91cf61cf16594192b5f789d6d0fce102ddef5f6 Mon Sep 17 00:00:00 2001 From: vaneuk Date: Sat, 9 Dec 2023 19:20:40 +0000 Subject: [PATCH 2/6] fixed golangci-lint --- netbox/data_source_netbox_device_module_bay.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/netbox/data_source_netbox_device_module_bay.go b/netbox/data_source_netbox_device_module_bay.go index e8062581..f993e141 100644 --- a/netbox/data_source_netbox_device_module_bay.go +++ b/netbox/data_source_netbox_device_module_bay.go @@ -51,9 +51,9 @@ func dataSourceNetboxDeviceModuleBayRead(d *schema.ResourceData, m interface{}) params := dcim.NewDcimModuleBaysListParams() params.Limit = int64ToPtr(2) - if deviceId, ok := d.Get("device_id").(int); ok && deviceId != 0 { - deviceId := strconv.Itoa(deviceId) - params.SetDeviceID(&deviceId) + if deviceID, ok := d.Get("device_id").(int); ok && deviceID != 0 { + deviceID := strconv.Itoa(deviceID) + params.SetDeviceID(&deviceID) } if name, ok := d.Get("name").(string); ok && name != "" { params.SetName(&name) From a29c70936849b84dbffc054214053e268f616262 Mon Sep 17 00:00:00 2001 From: vaneuk Date: Thu, 21 Dec 2023 20:00:58 +0100 Subject: [PATCH 3/6] added rear port data source --- netbox/data_source_netbox_device_rear_port.go | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 netbox/data_source_netbox_device_rear_port.go diff --git a/netbox/data_source_netbox_device_rear_port.go b/netbox/data_source_netbox_device_rear_port.go new file mode 100644 index 00000000..0e972c20 --- /dev/null +++ b/netbox/data_source_netbox_device_rear_port.go @@ -0,0 +1,105 @@ +package netbox + +import ( + "fmt" + "strconv" + + "github.com/fbreckle/go-netbox/netbox/client" + "github.com/fbreckle/go-netbox/netbox/client/dcim" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceNetboxDeviceRearPort() *schema.Resource { + return &schema.Resource{ + Read: dataSourceNetboxDeviceRearPortRead, + Description: `:meta:subcategory:Data Center Inventory Management (DCIM):From the [official documentation](https://docs.netbox.dev/en/stable/models/dcim/moduletype/):`, + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeInt, + Optional: true, + Computed: true, + }, + "device_id": { + Type: schema.TypeInt, + Required: true, + }, + "name": { + Type: schema.TypeString, + Required: true, + }, + "type": { + Type: schema.TypeString, + Optional: true, + Description: "One of [8p8c, 8p6c, 8p4c, 8p2c, 6p6c, 6p4c, 6p2c, 4p4c, 4p2c, gg45, tera-4p, tera-2p, tera-1p, 110-punch, bnc, f, n, mrj21, fc, lc, lc-pc, lc-upc, lc-apc, lsh, lsh-pc, lsh-upc, lsh-apc, mpo, mtrj, sc, sc-pc, sc-upc, sc-apc, st, cs, sn, sma-905, sma-906, urm-p2, urm-p4, urm-p8, splice, other]", + }, + "positions": { + Type: schema.TypeInt, + Optional: true, + }, + "module_id": { + Type: schema.TypeInt, + Optional: true, + }, + "label": { + Type: schema.TypeString, + Optional: true, + }, + "color_hex": { + Type: schema.TypeString, + Optional: true, + }, + "description": { + Type: schema.TypeString, + Optional: true, + }, + "mark_connected": { + Type: schema.TypeBool, + Default: false, + Optional: true, + }, + tagsKey: tagsSchema, + }, + } +} + +func dataSourceNetboxDeviceRearPortRead(d *schema.ResourceData, m interface{}) error { + api := m.(*client.NetBoxAPI) + params := dcim.NewDcimModuleTypesListParams() + + params.Limit = int64ToPtr(2) + if manufacturerID, ok := d.Get("manufacturer_id").(int); ok && manufacturerID != 0 { + manufacturerID := strconv.Itoa(manufacturerID) + params.SetManufacturerID(&manufacturerID) + } + if model, ok := d.Get("model").(string); ok && model != "" { + params.SetModel(&model) + } + + res, err := api.Dcim.DcimModuleTypesList(params, nil) + if err != nil { + return err + } + + if count := *res.GetPayload().Count; count != 1 { + return fmt.Errorf("expected one `netbox_module_type`, but got %d", count) + } + + result := res.GetPayload().Results[0] + d.SetId(strconv.FormatInt(result.ID, 10)) + d.Set("manufacturer_id", result.Manufacturer.ID) + d.Set("model", result.Model) + d.Set("part_number", result.PartNumber) + d.Set("weight", result.Weight) + + if result.WeightUnit != nil { + d.Set("weight_unit", result.WeightUnit.Value) + } else { + d.Set("weight_unit", nil) + } + + d.Set("description", result.Description) + d.Set("comments", result.Comments) + d.Set(tagsKey, getTagListFromNestedTagList(result.Tags)) + + return nil +} From f6cd04d220a08995d1dad724666d0de6b6959206 Mon Sep 17 00:00:00 2001 From: vaneuk Date: Fri, 22 Dec 2023 12:39:47 +0100 Subject: [PATCH 4/6] added rear port data source test --- netbox/data_source_netbox_device_rear_port.go | 36 +++---- ...ata_source_netbox_device_rear_port_test.go | 99 +++++++++++++++++++ netbox/provider.go | 1 + 3 files changed, 119 insertions(+), 17 deletions(-) create mode 100644 netbox/data_source_netbox_device_rear_port_test.go diff --git a/netbox/data_source_netbox_device_rear_port.go b/netbox/data_source_netbox_device_rear_port.go index 0e972c20..01f2d2c4 100644 --- a/netbox/data_source_netbox_device_rear_port.go +++ b/netbox/data_source_netbox_device_rear_port.go @@ -54,7 +54,6 @@ func dataSourceNetboxDeviceRearPort() *schema.Resource { }, "mark_connected": { Type: schema.TypeBool, - Default: false, Optional: true, }, tagsKey: tagsSchema, @@ -64,41 +63,44 @@ func dataSourceNetboxDeviceRearPort() *schema.Resource { func dataSourceNetboxDeviceRearPortRead(d *schema.ResourceData, m interface{}) error { api := m.(*client.NetBoxAPI) - params := dcim.NewDcimModuleTypesListParams() + params := dcim.NewDcimRearPortsListParams() params.Limit = int64ToPtr(2) - if manufacturerID, ok := d.Get("manufacturer_id").(int); ok && manufacturerID != 0 { - manufacturerID := strconv.Itoa(manufacturerID) - params.SetManufacturerID(&manufacturerID) + if deviceID, ok := d.Get("device_id").(int); ok && deviceID != 0 { + deviceID := strconv.Itoa(deviceID) + params.SetDeviceID(&deviceID) } - if model, ok := d.Get("model").(string); ok && model != "" { - params.SetModel(&model) + if name, ok := d.Get("name").(string); ok && name != "" { + params.SetName(&name) } - res, err := api.Dcim.DcimModuleTypesList(params, nil) + res, err := api.Dcim.DcimRearPortsList(params, nil) if err != nil { return err } if count := *res.GetPayload().Count; count != 1 { - return fmt.Errorf("expected one `netbox_module_type`, but got %d", count) + return fmt.Errorf("expected one `netbox_device_rear_port`, but got %d", count) } result := res.GetPayload().Results[0] d.SetId(strconv.FormatInt(result.ID, 10)) - d.Set("manufacturer_id", result.Manufacturer.ID) - d.Set("model", result.Model) - d.Set("part_number", result.PartNumber) - d.Set("weight", result.Weight) + d.Set("device_id", result.Device.ID) + d.Set("name", result.Name) + d.Set("type", result.Type.Value) + d.Set("positions", result.Positions) - if result.WeightUnit != nil { - d.Set("weight_unit", result.WeightUnit.Value) + if result.Module != nil { + d.Set("module_id", result.Module.ID) } else { - d.Set("weight_unit", nil) + d.Set("module_id", nil) } + d.Set("label", result.Label) + d.Set("color_hex", result.Color) d.Set("description", result.Description) - d.Set("comments", result.Comments) + d.Set("mark_connected", result.MarkConnected) + d.Set(tagsKey, getTagListFromNestedTagList(result.Tags)) return nil diff --git a/netbox/data_source_netbox_device_rear_port_test.go b/netbox/data_source_netbox_device_rear_port_test.go new file mode 100644 index 00000000..28afe8b5 --- /dev/null +++ b/netbox/data_source_netbox_device_rear_port_test.go @@ -0,0 +1,99 @@ +package netbox + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func testAccNetboxDeviceRearPortSetUp(testName string) string { + return fmt.Sprintf(` +resource "netbox_manufacturer" "test" { + name = "%[1]s" +} + +resource "netbox_device_type" "test" { + model = "%[1]s" + manufacturer_id = netbox_manufacturer.test.id +} + +resource "netbox_site" "test" { + name = "%[1]s" + status = "active" +} + +resource "netbox_device_role" "test" { + name = "%[1]s" + color_hex = "123456" +} + +resource "netbox_device" "test" { + name = "%[1]s" + device_type_id = netbox_device_type.test.id + role_id = netbox_device_role.test.id + site_id = netbox_site.test.id +} + +resource "netbox_device" "test_2" { + name = "%[1]s_2" + device_type_id = netbox_device_type.test.id + role_id = netbox_device_role.test.id + site_id = netbox_site.test.id +} + +resource "netbox_device_rear_port" "test" { + device_id = netbox_device.test.id + name = "%[1]s" + description = "test_description" + type = "8p8c" + positions = 2 + mark_connected = true +}`, testName) +} + +const testAccNetboxDeviceRearPortNoResult = ` +data "netbox_device_rear_port" "test" { + device_id = netbox_device.test.id + name = "_does_not_exist_" + depends_on = [ + netbox_device_rear_port.test + ] +}` + +func testAccNetboxDeviceRearPortByName(testName string) string { + return fmt.Sprintf(` +data "netbox_device_rear_port" "test" { + device_id = netbox_device.test.id + name = "%[1]s" + depends_on = [ + netbox_device_rear_port.test + ] +}`, testName) +} + +func TestAccNetboxDeviceRearPortDataSource_basic(t *testing.T) { + testName := testAccGetTestName("module_bay_ds_basic") + setUp := testAccNetboxDeviceRearPortSetUp(testName) + resource.Test(t, resource.TestCase{ + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: setUp + testAccNetboxDeviceRearPortNoResult, + ExpectError: regexp.MustCompile("expected one"), + }, + { + Config: setUp + testAccNetboxDeviceRearPortByName(testName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair("data.netbox_device_rear_port.test", "id", "netbox_device_rear_port.test", "id"), + resource.TestCheckResourceAttr("data.netbox_device_rear_port.test", "name", testName), + resource.TestCheckResourceAttr("data.netbox_device_rear_port.test", "description", "test_description"), + resource.TestCheckResourceAttr("data.netbox_device_rear_port.test", "type", "8p8c"), + resource.TestCheckResourceAttr("data.netbox_device_rear_port.test", "positions", "2"), + resource.TestCheckResourceAttr("data.netbox_device_rear_port.test", "mark_connected", "true"), + ), + }, + }, + }) +} diff --git a/netbox/provider.go b/netbox/provider.go index 792fdd13..f9d60d67 100644 --- a/netbox/provider.go +++ b/netbox/provider.go @@ -159,6 +159,7 @@ func Provider() *schema.Provider { "netbox_prefix": dataSourceNetboxPrefix(), "netbox_prefixes": dataSourceNetboxPrefixes(), "netbox_devices": dataSourceNetboxDevices(), + "netbox_device_rear_port": dataSourceNetboxDeviceRearPort(), "netbox_device_module_bay": dataSourceNetboxDeviceModuleBay(), "netbox_device_role": dataSourceNetboxDeviceRole(), "netbox_device_type": dataSourceNetboxDeviceType(), From e4c05747b93b03770aaeb14f78b2baa759fcd0a8 Mon Sep 17 00:00:00 2001 From: vaneuk Date: Fri, 22 Dec 2023 12:47:03 +0100 Subject: [PATCH 5/6] created front port files --- .../data_source_netbox_device_front_port.go | 107 ++++++++++++++++++ ...ta_source_netbox_device_front_port_test.go | 99 ++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 netbox/data_source_netbox_device_front_port.go create mode 100644 netbox/data_source_netbox_device_front_port_test.go diff --git a/netbox/data_source_netbox_device_front_port.go b/netbox/data_source_netbox_device_front_port.go new file mode 100644 index 00000000..01f2d2c4 --- /dev/null +++ b/netbox/data_source_netbox_device_front_port.go @@ -0,0 +1,107 @@ +package netbox + +import ( + "fmt" + "strconv" + + "github.com/fbreckle/go-netbox/netbox/client" + "github.com/fbreckle/go-netbox/netbox/client/dcim" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceNetboxDeviceRearPort() *schema.Resource { + return &schema.Resource{ + Read: dataSourceNetboxDeviceRearPortRead, + Description: `:meta:subcategory:Data Center Inventory Management (DCIM):From the [official documentation](https://docs.netbox.dev/en/stable/models/dcim/moduletype/):`, + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeInt, + Optional: true, + Computed: true, + }, + "device_id": { + Type: schema.TypeInt, + Required: true, + }, + "name": { + Type: schema.TypeString, + Required: true, + }, + "type": { + Type: schema.TypeString, + Optional: true, + Description: "One of [8p8c, 8p6c, 8p4c, 8p2c, 6p6c, 6p4c, 6p2c, 4p4c, 4p2c, gg45, tera-4p, tera-2p, tera-1p, 110-punch, bnc, f, n, mrj21, fc, lc, lc-pc, lc-upc, lc-apc, lsh, lsh-pc, lsh-upc, lsh-apc, mpo, mtrj, sc, sc-pc, sc-upc, sc-apc, st, cs, sn, sma-905, sma-906, urm-p2, urm-p4, urm-p8, splice, other]", + }, + "positions": { + Type: schema.TypeInt, + Optional: true, + }, + "module_id": { + Type: schema.TypeInt, + Optional: true, + }, + "label": { + Type: schema.TypeString, + Optional: true, + }, + "color_hex": { + Type: schema.TypeString, + Optional: true, + }, + "description": { + Type: schema.TypeString, + Optional: true, + }, + "mark_connected": { + Type: schema.TypeBool, + Optional: true, + }, + tagsKey: tagsSchema, + }, + } +} + +func dataSourceNetboxDeviceRearPortRead(d *schema.ResourceData, m interface{}) error { + api := m.(*client.NetBoxAPI) + params := dcim.NewDcimRearPortsListParams() + + params.Limit = int64ToPtr(2) + if deviceID, ok := d.Get("device_id").(int); ok && deviceID != 0 { + deviceID := strconv.Itoa(deviceID) + params.SetDeviceID(&deviceID) + } + if name, ok := d.Get("name").(string); ok && name != "" { + params.SetName(&name) + } + + res, err := api.Dcim.DcimRearPortsList(params, nil) + if err != nil { + return err + } + + if count := *res.GetPayload().Count; count != 1 { + return fmt.Errorf("expected one `netbox_device_rear_port`, but got %d", count) + } + + result := res.GetPayload().Results[0] + d.SetId(strconv.FormatInt(result.ID, 10)) + d.Set("device_id", result.Device.ID) + d.Set("name", result.Name) + d.Set("type", result.Type.Value) + d.Set("positions", result.Positions) + + if result.Module != nil { + d.Set("module_id", result.Module.ID) + } else { + d.Set("module_id", nil) + } + + d.Set("label", result.Label) + d.Set("color_hex", result.Color) + d.Set("description", result.Description) + d.Set("mark_connected", result.MarkConnected) + + d.Set(tagsKey, getTagListFromNestedTagList(result.Tags)) + + return nil +} diff --git a/netbox/data_source_netbox_device_front_port_test.go b/netbox/data_source_netbox_device_front_port_test.go new file mode 100644 index 00000000..28afe8b5 --- /dev/null +++ b/netbox/data_source_netbox_device_front_port_test.go @@ -0,0 +1,99 @@ +package netbox + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func testAccNetboxDeviceRearPortSetUp(testName string) string { + return fmt.Sprintf(` +resource "netbox_manufacturer" "test" { + name = "%[1]s" +} + +resource "netbox_device_type" "test" { + model = "%[1]s" + manufacturer_id = netbox_manufacturer.test.id +} + +resource "netbox_site" "test" { + name = "%[1]s" + status = "active" +} + +resource "netbox_device_role" "test" { + name = "%[1]s" + color_hex = "123456" +} + +resource "netbox_device" "test" { + name = "%[1]s" + device_type_id = netbox_device_type.test.id + role_id = netbox_device_role.test.id + site_id = netbox_site.test.id +} + +resource "netbox_device" "test_2" { + name = "%[1]s_2" + device_type_id = netbox_device_type.test.id + role_id = netbox_device_role.test.id + site_id = netbox_site.test.id +} + +resource "netbox_device_rear_port" "test" { + device_id = netbox_device.test.id + name = "%[1]s" + description = "test_description" + type = "8p8c" + positions = 2 + mark_connected = true +}`, testName) +} + +const testAccNetboxDeviceRearPortNoResult = ` +data "netbox_device_rear_port" "test" { + device_id = netbox_device.test.id + name = "_does_not_exist_" + depends_on = [ + netbox_device_rear_port.test + ] +}` + +func testAccNetboxDeviceRearPortByName(testName string) string { + return fmt.Sprintf(` +data "netbox_device_rear_port" "test" { + device_id = netbox_device.test.id + name = "%[1]s" + depends_on = [ + netbox_device_rear_port.test + ] +}`, testName) +} + +func TestAccNetboxDeviceRearPortDataSource_basic(t *testing.T) { + testName := testAccGetTestName("module_bay_ds_basic") + setUp := testAccNetboxDeviceRearPortSetUp(testName) + resource.Test(t, resource.TestCase{ + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: setUp + testAccNetboxDeviceRearPortNoResult, + ExpectError: regexp.MustCompile("expected one"), + }, + { + Config: setUp + testAccNetboxDeviceRearPortByName(testName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair("data.netbox_device_rear_port.test", "id", "netbox_device_rear_port.test", "id"), + resource.TestCheckResourceAttr("data.netbox_device_rear_port.test", "name", testName), + resource.TestCheckResourceAttr("data.netbox_device_rear_port.test", "description", "test_description"), + resource.TestCheckResourceAttr("data.netbox_device_rear_port.test", "type", "8p8c"), + resource.TestCheckResourceAttr("data.netbox_device_rear_port.test", "positions", "2"), + resource.TestCheckResourceAttr("data.netbox_device_rear_port.test", "mark_connected", "true"), + ), + }, + }, + }) +} From 364465d329068b1d142633b140ff3d877dad9c38 Mon Sep 17 00:00:00 2001 From: vaneuk Date: Fri, 22 Dec 2023 14:50:49 +0100 Subject: [PATCH 6/6] feat: added front ports data source and test --- docs/data-sources/device_front_port.md | 39 ++++++++++++++ docs/data-sources/device_rear_port.md | 38 +++++++++++++ docs/data-sources/site.md | 2 +- .../data_source_netbox_device_front_port.go | 23 ++++---- ...ta_source_netbox_device_front_port_test.go | 54 ++++++++++++------- netbox/data_source_netbox_device_rear_port.go | 2 +- ...ata_source_netbox_device_rear_port_test.go | 2 +- netbox/provider.go | 1 + 8 files changed, 131 insertions(+), 30 deletions(-) create mode 100644 docs/data-sources/device_front_port.md create mode 100644 docs/data-sources/device_rear_port.md diff --git a/docs/data-sources/device_front_port.md b/docs/data-sources/device_front_port.md new file mode 100644 index 00000000..cdb74998 --- /dev/null +++ b/docs/data-sources/device_front_port.md @@ -0,0 +1,39 @@ +--- +# generated by https://github.com/fbreckle/terraform-plugin-docs +page_title: "netbox_device_front_port Data Source - terraform-provider-netbox" +subcategory: "Data Center Inventory Management (DCIM)" +description: |- + From the official documentation https://docs.netbox.dev/en/stable/models/dcim/moduletype/: +--- + +# netbox_device_front_port (Data Source) + +From the [official documentation](https://docs.netbox.dev/en/stable/models/dcim/moduletype/): + + + + +## Schema + +### Required + +- `device_id` (Number) +- `name` (String) + +### Optional + +- `color_hex` (String) +- `description` (String) +- `label` (String) +- `mark_connected` (Boolean) +- `module_id` (Number) +- `rear_port_id` (Number) +- `rear_port_position` (Number) +- `type` (String) One of [8p8c, 8p6c, 8p4c, 8p2c, 6p6c, 6p4c, 6p2c, 4p4c, 4p2c, gg45, tera-4p, tera-2p, tera-1p, 110-punch, bnc, f, n, mrj21, fc, lc, lc-pc, lc-upc, lc-apc, lsh, lsh-pc, lsh-upc, lsh-apc, mpo, mtrj, sc, sc-pc, sc-upc, sc-apc, st, cs, sn, sma-905, sma-906, urm-p2, urm-p4, urm-p8, splice, other]. + +### Read-Only + +- `id` (Number) The ID of this resource. +- `tags` (Set of String) + + diff --git a/docs/data-sources/device_rear_port.md b/docs/data-sources/device_rear_port.md new file mode 100644 index 00000000..b5ddf2c0 --- /dev/null +++ b/docs/data-sources/device_rear_port.md @@ -0,0 +1,38 @@ +--- +# generated by https://github.com/fbreckle/terraform-plugin-docs +page_title: "netbox_device_rear_port Data Source - terraform-provider-netbox" +subcategory: "Data Center Inventory Management (DCIM)" +description: |- + From the official documentation https://docs.netbox.dev/en/stable/models/dcim/moduletype/: +--- + +# netbox_device_rear_port (Data Source) + +From the [official documentation](https://docs.netbox.dev/en/stable/models/dcim/moduletype/): + + + + +## Schema + +### Required + +- `device_id` (Number) +- `name` (String) + +### Optional + +- `color_hex` (String) +- `description` (String) +- `label` (String) +- `mark_connected` (Boolean) +- `module_id` (Number) +- `positions` (Number) +- `type` (String) One of [8p8c, 8p6c, 8p4c, 8p2c, 6p6c, 6p4c, 6p2c, 4p4c, 4p2c, gg45, tera-4p, tera-2p, tera-1p, 110-punch, bnc, f, n, mrj21, fc, lc, lc-pc, lc-upc, lc-apc, lsh, lsh-pc, lsh-upc, lsh-apc, mpo, mtrj, sc, sc-pc, sc-upc, sc-apc, st, cs, sn, sma-905, sma-906, urm-p2, urm-p4, urm-p8, splice, other]. + +### Read-Only + +- `id` (Number) The ID of this resource. +- `tags` (Set of String) + + diff --git a/docs/data-sources/site.md b/docs/data-sources/site.md index 3a7536d4..9dff65c6 100644 --- a/docs/data-sources/site.md +++ b/docs/data-sources/site.md @@ -27,13 +27,13 @@ data "netbox_site" "get_by_slug" { ### Optional -- `asn_ids` (Set of Number) - `facility` (String) - `name` (String) - `slug` (String) ### Read-Only +- `asn_ids` (Set of Number) - `comments` (String) - `description` (String) - `group_id` (Number) diff --git a/netbox/data_source_netbox_device_front_port.go b/netbox/data_source_netbox_device_front_port.go index 01f2d2c4..c2910007 100644 --- a/netbox/data_source_netbox_device_front_port.go +++ b/netbox/data_source_netbox_device_front_port.go @@ -9,9 +9,9 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -func dataSourceNetboxDeviceRearPort() *schema.Resource { +func dataSourceNetboxDeviceFrontPort() *schema.Resource { return &schema.Resource{ - Read: dataSourceNetboxDeviceRearPortRead, + Read: dataSourceNetboxDeviceFrontPortRead, Description: `:meta:subcategory:Data Center Inventory Management (DCIM):From the [official documentation](https://docs.netbox.dev/en/stable/models/dcim/moduletype/):`, Schema: map[string]*schema.Schema{ "id": { @@ -32,7 +32,11 @@ func dataSourceNetboxDeviceRearPort() *schema.Resource { Optional: true, Description: "One of [8p8c, 8p6c, 8p4c, 8p2c, 6p6c, 6p4c, 6p2c, 4p4c, 4p2c, gg45, tera-4p, tera-2p, tera-1p, 110-punch, bnc, f, n, mrj21, fc, lc, lc-pc, lc-upc, lc-apc, lsh, lsh-pc, lsh-upc, lsh-apc, mpo, mtrj, sc, sc-pc, sc-upc, sc-apc, st, cs, sn, sma-905, sma-906, urm-p2, urm-p4, urm-p8, splice, other]", }, - "positions": { + "rear_port_id": { + Type: schema.TypeInt, + Optional: true, + }, + "rear_port_position": { Type: schema.TypeInt, Optional: true, }, @@ -56,14 +60,14 @@ func dataSourceNetboxDeviceRearPort() *schema.Resource { Type: schema.TypeBool, Optional: true, }, - tagsKey: tagsSchema, + tagsKey: tagsSchemaRead, }, } } -func dataSourceNetboxDeviceRearPortRead(d *schema.ResourceData, m interface{}) error { +func dataSourceNetboxDeviceFrontPortRead(d *schema.ResourceData, m interface{}) error { api := m.(*client.NetBoxAPI) - params := dcim.NewDcimRearPortsListParams() + params := dcim.NewDcimFrontPortsListParams() params.Limit = int64ToPtr(2) if deviceID, ok := d.Get("device_id").(int); ok && deviceID != 0 { @@ -74,13 +78,13 @@ func dataSourceNetboxDeviceRearPortRead(d *schema.ResourceData, m interface{}) e params.SetName(&name) } - res, err := api.Dcim.DcimRearPortsList(params, nil) + res, err := api.Dcim.DcimFrontPortsList(params, nil) if err != nil { return err } if count := *res.GetPayload().Count; count != 1 { - return fmt.Errorf("expected one `netbox_device_rear_port`, but got %d", count) + return fmt.Errorf("expected one `netbox_device_front_port`, but got %d", count) } result := res.GetPayload().Results[0] @@ -88,7 +92,8 @@ func dataSourceNetboxDeviceRearPortRead(d *schema.ResourceData, m interface{}) e d.Set("device_id", result.Device.ID) d.Set("name", result.Name) d.Set("type", result.Type.Value) - d.Set("positions", result.Positions) + d.Set("rear_port_id", result.RearPort.ID) + d.Set("rear_port_position", result.RearPortPosition) if result.Module != nil { d.Set("module_id", result.Module.ID) diff --git a/netbox/data_source_netbox_device_front_port_test.go b/netbox/data_source_netbox_device_front_port_test.go index 28afe8b5..25cc9df6 100644 --- a/netbox/data_source_netbox_device_front_port_test.go +++ b/netbox/data_source_netbox_device_front_port_test.go @@ -8,7 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) -func testAccNetboxDeviceRearPortSetUp(testName string) string { +func testAccNetboxDeviceFrontPortSetUp(testName string) string { return fmt.Sprintf(` resource "netbox_manufacturer" "test" { name = "%[1]s" @@ -50,48 +50,66 @@ resource "netbox_device_rear_port" "test" { type = "8p8c" positions = 2 mark_connected = true +} + +resource "netbox_device_front_port" "test" { + device_id = netbox_device.test.id + name = "%[1]s" + type = "8p8c" + rear_port_id = netbox_device_rear_port.test.id + rear_port_position = 1 + + mark_connected = true + label = "%[1]s_label" + #color_hex = "123456" + description = "test_description" + #tags = ["%[1]sa"] }`, testName) } -const testAccNetboxDeviceRearPortNoResult = ` -data "netbox_device_rear_port" "test" { +const testAccNetboxDeviceFrontPortNoResult = ` +data "netbox_device_front_port" "test" { device_id = netbox_device.test.id name = "_does_not_exist_" depends_on = [ - netbox_device_rear_port.test + netbox_device_front_port.test ] }` -func testAccNetboxDeviceRearPortByName(testName string) string { +func testAccNetboxDeviceFrontPortByName(testName string) string { return fmt.Sprintf(` -data "netbox_device_rear_port" "test" { +data "netbox_device_front_port" "test" { device_id = netbox_device.test.id name = "%[1]s" depends_on = [ - netbox_device_rear_port.test + netbox_device_front_port.test ] }`, testName) } -func TestAccNetboxDeviceRearPortDataSource_basic(t *testing.T) { - testName := testAccGetTestName("module_bay_ds_basic") - setUp := testAccNetboxDeviceRearPortSetUp(testName) +func TestAccNetboxDeviceFrontPortDataSource_basic(t *testing.T) { + testName := testAccGetTestName("device_front_port_ds_basic") + setUp := testAccNetboxDeviceFrontPortSetUp(testName) resource.Test(t, resource.TestCase{ Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: setUp + testAccNetboxDeviceRearPortNoResult, + Config: setUp + testAccNetboxDeviceFrontPortNoResult, ExpectError: regexp.MustCompile("expected one"), }, { - Config: setUp + testAccNetboxDeviceRearPortByName(testName), + Config: setUp + testAccNetboxDeviceFrontPortByName(testName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrPair("data.netbox_device_rear_port.test", "id", "netbox_device_rear_port.test", "id"), - resource.TestCheckResourceAttr("data.netbox_device_rear_port.test", "name", testName), - resource.TestCheckResourceAttr("data.netbox_device_rear_port.test", "description", "test_description"), - resource.TestCheckResourceAttr("data.netbox_device_rear_port.test", "type", "8p8c"), - resource.TestCheckResourceAttr("data.netbox_device_rear_port.test", "positions", "2"), - resource.TestCheckResourceAttr("data.netbox_device_rear_port.test", "mark_connected", "true"), + resource.TestCheckResourceAttrPair("data.netbox_device_front_port.test", "id", "netbox_device_front_port.test", "id"), + resource.TestCheckResourceAttr("data.netbox_device_front_port.test", "name", testName), + resource.TestCheckResourceAttr("data.netbox_device_front_port.test", "type", "8p8c"), + resource.TestCheckResourceAttrPair("data.netbox_device_front_port.test", "rear_port_id", "netbox_device_front_port.test", "rear_port_id"), + resource.TestCheckResourceAttr("data.netbox_device_front_port.test", "rear_port_position", "1"), + resource.TestCheckResourceAttr("data.netbox_device_front_port.test", "module_id", "0"), + resource.TestCheckResourceAttr("data.netbox_device_front_port.test", "label", testName+"_label"), + resource.TestCheckResourceAttr("data.netbox_device_front_port.test", "color_hex", ""), + resource.TestCheckResourceAttr("data.netbox_device_front_port.test", "description", "test_description"), + resource.TestCheckResourceAttr("data.netbox_device_front_port.test", "mark_connected", "true"), ), }, }, diff --git a/netbox/data_source_netbox_device_rear_port.go b/netbox/data_source_netbox_device_rear_port.go index 01f2d2c4..0755e0d3 100644 --- a/netbox/data_source_netbox_device_rear_port.go +++ b/netbox/data_source_netbox_device_rear_port.go @@ -56,7 +56,7 @@ func dataSourceNetboxDeviceRearPort() *schema.Resource { Type: schema.TypeBool, Optional: true, }, - tagsKey: tagsSchema, + tagsKey: tagsSchemaRead, }, } } diff --git a/netbox/data_source_netbox_device_rear_port_test.go b/netbox/data_source_netbox_device_rear_port_test.go index 28afe8b5..39440d24 100644 --- a/netbox/data_source_netbox_device_rear_port_test.go +++ b/netbox/data_source_netbox_device_rear_port_test.go @@ -74,7 +74,7 @@ data "netbox_device_rear_port" "test" { } func TestAccNetboxDeviceRearPortDataSource_basic(t *testing.T) { - testName := testAccGetTestName("module_bay_ds_basic") + testName := testAccGetTestName("device_rear_port_ds_basic") setUp := testAccNetboxDeviceRearPortSetUp(testName) resource.Test(t, resource.TestCase{ Providers: testAccProviders, diff --git a/netbox/provider.go b/netbox/provider.go index f9d60d67..5b9545f5 100644 --- a/netbox/provider.go +++ b/netbox/provider.go @@ -159,6 +159,7 @@ func Provider() *schema.Provider { "netbox_prefix": dataSourceNetboxPrefix(), "netbox_prefixes": dataSourceNetboxPrefixes(), "netbox_devices": dataSourceNetboxDevices(), + "netbox_device_front_port": dataSourceNetboxDeviceFrontPort(), "netbox_device_rear_port": dataSourceNetboxDeviceRearPort(), "netbox_device_module_bay": dataSourceNetboxDeviceModuleBay(), "netbox_device_role": dataSourceNetboxDeviceRole(),