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

Vaerh/issue268 #287

Merged
merged 16 commits into from
Nov 6, 2023
Merged
1 change: 1 addition & 0 deletions examples/resources/routeros_bridge_mlag/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import routeros_ip_cloud.test .
4 changes: 4 additions & 0 deletions examples/resources/routeros_bridge_mlag/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "routeros_bridge_mlag" "mlag" {
bridge = "bridge1"
peer_port = "stack-link"
}
5 changes: 3 additions & 2 deletions examples/resources/routeros_capsman_channel/import.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Import with the name of the CAPsMAN channel configuration in case of the example use test-channel-config
terraform import routeros_capsman_channel.test_channel test-channel-config
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/caps-man/channel get [print show-ids]]
terraform import routeros_capsman_channel.test_channel "*1"
5 changes: 3 additions & 2 deletions examples/resources/routeros_capsman_configuration/import.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Import with the name of the CAPsMAN configuration in case of the example use test_configuration_name
terraform import routeros_capsman_configuration.test_configuration_2 test_configuration_name
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/caps-man/configuration get [print show-ids]]
terraform import routeros_capsman_configuration.test_configuration_2 "*1"
5 changes: 3 additions & 2 deletions examples/resources/routeros_capsman_datapath/import.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Import with the name of the CAPsMAN datapath configuration in case of the example use test-datapath-config
terraform import routeros_capsman_datapath.test_datapath test-datapath-config
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/caps-man/datapath get [print show-ids]]
terraform import routeros_capsman_datapath.test_datapath "*1"
5 changes: 3 additions & 2 deletions examples/resources/routeros_capsman_rates/import.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Import with the name of the CAPsMAN rates configuration in case of the example use test-rates-config
terraform import routeros_capsman_rates.test_rates test-rates-config
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/caps-man/rates get [print show-ids]]
terraform import routeros_capsman_rates.test_rates "*1"
5 changes: 3 additions & 2 deletions examples/resources/routeros_capsman_security/import.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Import with the name of the CAPsMAN security profile in case of the example use test-security-config
terraform import routeros_capsman_security.test_security test-security-config
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/caps-man/security get [print show-ids]]
terraform import routeros_capsman_security.test_security "*1"
5 changes: 3 additions & 2 deletions examples/resources/routeros_interface_bridge/import.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Import with the name of the bridge in case of the example use bridge
terraform import routeros_interface_bridge.bridge bridge
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/interface/bridge get [print show-ids]]
terraform import routeros_interface_bridge.bridge "*1"
5 changes: 3 additions & 2 deletions examples/resources/routeros_interface_eoip/import.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Import with the name of the EoIP interface in case of the example, use `eoip-tunnel1`
terraform import routeros_interface_eoip.eoip_tunnel1 eoip-tunnel1
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/interface/eoip get [print show-ids]]
terraform import routeros_interface_eoip.eoip_tunnel1 *B
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 [/interface/ethernet/switch get [print show-ids]]
terraform import routeros_interface_ethernet_switch.sw0 *0
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "routeros_interface_ethernet_switch" "sw0" {
switch_id = 0 # Optional
name = "new switch"
}
5 changes: 3 additions & 2 deletions examples/resources/routeros_interface_gre/import.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Import with the name of the gre interface in case of the example use gre-hq-1
terraform import routeros_interface_gre.gre_hq gre-hq-1
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/interface/gre get [print show-ids]]
terraform import routeros_interface_gre.gre_hq "*1"
5 changes: 3 additions & 2 deletions examples/resources/routeros_interface_list/import.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Import with the name of the interface list in case of the example use my-list
terraform import routeros_interface_list.list my-list
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/interface/list get [print show-ids]]
terraform import routeros_interface_list.list "*2000010"
5 changes: 3 additions & 2 deletions examples/resources/routeros_interface_vlan/import.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Import with the name of the vlan interface in case of the example use VLAN_TEST
terraform import routeros_interface_vlan.interface_vlan VLAN_TEST
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/interface/vlan get [print show-ids]]
terraform import routeros_interface_vlan.interface_vlan "*1"
5 changes: 3 additions & 2 deletions examples/resources/routeros_interface_wireguard/import.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Import with the name of the wireguard interface in case of the example use test_wg_interface
terraform import routeros_interface_wireguard.test_wg_interface test_wg_interface
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/interface/wireguard get [print show-ids]]
terraform import routeros_interface_wireguard.test_wg_interface "*1"
5 changes: 3 additions & 2 deletions examples/resources/routeros_ip_dhcp_server/import.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Import with the name of the DHCP server in case of the example use bridge_dhcp
terraform import routeros_ip_dhcp_server.server bridge_dhcp
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/ip/dhcp-server get [print show-ids]]
terraform import routeros_ip_dhcp_server.server "*1"
5 changes: 3 additions & 2 deletions examples/resources/routeros_ip_pool/import.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Import with the name of the ip pool in case of the example use my_ip_pool
terraform import routeros_ip_pool.pool my_ip_pool
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/ip/pool get [print show-ids]]
terraform import routeros_ip_pool.pool "*1"
31 changes: 30 additions & 1 deletion routeros/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@ package routeros

import (
"context"
"os"

"github.com/fatih/color"
"github.com/hashicorp/terraform-plugin-log/tflog"
"os"
)

type logLevel byte

const (
TRACE logLevel = 1 + iota
DEBUG
INFO
WARN
ERROR
)

// ColorizedDebug Used to display provider log color messages.
Expand All @@ -15,3 +26,21 @@ func ColorizedDebug(ctx context.Context, msg string, args ...map[string]interfac
}
tflog.Debug(ctx, color.GreenString(msg), args...)
}

func ColorizedMessage(ctx context.Context, level logLevel, msg string, args ...map[string]interface{}) {
if _, set := os.LookupEnv("ROS_LOG_COLOR"); set {
color.NoColor = false
}
switch level {
case TRACE:
tflog.Trace(ctx, color.GreenString(msg), args...)
case DEBUG:
tflog.Debug(ctx, color.GreenString(msg), args...)
case INFO:
tflog.Info(ctx, color.HiBlueString(msg), args...)
case WARN:
tflog.Warn(ctx, color.HiRedString(msg), args...)
case ERROR:
tflog.Error(ctx, color.HiRedString(msg), args...)
}
}
2 changes: 2 additions & 0 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func Provider() *schema.Provider {
"routeros_interface_bridge_vlan": ResourceInterfaceBridgeVlan(),
"routeros_interface_bridge_settings": ResourceInterfaceBridgeSettings(),
"routeros_interface_eoip": ResourceInterfaceEoip(),
"routeros_interface_ethernet_switch": ResourceInterfaceEthernetSwitch(),
"routeros_interface_gre": ResourceInterfaceGre(),
"routeros_interface_vlan": ResourceInterfaceVlan(),
"routeros_interface_vrrp": ResourceInterfaceVrrp(),
Expand All @@ -119,6 +120,7 @@ func Provider() *schema.Provider {

// Aliases for interface objects to retain compatibility between original and fork
"routeros_bridge": ResourceInterfaceBridge(),
"routeros_bridge_mlag": ResourceInterfaceBridgeMlag(),
"routeros_bridge_port": ResourceInterfaceBridgePort(),
"routeros_bridge_vlan": ResourceInterfaceBridgeVlan(),
"routeros_gre": ResourceInterfaceGre(),
Expand Down
35 changes: 35 additions & 0 deletions routeros/provider_resource_state_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package routeros

import (
"context"
"fmt"

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

func stateMigrationNameToId(resourcePath string) schema.StateUpgradeFunc {
return func(ctx context.Context, rawState map[string]interface{}, m interface{}) (map[string]interface{}, error) {
ColorizedMessage(ctx, INFO, fmt.Sprintf("ID attribute before migration: %#v", rawState["id"]))

if rawState["id"] != nil {
res, err := ReadItems(&ItemId{Name, rawState["id"].(string)}, resourcePath, m.(Client))
if err != nil {
return nil, err
}

// Resource not found.
if len(*res) == 0 {
rawState["id"] = ""
ColorizedMessage(ctx, WARN, "No resource found, but the scheme has been updated.",
map[string]interface{}{"path": resourcePath, "id": rawState["id"]})
return rawState, nil
}

rawState["id"] = (*res)[0].GetID(Id)
}

ColorizedMessage(ctx, INFO, fmt.Sprintf("ID attribute after migration: %#v", rawState["id"]))

return rawState, nil
}
}
11 changes: 10 additions & 1 deletion routeros/resource_capsman_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func ResourceCapsManChannel() *schema.Resource {

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

"band": {
Type: schema.TypeString,
Expand Down Expand Up @@ -109,6 +109,15 @@ func ResourceCapsManChannel() *schema.Resource {
StateContext: schema.ImportStatePassthroughContext,
},

SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: ResourceCapsManChannelV0().CoreConfigSchema().ImpliedType(),
Upgrade: stateMigrationNameToId(resSchema[MetaResourcePath].Default.(string)),
Version: 0,
},
},

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

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

func ResourceCapsManChannelV0() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/caps-man/channel"),
MetaId: PropId(Name),

"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.",
},
},
}
}
11 changes: 10 additions & 1 deletion routeros/resource_capsman_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
func ResourceCapsManConfiguration() *schema.Resource {
resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/caps-man/configuration"),
MetaId: PropId(Name),
MetaId: PropId(Id),
MetaTransformSet: PropTransformSet(`"channel": "channel.config", "datapath": "datapath.config",
"rates": "rates.config", "security": "security.config"`),

Expand Down Expand Up @@ -211,6 +211,15 @@ func ResourceCapsManConfiguration() *schema.Resource {
StateContext: schema.ImportStatePassthroughContext,
},

SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: ResourceCapsManConfigurationV0().CoreConfigSchema().ImpliedType(),
Upgrade: stateMigrationNameToId(resSchema[MetaResourcePath].Default.(string)),
Version: 0,
},
},

Schema: resSchema,
}
}
Loading
Loading