From 10597288a5b90f836f9895607751d9f43cf1a682 Mon Sep 17 00:00:00 2001 From: Melchior Moulin Date: Sat, 10 Feb 2024 21:39:24 +0100 Subject: [PATCH] add dhcp client option Add the dhcp client option --- .../routeros_ip_dhcp_client_option/import.sh | 3 ++ .../resource.tf | 4 ++ routeros/provider.go | 6 ++- routeros/resource_ip_dhcp_client option.go | 47 +++++++++++++++++++ .../resource_ip_dhcp_client_option_test.go | 47 +++++++++++++++++++ 5 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 examples/resources/routeros_ip_dhcp_client_option/import.sh create mode 100644 examples/resources/routeros_ip_dhcp_client_option/resource.tf create mode 100644 routeros/resource_ip_dhcp_client option.go create mode 100644 routeros/resource_ip_dhcp_client_option_test.go diff --git a/examples/resources/routeros_ip_dhcp_client_option/import.sh b/examples/resources/routeros_ip_dhcp_client_option/import.sh new file mode 100644 index 00000000..3b68c7c7 --- /dev/null +++ b/examples/resources/routeros_ip_dhcp_client_option/import.sh @@ -0,0 +1,3 @@ +#The ID can be found via API or the terminal +#The command for the terminal is -> :put [/ip/dhcp-client/option get [print show-ids]] +terraform import routeros_ip_dhcp_client_option.option "*0" \ No newline at end of file diff --git a/examples/resources/routeros_ip_dhcp_client_option/resource.tf b/examples/resources/routeros_ip_dhcp_client_option/resource.tf new file mode 100644 index 00000000..c02794f5 --- /dev/null +++ b/examples/resources/routeros_ip_dhcp_client_option/resource.tf @@ -0,0 +1,4 @@ +resource "routeros_ip_dhcp_client_option" "option" { + name = "my-dhcp-option" + code = 60 +} \ No newline at end of file diff --git a/routeros/provider.go b/routeros/provider.go index c5b4ece1..132c2c38 100644 --- a/routeros/provider.go +++ b/routeros/provider.go @@ -21,14 +21,14 @@ func Provider() *schema.Provider { []string{"ROS_HOSTURL", "MIKROTIK_HOST"}, nil, ), - Description: `URL of the MikroTik router, default is TLS connection to REST. + Description: `URL of the MikroTik router, default is TLS connection to REST. * API: api[s]://host[:port] * api://router.local * apis://router.local:8729 * REST: https://host * https://router.local * router.local - * 127.0.0.1 + * 127.0.0.1 export ROS_HOSTURL=router.local or export MIKROTIK_HOST=router.local @@ -80,6 +80,7 @@ func Provider() *schema.Provider { // IP objects "routeros_ip_dhcp_client": ResourceDhcpClient(), + "routeros_ip_dhcp_client_option": ResourceDhcpClientOption(), "routeros_ip_dhcp_server": ResourceDhcpServer(), "routeros_ip_dhcp_server_config": ResourceDhcpServerConfig(), "routeros_ip_dhcp_server_network": ResourceDhcpServerNetwork(), @@ -104,6 +105,7 @@ func Provider() *schema.Provider { // Aliases for IP objects to retain compatibility between original and fork "routeros_dhcp_client": ResourceDhcpClient(), + "routeros_dhcp_client_option": ResourceDhcpClientOption(), "routeros_dhcp_server": ResourceDhcpServer(), "routeros_dhcp_server_network": ResourceDhcpServerNetwork(), "routeros_dhcp_server_lease": ResourceDhcpServerLease(), diff --git a/routeros/resource_ip_dhcp_client option.go b/routeros/resource_ip_dhcp_client option.go new file mode 100644 index 00000000..f62412a0 --- /dev/null +++ b/routeros/resource_ip_dhcp_client option.go @@ -0,0 +1,47 @@ +package routeros + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +// ResourceDhcpClient https://help.mikrotik.com/docs/display/ROS/DHCP#DHCP-DHCPClient +func ResourceDhcpClientOption() *schema.Resource { + resSchema := map[string]*schema.Schema{ + MetaResourcePath: PropResourcePath("/ip/dhcp-client/option"), + MetaId: PropId(Id), + "name": { + Type: schema.TypeString, + Required: true, + Description: "The name that will be used in dhcp-client.", + }, + "code": { + Type: schema.TypeInt, + Required: true, + Description: "The dhcp-client option code.", + }, + "value": { + Type: schema.TypeString, + Optional: true, + Description: "The dhcp-client option", + }, + "raw_value": + { + Type: schema.TypeString, + Optional: true, + Computed: true, + Description: "raw_value is computed from value.", + }, + + } + return &schema.Resource{ + CreateContext: DefaultCreate(resSchema), + ReadContext: DefaultRead(resSchema), + UpdateContext: DefaultUpdate(resSchema), + DeleteContext: DefaultDelete(resSchema), + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + + Schema: resSchema, + } +} diff --git a/routeros/resource_ip_dhcp_client_option_test.go b/routeros/resource_ip_dhcp_client_option_test.go new file mode 100644 index 00000000..a60e367d --- /dev/null +++ b/routeros/resource_ip_dhcp_client_option_test.go @@ -0,0 +1,47 @@ +package routeros + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +const testIpDhcpClientOptionAddress = "routeros_ip_dhcp_client_option.test_dhcp" + +func TestAccIpDhcpClientOptionTest_basic(t *testing.T) { + for _, name := range testNames { + t.Run(name, func(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testSetTransportEnv(t, name) + }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testCheckResourceDestroy("/ip/dhcp-client-option", "routeros_ip_dhcp_client_option"), + Steps: []resource.TestStep{ + { + Config: testAccIpDhcpClientOptionConfig(), + Check: resource.ComposeTestCheckFunc( + testResourcePrimaryInstanceId(testIpDhcpClientOptionAddress), + resource.TestCheckResourceAttr(testIpDhcpClientOptionAddress, "name", "my-dhcp-option"), + resource.TestCheckResourceAttr(testIpDhcpClientOptionAddress, "code", "60"), + + ), + }, + }, + }) + + }) + } +} + +func testAccIpDhcpClientOptionConfig() string { + return providerConfig + ` + +resource "routeros_ip_dhcp_client_option" "test_dhcp" { + name = "my-dhcp-option" + code = 60 + } + +` +}