Skip to content

Commit

Permalink
Workaround to deal with non-bool values in auth_strategies config map
Browse files Browse the repository at this point in the history
  • Loading branch information
kjschubert committed Jul 12, 2023
1 parent c2d855e commit be36298
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
18 changes: 14 additions & 4 deletions internal/provider/auth_strategies_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"strconv"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
Expand Down Expand Up @@ -127,7 +128,7 @@ func (d *authStrategiesDataSource) Read(ctx context.Context, req datasource.Read
}

type configValue struct {
Value string `json:"value"`
Value any `json:"value"`
}

state.Strategies = state.Strategies[:0]
Expand All @@ -138,12 +139,21 @@ func (d *authStrategiesDataSource) Read(ctx context.Context, req datasource.Read

configTmp := map[string]string{}
for _, c := range s.Config {
value := configValue{}
if err := json.Unmarshal([]byte(c.Value), &value); err != nil {
valueObj := configValue{}
if err := json.Unmarshal([]byte(c.Value), &valueObj); err != nil {
resp.Diagnostics.AddError("could not unmarshal json", err.Error())
return
}
configTmp[c.Key] = value.Value
switch value := valueObj.Value.(type) {
case bool:
configTmp[c.Key] = strconv.FormatBool(value)
case json.Number:
configTmp[c.Key] = value.String()
case string:
configTmp[c.Key] = value
default:
configTmp[c.Key] = fmt.Sprintf("%v", value)
}
}

config, diag := types.MapValueFrom(ctx, types.StringType, configTmp)
Expand Down
18 changes: 14 additions & 4 deletions internal/provider/auth_strategies_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"strconv"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator"
Expand Down Expand Up @@ -270,7 +271,7 @@ func (r *authStrategiesResource) Read(ctx context.Context, req resource.ReadRequ
}

type configValue struct {
Value string `json:"value"`
Value any `json:"value"`
}

data.Strategies = data.Strategies[:0]
Expand All @@ -281,12 +282,21 @@ func (r *authStrategiesResource) Read(ctx context.Context, req resource.ReadRequ

configTmp := map[string]string{}
for _, c := range s.Config {
value := configValue{}
if err := json.Unmarshal([]byte(c.Value), &value); err != nil {
valueObj := configValue{}
if err := json.Unmarshal([]byte(c.Value), &valueObj); err != nil {
resp.Diagnostics.AddError("could not unmarshal json", err.Error())
return
}
configTmp[c.Key] = value.Value
switch value := valueObj.Value.(type) {
case bool:
configTmp[c.Key] = strconv.FormatBool(value)
case json.Number:
configTmp[c.Key] = value.String()
case string:
configTmp[c.Key] = value
default:
configTmp[c.Key] = fmt.Sprintf("%v", value)
}
}

config, diag := types.MapValueFrom(ctx, types.StringType, configTmp)
Expand Down

0 comments on commit be36298

Please sign in to comment.