Skip to content

Commit

Permalink
feat(hotspot): Add new resource routeros_ip_hotspot_walled_garden_ip
Browse files Browse the repository at this point in the history
  • Loading branch information
vaerh committed Sep 24, 2024
1 parent bfd85d5 commit 92778ff
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 0 deletions.
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 [/ip/hotspot/walled-garden/ip get [print show-ids]]
terraform import routeros_ip_hotspot_walled_garden_ip.test *3
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "routeros_ip_hotspot_walled_garden_ip" "test" {
action = "reject"
dst_address = "!0.0.0.0"
dst_address_list = "dlist"
dst_port = "0-65535"
protocol = "tcp"
src_address = "0.0.0.0"
src_address_list = "slist"
}
1 change: 1 addition & 0 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func Provider() *schema.Provider {
"routeros_ip_firewall_nat": ResourceIPFirewallNat(),
"routeros_ip_firewall_raw": ResourceIPFirewallRaw(),
"routeros_ip_hotspot_walled_garden": ResourceIpHotspotWalledGarden(),
"routeros_ip_hotspot_walled_garden_ip": ResourceIpHotspotWalledGardenIp(),
"routeros_ip_neighbor_discovery_settings": ResourceIpNeighborDiscoverySettings(),
"routeros_ip_pool": ResourceIPPool(),
"routeros_ip_route": ResourceIPRoute(),
Expand Down
100 changes: 100 additions & 0 deletions routeros/resource_ip_hotspot_walled_garden_ip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package routeros

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

/*
{
".id": "*4",
"action": "reject",
"disabled": "false",
"dst-address": "!0.0.0.0",
"dst-address-list": "bbb",
"dst-port": "0-65535",
"invalid": "false",
"protocol": "tcp",
"server": "server1",
"src-address": "0.0.0.0",
"src-address-list": "aaa"
}
*/

// https://wiki.mikrotik.com/wiki/Manual:IP/Hotspot/Walled_Garden#IP_Walled_Garden
func ResourceIpHotspotWalledGardenIp() *schema.Resource {
resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/ip/hotspot/walled-garden/ip"),
MetaId: PropId(Id),

"action": {
Type: schema.TypeString,
Optional: true,
Description: "Action to perform, when packet matches the rule allow - allow access to the web-page without " +
"authorization deny - the authorization is required to access the web-page reject - the authorization " +
"is required to access the resource, ICMP reject message will be sent to client, when packet will match " +
"the rule.",
ValidateFunc: validation.StringInSlice([]string{"allow", "deny", "reject"}, false),
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
KeyComment: PropCommentRw,
KeyDisabled: PropDisabledRw,
"dst_address": {
Type: schema.TypeString,
Optional: true,
Description: "Destination IP address, IP address of the WEB-server. Ignored if dst-host is already specified.",
ConflictsWith: []string{"dst_host"},
},
"dst_address_list": {
Type: schema.TypeString,
Optional: true,
Description: "Destination IP address list. Ignored if dst-host is already specified.",
},
"dst_host": {
Type: schema.TypeString,
Optional: true,
Description: "Domain name of the destination web-server. When this parameter is specified dynamic entry " +
"is added to Walled Garden.",
ConflictsWith: []string{"dst_address"},
},
"dst_port": {
Type: schema.TypeString,
Optional: true,
Description: "TCP port number, client sends request to.",
},
KeyInvalid: PropInvalidRo,
"protocol": {
Type: schema.TypeString,
Optional: true,
Description: "IP protocol.",
},
"server": {
Type: schema.TypeString,
Optional: true,
Description: "Name of the HotSpot server, rule is applied to.",
},
"src_address": {
Type: schema.TypeString,
Optional: true,
Description: "Source address of the user, usually IP address of the HotSpot client.",
},
"src_address_list": {
Type: schema.TypeString,
Optional: true,
Description: "Source IP address list.",
},
}

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

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

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

import (
"fmt"
"testing"

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

const testIpHotspotWalledGardenIp = "routeros_ip_hotspot_walled_garden_ip.test"

func TestAccIpHotspotWalledGardenIpTest_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("/ip/hotspot/walled-garden/ip", "routeros_ip_hotspot_walled_garden_ip"),
Steps: []resource.TestStep{
{
Config: testAccIpHotspotWalledGardenIpConfig(),
Check: resource.ComposeTestCheckFunc(
testResourcePrimaryInstanceId(testIpHotspotWalledGardenIp),
resource.TestCheckResourceAttr(testIpHotspotWalledGardenIp, "action", "reject"),
resource.TestCheckResourceAttr(testIpHotspotWalledGardenIp, "dst_address", "!0.0.0.0"),
resource.TestCheckResourceAttr(testIpHotspotWalledGardenIp, "dst_address_list", "dlist"),
resource.TestCheckResourceAttr(testIpHotspotWalledGardenIp, "dst_port", "0-65535"),
resource.TestCheckResourceAttr(testIpHotspotWalledGardenIp, "protocol", "tcp"),
resource.TestCheckResourceAttr(testIpHotspotWalledGardenIp, "src_address", "0.0.0.0"),
resource.TestCheckResourceAttr(testIpHotspotWalledGardenIp, "src_address_list", "slist"),
),
},
},
})

})
}
}

func testAccIpHotspotWalledGardenIpConfig() string {
return fmt.Sprintf(`%v
resource "routeros_ip_hotspot_walled_garden_ip" "test" {
action = "reject"
dst_address = "!0.0.0.0"
dst_address_list = "dlist"
dst_port = "0-65535"
protocol = "tcp"
src_address = "0.0.0.0"
src_address_list = "slist"
}
`, providerConfig)
}

0 comments on commit 92778ff

Please sign in to comment.