From 93af08a988e770445486fe66a74a8bfd112d32dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergen=20Yal=C3=A7=C4=B1n?= Date: Fri, 1 Mar 2024 11:29:59 +0300 Subject: [PATCH 1/2] Remove StateFunc calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sergen Yalçın --- pkg/controller/external_tfpluginsdk.go | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/pkg/controller/external_tfpluginsdk.go b/pkg/controller/external_tfpluginsdk.go index a98543a6..9d33e111 100644 --- a/pkg/controller/external_tfpluginsdk.go +++ b/pkg/controller/external_tfpluginsdk.go @@ -158,13 +158,13 @@ func getExtendedParameters(ctx context.Context, tr resource.Terraformed, externa return params, nil } -func (c *TerraformPluginSDKConnector) processParamsWithStateFunc(schemaMap map[string]*schema.Schema, params map[string]any) map[string]any { +func (c *TerraformPluginSDKConnector) processParamsWithHCLParser(schemaMap map[string]*schema.Schema, params map[string]any) map[string]any { if params == nil { return params } for key, param := range params { if sc, ok := schemaMap[key]; ok { - params[key] = c.applyStateFuncToParam(sc, param) + params[key] = c.applyHCLParserToParam(sc, param) } else { params[key] = param } @@ -172,7 +172,7 @@ func (c *TerraformPluginSDKConnector) processParamsWithStateFunc(schemaMap map[s return params } -func (c *TerraformPluginSDKConnector) applyStateFuncToParam(sc *schema.Schema, param any) any { //nolint:gocyclo +func (c *TerraformPluginSDKConnector) applyHCLParserToParam(sc *schema.Schema, param any) any { //nolint:gocyclo if param == nil { return param } @@ -185,7 +185,7 @@ func (c *TerraformPluginSDKConnector) applyStateFuncToParam(sc *schema.Schema, p // TypeMap only supports schema in Elem if mapSchema, ok := sc.Elem.(*schema.Schema); ok && okParam { for pk, pv := range pmap { - pmap[pk] = c.applyStateFuncToParam(mapSchema, pv) + pmap[pk] = c.applyHCLParserToParam(mapSchema, pv) } return pmap } @@ -196,13 +196,13 @@ func (c *TerraformPluginSDKConnector) applyStateFuncToParam(sc *schema.Schema, p pArray, okParam := param.([]any) if setSchema, ok := sc.Elem.(*schema.Schema); ok && okParam { for i, p := range pArray { - pArray[i] = c.applyStateFuncToParam(setSchema, p) + pArray[i] = c.applyHCLParserToParam(setSchema, p) } return pArray } else if setResource, ok := sc.Elem.(*schema.Resource); ok { for i, p := range pArray { if resParam, okRParam := p.(map[string]any); okRParam { - pArray[i] = c.processParamsWithStateFunc(setResource.Schema, resParam) + pArray[i] = c.processParamsWithHCLParser(setResource.Schema, resParam) } } } @@ -216,16 +216,8 @@ func (c *TerraformPluginSDKConnector) applyStateFuncToParam(sc *schema.Schema, p param = hclProccessedParam } } - if sc.StateFunc != nil { - return sc.StateFunc(param) - } - return param - case schema.TypeBool, schema.TypeInt, schema.TypeFloat: - if sc.StateFunc != nil { - return sc.StateFunc(param) - } return param - case schema.TypeInvalid: + case schema.TypeBool, schema.TypeInt, schema.TypeFloat, schema.TypeInvalid: return param default: return param @@ -252,7 +244,7 @@ func (c *TerraformPluginSDKConnector) Connect(ctx context.Context, mg xpresource if err != nil { return nil, errors.Wrapf(err, "failed to get the extended parameters for resource %q", mg.GetName()) } - params = c.processParamsWithStateFunc(c.config.TerraformResource.Schema, params) + params = c.processParamsWithHCLParser(c.config.TerraformResource.Schema, params) schemaBlock := c.config.TerraformResource.CoreConfigSchema() rawConfig, err := schema.JSONMapToStateValue(params, schemaBlock) From 956c7d489b4644feef8ba2faecd5376b5a9b5138 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergen=20Yal=C3=A7=C4=B1n?= Date: Tue, 5 Mar 2024 21:06:55 +0300 Subject: [PATCH 2/2] Remove the unnecessary case and add nolint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sergen Yalçın --- pkg/controller/external_tfpluginsdk.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/controller/external_tfpluginsdk.go b/pkg/controller/external_tfpluginsdk.go index 9d33e111..ff37ec76 100644 --- a/pkg/controller/external_tfpluginsdk.go +++ b/pkg/controller/external_tfpluginsdk.go @@ -176,7 +176,7 @@ func (c *TerraformPluginSDKConnector) applyHCLParserToParam(sc *schema.Schema, p if param == nil { return param } - switch sc.Type { + switch sc.Type { //nolint:exhaustive case schema.TypeMap: if sc.Elem == nil { return param @@ -217,8 +217,6 @@ func (c *TerraformPluginSDKConnector) applyHCLParserToParam(sc *schema.Schema, p } } return param - case schema.TypeBool, schema.TypeInt, schema.TypeFloat, schema.TypeInvalid: - return param default: return param }