Skip to content

Commit

Permalink
fix: Add missing schema fields (#303)
Browse files Browse the repository at this point in the history
* feat: Add support of default values depending on the RouterOS capabilities

* refactor: Fix RADIUS client resource to use compatible defaults helper

* feat: Add `address_list_extra_time` field to the DNS settings resource

* refactor: Refactor ethernet resource schema to use compatible defaults helper

* feat: Add the missing PoE field to the ethernet resource schema

* fix: Fix `AlwaysPresentNotUserProvided` helper to check that the value is not present in the raw config

* refactor: Update schema fields with conditional defaults to use `AlwaysPresentNotUserProvided` helper instead

* refactor: Revert adding support of default values depending on the RouterOS capabilities

* fix: Revert `cable-settings` rename
  • Loading branch information
dokmic committed Nov 27, 2023
1 parent 9ee6d70 commit bd245b9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 62 deletions.
2 changes: 1 addition & 1 deletion routeros/provider_schema_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ var (
// Prevents the need of hardcode values for default values, as those are harder to track over time/versions of
// routeros
AlwaysPresentNotUserProvided = func(k, old, new string, d *schema.ResourceData) bool {
if old != "" && new == "" {
if old != "" && d.GetRawConfig().GetAttr(k).IsNull() {
return true
}
return false
Expand Down
97 changes: 57 additions & 40 deletions routeros/resource_interface_ethernet.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ import (
}
*/

const poeOutField = "poe_out"
const cableSettingsField = "cable_settings"
const runningCheckField = "disable_running_check"

// ResourceInterfaceEthernet is the schema for ethernet interfaces
// https://help.mikrotik.com/docs/display/ROS/Ethernet#Ethernet-Properties
func ResourceInterfaceEthernet() *schema.Resource {
Expand Down Expand Up @@ -118,11 +114,11 @@ func ResourceInterfaceEthernet() *schema.Resource {
},
KeyDisabled: PropDisabledRw,
"disable_running_check": {
Type: schema.TypeBool,
Type: schema.TypeBool,
Optional: true,
Description: `Disable running check. If this value is set to 'no', the router automatically detects whether the NIC is connected with a device in the network or not.
Default value is 'yes' because older NICs do not support it. (only applicable to x86)`,
Default: true,
Optional: true,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"factory_name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -171,19 +167,59 @@ func ResourceInterfaceEthernet() *schema.Resource {
Description: "Original Media Access Control number of an interface. (read only)",
Computed: true,
},
poeOutField: {
"poe_lldp_enabled": {
Type: schema.TypeBool,
Optional: true,
Description: "An option that enables LLDP for managing devices.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"poe_out": {
Type: schema.TypeString,
Description: "PoE settings: (https://wiki.mikrotik.com/wiki/Manual:PoE-Out)",
Default: "off",
Optional: true,
Description: "PoE settings: (https://wiki.mikrotik.com/wiki/Manual:PoE-Out)",
ValidateFunc: validation.StringInSlice([]string{"auto-on", "forced-on", "off"}, false),
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"poe_priority": {
Type: schema.TypeInt,
Description: "PoE settings: (https://wiki.mikrotik.com/wiki/Manual:PoE-Out)",
Type: schema.TypeInt,
Optional: true,
Description: "PoE settings: (https://wiki.mikrotik.com/wiki/Manual:PoE-Out)",
ValidateFunc: validation.IntBetween(0, 99),
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"poe_voltage": {
Type: schema.TypeString,
Optional: true,
Description: "An option that allows us to manually control the voltage outputs on the PoE port.",
ValidateFunc: validation.StringInSlice([]string{"auto", "high", "low"}, false),
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"power_cycle_interval": {
Type: schema.TypeString,
Optional: true,
Description: "An options that disables PoE-Out power for 5s between the specified intervals.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"power_cycle_ping_enabled": {
Type: schema.TypeBool,
Optional: true,
Description: "An option that enables ping watchdog of power cycles on the port if a host does not respond to ICMP or MAC-Telnet packets.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"power_cycle_ping_address": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IntBetween(0, 99),
Description: "An address to monitor.",
ValidateFunc: validation.IsIPAddress,
RequiredWith: []string{"power_cycle_ping_enabled"},
},
"power_cycle_ping_timeout": {
Type: schema.TypeString,
Optional: true,
Description: "If the host does not respond over the specified period, the PoE-Out port is switched off for 5s.",
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return AlwaysPresentNotUserProvided(k, old, new, d) || TimeEquall(k, old, new, d)
},
},
"running": {
Type: schema.TypeBool,
Expand All @@ -200,17 +236,18 @@ func ResourceInterfaceEthernet() *schema.Resource {
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"sfp_rate_select": {
Type: schema.TypeString,
Optional: true,
Description: `Allows to control rate select pin for SFP ports. Values: high | low`,
Default: "high",
ValidateFunc: validation.StringInSlice([]string{"high", "low"}, false),
Type: schema.TypeString,
Optional: true,
Description: `Allows to control rate select pin for SFP ports. Values: high | low`,
ValidateFunc: validation.StringInSlice([]string{"high", "low"}, false),
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"sfp_shutdown_temperature": {
Type: schema.TypeInt,
Type: schema.TypeInt,
Optional: true,
Description: "The temperature in Celsius at which the interface will be temporarily turned off due to too high detected SFP module temperature (introduced v6.48)." +
"The default value for SFP/SFP+/SFP28 interfaces is 95, and for QSFP+/QSFP28 interfaces 80 (introduced v7.6).",
Optional: true,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"slave": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -288,26 +325,6 @@ func updateEthernetInterface(ctx context.Context, s map[string]*schema.Schema, d
return diag.FromErr(err)
}

// Router won't accept poe-out parameter if the interface does not support it.
poeDesiredState := d.Get(poeOutField)
_, supportsPoE := ethernetInterface[SnakeToKebab(poeOutField)]
switch {
// if the user has specified it, but it's not supported, lets error out
case poeDesiredState != "off" && !supportsPoE:
return diag.FromErr(errors.New("can't configure PoE, router does not supports it"))
// if the router does not support PoE, avoid sending the parameter as it returns an error.
case !supportsPoE:
s[MetaSkipFields].Default = skipFieldInSchema(s[MetaSkipFields].Default, poeOutField)
}

if _, supportsCableSettings := ethernetInterface[SnakeToKebab(cableSettingsField)]; !supportsCableSettings {
s[MetaSkipFields].Default = skipFieldInSchema(s[MetaSkipFields].Default, cableSettingsField)
}

if _, supportsRunningCheck := ethernetInterface[SnakeToKebab(runningCheckField)]; !supportsRunningCheck {
s[MetaSkipFields].Default = skipFieldInSchema(s[MetaSkipFields].Default, runningCheckField)
}

d.SetId(ethernetInterface.GetID(Id))
if updateDiag := ResourceUpdate(ctx, s, d, m); updateDiag.HasError() {
return updateDiag
Expand Down
9 changes: 9 additions & 0 deletions routeros/resource_ip_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ func ResourceDns() *schema.Resource {
MetaResourcePath: PropResourcePath("/ip/dns"),
MetaId: PropId(Name),

"address_list_extra_time": {
Type: schema.TypeString,
Optional: true,
Description: "",
ValidateFunc: ValidationTime,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return AlwaysPresentNotUserProvided(k, old, new, d) || TimeEquall(k, old, new, d)
},
},
"allow_remote_requests": {
Type: schema.TypeBool,
Optional: true,
Expand Down
27 changes: 6 additions & 21 deletions routeros/resource_radius.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package routeros

import (
"context"

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

const vrfKey = "vrf"

// https://help.mikrotik.com/docs/display/ROS/RADIUS#RADIUS-RADIUSClient
func ResourceRadius() *schema.Resource {
resSchema := map[string]*schema.Schema{
Expand Down Expand Up @@ -131,16 +126,17 @@ func ResourceRadiusIncoming() *schema.Resource {
Description: "The port number to listen for the requests on.",
ValidateFunc: validation.IntBetween(0, 65535),
},
vrfKey: {
Type: schema.TypeString,
Optional: true,
Description: "VRF on which service is listening for incoming connections. This option is available in RouterOS starting from version 7.4.",
"vrf": {
Type: schema.TypeString,
Optional: true,
Description: "VRF on which service is listening for incoming connections. This option is available in RouterOS starting from version 7.4.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
}

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

Expand All @@ -151,14 +147,3 @@ func ResourceRadiusIncoming() *schema.Resource {
Schema: resSchema,
}
}

func compatibleSystemRead(s map[string]*schema.Schema) schema.ReadContextFunc {
return func (ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
diags := SystemResourceRead(ctx, s, d, m)
if _, exists := d.GetOk(vrfKey); exists {
s[vrfKey].Default = "main"
}

return diags
}
}

0 comments on commit bd245b9

Please sign in to comment.