Skip to content

Commit

Permalink
Fix conversion paths
Browse files Browse the repository at this point in the history
Signed-off-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
  • Loading branch information
ulucinar committed Apr 16, 2024
1 parent 27f39b0 commit 194b70f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,11 +583,12 @@ func (r *Resource) TFListConversionPaths() []string {
// index values are not allowed as this function deals with singleton lists.
func (r *Resource) AddSingletonListConversion(crdPath, tfPath string) {
r.SchemaElementOptions.SetEmbeddedObject(strings.ReplaceAll(
strings.ReplaceAll(crdPath, "[*]", ""), "[0]", ""))
strings.ReplaceAll(tfPath, "[*]", ""), "[0]", ""))
r.listConversionPaths[crdPath] = tfPath
}

// SetEmbeddedObject sets the EmbeddedObject for the specified key.
// The key is a Terraform field path without the wildcard segments.
func (m SchemaElementOptions) SetEmbeddedObject(el string) {
if m[el] == nil {
m[el] = &SchemaElementOption{}
Expand Down
20 changes: 13 additions & 7 deletions pkg/controller/external_tfpluginsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ type terraformPluginSDKExternal struct {
}

func convert(params map[string]any, paths []string, toList bool) (map[string]any, error) {
pv := fieldpath.Pave(params)
pv := fieldpath.Pave(params, fieldpath.WithDisableJSONValidation())

if toList {
slices.Sort(paths)
Expand Down Expand Up @@ -154,10 +154,16 @@ func convert(params map[string]any, paths []string, toList bool) (map[string]any
return nil, errors.Wrapf(err, "cannot set the singleton list's value at the field path %s", exp[0])
}
default:
if s, ok := v.([]any); !ok || len(s) != 1 {
continue
s, ok := v.([]any)
if !ok || len(s) > 1 {
// if len(s) is 0, then it's not a slice
return nil, errors.Errorf("singleton list, at the field path %s, must have a length of 1 but it has a length of %d", exp[0], len(s))
}
if err := pv.SetValue(exp[0], v.([]any)[0]); err != nil {
var newVal any = map[string]any{}
if len(s) > 0 {
newVal = s[0]
}
if err := pv.SetValue(exp[0], newVal); err != nil {
return nil, errors.Wrapf(err, "cannot set the embedded object's value at the field path %s", exp[0])
}
}
Expand Down Expand Up @@ -201,7 +207,7 @@ func getExtendedParameters(ctx context.Context, tr resource.Terraformed, externa
params["tags_all"] = params["tags"]
}
}
return convert(params, config.CRDListConversionPaths(), true)
return convert(params, config.TFListConversionPaths(), true)
}

func (c *TerraformPluginSDKConnector) processParamsWithHCLParser(schemaMap map[string]*schema.Schema, params map[string]any) map[string]any {
Expand Down Expand Up @@ -301,7 +307,7 @@ func (c *TerraformPluginSDKConnector) Connect(ctx context.Context, mg xpresource
if err != nil {
return nil, errors.Wrap(err, "failed to get the observation")
}
tfState, err = convert(tfState, c.config.CRDListConversionPaths(), true)
tfState, err = convert(tfState, c.config.TFListConversionPaths(), true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -677,7 +683,7 @@ func (n *terraformPluginSDKExternal) Create(ctx context.Context, mg xpresource.M
if _, err := n.setExternalName(mg, stateValueMap); err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, "failed to set the external-name of the managed resource during create")
}
stateValueMap, err = convert(stateValueMap, n.config.CRDListConversionPaths(), false)
stateValueMap, err = convert(stateValueMap, n.config.TFListConversionPaths(), false)
if err != nil {
return managed.ExternalCreation{}, err
}
Expand Down

0 comments on commit 194b70f

Please sign in to comment.