Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update some properties to support multiple values #501

Merged
merged 10 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/resources/capsman_channel.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ resource "routeros_capsman_channel" "test_channel" {
band = "2ghz-b/g/n"
control_channel_width = "10mhz"
extension_channel = "eCee"
frequency = 2412
frequency = [2412]
reselect_interval = "1h"
save_selected = true
secondary_frequency = "disabled"
secondary_frequency = ["disabled"]
skip_dfs_channels = true
tx_power = 20
}
Expand Down
4 changes: 2 additions & 2 deletions docs/resources/capsman_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ resource "routeros_capsman_configuration" "test_configuration_2" {
band = "2ghz-b/g/n"
control_channel_width = "10mhz"
extension_channel = "eCee"
frequency = 2412
frequency = [2412]
reselect_interval = "1h"
save_selected = "true"
secondary_frequency = "disabled"
secondary_frequency = ["disabled"]
skip_dfs_channels = "true"
tx_power = 20
}
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/capsman_interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
resource "routeros_capsman_channel" "channel1" {
name = "1"
band = "2ghz-g/n"
frequency = 2412
frequency = [2412]
}

resource "routeros_capsman_interface" "cap1" {
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/interface_bridge_vlan.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## Example Usage
```terraform
resource "routeros_interface_bridge_vlan" "bridge_vlan" {
vlan_ids = "50"
vlan_ids = ["50"]
bridge = "bridge"
tagged = [
"bridge",
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/interface_dot1x_client.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## Example Usage
```terraform
resource "routeros_interface_dot1x_client" "ether2" {
eap_methods = "eap-peap,eap-mschapv2"
eap_methods = ["eap-peap", "eap-mschapv2"]
identity = "router"
interface = "ether2"
}
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/interface_dot1x_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## Example Usage
```terraform
resource "routeros_interface_dot1x_server" "ether2" {
auth_types = "mac-auth"
auth_types = ["mac-auth"]
interface = "ether2"
}
```
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/ip_dhcp_server_network.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
resource "routeros_ip_dhcp_server_network" "dhcp_server_network" {
address = "10.0.0.0/24"
gateway = "10.0.0.1"
dns_server = "1.1.1.1"
dns_server = ["1.1.1.1"]
}
```

Expand Down
5 changes: 4 additions & 1 deletion docs/resources/ip_dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ A MikroTik router with DNS feature enabled can be set as a DNS server for any DN
```terraform
resource "routeros_dns" "dns-server" {
allow_remote_requests = true
servers = "2606:4700:4700::1111,1.1.1.1,2606:4700:4700::1001,1.0.0.1"
servers = [
"2606:4700:4700::1111,1.1.1.1",
"2606:4700:4700::1001,1.0.0.1",
]
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/resources/radius.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
```terraform
resource "routeros_radius" "user_manager" {
address = "127.0.0.1"
service = "ppp,login"
service = ["ppp", "login"]
}
```

Expand Down
4 changes: 2 additions & 2 deletions examples/resources/routeros_capsman_channel/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ resource "routeros_capsman_channel" "test_channel" {
band = "2ghz-b/g/n"
control_channel_width = "10mhz"
extension_channel = "eCee"
frequency = 2412
frequency = [2412]
reselect_interval = "1h"
save_selected = true
secondary_frequency = "disabled"
secondary_frequency = ["disabled"]
skip_dfs_channels = true
tx_power = 20
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
resource "routeros_capsman_channel" "channel1" {
name = "1"
band = "2ghz-g/n"
frequency = 2412
frequency = [2412]
}

resource "routeros_capsman_interface" "cap1" {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "routeros_interface_bridge_vlan" "bridge_vlan" {
vlan_ids = "50"
vlan_ids = ["50"]
bridge = "bridge"
tagged = [
"bridge",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "routeros_interface_dot1x_client" "ether2" {
eap_methods = "eap-peap,eap-mschapv2"
eap_methods = ["eap-peap", "eap-mschapv2"]
identity = "router"
interface = "ether2"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resource "routeros_interface_dot1x_server" "ether2" {
auth_types = "mac-auth"
auth_types = ["mac-auth"]
interface = "ether2"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "routeros_ip_dhcp_server_network" "dhcp_server_network" {
address = "10.0.0.0/24"
gateway = "10.0.0.1"
dns_server = "1.1.1.1"
dns_server = ["1.1.1.1"]
}
5 changes: 4 additions & 1 deletion examples/resources/routeros_ip_dns/resource.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
resource "routeros_dns" "dns-server" {
allow_remote_requests = true
servers = "2606:4700:4700::1111,1.1.1.1,2606:4700:4700::1001,1.0.0.1"
servers = [
"2606:4700:4700::1111,1.1.1.1",
"2606:4700:4700::1001,1.0.0.1",
]
}
2 changes: 1 addition & 1 deletion examples/resources/routeros_radius/resource.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resource "routeros_radius" "user_manager" {
address = "127.0.0.1"
service = "ppp,login"
service = ["ppp", "login"]
}
27 changes: 27 additions & 0 deletions routeros/provider_resource_state_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package routeros
import (
"context"
"fmt"
"reflect"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -33,3 +35,28 @@ func stateMigrationNameToId(resourcePath string) schema.StateUpgradeFunc {
return rawState, nil
}
}

func stateMigrationScalarToList(keys ...string) schema.StateUpgradeFunc {
return func(ctx context.Context, rawState map[string]interface{}, m interface{}) (map[string]interface{}, error) {
for _, key := range keys {
if rawState[key] == nil {
continue
}

value := reflect.ValueOf(rawState[key])
if value.IsZero() {
rawState[key] = []interface{}{}
}

if reflect.ValueOf(value).Kind() == reflect.String {
rawState[key] = strings.Split(rawState[key].(string), ",")
}

slice := reflect.MakeSlice(reflect.SliceOf(value.Type()), 0, 1)
reflect.Append(slice, value)
rawState[key] = slice.Interface()
}

return rawState, nil
}
}
22 changes: 15 additions & 7 deletions routeros/resource_capsman_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

// https://help.mikrotik.com/docs/display/ROS/CAPsMAN
func ResourceCapsManChannel() *schema.Resource {

resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/caps-man/channel"),
MetaId: PropId(Id),
Expand Down Expand Up @@ -52,9 +51,11 @@ func ResourceCapsManChannel() *schema.Resource {
"xx", "xxxx", "xxxxxxxx", "disabled"}, false),
},
"frequency": {
Type: schema.TypeInt,
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Description: "Channel frequency value in MHz on which AP will operate. If left blank, CAPsMAN will " +
"automatically determine the best frequency that is least occupied.",
},
Expand All @@ -74,8 +75,11 @@ func ResourceCapsManChannel() *schema.Resource {
"saves the last picked frequency.",
},
"secondary_frequency": {
Type: schema.TypeString,
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "Specifies the second frequency that will be used for 80+80MHz configuration. " +
"Set it to Disabled in order to disable 80+80MHz capability.",
},
Expand Down Expand Up @@ -109,15 +113,19 @@ func ResourceCapsManChannel() *schema.Resource {
StateContext: schema.ImportStatePassthroughContext,
},

SchemaVersion: 1,
Schema: resSchema,
SchemaVersion: 2,
StateUpgraders: []schema.StateUpgrader{
{
Type: ResourceCapsManChannelV0().CoreConfigSchema().ImpliedType(),
Upgrade: stateMigrationNameToId(resSchema[MetaResourcePath].Default.(string)),
Version: 0,
},
{
Type: ResourceCapsManChannelV1().CoreConfigSchema().ImpliedType(),
Upgrade: stateMigrationScalarToList("frequency", "secondary_frequency"),
Version: 1,
},
},

Schema: resSchema,
}
}
4 changes: 2 additions & 2 deletions routeros/resource_capsman_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ resource "routeros_capsman_channel" "test_channel" {
band = "2ghz-b/g/n"
control_channel_width = "10mhz"
extension_channel = "eCee"
frequency = 2412
frequency = [2412]
reselect_interval = "1h"
save_selected = true
secondary_frequency = "disabled"
secondary_frequency = ["disabled"]
skip_dfs_channels = true
tx_power = 20
}
Expand Down
114 changes: 114 additions & 0 deletions routeros/resource_capsman_channel_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package routeros

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

/*
{
".id": "*1",
"band": "2ghz-b",
"control-channel-width": "5mhz",
"extension-channel": "disabled",
"frequency": "2112",
"name": "channel1",
"reselect-interval": "10m",
"save-selected": "true",
"secondary-frequency": "disabled",
"skip-dfs-channels": "true",
"tx-power": "-20"
}
*/

// https://help.mikrotik.com/docs/display/ROS/CAPsMAN
func ResourceCapsManChannelV1() *schema.Resource {

resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/caps-man/channel"),
MetaId: PropId(Id),

"band": {
Type: schema.TypeString,
Optional: true,
Description: "Define operational radio frequency band and mode taken from hardware capability of wireless card.",
ValidateFunc: validation.StringInSlice([]string{"2ghz-b", "2ghz-b/g", "2ghz-b/g/n", "2ghz-g/n", "2ghz-onlyg", "2ghz-onlyn",
"5ghz-a", "5ghz-a/n", "5ghz-a/n/ac", "5ghz-n/ac", "5ghz-onlyac", "5ghz-onlyn"}, false),
},
KeyComment: PropCommentRw,
"control_channel_width": {
Type: schema.TypeString,
Optional: true,
Description: "Control channel width.",
ValidateFunc: validation.StringInSlice([]string{"5mhz", "10mhz", "20mhz", "40mhz-turbo"}, false),
},
"extension_channel": {
Type: schema.TypeString,
Optional: true,
Description: "Extension channel configuration. (E.g. Ce = extension channel is above Control channel, " +
"eC = extension channel is below Control channel)",
ValidateFunc: validation.StringInSlice([]string{"Ce", "Ceee", "Ceeeeeee", "eC", "eCee", "eCeeeeee",
"eeCe", "eeCeeeee", "eeeC", "eeeCeeee", "eeeeCeee", "eeeeeCee", "eeeeeeCe", "eeeeeeeC",
"xx", "xxxx", "xxxxxxxx", "disabled"}, false),
},
"frequency": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "Channel frequency value in MHz on which AP will operate. If left blank, CAPsMAN will " +
"automatically determine the best frequency that is least occupied.",
},
KeyName: PropNameForceNewRw,
"reselect_interval": {
Type: schema.TypeString,
Optional: true,
Description: "The interval after which the least occupied frequency is chosen, can be defined as a random " +
"interval, ex. as '30m..60m'. Works only if channel.frequency is left blank.",
// We may need to write a custom DiffSuppressFunc.
// DiffSuppressFunc: TimeEquall, not for time ranges
},
"save_selected": {
Type: schema.TypeBool,
Optional: true,
Description: "If channel frequency is chosen automatically and channel.reselect-interval is used, then " +
"saves the last picked frequency.",
},
"secondary_frequency": {
Type: schema.TypeString,
Optional: true,
Description: "Specifies the second frequency that will be used for 80+80MHz configuration. " +
"Set it to Disabled in order to disable 80+80MHz capability.",
},
"skip_dfs_channels": {
Type: schema.TypeBool,
Optional: true,
Description: "If channel.frequency is left blank, the selection will skip DFS channels.",
},
"tx_power": {
Type: schema.TypeInt,
Optional: true,
Description: "TX Power for CAP interface (for the whole interface not for individual chains) in dBm. " +
"It is not possible to set higher than allowed by country regulations or interface. By " +
"default max allowed by country or interface is used.",
ValidateFunc: validation.IntBetween(-30, 40),
},
"width": {
Type: schema.TypeString,
Optional: true,
Description: "Channel Width in MHz.",
},
}

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

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

Schema: resSchema,
}
}
Loading