Skip to content

Commit

Permalink
Add manufacturer_id to netbox_platfrom data and resource blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ad8lmondy committed Jun 17, 2024
1 parent 368ebf7 commit 3ec24bd
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/platform.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ data "netbox_platform" "PANOS" {

- `id` (String) The ID of this resource.
- `slug` (String)
- `manufacturer_id` (String)


1 change: 1 addition & 0 deletions docs/resources/platform.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ resource "netbox_platform" "PANOS" {
### Optional

- `slug` (String)
- `manufacturer_id` (String)

### Read-Only

Expand Down
7 changes: 7 additions & 0 deletions netbox/data_source_netbox_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func dataSourceNetboxPlatform() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"manufacturer_id": {
Type: schema.TypeInt,
Optional: true,
},
},
}
}
Expand Down Expand Up @@ -50,5 +54,8 @@ func dataSourceNetboxPlatformRead(d *schema.ResourceData, m interface{}) error {
d.SetId(strconv.FormatInt(result.ID, 10))
d.Set("name", result.Name)
d.Set("slug", result.Slug)
if result.Manufacturer != nil {
d.Set("manufacturer_id", result.Manufacturer.ID)
}
return nil
}
29 changes: 29 additions & 0 deletions netbox/data_source_netbox_platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,32 @@ data "netbox_platform" "test" {
},
})
}

func TestAccNetboxPlatformDataSource_manufacturer(t *testing.T) {
testSlug := "pltf_ds_manufacturer"
testName := testAccGetTestName(testSlug)
resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "netbox_manufacturer" "test" {
name = "%[1]s"
}
resource "netbox_platform" "test" {
name = "%[1]s"
manufacturer_id = netbox_manufacturer.test.id
}
data "netbox_platform" "test" {
depends_on = [netbox_platform.test]
name = "%[1]s"
}`, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair("data.netbox_platform.test", "id", "netbox_platform.test", "id"),
resource.TestCheckResourceAttrPair("data.netbox_platform.test", "manufacturer_id", "netbox_manufacturer.test", "id"),
),
},
},
})
}
37 changes: 28 additions & 9 deletions netbox/resource_netbox_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func resourceNetboxPlatform() *schema.Resource {
Computed: true,
ValidateFunc: validation.StringLenBetween(1, 100),
},
"manufacturer_id": {
Type: schema.TypeInt,
Optional: true,
},
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
Expand All @@ -53,13 +57,18 @@ func resourceNetboxPlatformCreate(d *schema.ResourceData, m interface{}) error {
slug = slugValue.(string)
}

params := dcim.NewDcimPlatformsCreateParams().WithData(
&models.WritablePlatform{
Name: &name,
Slug: &slug,
Tags: []*models.NestedTag{},
},
)
data := models.WritablePlatform{
Name: &name,
Slug: &slug,
Tags: []*models.NestedTag{},
}

manufacturerIDValue, ok := d.GetOk("manufacturer_id")
if ok {
data.Manufacturer = int64ToPtr(int64(manufacturerIDValue.(int)))
}

params := dcim.NewDcimPlatformsCreateParams().WithData(&data)

res, err := api.Dcim.DcimPlatformsCreate(params, nil)
if err != nil {
Expand Down Expand Up @@ -91,8 +100,13 @@ func resourceNetboxPlatformRead(d *schema.ResourceData, m interface{}) error {
return err
}

d.Set("name", res.GetPayload().Name)
d.Set("slug", res.GetPayload().Slug)
result := res.GetPayload()

d.Set("name", result.Name)
d.Set("slug", result.Slug)
if result.Manufacturer != nil {
d.Set("manufacturer_id", result.Manufacturer.ID)
}
return nil
}

Expand All @@ -117,6 +131,11 @@ func resourceNetboxPlatformUpdate(d *schema.ResourceData, m interface{}) error {
data.Name = &name
data.Tags = []*models.NestedTag{}

manufacturerIDValue, ok := d.GetOk("manufacturer_id")
if ok {
data.Manufacturer = int64ToPtr(int64(manufacturerIDValue.(int)))
}

params := dcim.NewDcimPlatformsPartialUpdateParams().WithID(id).WithData(&data)

_, err := api.Dcim.DcimPlatformsPartialUpdate(params, nil)
Expand Down
35 changes: 35 additions & 0 deletions netbox/resource_netbox_platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,41 @@ resource "netbox_platform" "test" {
})
}

func TestAccNetboxPlatform_manufacturer(t *testing.T) {
testSlug := "platform_manufacturer"
testName := testAccGetTestName(testSlug)
testManufacturer := "manu_test"
randomSlug := testAccGetTestName(testSlug)
resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "netbox_manufacturer" "test" {
name = "%[3]s"
}
resource "netbox_platform" "test" {
name = "%[1]s"
slug = "%[2]s"
manufacturer_id = netbox_manufacturer.test.id
}`, testName, randomSlug, testManufacturer),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_platform.test", "name", testName),
resource.TestCheckResourceAttr("netbox_platform.test", "slug", randomSlug),
resource.TestCheckResourceAttrPair("netbox_platform.test", "manufacturer_id", "netbox_manufacturer.test", "id"),
),
},
{
ResourceName: "netbox_platform.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccNetboxPlatform_defaultSlug(t *testing.T) {
testSlug := "platform_defSlug"
testName := testAccGetTestName(testSlug)
Expand Down

0 comments on commit 3ec24bd

Please sign in to comment.