Skip to content

Commit

Permalink
Set DefaultValue for models, change GetModel signature,
Browse files Browse the repository at this point in the history
  • Loading branch information
yogeshlonkar committed Jun 22, 2023
1 parent 46016fc commit 9a13dd7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion extensionimpl/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (a *attribute) DefaultValue() string {
}

func (a *attribute) NeedsDefaultValue() bool {
return !a.IsPointer() && a.Optional()
return !a.IsPointer() && !a.TypeValue().IsList() && !a.TypeValue().IsMap() && !a.TypeValue().IsNestedSingleObject()
}

func (a *attribute) Computed() bool {
Expand Down
3 changes: 2 additions & 1 deletion gocode/tmpls/data_source.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ func (d *{{ .Block.GoName }}) ValidateConfig(ctx context.Context, req datasource
}

func (d *{{ .Block.GoName }}) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
data, diagnostics := gotf.GetModel[{{ .Block.ModelGoName }}](ctx, req.Config.Raw, req.Config.Get)
data := &{{ .Block.ModelGoName }}{}
diagnostics := gotf.GetModel(ctx, req.Config.Raw, data, req.Config.Get)
if diagnostics.HasError() {
resp.Diagnostics.Append(diagnostics...)
return
Expand Down
2 changes: 1 addition & 1 deletion gocode/tmpls/proto_tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (x *{{ .Message.GoIdent.GoName }}) ResourceSchema() map[string]rschema.Attr
}
{{- end }}

func (x {{ .Message.GoIdent.GoName }}) AttributeDefaultValues(ctx context.Context) map[string]tftypes.Value {
func (x *{{ .Message.GoIdent.GoName }}) AttributeDefaultValues(ctx context.Context) map[string]tftypes.Value {
attributes := map[string]tftypes.Value{
{{- range .Attributes }}
{{- if .NeedsDefaultValue }}
Expand Down
3 changes: 2 additions & 1 deletion gocode/tmpls/provider.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ func (p *{{ .Provider.GoName }}Provider) Configure(ctx context.Context, req prov
return
}
{{- if .Provider.HasServiceClient }}
data, diagnostics := gotf.GetModel[{{ .Provider.ModelGoName }}](ctx, req.Config.Raw, req.Config.Get)
data := &{{ .Provider.ModelGoName }}{}
diagnostics := gotf.GetModel(ctx, req.Config.Raw, data, req.Config.Get)
if diagnostics.HasError() {
resp.Diagnostics.Append(diagnostics...)
return
Expand Down
12 changes: 8 additions & 4 deletions gocode/tmpls/resource.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ func (r *{{ .Block.GoName }}Resource) UpgradeState(ctx context.Context) map[int6
}

func (r *{{ .Block.GoName }}Resource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
data, diagnostics := gotf.GetModel[{{ .Block.ModelGoName }}](ctx, req.State.Raw, req.State.Get)
data := &{{ .Block.ModelGoName }}{}
diagnostics := gotf.GetModel(ctx, req.State.Raw, data, req.State.Get)
if diagnostics.HasError() {
resp.Diagnostics.Append(diagnostics...)
return
Expand All @@ -120,7 +121,8 @@ func (r *{{ .Block.GoName }}Resource) Read(ctx context.Context, req resource.Rea
}

func (r *{{ .Block.GoName }}Resource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
data, diagnostics := gotf.GetModel[{{ .Block.ModelGoName }}](ctx, req.Config.Raw, req.Config.Get)
data := &{{ .Block.ModelGoName }}{}
diagnostics := gotf.GetModel(ctx, req.Config.Raw, data, req.Config.Get)
if diagnostics.HasError() {
resp.Diagnostics.Append(diagnostics...)
return
Expand All @@ -135,7 +137,8 @@ func (r *{{ .Block.GoName }}Resource) Create(ctx context.Context, req resource.C
}

func (r *{{ .Block.GoName }}Resource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
data, diagnostics := gotf.GetModel[{{ .Block.ModelGoName }}](ctx, req.Config.Raw, req.Config.Get)
data := &{{ .Block.ModelGoName }}{}
diagnostics := gotf.GetModel(ctx, req.Config.Raw, data, req.Config.Get)
if diagnostics.HasError() {
resp.Diagnostics.Append(diagnostics...)
return
Expand All @@ -150,7 +153,8 @@ func (r *{{ .Block.GoName }}Resource) Update(ctx context.Context, req resource.U
}

func (r *{{ .Block.GoName }}Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
data, diagnostics := gotf.GetModel[{{ .Block.ModelGoName }}](ctx, req.State.Raw, req.State.Get)
data := &{{ .Block.ModelGoName }}{}
diagnostics := gotf.GetModel(ctx, req.State.Raw, data, req.State.Get)
if diagnostics.HasError() {
resp.Diagnostics.Append(diagnostics...)
return
Expand Down

0 comments on commit 9a13dd7

Please sign in to comment.