Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ipv6 dhcp client resource #354

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ VERSION=$(shell git describe --tags --abbrev=0)
all: docs compile checksum clean

test:
/usr/bin/go test -timeout 30s github.com/terraform-routeros/terraform-provider-routeros
go test -timeout 30s github.com/terraform-routeros/terraform-provider-routeros
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can happily change this. I don't have go installed on the same path and was giving me errors. The rest of the file is using just "go" instead of "/usr/bin/go"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


docs:
go generate
Expand Down
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
48 changes: 48 additions & 0 deletions docs/resources/ipv6_dhcp_client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# routeros_ipv6_dhcp_client (Resource)


## Example Usage
```terraform
resource "routeros_ipv6_dhcp_client" "inet_provider" {
pool_name = "pub-add-pool"
interface = "ether1"
add-default-route = true
pool_prefix_length = 64
request = ["prefix"]
disabled = false
}
```

<!-- 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"

## Import
Import is supported using the following syntax:
```shell
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/ipv6/dhcp-client/ get [print show-ids]]
terraform import routeros_ipv6_dhcp_client.inet_provider "*1"
```
3 changes: 3 additions & 0 deletions examples/resources/routeros_ipv6_dhcp_client/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 [/ipv6/dhcp-client/ get [print show-ids]]
terraform import routeros_ipv6_dhcp_client.inet_provider "*1"
17 changes: 17 additions & 0 deletions examples/resources/routeros_ipv6_dhcp_client/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
resource "routeros_ipv6_dhcp_client" "inet_provider" {
pool_name = "pub-add-pool"
interface = "ether1"
add-default-route = true
pool_prefix_length = 64
request = ["prefix"]
disabled = false
}

resource "routeros_ipv6_dhcp_client" "client" {
pool_name = "pub-add-pool"
interface = "ether1"
add-default-route = true
pool_prefix_length = "64"
request = ["prefix"]
interface = "ether1"
}
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
129 changes: 129 additions & 0 deletions routeros/resource_ipv6_dhcp_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package routeros

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

/*
[
{
".id": "*1",
"add-default-route": "false",
"dhcp-options": "",
"dhcp-server-v6": "fe80::",
"disabled": "false",
"duid": "0x000003434343443",
"interface": "if-name",
"invalid": "false",
"pool-name": "blacknight-pub-addr",
"pool-prefix-length": "64",
"prefix": "2a01:----:/56, 6d16h56m8s",
"prefix-hint": "::/0",
"request": "prefix",
"status": "bound",
"use-peer-dns": "true"
}
]
*/

// 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.TypeString,
Optional: true,
Description: "Include a preferred prefix length.",
ValidateFunc: validation.IsIPv6Address,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"request": {
Type: schema.TypeList,
Required: true,
Description: "To choose if the DHCPv6 request will ask for the address or the IPv6 prefix, or both.",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{"info", "address", "prefix"}, false),
},
},
"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,
}
}
48 changes: 48 additions & 0 deletions routeros/resource_ipv6_dhcp_client_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 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", "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"),
resource.TestCheckResourceAttr(testIPv6DhcpClient, "request.0", "prefix"),
),
},
},
})
})
}
}

func testAccIPv6DhcpClientConfig() string {
return providerConfig + `

resource "routeros_ipv6_dhcp_client" "client" {
request = ["prefix"]
pool_name = "inet-provider-pool"
pool_prefix_length = "64"
interface = "ether1"
}

`
}
Loading