Skip to content

Commit

Permalink
add support for custom fields in service resource
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianreloaded authored and fbreckle committed Aug 4, 2023
1 parent 21c3d21 commit 387e964
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
16 changes: 16 additions & 0 deletions netbox/resource_netbox_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func resourceNetboxService() *schema.Resource {
Type: schema.TypeInt,
},
},
customFieldsKey: customFieldsSchema,
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
Expand Down Expand Up @@ -94,6 +95,11 @@ func resourceNetboxServiceCreate(d *schema.ResourceData, m interface{}) error {
data.Tags = []*models.NestedTag{}
data.Ipaddresses = []int64{}

ct, ok := d.GetOk(customFieldsKey)
if ok {
data.CustomFields = ct
}

params := ipam.NewIpamServicesCreateParams().WithData(&data)
res, err := api.Ipam.IpamServicesCreate(params, nil)
if err != nil {
Expand Down Expand Up @@ -127,6 +133,11 @@ func resourceNetboxServiceRead(d *schema.ResourceData, m interface{}) error {
d.Set("ports", res.GetPayload().Ports)
d.Set("virtual_machine_id", res.GetPayload().VirtualMachine.ID)

cf := getCustomFields(res.GetPayload().CustomFields)
if cf != nil {
d.Set(customFieldsKey, cf)
}

return nil
}

Expand Down Expand Up @@ -161,6 +172,11 @@ func resourceNetboxServiceUpdate(d *schema.ResourceData, m interface{}) error {
dataVirtualMachineID := int64(d.Get("virtual_machine_id").(int))
data.VirtualMachine = &dataVirtualMachineID

cf, ok := d.GetOk(customFieldsKey)
if ok {
data.CustomFields = cf
}

params := ipam.NewIpamServicesUpdateParams().WithID(id).WithData(&data)
_, err := api.Ipam.IpamServicesUpdate(params, nil)
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions netbox/resource_netbox_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,46 @@ resource "netbox_service" "test" {
})
}

func TestAccNetboxService_customFields(t *testing.T) {
testSlug := "svc_custom_fields"
testName := testAccGetTestName(testSlug)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccNetboxServiceFullDependencies(testName) + fmt.Sprintf(`
resource "netbox_custom_field" "test" {
name = "custom_field"
type = "text"
content_types = ["ipam.service"]
}
resource "netbox_service" "test_customfield" {
name = "%s"
virtual_machine_id = netbox_virtual_machine.test.id
ports = [333]
protocol = "tcp"
custom_fields = {"${netbox_custom_field.test.name}" = "testtext"}
}`, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_service.test_customfield", "name", testName),
resource.TestCheckResourceAttrPair("netbox_service.test_customfield", "virtual_machine_id", "netbox_virtual_machine.test", "id"),
resource.TestCheckResourceAttr("netbox_service.test_customfield", "ports.#", "1"),
resource.TestCheckResourceAttr("netbox_service.test_customfield", "ports.0", "333"),
resource.TestCheckResourceAttr("netbox_service.test_customfield", "protocol", "tcp"),
resource.TestCheckResourceAttr("netbox_service.test_customfield", "custom_fields.custom_field", "testtext"),
),
},
{
ResourceName: "netbox_service.test_customfield",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckServiceDestroy(s *terraform.State) error {
// retrieve the connection established in Provider configuration
conn := testAccProvider.Meta().(*client.NetBoxAPI)
Expand Down

0 comments on commit 387e964

Please sign in to comment.