Skip to content

Commit

Permalink
Add a new `Required configuration option
Browse files Browse the repository at this point in the history
Signed-off-by: Sergen Yalçın <yalcinsergen97@gmail.com>
  • Loading branch information
sergenyalcin committed Mar 18, 2024
1 parent 2ef7077 commit bdfbe67
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func MoveToStatus(sch *schema.Resource, fieldpaths ...string) {
// 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.
func MarkAsRequired(sch *schema.Resource, fieldpaths ...string) {
for _, fieldpath := range fieldpaths {
if s := GetSchema(sch, fieldpath); s != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ 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
3 changes: 3 additions & 0 deletions pkg/types/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,9 @@ func newTopLevelRequiredParam(path string, includeInit bool) *topLevelRequiredPa

func (r *resource) addParameterField(f *Field, field *types.Var) {
requiredBySchema := !f.Schema.Optional
if f.Required {
requiredBySchema = 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.ExternalName.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 bdfbe67

Please sign in to comment.