Skip to content

Commit

Permalink
fix(no-release): Some boolean params can't be reset and the provider …
Browse files Browse the repository at this point in the history
…does not understand the value (#269)

Fixes #253
  • Loading branch information
vaerh authored Oct 31, 2023
1 parent 4c9546d commit 678c9a4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
28 changes: 25 additions & 3 deletions routeros/mikrotik_serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TerraformResourceDataToMikrotik(s map[string]*schema.Schema, d *schema.Reso
meta := &MikrotikItemMetadata{}
rawConfig := d.GetRawConfig()
var transformSet map[string]string
var skipFields map[string]struct{}
var skipFields, setUnsetFields map[string]struct{}

// {"channel.config": "channel", "schema-field-name": "mikrotik-field-name"}
if ts, ok := s[MetaTransformSet]; ok {
Expand All @@ -126,6 +126,9 @@ func TerraformResourceDataToMikrotik(s map[string]*schema.Schema, d *schema.Reso
if sf, ok := s[MetaSkipFields]; ok {
skipFields = loadSkipFields(sf.Default.(string))
}
if suf, ok := s[MetaSetUnsetFields]; ok {
setUnsetFields = loadSkipFields(suf.Default.(string))
}

// Schema map iteration.
for terraformSnakeName, terraformMetadata := range s {
Expand All @@ -136,7 +139,7 @@ func TerraformResourceDataToMikrotik(s map[string]*schema.Schema, d *schema.Reso
meta.IdType = IdType(terraformMetadata.Default.(int))
case MetaResourcePath:
meta.Path = terraformMetadata.Default.(string)
case MetaTransformSet, MetaSkipFields:
case MetaTransformSet, MetaSkipFields, MetaSetUnsetFields:
continue
default:
meta.Meta[terraformSnakeName] = terraformMetadata.Default.(string)
Expand Down Expand Up @@ -195,6 +198,17 @@ func TerraformResourceDataToMikrotik(s map[string]*schema.Schema, d *schema.Reso
case schema.TypeInt:
item[mikrotikKebabName] = strconv.Itoa(value.(int))
case schema.TypeBool:
// true: {...,"interfaces":"ether3","passive":"","priority":"128",...}
// false: {...,"interfaces":"ether3", "priority":"128",...}
if _, ok := setUnsetFields[terraformSnakeName]; ok {
if value.(bool) {
item[mikrotikKebabName] = ""
} else {
// Unset
item["!"+mikrotikKebabName] = ""
}
continue
}
item[mikrotikKebabName] = BoolToMikrotikJSON(value.(bool))
// Used to represent an ordered collection of items.
case schema.TypeList:
Expand Down Expand Up @@ -269,14 +283,17 @@ func MikrotikResourceDataToTerraform(item MikrotikItem, s map[string]*schema.Sch
var diags diag.Diagnostics
var err error
var transformSet map[string]string
var skipFields map[string]struct{}
var setUnsetFields, skipFields map[string]struct{}

// {"channel": "channel.config", "mikrotik-field-name": "schema-field-name"}
if ts, ok := s[MetaTransformSet]; ok {
transformSet = loadTransformSet(ts.Default.(string), false)
}

// "field_first", "field_second", "field_third"
if suf, ok := s[MetaSetUnsetFields]; ok {
setUnsetFields = loadSkipFields(suf.Default.(string))
}
if sf, ok := s[MetaSkipFields]; ok {
skipFields = loadSkipFields(sf.Default.(string))
}
Expand Down Expand Up @@ -352,6 +369,10 @@ func MikrotikResourceDataToTerraform(item MikrotikItem, s map[string]*schema.Sch
err = d.Set(terraformSnakeName, i)

case schema.TypeBool:
if _, ok := setUnsetFields[terraformSnakeName]; ok {
err = d.Set(terraformSnakeName, true)
break
}
err = d.Set(terraformSnakeName, BoolFromMikrotikJSON(mikrotikValue))

case schema.TypeList, schema.TypeSet:
Expand Down Expand Up @@ -544,6 +565,7 @@ func MikrotikResourceDataToTerraformDatasource(items *[]MikrotikItem, resourceDa
propValue = i

case schema.TypeBool:
// TODO Add support for set/unset fields?
propValue = BoolFromMikrotikJSON(mikrotikValue)

case schema.TypeList:
Expand Down
22 changes: 18 additions & 4 deletions routeros/provider_schema_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import (

// All metadata fields must be present in each resource schema, and the field type must be string.
const (
MetaId = "___id___"
MetaResourcePath = "___path___"
MetaTransformSet = "___ts___"
MetaSkipFields = "___skip___"
MetaId = "___id___"
MetaResourcePath = "___path___"
MetaTransformSet = "___ts___"
MetaSkipFields = "___skip___"
MetaSetUnsetFields = "___unset___"
)

const (
Expand Down Expand Up @@ -104,6 +105,19 @@ func PropSkipFields(s string) *schema.Schema {
}
}

// PropSetUnsetFields
func PropSetUnsetFields(s string) *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: s,
Description: "<em>A set of fields that require setting/unsetting. This is an internal service field, setting a value is not required.</em>",
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return true
},
}
}

// PropName
func PropName(description string) *schema.Schema {
return &schema.Schema{
Expand Down
13 changes: 8 additions & 5 deletions routeros/resource_routing_ospf_area.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import (
// ResourceRoutingOspfArea https://help.mikrotik.com/docs/display/ROS/OSPF
func ResourceRoutingOspfArea() *schema.Resource {
resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/routing/ospf/area"),
MetaId: PropId(Id),
MetaResourcePath: PropResourcePath("/routing/ospf/area"),
MetaId: PropId(Id),
MetaSetUnsetFields: PropSetUnsetFields(`"no_summaries"`),

"area_id": {
Type: schema.TypeString,
Expand All @@ -45,9 +46,11 @@ func ResourceRoutingOspfArea() *schema.Resource {
},
KeyName: PropNameForceNewRw,
"no_summaries": {
Type: schema.TypeBool,
Optional: true,
Description: "If set then the area will not flood summary LSAs in the stub area.",
Type: schema.TypeBool,
Optional: true,
Description: "If set then the area will not flood summary LSAs in the stub area. " +
"<em>The correct value of this attribute may not be displayed in Winbox. " +
"Please check the parameters in the console!</em>",
},
"nssa_translate": {
Type: schema.TypeString,
Expand Down
15 changes: 9 additions & 6 deletions routeros/resource_routing_ospf_interface_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ import (
// ResourceRoutingOspfInterfaceTemplate https://help.mikrotik.com/docs/display/ROS/OSPF
func ResourceRoutingOspfInterfaceTemplate() *schema.Resource {
resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/routing/ospf/interface-template"),
MetaId: PropId(Id),
MetaResourcePath: PropResourcePath("/routing/ospf/interface-template"),
MetaId: PropId(Id),
MetaSetUnsetFields: PropSetUnsetFields(`"passive"`),

"area": {
Type: schema.TypeString,
Expand Down Expand Up @@ -107,10 +108,12 @@ func ResourceRoutingOspfInterfaceTemplate() *schema.Resource {
Description: "The network prefix associated with the area.",
},
"passive": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "If enabled, then do not send or receive OSPF traffic on the matching interfaces",
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "If enabled, then do not send or receive OSPF traffic on the matching interfaces. " +
"<em>The correct value of this attribute may not be displayed in Winbox. " +
"Please check the parameters in the console!</em>",
},
"prefix_list": {
Type: schema.TypeString,
Expand Down
1 change: 1 addition & 0 deletions routeros/resource_routing_ospf_interface_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ resource "routeros_routing_ospf_area" "test_routing_ospf_area" {
resource "routeros_routing_ospf_interface_template" "test_routing_ospf_interface_template" {
area = routeros_routing_ospf_area.test_routing_ospf_area.name
interfaces = ["ether3"]
passive = true
}
`
Expand Down

0 comments on commit 678c9a4

Please sign in to comment.