diff --git a/CHANGELOG.md b/CHANGELOG.md index c4f8118a..65b1c245 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [1.15.0](https://github.com/terraform-routeros/terraform-provider-routeros/compare/v1.14.0...v1.15.0) (2023-09-20) + + +### Features + +* Add routeros_ip_dhcp_server_option and routeros_ip_dhcp_server_option_set ([#259](https://github.com/terraform-routeros/terraform-provider-routeros/issues/259)) ([3722afb](https://github.com/terraform-routeros/terraform-provider-routeros/commit/3722afb8574250a7a7e3211f2e40f3b4acfdc56f)) + ## [1.14.0](https://github.com/terraform-routeros/terraform-provider-routeros/compare/v1.13.3...v1.14.0) (2023-09-19) diff --git a/docs/resources/ip_dhcp_server_option.md b/docs/resources/ip_dhcp_server_option.md new file mode 100644 index 00000000..06af7c8f --- /dev/null +++ b/docs/resources/ip_dhcp_server_option.md @@ -0,0 +1,42 @@ +# routeros_ip_dhcp_server_option (Resource) +Creates a DHCP lease on the mikrotik device. + +## Example Usage +```terraform +resource "routeros_ip_dhcp_server_option" "jumbo_frame_opt" { + code = 77 + name = "jumbo-mtu-opt" + value = "0x2336" +} + +resource "routeros_ip_dhcp_server_option" "tftp_option" { + code = 66 + name = "tftpserver-66" + value = "s'10.10.10.22'" +} +``` + + +## Schema + +### Required + +- `code` (Number) The number of the DHCP option +- `name` (String) The name of the DHCP option +- `value` (String) The value with formatting using Mikrotik settings https://wiki.mikrotik.com/wiki/Manual:IP/DHCP_Server#DHCP_Options + +### Optional + + +### Read-Only + +- `id` (String) The ID of this resource. +- `raw_value` (String) The computed value of the option as an hex value + +## 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 [/ip/dhcp-server/option/get [print show-ids]] +terraform import routeros_ip_dhcp_server_option.tftp_option "*1" +``` diff --git a/docs/resources/ip_dhcp_server_option_set.md b/docs/resources/ip_dhcp_server_option_set.md new file mode 100644 index 00000000..f6f481b0 --- /dev/null +++ b/docs/resources/ip_dhcp_server_option_set.md @@ -0,0 +1,45 @@ +# routeros_ip_dhcp_server_option_set (Resource) +Creates a DHCP lease on the mikrotik device. + +## Example Usage +```terraform +resource "routeros_ip_dhcp_server_option" "jumbo_frame_opt" { + code = 77 + name = "jumbo-mtu-opt" + value = "0x2336" +} + +resource "routeros_ip_dhcp_server_option" "tftp_option" { + code = 66 + name = "tftpserver-66" + value = "s'10.10.10.22'" +} + +resource "routeros_ip_dhcp_server_option_set" "lan_option_set" { + name = "lan-option-set" + options = join(",", [routeros_ip_dhcp_server_option.jumbo_frame_opt.name, routeros_ip_dhcp_server_option.tftp_option.name]) +} +``` + + +## Schema + +### Required + +- `name` (String) The name of the DHCP option +- `options` (String) The comma sepparated list of options + +### Optional + + +### Read-Only + +- `id` (String) The ID of this resource. + +## 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 [/ip/dhcp-server/option/sets/get [print show-ids]] +terraform import routeros_ip_dhcp_server_option_set.lan_option_set "*1" +``` diff --git a/examples/resources/routeros_ip_dhcp_server_option/import.sh b/examples/resources/routeros_ip_dhcp_server_option/import.sh new file mode 100644 index 00000000..c7fe9d84 --- /dev/null +++ b/examples/resources/routeros_ip_dhcp_server_option/import.sh @@ -0,0 +1,3 @@ +#The ID can be found via API or the terminal +#The command for the terminal is -> :put [/ip/dhcp-server/option/get [print show-ids]] +terraform import routeros_ip_dhcp_server_option.tftp_option "*1" \ No newline at end of file diff --git a/examples/resources/routeros_ip_dhcp_server_option/resource.tf b/examples/resources/routeros_ip_dhcp_server_option/resource.tf new file mode 100644 index 00000000..59a82132 --- /dev/null +++ b/examples/resources/routeros_ip_dhcp_server_option/resource.tf @@ -0,0 +1,12 @@ + +resource "routeros_ip_dhcp_server_option" "jumbo_frame_opt" { + code = 77 + name = "jumbo-mtu-opt" + value = "0x2336" +} + +resource "routeros_ip_dhcp_server_option" "tftp_option" { + code = 66 + name = "tftpserver-66" + value = "s'10.10.10.22'" +} diff --git a/examples/resources/routeros_ip_dhcp_server_option_set/import.sh b/examples/resources/routeros_ip_dhcp_server_option_set/import.sh new file mode 100644 index 00000000..d58b99c0 --- /dev/null +++ b/examples/resources/routeros_ip_dhcp_server_option_set/import.sh @@ -0,0 +1,3 @@ +#The ID can be found via API or the terminal +#The command for the terminal is -> :put [/ip/dhcp-server/option/sets/get [print show-ids]] +terraform import routeros_ip_dhcp_server_option_set.lan_option_set "*1" \ No newline at end of file diff --git a/examples/resources/routeros_ip_dhcp_server_option_set/resource.tf b/examples/resources/routeros_ip_dhcp_server_option_set/resource.tf new file mode 100644 index 00000000..6c80b280 --- /dev/null +++ b/examples/resources/routeros_ip_dhcp_server_option_set/resource.tf @@ -0,0 +1,17 @@ + +resource "routeros_ip_dhcp_server_option" "jumbo_frame_opt" { + code = 77 + name = "jumbo-mtu-opt" + value = "0x2336" +} + +resource "routeros_ip_dhcp_server_option" "tftp_option" { + code = 66 + name = "tftpserver-66" + value = "s'10.10.10.22'" +} + +resource "routeros_ip_dhcp_server_option_set" "lan_option_set" { + name = "lan-option-set" + options = join(",", [routeros_ip_dhcp_server_option.jumbo_frame_opt.name, routeros_ip_dhcp_server_option.tftp_option.name]) +} \ No newline at end of file diff --git a/package.json b/package.json index 75d43efd..961985a4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "terraform-provider-routeros", - "version": "1.14.0", + "version": "1.15.0", "repository": { "type": "git", "url": "https://github.com/terraform-routeros/terraform-provider-routeros" diff --git a/routeros/provider.go b/routeros/provider.go index 12b0fd3f..357209c9 100644 --- a/routeros/provider.go +++ b/routeros/provider.go @@ -64,6 +64,7 @@ func Provider() *schema.Provider { ResourcesMap: map[string]*schema.Resource{ // IP objects +<<<<<<< HEAD "routeros_ip_dhcp_client": ResourceDhcpClient(), "routeros_ip_dhcp_server": ResourceDhcpServer(), "routeros_ip_dhcp_server_network": ResourceDhcpServerNetwork(), @@ -83,6 +84,28 @@ func Provider() *schema.Provider { "routeros_ipv6_firewall_addr_list": ResourceIPv6FirewallAddrList(), "routeros_ipv6_firewall_filter": ResourceIPv6FirewallFilter(), "routeros_ipv6_route": ResourceIPv6Route(), +======= + "routeros_ip_dhcp_client": ResourceDhcpClient(), + "routeros_ip_dhcp_server": ResourceDhcpServer(), + "routeros_ip_dhcp_server_network": ResourceDhcpServerNetwork(), + "routeros_ip_dhcp_server_lease": ResourceDhcpServerLease(), + "routeros_ip_dhcp_server_option": ResourceDhcpServerOption(), + "routeros_ip_dhcp_server_option_set": ResourceDhcpServerOptionSet(), + "routeros_ip_firewall_addr_list": ResourceIPFirewallAddrList(), + "routeros_ip_firewall_filter": ResourceIPFirewallFilter(), + "routeros_ip_firewall_mangle": ResourceIPFirewallMangle(), + "routeros_ip_firewall_nat": ResourceIPFirewallNat(), + "routeros_ip_address": ResourceIPAddress(), + "routeros_ip_pool": ResourceIPPool(), + "routeros_ip_route": ResourceIPRoute(), + "routeros_ip_dns": ResourceDns(), + "routeros_ip_dns_record": ResourceDnsRecord(), + "routeros_ip_service": ResourceIpService(), + "routeros_ipv6_address": ResourceIPv6Address(), + "routeros_ipv6_firewall_addr_list": ResourceIPv6FirewallAddrList(), + "routeros_ipv6_firewall_filter": ResourceIPv6FirewallFilter(), + "routeros_ipv6_route": ResourceIPv6Route(), +>>>>>>> main // Aliases for IP objects to retain compatibility between original and fork "routeros_dhcp_client": ResourceDhcpClient(), diff --git a/routeros/resource_ip_dhcp_server_option.go b/routeros/resource_ip_dhcp_server_option.go new file mode 100644 index 00000000..32902567 --- /dev/null +++ b/routeros/resource_ip_dhcp_server_option.go @@ -0,0 +1,60 @@ +package routeros + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" +) + +/* +[ + { + ".id": "*4", + "code": "66", + "name": "optionname", + "raw-value": "00002311", + "value": "0x00002311" + } +] +*/ + +// ResourceDhcpServerOption https://wiki.mikrotik.com/wiki/Manual:IP/DHCP_Server +func ResourceDhcpServerOption() *schema.Resource { + resSchema := map[string]*schema.Schema{ + MetaResourcePath: PropResourcePath("/ip/dhcp-server/option"), + MetaId: PropId(Id), + "code": { + Type: schema.TypeInt, + Required: true, + Description: "The number of the DHCP option", + ValidateFunc: validation.IntBetween(1, 254), + }, + "name": { + Type: schema.TypeString, + Required: true, + Description: "The name of the DHCP option", + }, + "raw_value": { + Type: schema.TypeString, + Computed: true, + Description: "The computed value of the option as an hex value", + }, + "value": { + Type: schema.TypeString, + Required: true, + Description: "The value with formatting using Mikrotik settings https://wiki.mikrotik.com/wiki/Manual:IP/DHCP_Server#DHCP_Options", + }, + } + return &schema.Resource{ + Description: "Creates a DHCP lease on the mikrotik device.", + + CreateContext: DefaultCreate(resSchema), + ReadContext: DefaultRead(resSchema), + UpdateContext: DefaultUpdate(resSchema), + DeleteContext: DefaultDelete(resSchema), + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + + Schema: resSchema, + } +} diff --git a/routeros/resource_ip_dhcp_server_option_sets.go b/routeros/resource_ip_dhcp_server_option_sets.go new file mode 100644 index 00000000..724b0cfa --- /dev/null +++ b/routeros/resource_ip_dhcp_server_option_sets.go @@ -0,0 +1,46 @@ +package routeros + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +/* +[ + { + ".id": "*2", + "name": "netboot", + "options": "tftpserver-66,unifi,mtu-jumbo" + } +] +*/ + +// ResourceDhcpServerOption https://wiki.mikrotik.com/wiki/Manual:IP/DHCP_Server +func ResourceDhcpServerOptionSet() *schema.Resource { + resSchema := map[string]*schema.Schema{ + MetaResourcePath: PropResourcePath("/ip/dhcp-server/option/sets"), + MetaId: PropId(Id), + "name": { + Type: schema.TypeString, + Required: true, + Description: "The name of the DHCP option", + }, + "options": { + Type: schema.TypeString, + Required: true, + Description: "The comma sepparated list of options", + }, + } + return &schema.Resource{ + Description: "Creates a DHCP lease on the mikrotik device.", + + CreateContext: DefaultCreate(resSchema), + ReadContext: DefaultRead(resSchema), + UpdateContext: DefaultUpdate(resSchema), + DeleteContext: DefaultDelete(resSchema), + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + + Schema: resSchema, + } +} diff --git a/routeros/resource_ip_dhcp_server_option_sets_test.go b/routeros/resource_ip_dhcp_server_option_sets_test.go new file mode 100644 index 00000000..db61ed87 --- /dev/null +++ b/routeros/resource_ip_dhcp_server_option_sets_test.go @@ -0,0 +1,73 @@ +package routeros + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" +) + +const testIpDhcpServerOptionSet = "routeros_ip_dhcp_server_option_set.test_option_set" + +func TestAccIpDhcpServerNetworkOptionSet_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-server/option/sets", "routeros_ip_dhcp_server_option_set"), + Steps: []resource.TestStep{ + { + Config: testAccIpDhcpServerOptionSetConfig(), + Check: resource.ComposeTestCheckFunc( + testAccCheckIpDhcpServerOptionSetExists(testIpDhcpServerOptionSet), + resource.TestCheckResourceAttr(testIpDhcpServerOptionSet, "name", "test-opt-set"), + resource.TestCheckResourceAttr(testIpDhcpServerOptionSet, "options", "test-opt1,test-opt2"), + ), + }, + }, + }) + + }) + } +} + +func testAccCheckIpDhcpServerOptionSetExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("not found: %s", name) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("no id is set") + } + + return nil + } +} + +func testAccIpDhcpServerOptionSetConfig() string { + return providerConfig + ` +resource "routeros_ip_dhcp_server_option" "test_option_1" { + code = 77 + name = "test-opt1" + value = "s'10.10.10.22'" + } + +resource "routeros_ip_dhcp_server_option" "test_option_2" { + code = 90 + name = "test-opt2" + value = "s'10.10.10.22'" + } + +resource "routeros_ip_dhcp_server_option_set" "test_option_set" { + name = "test-opt-set" + options = join(",", [routeros_ip_dhcp_server_option.test_option_1.name, routeros_ip_dhcp_server_option.test_option_2.name]) + } +` +} diff --git a/routeros/resource_ip_dhcp_server_option_test.go b/routeros/resource_ip_dhcp_server_option_test.go new file mode 100644 index 00000000..09b495c9 --- /dev/null +++ b/routeros/resource_ip_dhcp_server_option_test.go @@ -0,0 +1,65 @@ +package routeros + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" +) + +const testIpDhcpServerOption = "routeros_ip_dhcp_server_option.test_option" + +func TestAccIpDhcpServerNetworkOption_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-server/option", "routeros_ip_dhcp_server_option"), + Steps: []resource.TestStep{ + { + Config: testAccIpDhcpServerOptionConfig(), + Check: resource.ComposeTestCheckFunc( + testAccCheckIpDhcpServerOptionExists(testIpDhcpServerOption), + resource.TestCheckResourceAttr(testIpDhcpServerOption, "code", "77"), + resource.TestCheckResourceAttr(testIpDhcpServerOption, "name", "test-opt"), + resource.TestCheckResourceAttr(testIpDhcpServerOption, "value", "s'10.10.10.22'"), + + resource.TestCheckResourceAttr(testIpDhcpServerOption, "raw_value", "31302e31302e31302e3232"), + ), + }, + }, + }) + + }) + } +} + +func testAccCheckIpDhcpServerOptionExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("not found: %s", name) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("no id is set") + } + + return nil + } +} + +func testAccIpDhcpServerOptionConfig() string { + return providerConfig + ` +resource "routeros_ip_dhcp_server_option" "test_option" { + code = 77 + name = "test-opt" + value = "s'10.10.10.22'" + } +` +}