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

Issue/438 #583

Merged
merged 6 commits into from
Oct 11, 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
5 changes: 5 additions & 0 deletions examples/resources/routeros_ipv6_dhcp_server/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/ipv6/dhcp/server get [print show-ids]]
terraform import routeros_ipv6_dhcp_server.test *3
#Or you can import a resource using one of its attributes
terraform import routeros_ipv6_dhcp_server.test "name=test-dhcpv6"
13 changes: 13 additions & 0 deletions examples/resources/routeros_ipv6_dhcp_server/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "routeros_ipv6_pool" "pool-0" {
name = "test-pool-0"
prefix = "2001:db8:40::/48"
prefix_length = 64
}

resource "routeros_ipv6_dhcp_server" "test" {
address_pool = routeros_ipv6_pool.pool-0.name
interface = "bridge"
lease_time = "1m"
name = "test-dhcpv6"
preference = 128
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/ipv6/dhcp/server/option get [print show-ids]]
terraform import routeros_ipv6_dhcp_server_option.test *3
#Or you can import a resource using one of its attributes
terraform import routeros_ipv6_dhcp_server_option.test "name=domain-search"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "routeros_ipv6_dhcp_server_option" "test" {
name = "domain-search"
code = 24
value = "0x07'example'0x05'local'0x00"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/ipv6/dhcp/server/option/sets get [print show-ids]]
terraform import routeros_ipv6_dhcp_server_option_sets.test *3
#Or you can import a resource using one of its attributes
terraform import routeros_ipv6_dhcp_server_option_sets.test "name=test-set"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "routeros_ipv6_dhcp_server_option" "domain-search" {
name = "domain-search"
code = 24
value = "0x07'example'0x05'local'0x00"
}

resource "routeros_ipv6_dhcp_server_option_sets" "test" {
name = "test-set"
options = [routeros_ipv6_dhcp_server_option.domain-search.name]
}
5 changes: 5 additions & 0 deletions examples/resources/routeros_ipv6_pool/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/ipv6/pool get [print show-ids]]
terraform import routeros_ipv6_pool.test *3
#Or you can import a resource using one of its attributes
terraform import routeros_ipv6_pool.test "name=test-pool"
5 changes: 5 additions & 0 deletions examples/resources/routeros_ipv6_pool/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "routeros_ipv6_pool" "test" {
name = "test-pool"
prefix = "2001:db8:12::/48"
prefix_length = 64
}
4 changes: 4 additions & 0 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,13 @@ func Provider() *schema.Provider {
"routeros_ipv6_address": ResourceIPv6Address(),
"routeros_ipv6_dhcp_client": ResourceIPv6DhcpClient(),
"routeros_ipv6_dhcp_client_option": ResourceIPv6DhcpClientOption(),
"routeros_ipv6_dhcp_server": ResourceIpv6DhcpServer(),
"routeros_ipv6_dhcp_server_option": ResourceIpv6DhcpServerOption(),
"routeros_ipv6_dhcp_server_option_sets": ResourceIpv6DhcpServerOptionSets(),
"routeros_ipv6_firewall_addr_list": ResourceIPv6FirewallAddrList(),
"routeros_ipv6_firewall_filter": ResourceIPv6FirewallFilter(),
"routeros_ipv6_neighbor_discovery": ResourceIPv6NeighborDiscovery(),
"routeros_ipv6_pool": ResourceIpv6Pool(),
"routeros_ipv6_route": ResourceIPv6Route(),

// Aliases for IP objects to retain compatibility between original and fork
Expand Down
135 changes: 135 additions & 0 deletions routeros/resource_ipv6_dhcp_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package routeros

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

/*
{
".id": "*1",
"address-pool": "ULA",
"comment": "https://ula.ungleich.ch/random/",
"dhcp-option": "dns",
"disabled": "false",
"duid": "0x00030001d401c330e280",
"dynamic": "false",
"interface": "span-bridge",
"invalid": "false",
"lease-time": "10m",
"name": "server1",
"preference": "255",
"rapid-commit": "true",
"route-distance": "1",
"use-radius": "false"
}
*/

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

"address_pool": {
Type: schema.TypeString,
Required: true,
Description: "IPv6 pool, from which to take IPv6 prefix for the clients.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"allow_dual_stack_queue": {
Type: schema.TypeBool,
Optional: true,
Description: "Creates a single simple queue entry for both IPv4 and IPv6 addresses, and uses the MAC address " +
"and DUID for identification. Requires IPv6 DHCP Server to have this option enabled as well to work properly.",
},
"binding_script": {
Type: schema.TypeString,
Optional: true,
Description: "A script that will be executed after binding is assigned or de-assigned. Internal `global` " +
"variables that can be used in the script:\n - bindingBound - set to `1` if bound, otherwise set to `0`\n" +
" - bindingServerName - dhcp server name\n - bindingDUID - DUID\n - bindingAddress - active " +
"address\n - bindingPrefix - active prefix.",
},
KeyComment: PropCommentRw,
"dhcp_option": {
Type: schema.TypeSet,
Optional: true,
Description: "Add additional DHCP options from option list.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
KeyDisabled: PropDisabledRw,
"duid": {
Type: schema.TypeString,
Computed: true,
Description: "DUID value.",
},
KeyDynamic: PropDynamicRo,
"insert_queue_before": {
Type: schema.TypeString,
Optional: true,
Description: "Specify where to place dynamic simple queue entries for static DCHP leases with a " +
"rate-limit parameter set.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
ValidateFunc: validation.StringInSlice([]string{"bottom", "first"}, false),
},
"interface": {
Type: schema.TypeString,
Required: true,
Description: "The interface on which server will be running.",
},
KeyInvalid: PropInvalidRo,
"lease_time": {
Type: schema.TypeString,
Optional: true,
Description: "The time that a client may use the assigned address. The client will try to renew this address " +
"after half of this time and will request a new address after the time limit expires.",
DiffSuppressFunc: TimeEquall,
},
KeyName: PropName("Reference name."),
"parent_queue": {
Type: schema.TypeString,
Optional: true,
Description: "A dynamically created queue for this lease will be configured as a child queue of the specified parent queue.",
},
"preference": {
Type: schema.TypeInt,
Optional: true,
Description: "",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"rapid_commit": {
Type: schema.TypeBool,
Optional: true,
Description: "",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"route_distance": {
Type: schema.TypeInt,
Optional: true,
Description: "Distance of the route.",
ValidateFunc: validation.IntBetween(1, 255),
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"use_radius": {
Type: schema.TypeBool,
Optional: true,
Description: "Whether to use RADIUS server.",
},
}

return &schema.Resource{
CreateContext: DefaultCreate(resSchema),
ReadContext: DefaultRead(resSchema),
UpdateContext: DefaultUpdate(resSchema),
DeleteContext: DefaultDelete(resSchema),

Importer: &schema.ResourceImporter{
StateContext: ImportStateCustomContext(resSchema),
},

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

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

/*
{
".id": "*80000004",
"code": "24",
"name": "domain-search",
"raw-value": "076578616d706c65056c6f63616c00",
"value": "0x07'example'0x05'local'0x00"
}
*/

// https://help.mikrotik.com/docs/display/ROS/DHCP#DHCP-DHCPOptions.1
// https://www.ipamworldwide.com/ipam/isc-dhcpv6-options.html
// https://jjjordan.github.io/dhcp119/
func ResourceIpv6DhcpServerOption() *schema.Resource {
resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/ipv6/dhcp-server/option"),
MetaId: PropId(Id),

"code": {
Type: schema.TypeInt,
Required: true,
Description: "Dhcp option [code](https://www.ipamworldwide.com/ipam/isc-dhcpv6-options.html).",
ValidateFunc: validation.IntBetween(1, 254),
},
KeyComment: PropCommentRw,
KeyName: PropName("Descriptive name of the option."),
"value": {
Type: schema.TypeString,
Optional: true,
Description: "Parameter's value. Available data types for options are:\n" +
" - `'test'` -> ASCII to Hex 0x74657374\n" +
" - `'10.10.10.10'` -> Unicode IP to Hex 0x0a0a0a0a\n" +
" - `s'10.10.10.10'` -> ASCII to Hex 0x31302e31302e31302e3130\n" +
" - `s'160'` -> ASCII to Hex 0x313630\n" +
" - `'10'` -> Decimal to Hex 0x0a\n" +
" - `0x0a0a` -> No conversion\n" +
" - `$(VARIABLE)` -> hardcoded values\n\n" +
"RouterOS has predefined variables that can be used:\n" +
" - `HOSTNAME` - client hostname\n" +
" - `RADIUS_MT_STR1` - from radius MT attr nr. `24`\n" +
" - `RADIUS_MT_STR2` - from radius MT attr nr. `25`\n" +
" - `REMOTE_ID` - agent remote-id\n" +
" - `NETWORK_GATEWAY - the first gateway from `/ip dhcp-server network`, note that this option " +
"won't work if used from lease.\n\nNow it is also possible to combine data types into one, for example: " +
"`0x01'vards'$(HOSTNAME)`For example if HOSTNAME is 'kvm', then raw value will be 0x0176617264736b766d.",
},
"raw_value": {
Type: schema.TypeString,
Computed: true,
Description: "Read-only field which shows raw DHCP option value (the format actually sent out).",
},
}

return &schema.Resource{
CreateContext: DefaultCreate(resSchema),
ReadContext: DefaultRead(resSchema),
UpdateContext: DefaultUpdate(resSchema),
DeleteContext: DefaultDelete(resSchema),

Importer: &schema.ResourceImporter{
StateContext: ImportStateCustomContext(resSchema),
},

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

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

/*
{
".id": "*1",
"name": "set1",
"options": "dns,option1"
}
*/

// https://help.mikrotik.com/docs/display/ROS/
func ResourceIpv6DhcpServerOptionSets() *schema.Resource {
resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/ipv6/dhcp-server/option/sets"),
MetaId: PropId(Id),

KeyComment: PropCommentRw,
KeyName: PropName("The name of the DHCPv6 option."),
"options": {
Type: schema.TypeSet,
Optional: true,
Description: "The list of options.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
}

return &schema.Resource{
CreateContext: DefaultCreate(resSchema),
ReadContext: DefaultRead(resSchema),
UpdateContext: DefaultUpdate(resSchema),
DeleteContext: DefaultDelete(resSchema),

Importer: &schema.ResourceImporter{
StateContext: ImportStateCustomContext(resSchema),
},

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

import (
"fmt"
"testing"

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

const testIpv6DhcpServerOptionSets = "routeros_ipv6_dhcp_server_option_sets.test"

func TestAccIpv6DhcpServerOptionSetsTest_basic(t *testing.T) {
t.Parallel()
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-server/option/sets", "routeros_ipv6_dhcp_server_option_sets"),
Steps: []resource.TestStep{
{
Config: testAccIpv6DhcpServerOptionSetsConfig(),
Check: resource.ComposeTestCheckFunc(
testResourcePrimaryInstanceId(testIpv6DhcpServerOptionSets),
resource.TestCheckResourceAttr(testIpv6DhcpServerOptionSets, "name", "test-set"),
resource.TestCheckResourceAttr(testIpv6DhcpServerOptionSets, "options.#", "1"),
resource.TestCheckResourceAttr(testIpv6DhcpServerOptionSets, "options.0", "domain-search-o24"),
),
},
},
})

})
}
}

func testAccIpv6DhcpServerOptionSetsConfig() string {
return fmt.Sprintf(`%v

resource "routeros_ipv6_dhcp_server_option" "domain-search" {
name = "domain-search-o24"
code = 24
value = "0x07'example'0x05'local'0x00"
}

resource "routeros_ipv6_dhcp_server_option_sets" "test" {
name = "test-set"
options = [routeros_ipv6_dhcp_server_option.domain-search.name]
}
`, providerConfig)
}
Loading