Skip to content

Commit

Permalink
Merge pull request #382 from sergenyalcin/backport-381-to-release-1.1
Browse files Browse the repository at this point in the history
[Backport release-1.1] Add a new configuration option for required field generation
  • Loading branch information
sergenyalcin authored Mar 19, 2024
2 parents 12b954b + 36a16b2 commit e744bf2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pkg/config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/config/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/types/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions pkg/types/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit e744bf2

Please sign in to comment.