Skip to content

Commit

Permalink
feat(dhcp-relay): Add DHCP Relay support
Browse files Browse the repository at this point in the history
Closes #413
  • Loading branch information
vaerh committed Apr 9, 2024
1 parent 74333f2 commit 6eb5901
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/resources/routeros_ip_dhcp_relay/import.sh
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 5 additions & 0 deletions examples/resources/routeros_ip_dhcp_relay/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "routeros_ip_dhcp_relay" "relay" {
name = "test relay"
interface = "ether1"
dhcp_server = "0.0.0.1"
}
1 change: 1 addition & 0 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 4 additions & 0 deletions routeros/provider_schema_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,10 @@ var (
return true
}

if (old == "none" && new == "") || (old == "" && new == "none") {
return true
}

if old == "" || new == "" {
return false
}
Expand Down
66 changes: 66 additions & 0 deletions routeros/resource_ip_dhcp_relay.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
48 changes: 48 additions & 0 deletions routeros/resource_ip_dhcp_relay_test.go
Original file line number Diff line number Diff line change
@@ -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"
}
`
}

0 comments on commit 6eb5901

Please sign in to comment.