Skip to content

Commit

Permalink
feat: Add bandwidth server resource (#475)
Browse files Browse the repository at this point in the history
Closes #474 

* add bandwitdh server resource

* add tests for the bandwitdh server resource

* specify the bandwitdh server resource in provider

* add examples for the bandwidth server resource

* fix typo

* fix function name

* generate docs for bandwidth server resource

* refactor: Small fixes to the added resource

* refactor: Add a helper for KeyEnabled

* fix: Resolve merge conflict

---------

Co-authored-by: Vaerh <vaerh@tutanota.com>
  • Loading branch information
mirceanton and vaerh committed May 29, 2024
1 parent f672534 commit d98ce0f
Show file tree
Hide file tree
Showing 17 changed files with 182 additions and 54 deletions.
32 changes: 32 additions & 0 deletions docs/resources/tool_bandwidth_server.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# routeros_tool_bandwidth_server (Resource)


## Example Usage
```terraform
resource "routeros_tool_bandwidth_test_server" "test" {
enabled = true
authenticate = false
max_sessions = 100
allocate_udp_ports_from = 2000
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `allocate_udp_ports_from ` (Boolean) Beginning of UDP port range.
- `authenticate` (Boolean) Communicate only with authenticated clients.
- `enabled` (Boolean) Defines whether bandwidth server is enabled or not.
- `max_sessions ` (Number) Maximal simultaneous test count.

### Read-Only

- `id` (String) The ID of this resource.

## Import
Import is supported using the following syntax:
```shell
terraform import routeros_tool_bandwidth_server.test .
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import routeros_tool_bandwidth_server.test .
6 changes: 6 additions & 0 deletions examples/resources/routeros_tool_bandwidth_server/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resource "routeros_tool_bandwidth_test_server" "test" {
enabled = true
authenticate = false
max_sessions = 100
allocate_udp_ports_from = 2000
}
1 change: 1 addition & 0 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ func Provider() *schema.Provider {
"routeros_move_items": ResourceMoveItems(),

// Tools
"routeros_tool_bandwidth_server": ResourceToolBandwidthServer(),
"routeros_tool_mac_server": ResourceToolMacServer(),
"routeros_tool_mac_server_winbox": ResourceToolMacServerWinBox(),

Expand Down
11 changes: 11 additions & 0 deletions routeros/provider_schema_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
KeyDisabled = "disabled"
KeyDontFragment = "dont_fragment"
KeyDscp = "dscp"
KeyEnabled = "enabled"
KeyFilter = "filter"
KeyInactive = "inactive"
KeyInterface = "interface"
Expand Down Expand Up @@ -154,6 +155,16 @@ func PropName(description string) *schema.Schema {
}
}

// PropEnabled
func PropEnabled(description string) *schema.Schema {
return &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Description: description,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
}
}

// PropMacAddress
func PropMacAddressRw(description string, required bool) *schema.Schema {
mac := &schema.Schema{
Expand Down
6 changes: 1 addition & 5 deletions routeros/resource_capsman_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ func ResourceCapsManManager() *schema.Resource {
Default: "none",
Description: "Device certificate.",
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Description: "Disable or enable CAPsMAN functionality.",
},
KeyEnabled: PropEnabled("Disable or enable CAPsMAN functionality."),
"generated_ca_certificate": {
Type: schema.TypeString,
Computed: true,
Expand Down
6 changes: 1 addition & 5 deletions routeros/resource_interface_wireless_cap.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@ func ResourceInterfaceWirelessCap() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
Description: "List of interfaces over which CAP should attempt to discover CAPs Manager.",
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Description: "Disable or enable the CAP functionality.",
},
KeyEnabled: PropEnabled("Disable or enable the CAP functionality."),
"interfaces": {
Type: schema.TypeSet,
Optional: true,
Expand Down
6 changes: 1 addition & 5 deletions routeros/resource_ip_upnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ func ResourceUPNPSettings() *schema.Resource {
"unwanted in UPnP deployments which the standard was not designed for (it was designed mostly for " +
"home users to establish their own local networks), you can disable this behavior",
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Description: "Enable UPnP service.",
},
KeyEnabled: PropEnabled("Enable UPnP service."),
"show_dummy_rule": {
Type: schema.TypeBool,
Optional: true,
Expand Down
6 changes: 1 addition & 5 deletions routeros/resource_ovpn_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@ func ResourceOpenVPNServer() *schema.Resource {
Optional: true,
Description: "Specifies if IPv6 IP tunneling mode should be possible with this OVPN server.",
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Description: "Defines whether the OVPN server is enabled or not.",
},
KeyEnabled: PropEnabled("Defines whether the OVPN server is enabled or not."),
"ipv6_prefix_len": {
Type: schema.TypeInt,
Optional: true,
Expand Down
6 changes: 1 addition & 5 deletions routeros/resource_snmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ func ResourceSNMP() *schema.Resource {
Optional: true,
Description: "Contact information.",
},
"enabled": {
Type: schema.TypeBool,
Required: true,
Description: "Used to disable/enable SNMP service",
},
KeyEnabled: PropEnabled("Used to disable/enable SNMP service"),
"engine_id": {
Type: schema.TypeString,
Computed: true,
Expand Down
7 changes: 1 addition & 6 deletions routeros/resource_system_ntp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ func ResourceSystemNtpClient() *schema.Resource {
MetaResourcePath: PropResourcePath("/system/ntp/client"),
MetaId: PropId(Id),

"enabled": {
Type: schema.TypeBool,
Optional: true,
Description: "Enable NTP client.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
KeyEnabled: PropEnabled("Enable NTP client."),
"freq_drift": {
Type: schema.TypeString,
Computed: true,
Expand Down
7 changes: 1 addition & 6 deletions routeros/resource_system_ntp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ func ResourceSystemNtpServer() *schema.Resource {
Optional: true,
Description: "Set broadcast address to use for NTP server broadcast mode.",
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Description: "Enable NTP server.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
KeyEnabled: PropEnabled("Enable NTP server."),
"local_clock_stratum": {
Type: schema.TypeInt,
Optional: true,
Expand Down
58 changes: 58 additions & 0 deletions routeros/resource_tool_bandwidth_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package routeros

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

/*
{
"allocate-udp-ports-from": "2000",
"authenticate": "true",
"enabled": "true",
"max-sessions": "100"
}
*/

// https://help.mikrotik.com/docs/display/ROS/Bandwidth+Test
func ResourceToolBandwidthServer() *schema.Resource {
resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/tool/bandwidth-server"),
MetaId: PropId(Id),

"allocate_udp_ports_from": {
Type: schema.TypeInt,
Optional: true,
Description: "Beginning of UDP port range.",
ValidateFunc: validation.IntBetween(1000, 65535),
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"authenticate": {
Type: schema.TypeBool,
Optional: true,
Description: "Communicate only with authenticated clients.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
KeyEnabled: PropEnabled("Defines whether bandwidth server is enabled or not."),
"max_sessions": {
Type: schema.TypeInt,
Optional: true,
Description: "Maximal simultaneous test count.",
ValidateFunc: validation.IntBetween(1, 1000),
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
}

return &schema.Resource{
CreateContext: DefaultSystemCreate(resSchema),
ReadContext: DefaultSystemRead(resSchema),
UpdateContext: DefaultSystemUpdate(resSchema),
DeleteContext: DefaultSystemDelete(resSchema),

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

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

import (
"testing"

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

const testToolsBandwidthServer = "routeros_tool_bandwidth_server.test"

func TestAccToolsBandwidthServerTest_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,
Steps: []resource.TestStep{
{
Config: testAccToolsBandwidthServerConfig_none(),
Check: resource.ComposeTestCheckFunc(
testResourcePrimaryInstanceId(testToolsBandwidthServer),
resource.TestCheckResourceAttr(testToolsBandwidthServer, "enabled", "false"),
),
},
{
Config: testAccToolsBandwidthServerConfig_complex(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(testToolsBandwidthServer, "enabled", "true"),
resource.TestCheckResourceAttr(testToolsBandwidthServer, "authenticate", "false"),
resource.TestCheckResourceAttr(testToolsBandwidthServer, "max_sessions", "100"),
resource.TestCheckResourceAttr(testToolsBandwidthServer, "allocate_udp_ports_from", "2000"),
),
},
},
})
})
}
}

func testAccToolsBandwidthServerConfig_none() string {
return providerConfig + `
resource "routeros_tool_bandwidth_server" "test" {
enabled = false
}
`
}

func testAccToolsBandwidthServerConfig_complex() string {
return providerConfig + `
resource "routeros_tool_bandwidth_server" "test" {
enabled = true
authenticate = false
max_sessions = 100
allocate_udp_ports_from = 2000
}
`
}
7 changes: 1 addition & 6 deletions routeros/resource_user_manager_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ func ResourceUserManagerSettings() *schema.Resource {
Default: "none",
Description: "Certificate for use in EAP TLS-type authentication methods.",
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "An option whether the User Manager functionality is enabled.",
},
KeyEnabled: PropEnabled("An option whether the User Manager functionality is enabled."),
"use_profiles": {
Type: schema.TypeBool,
Optional: true,
Expand Down
8 changes: 2 additions & 6 deletions routeros/resource_wifi_cap.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func ResourceWifiCap() *schema.Resource {
"caps_man_addresses": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.IsIPAddress,
},
Expand Down Expand Up @@ -57,11 +57,7 @@ func ResourceWifiCap() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
Description: "List of interfaces over which CAP should attempt to discover CAPs Manager.",
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Description: "Disable or enable the CAP functionality.",
},
KeyEnabled: PropEnabled("Disable or enable the CAP functionality."),
"lock_to_caps_man": {
Type: schema.TypeBool,
Optional: true,
Expand Down
6 changes: 1 addition & 5 deletions routeros/resource_wifi_capsman.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ func ResourceWifiCapsman() *schema.Resource {
Optional: true,
Description: "Device certificate.",
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Description: "Disable or enable CAPsMAN functionality.",
},
KeyEnabled: PropEnabled("Disable or enable CAPsMAN functionality."),
"generated_ca_certificate": {
Type: schema.TypeString,
Computed: true,
Expand Down

0 comments on commit d98ce0f

Please sign in to comment.