From c5dea0a6cc02581f329b0f29019e3643f58bf0da Mon Sep 17 00:00:00 2001 From: Vaerh Date: Tue, 9 Apr 2024 22:50:46 +0300 Subject: [PATCH] feat(dhcp-relay): Add DHCP Relay support Closes #413 --- .../routeros_ip_dhcp_relay/import.sh | 3 + .../routeros_ip_dhcp_relay/resource.tf | 5 ++ routeros/provider.go | 1 + routeros/provider_schema_helpers.go | 4 ++ routeros/resource_ip_dhcp_relay.go | 66 +++++++++++++++++++ routeros/resource_ip_dhcp_relay_test.go | 48 ++++++++++++++ 6 files changed, 127 insertions(+) create mode 100644 examples/resources/routeros_ip_dhcp_relay/import.sh create mode 100644 examples/resources/routeros_ip_dhcp_relay/resource.tf create mode 100644 routeros/resource_ip_dhcp_relay.go create mode 100644 routeros/resource_ip_dhcp_relay_test.go diff --git a/examples/resources/routeros_ip_dhcp_relay/import.sh b/examples/resources/routeros_ip_dhcp_relay/import.sh new file mode 100644 index 00000000..d7e8e744 --- /dev/null +++ b/examples/resources/routeros_ip_dhcp_relay/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-relay get [print show-ids]] +terraform import routeros_ip_dhcp_relay.relay "*0" \ No newline at end of file diff --git a/examples/resources/routeros_ip_dhcp_relay/resource.tf b/examples/resources/routeros_ip_dhcp_relay/resource.tf new file mode 100644 index 00000000..77cab7d5 --- /dev/null +++ b/examples/resources/routeros_ip_dhcp_relay/resource.tf @@ -0,0 +1,5 @@ +resource "routeros_ip_dhcp_relay" "relay" { + name = "test relay" + interface = "ether1" + dhcp_server = "0.0.0.1" +} \ No newline at end of file diff --git a/routeros/provider.go b/routeros/provider.go index 13549b05..f3aeacb5 100644 --- a/routeros/provider.go +++ b/routeros/provider.go @@ -82,6 +82,7 @@ func Provider() *schema.Provider { // IP objects "routeros_ip_dhcp_client": ResourceDhcpClient(), "routeros_ip_dhcp_client_option": ResourceDhcpClientOption(), + "routeros_ip_dhcp_relay": ResourceDhcpRelay(), "routeros_ip_dhcp_server": ResourceDhcpServer(), "routeros_ip_dhcp_server_config": ResourceDhcpServerConfig(), "routeros_ip_dhcp_server_network": ResourceDhcpServerNetwork(), diff --git a/routeros/provider_schema_helpers.go b/routeros/provider_schema_helpers.go index a089b652..e8e240bb 100644 --- a/routeros/provider_schema_helpers.go +++ b/routeros/provider_schema_helpers.go @@ -575,6 +575,10 @@ var ( return true } + if (old == "none" && new == "") || (old == "" && new == "none") { + return true + } + if old == "" || new == "" { return false } diff --git a/routeros/resource_ip_dhcp_relay.go b/routeros/resource_ip_dhcp_relay.go new file mode 100644 index 00000000..8b4bf742 --- /dev/null +++ b/routeros/resource_ip_dhcp_relay.go @@ -0,0 +1,66 @@ +package routeros + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +// ResourceDhcpRelay https://wiki.mikrotik.com/wiki/Manual:IP/DHCP_Relay +func ResourceDhcpRelay() *schema.Resource { + resSchema := map[string]*schema.Schema{ + MetaResourcePath: PropResourcePath("/ip/dhcp-relay"), + MetaId: PropId(Id), + + "add_relay_info": { + Type: schema.TypeBool, + Optional: true, + Description: "Adds DHCP relay agent information if enabled according to RFC 3046. Agent Circuit ID " + + "Sub-option contains mac address of an interface, Agent Remote ID Sub-option contains MAC address " + + "of the client from which request was received.", + DiffSuppressFunc: AlwaysPresentNotUserProvided, + }, + "delay_threshold": { + Type: schema.TypeString, + Optional: true, + Description: "If secs field in DHCP packet is smaller than delay-threshold, then this packet is ignored.", + DiffSuppressFunc: TimeEquall, + }, + KeyDisabled: PropDisabledRw, + "dhcp_server": { + Type: schema.TypeString, + Required: true, + Description: "List of DHCP servers' IP addresses which should the DHCP requests be forwarded to.", + }, + "interface": { + Type: schema.TypeString, + Required: true, + Description: "Interface name the DHCP relay will be working on.", + }, + KeyInvalid: PropInvalidRo, + "local_address": { + Type: schema.TypeString, + Optional: true, + Description: "The unique IP address of this DHCP relay needed for DHCP server to distinguish relays. " + + "If set to 0.0.0.0 - the IP address will be chosen automatically", + DiffSuppressFunc: AlwaysPresentNotUserProvided, + }, + KeyName: PropName("Descriptive name for the relay."), + "relay_info_remote_id": { + Type: schema.TypeString, + Optional: true, + Description: "Specified string will be used to construct Option 82 instead of client's MAC address. Option " + + "82 consist of: interface from which packets was received + client mac address or relay-info-remote-id", + DiffSuppressFunc: AlwaysPresentNotUserProvided, + }, + } + 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_relay_test.go b/routeros/resource_ip_dhcp_relay_test.go new file mode 100644 index 00000000..b109e50d --- /dev/null +++ b/routeros/resource_ip_dhcp_relay_test.go @@ -0,0 +1,48 @@ +package routeros + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +const testIpDhcpRelayAddress = "routeros_ip_dhcp_relay.test" + +func TestAccIpDhcpRelayTest_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-relay", "routeros_ip_dhcp_relay"), + Steps: []resource.TestStep{ + { + Config: testAccIpDhcpRelayConfig(), + Check: resource.ComposeTestCheckFunc( + testResourcePrimaryInstanceId(testIpDhcpRelayAddress), + resource.TestCheckResourceAttr(testIpDhcpRelayAddress, "name", "test relay"), + resource.TestCheckResourceAttr(testIpDhcpRelayAddress, "interface", "ether1"), + resource.TestCheckResourceAttr(testIpDhcpRelayAddress, "dhcp_server", "0.0.0.1"), + ), + }, + }, + }) + + }) + } +} + +func testAccIpDhcpRelayConfig() string { + return providerConfig + ` + +resource "routeros_ip_dhcp_relay" "test" { + name = "test relay" + interface = "ether1" + dhcp_server = "0.0.0.1" + } + +` +}