Skip to content

Commit

Permalink
feat: Add float type support
Browse files Browse the repository at this point in the history
  • Loading branch information
dokmic authored and vaerh committed Dec 2, 2023
1 parent a97bc45 commit 4cc485b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
42 changes: 37 additions & 5 deletions routeros/mikrotik_serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func isEmpty(propName string, schemaProp *schema.Schema, d *schema.ResourceData,
return v.(string) == "" && schemaProp.Default.(string) == ""
}
return v.(string) == "" && confValue.IsNull()
case schema.TypeInt:
case schema.TypeFloat, schema.TypeInt:
return confValue.IsNull() && schemaProp.Default == nil
case schema.TypeBool:
// If true, it is always not empty:
Expand Down Expand Up @@ -195,6 +195,8 @@ func TerraformResourceDataToMikrotik(s map[string]*schema.Schema, d *schema.Reso
switch terraformMetadata.Type {
case schema.TypeString:
item[mikrotikKebabName] = value.(string)
case schema.TypeFloat:
item[mikrotikKebabName] = strconv.FormatFloat(value.(float64), 'f', -1, 64)
case schema.TypeInt:
item[mikrotikKebabName] = strconv.Itoa(value.(int))
case schema.TypeBool:
Expand Down Expand Up @@ -360,10 +362,18 @@ func MikrotikResourceDataToTerraform(item MikrotikItem, s map[string]*schema.Sch
case schema.TypeString:
err = d.Set(terraformSnakeName, mikrotikValue)

case schema.TypeFloat:
f, e := strconv.ParseFloat(mikrotikValue, 64)
if e != nil {
diags = diag.Errorf("%v for '%v' field", e, terraformSnakeName)
break
}
err = d.Set(terraformSnakeName, f)

case schema.TypeInt:
i, e := strconv.Atoi(mikrotikValue)
if e != nil {
diags = diag.Errorf("%v for '%v' field", err, terraformSnakeName)
diags = diag.Errorf("%v for '%v' field", e, terraformSnakeName)
break
}
err = d.Set(terraformSnakeName, i)
Expand Down Expand Up @@ -391,15 +401,24 @@ func MikrotikResourceDataToTerraform(item MikrotikItem, s map[string]*schema.Sch
// Flat Lists & Sets:
if _, ok := s[terraformSnakeName].Elem.(*schema.Schema); mikrotikValue != "" && ok {
for _, v := range strings.Split(mikrotikValue, ",") {
if s[terraformSnakeName].Elem.(*schema.Schema).Type == schema.TypeInt {
i, err := strconv.Atoi(v)
switch s[terraformSnakeName].Elem.(*schema.Schema).Type {
case schema.TypeFloat:
f, err := strconv.ParseFloat(v, 64)
if err != nil {
diags = diag.Errorf("%v for '%v' field", err, terraformSnakeName)
continue
}
l = append(l, f)

case schema.TypeInt:
i, err := strconv.Atoi(v)
if err != nil {
diags = diag.Errorf("%v for '%v' field", err, terraformSnakeName)
continue
}
l = append(l, i)
} else {

default:
l = append(l, v)
}
}
Expand Down Expand Up @@ -429,6 +448,11 @@ func MikrotikResourceDataToTerraform(item MikrotikItem, s map[string]*schema.Sch
switch s[terraformSnakeName].Elem.(*schema.Resource).Schema[subFieldSnakeName].Type {
case schema.TypeString:
v = mikrotikValue
case schema.TypeFloat:
v, err = strconv.ParseFloat(mikrotikValue, 64)
if err != nil {
diags = diag.Errorf("%v for '%v.%v' field", err, terraformSnakeName, subFieldSnakeName)
}
case schema.TypeInt:
v, err = strconv.Atoi(mikrotikValue)
if err != nil {
Expand Down Expand Up @@ -572,6 +596,14 @@ func MikrotikResourceDataToTerraformDatasource(items *[]MikrotikItem, resourceDa
case schema.TypeString:
propValue = mikrotikValue

case schema.TypeFloat:
f, err := strconv.ParseFloat(mikrotikValue, 64)
if err != nil {
diags = append(diags, diag.Errorf("%v for '%v' field", err, terraformSnakeName)...)
continue
}
propValue = f

case schema.TypeInt:
i, err := strconv.Atoi(mikrotikValue)
if err != nil {
Expand Down
21 changes: 14 additions & 7 deletions routeros/mikrotik_serialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ var (
"string": {
Type: schema.TypeString,
},
"float": {
Type: schema.TypeFloat,
},
"int": {
Type: schema.TypeInt,
},
Expand All @@ -40,6 +43,9 @@ var (
"string": {
Type: schema.TypeString,
},
"float": {
Type: schema.TypeFloat,
},
"int": {
Type: schema.TypeInt,
},
Expand All @@ -55,10 +61,10 @@ var (

func Test_mikrotikResourceDataToTerraform(t *testing.T) {

testItem := MikrotikItem{".id": "*39", "string": "string12345", "int": "10", "bool": "true"}
testItem := MikrotikItem{".id": "*39", "string": "string12345", "float": "0.01", "int": "10", "bool": "true"}

testResourceData := testResource.TestResourceData()
expectedRes := map[string]interface{}{"string": "string12345", "int": 10, "bool": true}
expectedRes := map[string]interface{}{"string": "string12345", "float": 0.01, "int": 10, "bool": true}

err := MikrotikResourceDataToTerraform(testItem, testResource.Schema, testResourceData)
if err != nil {
Expand All @@ -77,11 +83,12 @@ func Test_mikrotikResourceDataToTerraform(t *testing.T) {

func Test_terraformResourceDataToMikrotik(t *testing.T) {

expected := MikrotikItem{"string": "string12345", "int": "10", "bool": "yes"}
expected := MikrotikItem{"string": "string12345", "float": "0.01", "int": "10", "bool": "yes"}

testResourceData := testResource.TestResourceData()
testResourceData.SetId("*39")
testResourceData.Set("string", "string12345")
testResourceData.Set("float", 0.01)
testResourceData.Set("int", 10)
testResourceData.Set("bool", true)

Expand All @@ -94,14 +101,14 @@ func Test_terraformResourceDataToMikrotik(t *testing.T) {

func Test_mikrotikResourceDataToTerraformDatasource(t *testing.T) {
testItems := []MikrotikItem{
{"string": "string12345", "int": "10", "bool": "yes"},
{"string": "12345string", "int": "20", "bool": "no"},
{"string": "string12345", "float": "0.01", "int": "10", "bool": "yes"},
{"string": "12345string", "float": "0.02", "int": "20", "bool": "no"},
}

testResourceData := testDatasource.TestResourceData()
expectedRes := []map[string]interface{}{
{MetaResourcePath: "", MetaId: 0, "string": "string12345", "int": 10, "bool": true},
{MetaResourcePath: "", MetaId: 0, "string": "12345string", "int": 20, "bool": false},
{MetaResourcePath: "", MetaId: 0, "string": "string12345", "float": 0.01, "int": 10, "bool": true},
{MetaResourcePath: "", MetaId: 0, "string": "12345string", "float": 0.02, "int": 20, "bool": false},
}

err := MikrotikResourceDataToTerraformDatasource(&testItems, "test_name", testDatasource.Schema, testResourceData)
Expand Down

0 comments on commit 4cc485b

Please sign in to comment.