diff --git a/netbox/resource_netbox_service.go b/netbox/resource_netbox_service.go index 3988db03..c4cceed9 100644 --- a/netbox/resource_netbox_service.go +++ b/netbox/resource_netbox_service.go @@ -55,6 +55,7 @@ func resourceNetboxService() *schema.Resource { Type: schema.TypeInt, }, }, + customFieldsKey: customFieldsSchema, }, Importer: &schema.ResourceImporter{ StateContext: schema.ImportStatePassthroughContext, @@ -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 { @@ -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 } @@ -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 { diff --git a/netbox/resource_netbox_service_test.go b/netbox/resource_netbox_service_test.go index 000bebb3..5f346072 100644 --- a/netbox/resource_netbox_service_test.go +++ b/netbox/resource_netbox_service_test.go @@ -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)