Skip to content

Commit

Permalink
fix: Add a validator for map name keys.
Browse files Browse the repository at this point in the history
Closes #552
  • Loading branch information
vaerh committed Sep 1, 2024
1 parent a8192d6 commit 059de30
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 4 deletions.
33 changes: 29 additions & 4 deletions routeros/provider_schema_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,11 @@ var (
"and cannot be directly modified.",
}
PropFilterRw = &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Elem: schema.TypeString,
Description: "Additional request filtering options.",
Type: schema.TypeMap,
Optional: true,
Elem: schema.TypeString,
Description: "Additional request filtering options.",
ValidateDiagFunc: ValidationMapKeyNames,
}
PropInactiveRo = &schema.Schema{
Type: schema.TypeBool,
Expand Down Expand Up @@ -618,6 +619,30 @@ var (
return
}
}

reTerraformField = regexp.MustCompile("^[a-z0-9_]+$")
// ValidationMapKeyNames, A function to check map names for compliance with the TF standard.
// When copying keys from a real configuration it is easy to make a mistake and transfer a key
// containing dashes instead of underscores. This validator is added to prevent such errors.
ValidationMapKeyNames = func(v interface{}, path cty.Path) diag.Diagnostics {
var diags diag.Diagnostics

for key := range v.(map[string]interface{}) {
if reTerraformField.MatchString(key) {
continue
}

diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Invalid attribute name",
Detail: fmt.Sprintf("%s: Attribute name may only contain lowercase alphanumeric characters & "+
"underscores.", key),
AttributePath: append(path, cty.IndexStep{Key: cty.StringVal(key)}),
})
}

return diags
}
)

// Properties DiffSuppressFunc.
Expand Down
4 changes: 4 additions & 0 deletions routeros/resource_capsman_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func ResourceCapsManConfiguration() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
},
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
KeyComment: PropCommentRw,
Expand All @@ -65,6 +66,7 @@ func ResourceCapsManConfiguration() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
},
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"disconnect_timeout": {
Expand Down Expand Up @@ -167,6 +169,7 @@ func ResourceCapsManConfiguration() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
},
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"rx_chains": {
Expand All @@ -185,6 +188,7 @@ func ResourceCapsManConfiguration() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
},
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"ssid": {
Expand Down
4 changes: 4 additions & 0 deletions routeros/resource_capsman_configuration_v0.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func ResourceCapsManConfigurationV0() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
},
ValidateDiagFunc: ValidationMapKeyNames,
},
KeyComment: PropCommentRw,
"country": {
Expand All @@ -35,6 +36,7 @@ func ResourceCapsManConfigurationV0() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
},
ValidateDiagFunc: ValidationMapKeyNames,
},
"disconnect_timeout": {
Type: schema.TypeString,
Expand Down Expand Up @@ -136,6 +138,7 @@ func ResourceCapsManConfigurationV0() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
},
ValidateDiagFunc: ValidationMapKeyNames,
},
"rx_chains": {
Type: schema.TypeList,
Expand All @@ -153,6 +156,7 @@ func ResourceCapsManConfigurationV0() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
},
ValidateDiagFunc: ValidationMapKeyNames,
},
"ssid": {
Type: schema.TypeString,
Expand Down
5 changes: 5 additions & 0 deletions routeros/resource_capsman_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ func ResourceCapsManInterface() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Channel inline settings.",
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"configuration": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Configuration inline settings.",
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
KeyComment: PropCommentRw,
Expand All @@ -65,6 +67,7 @@ func ResourceCapsManInterface() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Datapath inline settings.",
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
KeyDisabled: PropDisabledRw,
Expand Down Expand Up @@ -109,6 +112,7 @@ func ResourceCapsManInterface() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Rates inline settings.",
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"running": {
Expand All @@ -121,6 +125,7 @@ func ResourceCapsManInterface() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Security inline settings.",
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
}
Expand Down
7 changes: 7 additions & 0 deletions routeros/resource_wifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func ResourceWifi() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "AAA inline settings.",
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
KeyArp: PropArpRw,
Expand All @@ -56,20 +57,23 @@ func ResourceWifi() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Channel inline settings.",
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"configuration": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Configuration inline settings.",
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"datapath": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Datapath inline settings.",
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
KeyComment: PropCommentRw,
Expand All @@ -91,6 +95,7 @@ func ResourceWifi() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Interworking inline settings.",
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
KeyL2Mtu: PropL2MtuRw,
Expand Down Expand Up @@ -133,13 +138,15 @@ func ResourceWifi() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Security inline settings.",
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"steering": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Steering inline settings.",
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
}
Expand Down
6 changes: 6 additions & 0 deletions routeros/resource_wifi_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func ResourceWifiConfiguration() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "AAA inline settings.",
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"antenna_gain": {
Expand Down Expand Up @@ -74,6 +75,7 @@ func ResourceWifiConfiguration() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Channel inline settings.",
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
KeyComment: PropCommentRw,
Expand All @@ -87,6 +89,7 @@ func ResourceWifiConfiguration() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Datapath inline settings.",
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
KeyDisabled: PropDisabledRw,
Expand All @@ -108,6 +111,7 @@ func ResourceWifiConfiguration() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Interworking inline settings.",
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"manager": {
Expand Down Expand Up @@ -140,6 +144,7 @@ func ResourceWifiConfiguration() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Security inline settings.",
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"ssid": {
Expand All @@ -152,6 +157,7 @@ func ResourceWifiConfiguration() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Steering inline settings.",
ValidateDiagFunc: ValidationMapKeyNames,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"tx_chains": {
Expand Down

0 comments on commit 059de30

Please sign in to comment.