mirror of https://github.com/crossplane/upjet.git
Compare commits
18 Commits
Author | SHA1 | Date |
---|---|---|
|
e744bf2856 | |
|
36a16b298c | |
|
335c06bceb | |
|
cfaa5f21c7 | |
|
1c8b166eb1 | |
|
12b954b958 | |
|
0f4f8ce4d7 | |
|
8f51d53339 | |
|
023d327351 | |
|
1b22cf08a9 | |
|
bd41429c40 | |
|
34de92ddae | |
|
ad2246ccc6 | |
|
6b30d2f338 | |
|
c305e4e517 | |
|
3342cad333 | |
|
cf9c9ad1ba | |
|
874266deb9 |
|
@ -124,10 +124,18 @@ func MoveToStatus(sch *schema.Resource, fieldpaths ...string) {
|
|||
}
|
||||
}
|
||||
|
||||
// MarkAsRequired marks the given fieldpaths as required without manipulating
|
||||
// the native field schema.
|
||||
func (r *Resource) MarkAsRequired(fieldpaths ...string) {
|
||||
r.requiredFields = append(r.requiredFields, fieldpaths...)
|
||||
}
|
||||
|
||||
// MarkAsRequired marks the schema of the given fieldpath as required. It's most
|
||||
// useful in cases where external name contains an optional parameter that is
|
||||
// defaulted by the provider but we need it to exist or to fix plain buggy
|
||||
// schemas.
|
||||
// Deprecated: Use Resource.MarkAsRequired instead.
|
||||
// This function will be removed in future versions.
|
||||
func MarkAsRequired(sch *schema.Resource, fieldpaths ...string) {
|
||||
for _, fieldpath := range fieldpaths {
|
||||
if s := GetSchema(sch, fieldpath); s != nil {
|
||||
|
|
|
@ -128,6 +128,7 @@ func TestDefaultResource(t *testing.T) {
|
|||
cmpopts.IgnoreFields(ExternalName{}, "SetIdentifierArgumentFn", "GetExternalNameFn", "GetIDFn"),
|
||||
cmpopts.IgnoreFields(Resource{}, "useTerraformPluginSDKClient"),
|
||||
cmpopts.IgnoreFields(Resource{}, "useTerraformPluginFrameworkClient"),
|
||||
cmpopts.IgnoreFields(Resource{}, "requiredFields"),
|
||||
}
|
||||
|
||||
for name, tc := range cases {
|
||||
|
|
|
@ -328,12 +328,9 @@ func NewProvider(schema []byte, prefix string, modulePath string, metadata []byt
|
|||
}
|
||||
terraformResource = p.TerraformProvider.ResourcesMap[name]
|
||||
if terraformResource.Schema == nil {
|
||||
if terraformResource.SchemaFunc == nil {
|
||||
p.skippedResourceNames = append(p.skippedResourceNames, name)
|
||||
fmt.Printf("Skipping resource %s because it has no schema and no schema function\n", name)
|
||||
continue
|
||||
}
|
||||
terraformResource.Schema = terraformResource.SchemaFunc()
|
||||
p.skippedResourceNames = append(p.skippedResourceNames, name)
|
||||
fmt.Printf("Skipping resource %s because it has no schema\n", name)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -489,6 +489,15 @@ type Resource struct {
|
|||
// the value of the generated Kind, for example:
|
||||
// "TagParameters": "ClusterTagParameters"
|
||||
OverrideFieldNames map[string]string
|
||||
|
||||
// requiredFields are the fields that will be marked as required in the
|
||||
// generated CRD schema, although they are not required in the TF schema.
|
||||
requiredFields []string
|
||||
}
|
||||
|
||||
// RequiredFields returns slice of the marked as required fieldpaths.
|
||||
func (r *Resource) RequiredFields() []string {
|
||||
return r.requiredFields
|
||||
}
|
||||
|
||||
// ShouldUseTerraformPluginSDKClient returns whether to generate an SDKv2-based
|
||||
|
|
|
@ -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,11 +172,11 @@ 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
|
||||
}
|
||||
switch sc.Type {
|
||||
switch sc.Type { //nolint:exhaustive
|
||||
case schema.TypeMap:
|
||||
if sc.Elem == 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,6 @@ 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:
|
||||
return param
|
||||
default:
|
||||
return param
|
||||
|
@ -252,7 +242,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)
|
||||
|
|
|
@ -1,7 +1,3 @@
|
|||
// SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
{{ .Header }}
|
||||
|
||||
{{ .GenStatement }}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
|
||||
|
||||
SPDX-License-Identifier: Apache-2.0
|
|
@ -1,7 +1,3 @@
|
|||
// SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
{{ .Header }}
|
||||
|
||||
{{ .GenStatement }}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
|
||||
|
||||
SPDX-License-Identifier: Apache-2.0
|
|
@ -1,7 +1,3 @@
|
|||
// SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
{{ .Header }}
|
||||
|
||||
{{ .GenStatement }}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
|
||||
|
||||
SPDX-License-Identifier: Apache-2.0
|
|
@ -1,7 +1,3 @@
|
|||
// SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
{{ .Header }}
|
||||
|
||||
{{ .GenStatement }}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
|
||||
|
||||
SPDX-License-Identifier: Apache-2.0
|
|
@ -1,7 +1,3 @@
|
|||
// SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
{{ .Header }}
|
||||
|
||||
{{ .GenStatement }}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
|
||||
|
||||
SPDX-License-Identifier: Apache-2.0
|
|
@ -1,7 +1,3 @@
|
|||
// SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
{{ .Header }}
|
||||
|
||||
{{ .GenStatement }}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
|
||||
|
||||
SPDX-License-Identifier: Apache-2.0
|
|
@ -1,6 +1,4 @@
|
|||
// SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
{{ .Header }}
|
||||
|
||||
package controller
|
||||
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
|
||||
|
||||
SPDX-License-Identifier: Apache-2.0
|
|
@ -1,7 +1,3 @@
|
|||
// SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
{{ .Header }}
|
||||
|
||||
{{ .GenStatement }}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
|
||||
|
||||
SPDX-License-Identifier: Apache-2.0
|
|
@ -380,7 +380,7 @@ func newTopLevelRequiredParam(path string, includeInit bool) *topLevelRequiredPa
|
|||
}
|
||||
|
||||
func (r *resource) addParameterField(f *Field, field *types.Var) {
|
||||
requiredBySchema := !f.Schema.Optional
|
||||
requiredBySchema := !f.Schema.Optional || f.Required
|
||||
// Note(turkenh): We are collecting the top level required parameters that
|
||||
// are not identifier fields. This is for generating CEL validation rules for
|
||||
// those parameters and not to require them if the management policy is set
|
||||
|
|
|
@ -45,6 +45,7 @@ type Field struct {
|
|||
TransformedName string
|
||||
SelectorName string
|
||||
Identifier bool
|
||||
Required bool
|
||||
// Injected is set if this Field is an injected field to the Terraform
|
||||
// schema as an object list map key for server-side apply merges.
|
||||
Injected bool
|
||||
|
@ -120,6 +121,12 @@ func NewField(g *Builder, cfg *config.Resource, r *resource, sch *schema.Schema,
|
|||
}
|
||||
}
|
||||
|
||||
for _, required := range cfg.RequiredFields() {
|
||||
if required == snakeFieldName {
|
||||
f.Required = true
|
||||
}
|
||||
}
|
||||
|
||||
var commentText string
|
||||
docString := getDocString(cfg, f, tfPath)
|
||||
if len(docString) > 0 {
|
||||
|
@ -268,7 +275,13 @@ func NewSensitiveField(g *Builder, cfg *config.Resource, r *resource, sch *schem
|
|||
return nil, true, nil
|
||||
}
|
||||
sfx := "SecretRef"
|
||||
cfg.Sensitive.AddFieldPath(fieldPathWithWildcard(f.TerraformPaths), "spec.forProvider."+fieldPathWithWildcard(f.CRDPaths)+sfx)
|
||||
switch f.FieldType.(type) {
|
||||
case *types.Slice:
|
||||
f.CRDPaths[len(f.CRDPaths)-2] = f.CRDPaths[len(f.CRDPaths)-2] + sfx
|
||||
cfg.Sensitive.AddFieldPath(fieldPathWithWildcard(f.TerraformPaths), "spec.forProvider."+fieldPathWithWildcard(f.CRDPaths))
|
||||
default:
|
||||
cfg.Sensitive.AddFieldPath(fieldPathWithWildcard(f.TerraformPaths), "spec.forProvider."+fieldPathWithWildcard(f.CRDPaths)+sfx)
|
||||
}
|
||||
// todo(turkenh): do we need to support other field types as sensitive?
|
||||
if f.FieldType.String() != "string" && f.FieldType.String() != "*string" && f.FieldType.String() != "[]string" &&
|
||||
f.FieldType.String() != "[]*string" && f.FieldType.String() != "map[string]string" && f.FieldType.String() != "map[string]*string" {
|
||||
|
|
Loading…
Reference in New Issue