Skip to content

Commit

Permalink
implement routeros_ip_dhcp_server_option_set
Browse files Browse the repository at this point in the history
  • Loading branch information
jlpedrosa committed Sep 20, 2023
1 parent 33abd0c commit f7081fc
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 22 deletions.
39 changes: 20 additions & 19 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,26 @@ func Provider() *schema.Provider {
ResourcesMap: map[string]*schema.Resource{

// IP objects
"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_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(),
"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(),

// Aliases for IP objects to retain compatibility between original and fork
"routeros_dhcp_client": ResourceDhcpClient(),
Expand Down
8 changes: 5 additions & 3 deletions routeros/resource_ip_dhcp_server_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package routeros

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

/*
Expand All @@ -22,9 +23,10 @@ func ResourceDhcpServerOption() *schema.Resource {
MetaResourcePath: PropResourcePath("/ip/dhcp-server/option"),
MetaId: PropId(Id),
"code": {
Type: schema.TypeString,
Required: true,
Description: "The number of the DHCP option",
Type: schema.TypeInt,
Required: true,
Description: "The number of the DHCP option",
ValidateFunc: validation.IntBetween(1, 254),
},
"name": {
Type: schema.TypeString,
Expand Down
46 changes: 46 additions & 0 deletions routeros/resource_ip_dhcp_server_option_sets.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
73 changes: 73 additions & 0 deletions routeros/resource_ip_dhcp_server_option_sets_test.go
Original file line number Diff line number Diff line change
@@ -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])
}
`
}

0 comments on commit f7081fc

Please sign in to comment.