diff --git a/examples/resources/routeros_ipv6_dhcp_server_option/import.sh b/examples/resources/routeros_ipv6_dhcp_server_option/import.sh new file mode 100644 index 00000000..b62baf25 --- /dev/null +++ b/examples/resources/routeros_ipv6_dhcp_server_option/import.sh @@ -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" \ No newline at end of file diff --git a/examples/resources/routeros_ipv6_dhcp_server_option/resource.tf b/examples/resources/routeros_ipv6_dhcp_server_option/resource.tf new file mode 100644 index 00000000..d61c0df9 --- /dev/null +++ b/examples/resources/routeros_ipv6_dhcp_server_option/resource.tf @@ -0,0 +1,5 @@ +resource "routeros_ipv6_dhcp_server_option" "test" { + name = "domain-search" + code = 24 + value = "0x07'example'0x05'local'0x00" +} \ No newline at end of file diff --git a/routeros/resource_ipv6_dhcp_server_option.go b/routeros/resource_ipv6_dhcp_server_option.go new file mode 100644 index 00000000..baf4df49 --- /dev/null +++ b/routeros/resource_ipv6_dhcp_server_option.go @@ -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, + } +} diff --git a/routeros/resource_ipv6_dhcp_server_option_test.go b/routeros/resource_ipv6_dhcp_server_option_test.go new file mode 100644 index 00000000..fd8b2580 --- /dev/null +++ b/routeros/resource_ipv6_dhcp_server_option_test.go @@ -0,0 +1,49 @@ +package routeros + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +const testIpv6DhcpServerOption = "routeros_ipv6_dhcp_server_option.test" + +func TestAccIpv6DhcpServerOptionTest_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", "routeros_ipv6_dhcp_server_option"), + Steps: []resource.TestStep{ + { + Config: testAccIpv6DhcpServerOptionConfig(), + Check: resource.ComposeTestCheckFunc( + testResourcePrimaryInstanceId(testIpv6DhcpServerOption), + resource.TestCheckResourceAttr(testIpv6DhcpServerOption, "name", "domain-search"), + resource.TestCheckResourceAttr(testIpv6DhcpServerOption, "code", "24"), + resource.TestCheckResourceAttr(testIpv6DhcpServerOption, "value", "0x07'example'0x05'local'0x00"), + ), + }, + }, + }) + + }) + } +} + +func testAccIpv6DhcpServerOptionConfig() string { + return fmt.Sprintf(`%v + +resource "routeros_ipv6_dhcp_server_option" "test" { + name = "domain-search" + code = 24 + value = "0x07'example'0x05'local'0x00" +} +`, providerConfig) +}