Skip to content

Commit

Permalink
Merge pull request #558 from terraform-routeros/adlist
Browse files Browse the repository at this point in the history
Closes #554 #555
  • Loading branch information
vaerh authored Sep 24, 2024
2 parents 93a2d51 + 22d333c commit fb95d72
Show file tree
Hide file tree
Showing 44 changed files with 2,098 additions and 41 deletions.
1 change: 1 addition & 0 deletions .github/scripts/setup_routeros.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
"/ip/pool/add name=dhcp ranges=192.168.88.100-192.168.88.200",
"/interface/wireguard/add name=wg1",
"/interface/list/add name=list",
"/interface/print",
}
)

Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ pkg/*
issues/*
terraform-provider-routeros*
terraform.tfstate*
node_modules
node_modules
tools/boilerplate/examples/**
tools/boilerplate/routeros/**
tools/boilerplate/*.csv
18 changes: 15 additions & 3 deletions docs/resources/dns_adlist.md → docs/resources/ip_dns_adlist.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# routeros_dns_adlist (Resource)

# routeros_ip_dns_adlist (Resource)


## Example Usage
```terraform
resource "routeros_ip_dns_adlist" "test" {
url = "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
ssl_verify = false
}
```

<!-- schema generated by tfplugindocs -->
## Schema
Expand All @@ -17,4 +23,10 @@

- `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/dns/adlist get [print show-ids]]
terraform import routeros_ip_dns_adlist.test "*0"
```
2 changes: 1 addition & 1 deletion examples/resources/routeros_ip_dns_adlist/resource.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resource "routeros_dns_adlist" "test" {
resource "routeros_ip_dns_adlist" "test" {
url = "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
ssl_verify = false
}
3 changes: 3 additions & 0 deletions examples/resources/routeros_ip_hotspot/import.sh
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 get [print show-ids]]
terraform import routeros_ip_hotspot.test *3
4 changes: 4 additions & 0 deletions examples/resources/routeros_ip_hotspot/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "routeros_ip_hotspot" "test" {
name = "server-1"
interface = "ether2"
}
3 changes: 3 additions & 0 deletions examples/resources/routeros_ip_hotspot_ip_binding/import.sh
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/ip-binding get [print show-ids]]
terraform import routeros_ip_hotspot_ip_binding.test *3
6 changes: 6 additions & 0 deletions examples/resources/routeros_ip_hotspot_ip_binding/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resource "routeros_ip_hotspot_ip_binding" "test" {
address = "0.0.0.1"
comment = "comment"
mac_address = "00:00:00:00:01:10"
to_address = "0.0.0.2"
}
3 changes: 3 additions & 0 deletions examples/resources/routeros_ip_hotspot_profile/import.sh
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/profile get [print show-ids]]
terraform import routeros_ip_hotspot_profile.test *3
5 changes: 5 additions & 0 deletions examples/resources/routeros_ip_hotspot_profile/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "routeros_ip_hotspot_profile" "test" {
name = "hsprof-1"
login_by = ["mac", "https", "trial"]
use_radius = true
}
3 changes: 3 additions & 0 deletions examples/resources/routeros_ip_hotspot_service_port/import.sh
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/service-port get [print show-ids]]
terraform import routeros_ip_hotspot_service_port.test *1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "routeros_ip_hotspot_service_port" "test" {
name = "ftp"
disabled = true
}
3 changes: 3 additions & 0 deletions examples/resources/routeros_ip_hotspot_user/import.sh
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/user get [print show-ids]]
terraform import routeros_ip_hotspot_user.test *3
3 changes: 3 additions & 0 deletions examples/resources/routeros_ip_hotspot_user/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "routeros_ip_hotspot_user" "test" {
name = "user-1"
}
3 changes: 3 additions & 0 deletions examples/resources/routeros_ip_hotspot_user_profile/import.sh
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/user/profile get [print show-ids]]
terraform import routeros_ip_hotspot_user_profile.test *3
12 changes: 12 additions & 0 deletions examples/resources/routeros_ip_hotspot_user_profile/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "routeros_ip_hotspot_user_profile" "test" {
add_mac_cookie = true
address_list = "list-1"
idle_timeout = "none"
keepalive_timeout = "2m"
mac_cookie_timeout = "3d"
name = "new-profile"
shared_users = 3
status_autorefresh = "2m"
transparent_proxy = true
advertise = true
}
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 get [print show-ids]]
terraform import routeros_ip_hotspot_walled_garden.test *3
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "routeros_ip_hotspot_walled_garden" "test" {
action = "deny"
dst_host = "1.2.3.4"
dst_port = "!443"
}
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 examples/resources/routeros_tool_sniffer/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import routeros_tool_sniffer.test .
9 changes: 9 additions & 0 deletions examples/resources/routeros_tool_sniffer/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "routeros_tool_sniffer" "test" {
streaming_enabled = true
streaming_server = "192.168.88.5:37008"
filter_stream = true

filter_interface = ["ether2"]
filter_direction = "rx"
filter_operator_between_entries = "and"
}
19 changes: 14 additions & 5 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func Provider() *schema.Provider {
ResourcesMap: map[string]*schema.Resource{

// IP objects
"routeros_ip_address": ResourceIPAddress(),
"routeros_ip_dhcp_client": ResourceDhcpClient(),
"routeros_ip_dhcp_client_option": ResourceDhcpClientOption(),
"routeros_ip_dhcp_relay": ResourceDhcpRelay(),
Expand All @@ -89,19 +90,27 @@ func Provider() *schema.Provider {
"routeros_ip_dhcp_server_lease": ResourceDhcpServerLease(),
"routeros_ip_dhcp_server_option": ResourceDhcpServerOption(),
"routeros_ip_dhcp_server_option_set": ResourceDhcpServerOptionSet(),
"routeros_ip_dns": ResourceDns(),
"routeros_ip_dns_adlist": ResourceDnsAdlist(),
"routeros_ip_dns_record": ResourceDnsRecord(),
"routeros_ip_firewall_addr_list": ResourceIPFirewallAddrList(),
"routeros_ip_firewall_connection_tracking": ResourceIPConnectionTracking(),
"routeros_ip_firewall_filter": ResourceIPFirewallFilter(),
"routeros_ip_firewall_mangle": ResourceIPFirewallMangle(),
"routeros_ip_firewall_nat": ResourceIPFirewallNat(),
"routeros_ip_firewall_raw": ResourceIPFirewallRaw(),
"routeros_ip_address": ResourceIPAddress(),
"routeros_ip_hotspot": ResourceIpHotspot(),
"routeros_ip_hotspot_ip_binding": ResourceIpHotspotIpBinding(),
"routeros_ip_hotspot_profile": ResourceIpHotspotProfile(),
"routeros_ip_hotspot_service_port": ResourceIpHotspotServicePort(),
"routeros_ip_hotspot_user": ResourceIpHotspotUser(),
"routeros_ip_hotspot_user_profile": ResourceIpHotspotUserProfile(),
"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(),
"routeros_ip_dns": ResourceDns(),
"routeros_ip_dns_record": ResourceDnsRecord(),
"routeros_ip_service": ResourceIpService(),
"routeros_ip_neighbor_discovery_settings": ResourceIpNeighborDiscoverySettings(),
"routeros_ip_ssh_server": ResourceIpSSHServer(),
"routeros_ip_upnp": ResourceUPNPSettings(),
"routeros_ip_upnp_interfaces": ResourceUPNPInterfaces(),
Expand All @@ -125,7 +134,6 @@ func Provider() *schema.Provider {
"routeros_firewall_mangle": ResourceIPFirewallMangle(),
"routeros_firewall_nat": ResourceIPFirewallNat(),
"routeros_dns": ResourceDns(),
"routeros_dns_adlist": ResourceDnsAdlist(),
"routeros_dns_record": ResourceDnsRecord(),

// Interface Objects
Expand Down Expand Up @@ -262,6 +270,7 @@ func Provider() *schema.Provider {
"routeros_tool_mac_server": ResourceToolMacServer(),
"routeros_tool_mac_server_winbox": ResourceToolMacServerWinBox(),
"routeros_tool_netwatch": ResourceToolNetwatch(),
"routeros_tool_sniffer": ResourceToolSniffer(),

// User Manager
"routeros_user_manager_advanced": ResourceUserManagerAdvanced(),
Expand Down
37 changes: 37 additions & 0 deletions routeros/resource_default_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,40 @@ func DefaultSystemDatasourceRead(s map[string]*schema.Schema) schema.ReadContext
return MikrotikResourceDataToTerraformDatasource(&[]MikrotikItem{res}, "", s, d)
}
}

// FIXME Replace fucntions in resources: ResourceInterfaceEthernetSwitchPortIsolation, ResourceInterfaceEthernetSwitchPort
// ResourceInterfaceEthernetSwitch, ResourceInterfaceLte, ResourceIpService
func DefaultCreateUpdate(s map[string]*schema.Schema) func(context.Context, *schema.ResourceData, interface{}) diag.Diagnostics {
return func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
item, metadata := TerraformResourceDataToMikrotik(s, d)

res, err := ReadItems(&ItemId{Name, d.Get("name").(string)}, metadata.Path, m.(Client))
if err != nil {
// API/REST client error.
ColorizedDebug(ctx, fmt.Sprintf(ErrorMsgPatch, err))
return diag.FromErr(err)
}

// Resource not found.
if len(*res) == 0 {
d.SetId("")
ColorizedDebug(ctx, fmt.Sprintf(ErrorMsgPatch, err))
return diag.FromErr(errorNoLongerExists)
}

d.SetId((*res)[0].GetID(Id))
item[".id"] = d.Id()

var resUrl string
if m.(Client).GetTransport() == TransportREST {
resUrl = "/set"
}

err = m.(Client).SendRequest(crudPost, &URL{Path: metadata.Path + resUrl}, item, nil)
if err != nil {
return diag.FromErr(err)
}

return ResourceRead(ctx, s, d, m)
}
}
6 changes: 3 additions & 3 deletions routeros/resource_ip_dns_adlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

const testDnsAdlistMinVersion = "7.15"
const testResourceDnsAdlist = "routeros_dns_adlist.test"
const testResourceDnsAdlist = "routeros_ip_dns_adlist.test"

func TestAccResourceDnsAdlistTest_basic(t *testing.T) {
if !testCheckMinVersion(t, testDnsAdlistMinVersion) {
Expand All @@ -23,7 +23,7 @@ func TestAccResourceDnsAdlistTest_basic(t *testing.T) {
testSetTransportEnv(t, name)
},
ProviderFactories: testAccProviderFactories,
CheckDestroy: testCheckResourceDestroy("/ip/dns/adlist", "routeros_dns_adlist"),
CheckDestroy: testCheckResourceDestroy("/ip/dns/adlist", "routeros_ip_dns_adlist"),
Steps: []resource.TestStep{
{
Config: testAccResourceDnsAdlistConfig(),
Expand All @@ -42,7 +42,7 @@ func TestAccResourceDnsAdlistTest_basic(t *testing.T) {

func testAccResourceDnsAdlistConfig() string {
return providerConfig + `
resource "routeros_dns_adlist" "test" {
resource "routeros_ip_dns_adlist" "test" {
url = "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
ssl_verify = false
}`
Expand Down
95 changes: 95 additions & 0 deletions routeros/resource_ip_hotspot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package routeros

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

/*
{
".id": "*5",
"HTTPS": "false",
"addresses-per-mac": "unlimited",
"disabled": "false",
"idle-timeout": "5m",
"interface": "ether4",
"invalid": "false",
"keepalive-timeout": "none",
"login-timeout": "none",
"name": "server1",
"profile": "default",
"proxy-status": "running"
}
*/

// https://help.mikrotik.com/docs/pages/viewpage.action?pageId=56459266#HotSpot(Captiveportal)-IPHotSpot
func ResourceIpHotspot() *schema.Resource {
resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/ip/hotspot"),
MetaId: PropId(Id),
MetaSkipFields: PropSkipFields("HTTPS", "keepalive-timeout", "proxy_status"),

"address_pool": {
Type: schema.TypeString,
Optional: true,
Description: "Address space used to change HotSpot client any IP address to a valid address. Useful for " +
"providing public network access to mobile clients that are not willing to change their networking settings.",
},
"addresses_per_mac": {
Type: schema.TypeString,
Optional: true,
Description: "Number of IP addresses allowed to be bind with the MAC address, when multiple HotSpot clients " +
"connected with one MAC-address.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
KeyDisabled: PropDisabledRw,
"idle_timeout": {
Type: schema.TypeString,
Optional: true,
Description: "Period of inactivity for unauthorized clients. When there is no traffic from this client (literally " +
"client computer should be switched off), once the timeout is reached, a user is dropped from the HotSpot " +
"host list, its used address becomes available.",
DiffSuppressFunc: TimeEquall,
},
"interface": {
Type: schema.TypeString,
Required: true,
Description: "Interface to run HotSpot on.",
},
KeyInvalid: PropInvalidRo,
"keepalive_timeout": {
Type: schema.TypeString,
Optional: true,
Description: "The exact value of the keepalive-timeout, that is applied to the user. Value shows how long " +
"the host can stay out of reach to be removed from the HotSpot.",
DiffSuppressFunc: TimeEquall,
},
"login_timeout": {
Type: schema.TypeString,
Optional: true,
Description: "Period of time after which if a host hasn't been authorized itself with a system the host " +
"entry gets deleted from host table. Loop repeats until the host logs in the system. Enable if there " +
"are situations where a host cannot log in after being too long in the host table unauthorized.",
DiffSuppressFunc: TimeEquall,
},
KeyName: PropName("HotSpot server's name or identifier."),
"profile": {
Type: schema.TypeString,
Optional: true,
Description: "HotSpot server default HotSpot profile, which is located in `/ip/hotspot/profile`.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
}

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

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

Schema: resSchema,
}
}
Loading

0 comments on commit fb95d72

Please sign in to comment.