Skip to content

Commit

Permalink
- Move the config.ExternalName.RequiredFields to config.Resource.requ…
Browse files Browse the repository at this point in the history
…iredFields

- Deprecate config.MarkAsRequired in favor of a new configuration function on *config.Resource that still accepts a slice to mark multiple fields as required without doing and invervention in native field schema.

Signed-off-by: Sergen Yalçın <yalcinsergen97@gmail.com>
  • Loading branch information
sergenyalcin committed Mar 19, 2024
1 parent bdfbe67 commit f25329f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
9 changes: 8 additions & 1 deletion pkg/config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +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 RequiredFields API instead.
// 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
12 changes: 8 additions & 4 deletions pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ type ExternalName struct {
// management policy is including the Observe Only, different from other
// (required) fields.
IdentifierFields []string

// RequiredFields are the fields that are marked as required, although
// it is not required in the TF schema.
RequiredFields []string
}

// References represents reference resolver configurations for the fields of a
Expand Down Expand Up @@ -493,6 +489,14 @@ 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
}

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/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func NewField(g *Builder, cfg *config.Resource, r *resource, sch *schema.Schema,
}
}

for _, required := range cfg.ExternalName.RequiredFields {
for _, required := range cfg.RequiredFields() {
if required == snakeFieldName {
f.Required = true
}
Expand Down

0 comments on commit f25329f

Please sign in to comment.