Skip to content

Commit

Permalink
Add ipv6 dhcp client resource
Browse files Browse the repository at this point in the history
  • Loading branch information
jlpedrosa committed Feb 12, 2024
1 parent 5b31239 commit 33e37b6
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 7 deletions.
4 changes: 1 addition & 3 deletions docs/resources/interface_ipip.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ resource "routeros_interface_ipip" "ipip_hq" {

### Required

- `name` (String) Changing the name of this resource will force it to be recreated.
> The links of other configuration properties to this resource may be lost!
> Changing the name of the resource outside of a Terraform will result in a loss of control integrity for that resource!
- `name` (String) Name of the ipip interface.

### Optional

Expand Down
4 changes: 1 addition & 3 deletions docs/resources/ipip.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

### Required

- `name` (String) Changing the name of this resource will force it to be recreated.
> The links of other configuration properties to this resource may be lost!
> Changing the name of the resource outside of a Terraform will result in a loss of control integrity for that resource!
- `name` (String) Name of the ipip interface.

### Optional

Expand Down
32 changes: 32 additions & 0 deletions docs/resources/ipv6_dhcp_client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# routeros_ipv6_dhcp_client (Resource)




<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `interface` (String) The interface on which the DHCPv6 client will be running.
- `pool_name` (String) Name of the IPv6 pool in which received IPv6 prefix will be added
- `pool_prefix_length` (Number) Prefix length parameter that will be set for IPv6 pool in which received IPv6 prefix is added. Prefix length must be greater than the length of the received prefix, otherwise, prefix-length will be set to received prefix length + 8 bits.

### Optional

- `add-default-route ` (Boolean) Whether to add default IPv6 route after a client connects.
- `comment` (String)
- `disabled` (Boolean)
- `prefix_hint ` (Number) Include a preferred prefix length.
- `script` (String) Run this script on the DHCP-client status change. Available variables:pd-valid - if the prefix is acquired by the client;pd-prefix - the prefix acquired by the client if any;na-valid - if the address is acquired by the client;na-address - the address acquired by the client if any.options - array of received options (only ROSv7)
- `use_peer_dns` (Boolean) Routing table this route belongs to.

### Read-Only

- `duid` (String) Auto-generated DUID that is sent to the server.DUID is generated using one of the MAC addresses available on the router.
- `id` (String) The ID of this resource.
- `prefix` (String) Shows received IPv6 prefix from DHCPv6-PD server
- `request` (List of String) To choose if the DHCPv6 request will ask for the address or the IPv6 prefix, or both.
- `status` (String) Shows the status of DHCPv6 Client:stopped - dhcpv6 client is stoppedsearching - sending "solicit" and trying to get "advertise" Shows actual (resolved) gateway and interface that will be used for packet forwarding.requesting - sent "request" waiting for "reply"bound - received "reply". Prefix assigned. renewing - sent "renew", waiting for "reply" rebinding - sent "rebind", waiting for "reply" error - reply was not received in time or some other error occurred. stopping - sent "release"


3 changes: 2 additions & 1 deletion routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func Provider() *schema.Provider {
"routeros_ip_dns": ResourceDns(),
"routeros_ip_dns_record": ResourceDnsRecord(),
"routeros_ip_service": ResourceIpService(),
"routeros_ipv6_dhcp_client": ResourceIPv6DhcpClient(),
"routeros_ipv6_address": ResourceIPv6Address(),
"routeros_ipv6_firewall_addr_list": ResourceIPv6FirewallAddrList(),
"routeros_ipv6_firewall_filter": ResourceIPv6FirewallFilter(),
Expand Down Expand Up @@ -210,7 +211,7 @@ func Provider() *schema.Provider {

// Helpers
"routeros_wireguard_keys": ResourceWireguardKeys(),
"routeros_move_items": ResourceMoveItems(),
"routeros_move_items": ResourceMoveItems(),

// User Manager
"routeros_user_manager_advanced": ResourceUserManagerAdvanced(),
Expand Down
102 changes: 102 additions & 0 deletions routeros/resource_ipv6_dhcp_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package routeros

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

// ResourceIPv6DhcpClient https://help.mikrotik.com/docs/display/ROS/DHCP#DHCP-DHCPv6Client
func ResourceIPv6DhcpClient() *schema.Resource {
resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/ipv6/dhcp-client/"),
MetaId: PropId(Id),

"add-default-route ": {
Type: schema.TypeBool,
Optional: true,
Description: "Whether to add default IPv6 route after a client connects.",
Default: true,
},
KeyComment: PropCommentRw,
KeyDisabled: PropDisabledRw,
"duid": {
Type: schema.TypeString,
Computed: true,
Description: "Auto-generated DUID that is sent to the server." +
"DUID is generated using one of the MAC addresses available on the router.",
},
"interface": {
Type: schema.TypeString,
Required: true,
Description: "The interface on which the DHCPv6 client will be running.",
},
"pool_name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the IPv6 pool in which received IPv6 prefix will be added",
},
"pool_prefix_length": {
Type: schema.TypeInt,
Computed: false,
Required: true,
Description: "Prefix length parameter that will be set for IPv6 pool in which received IPv6 prefix is added." +
" Prefix length must be greater than the length of the received prefix, otherwise, prefix-length will be set to received prefix length + 8 bits.",
ValidateFunc: validation.IntBetween(0, 128),
},
"prefix": {
Type: schema.TypeString,
Computed: true,
Description: "Shows received IPv6 prefix from DHCPv6-PD server",
},
"prefix_hint ": {
Type: schema.TypeInt,
Optional: true,
Description: "Include a preferred prefix length.",
ValidateFunc: validation.IntBetween(0, 128),
},
"request": {
Type: schema.TypeList,
Computed: true,
Description: "To choose if the DHCPv6 request will ask for the address or the IPv6 prefix, or both.",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "Shows the status of DHCPv6 Client:" +
"stopped - dhcpv6 client is stopped" +
"searching - sending \"solicit\" and trying to get \"advertise\" Shows actual (resolved) gateway and interface that will be used for packet forwarding.requesting - sent \"request\" waiting for \"reply\"" +
"bound - received \"reply\". Prefix assigned. " +
"renewing - sent \"renew\", waiting for \"reply\" " +
"rebinding - sent \"rebind\", waiting for \"reply\" " +
"error - reply was not received in time or some other error occurred. " +
"stopping - sent \"release\"",
},
"script": {
Type: schema.TypeString,
Optional: true,
Description: "Run this script on the DHCP-client status change. Available variables:" +
"pd-valid - if the prefix is acquired by the client;" +
"pd-prefix - the prefix acquired by the client if any;" +
"na-valid - if the address is acquired by the client;" +
"na-address - the address acquired by the client if any." +
"options - array of received options (only ROSv7)",
},
"use_peer_dns": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Routing table this route belongs to.",
},
}
return &schema.Resource{
CreateContext: DefaultCreate(resSchema),
ReadContext: DefaultRead(resSchema),
UpdateContext: DefaultUpdate(resSchema),
DeleteContext: DefaultDelete(resSchema),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: resSchema,
}
}
47 changes: 47 additions & 0 deletions routeros/resource_ipv6_dhcp_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package routeros

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

const testIPv6DhcpClient = "routeros_ipv6_dhcp_client.client"

func TestAccIPv6DhcpClient_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("/ipv6/dhcp-client/client", "routeros_ipv6_dhcp_client"),
Steps: []resource.TestStep{
{
Config: testAccIPv6DhcpClientConfig(),
Check: resource.ComposeTestCheckFunc(
testResourcePrimaryInstanceId(testIPv6DhcpClient),
resource.TestCheckResourceAttr(testIPv6DhcpClient, "interface", "ether1"),
resource.TestCheckResourceAttr(testIPv6DhcpClient, "pool_name", "inet-provider-pool"),
),
},
},
})

})
}
}

func testAccIPv6DhcpClientConfig() string {
return providerConfig + `
resource "routeros_ipv6_dhcp_client" "client" {
interface = "ether1"
request = ["prefix"]
pool_name = "inet-provider-pool"
}
`
}

0 comments on commit 33e37b6

Please sign in to comment.