From 92778ff184124ce8d2d7dc0dfc266b29b6f306ef Mon Sep 17 00:00:00 2001 From: Vaerh Date: Tue, 24 Sep 2024 13:39:16 +0300 Subject: [PATCH] feat(hotspot): Add new resource `routeros_ip_hotspot_walled_garden_ip` --- .../import.sh | 3 + .../resource.tf | 9 ++ routeros/provider.go | 1 + .../resource_ip_hotspot_walled_garden_ip.go | 100 ++++++++++++++++++ ...source_ip_hotspot_walled_garden_ip_test.go | 57 ++++++++++ 5 files changed, 170 insertions(+) create mode 100755 examples/resources/routeros_ip_hotspot_walled_garden_ip/import.sh create mode 100755 examples/resources/routeros_ip_hotspot_walled_garden_ip/resource.tf create mode 100644 routeros/resource_ip_hotspot_walled_garden_ip.go create mode 100644 routeros/resource_ip_hotspot_walled_garden_ip_test.go diff --git a/examples/resources/routeros_ip_hotspot_walled_garden_ip/import.sh b/examples/resources/routeros_ip_hotspot_walled_garden_ip/import.sh new file mode 100755 index 00000000..2a1481ff --- /dev/null +++ b/examples/resources/routeros_ip_hotspot_walled_garden_ip/import.sh @@ -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 \ No newline at end of file diff --git a/examples/resources/routeros_ip_hotspot_walled_garden_ip/resource.tf b/examples/resources/routeros_ip_hotspot_walled_garden_ip/resource.tf new file mode 100755 index 00000000..f759becf --- /dev/null +++ b/examples/resources/routeros_ip_hotspot_walled_garden_ip/resource.tf @@ -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" +} diff --git a/routeros/provider.go b/routeros/provider.go index 44ba190c..de90aa3b 100644 --- a/routeros/provider.go +++ b/routeros/provider.go @@ -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(), diff --git a/routeros/resource_ip_hotspot_walled_garden_ip.go b/routeros/resource_ip_hotspot_walled_garden_ip.go new file mode 100644 index 00000000..e450a4a5 --- /dev/null +++ b/routeros/resource_ip_hotspot_walled_garden_ip.go @@ -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, + } +} diff --git a/routeros/resource_ip_hotspot_walled_garden_ip_test.go b/routeros/resource_ip_hotspot_walled_garden_ip_test.go new file mode 100644 index 00000000..534d0c92 --- /dev/null +++ b/routeros/resource_ip_hotspot_walled_garden_ip_test.go @@ -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) +}