Skip to content

Commit

Permalink
add changes based on upstream import support
Browse files Browse the repository at this point in the history
  • Loading branch information
anGie44 committed Sep 20, 2021
1 parent 7f56ec3 commit f871646
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
6 changes: 3 additions & 3 deletions internal/generic/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ func DesiredStateErrorDiag(source string, err error) diag.Diagnostic {
)
}

func ResourceAttributeNotSetDiag(err error) diag.Diagnostic {
func ResourceAttributeNotSetInImportStateDiag(err error) diag.Diagnostic {
return diag.NewErrorDiagnostic(
"Terraform Resource Attribute Not Set",
fmt.Sprintf("Terraform resource attribute not set in State. This is typically an error with the Terraform provider implementation. Original Error: %s", err.Error()),
"Terraform Resource Attribute Not Set in Import State",
fmt.Sprintf("Terraform resource attribute not set in Import State. This is typically an error with the Terraform provider implementation. Original Error: %s", err.Error()),
)
}

Expand Down
60 changes: 29 additions & 31 deletions internal/generic/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,16 +723,12 @@ func (r *resource) ImportState(ctx context.Context, request tfsdk.ImportResource

tflog.Debug(ctx, "Request.ID", "value", hclog.Fmt("%v", request.ID))

state := &response.State
err := r.setEmptyAttributes(ctx, &response.State)

for name, attr := range state.Schema.Attributes {
err := r.setEmptyAttribute(ctx, attr, name, state)

if err != nil {
response.Diagnostics = append(response.Diagnostics, ResourceAttributeNotSetDiag(err))
if err != nil {
response.Diagnostics = append(response.Diagnostics, ResourceAttributeNotSetInImportStateDiag(err))

return
}
return
}

tfsdk.ResourceImportStatePassthroughID(ctx, idAttributePath, request, response)
Expand Down Expand Up @@ -782,34 +778,36 @@ func (r *resource) setId(ctx context.Context, val string, state *tfsdk.State) er
return nil
}

func (r *resource) setEmptyAttribute(ctx context.Context, attr tfsdk.Attribute, name string, state *tfsdk.State) error {
path := tftypes.NewAttributePath().WithAttributeName(name)
func (r *resource) setEmptyAttributes(ctx context.Context, state *tfsdk.State) error {
for name, attr := range r.resourceType.tfSchema.Attributes {
path := tftypes.NewAttributePath().WithAttributeName(name)

var diags diag.Diagnostics
var diags diag.Diagnostics

if t := attr.Type; t != nil {
if t.TerraformType(ctx).Is(tftypes.String) {
diags = state.SetAttribute(ctx, path, "")
} else if t.TerraformType(ctx).Is(tftypes.Number) {
diags = state.SetAttribute(ctx, path, float64(0))
} else if t.TerraformType(ctx).Is(tftypes.Bool) {
diags = state.SetAttribute(ctx, path, false)
} else if t.TerraformType(ctx).Is(tftypes.Set{}) || t.TerraformType(ctx).Is(tftypes.List{}) || t.TerraformType(ctx).Is(tftypes.Tuple{}) {
if t := attr.Type; t != nil {
if t.TerraformType(ctx).Is(tftypes.String) {
diags = state.SetAttribute(ctx, path, "")
} else if t.TerraformType(ctx).Is(tftypes.Number) {
diags = state.SetAttribute(ctx, path, float64(0))
} else if t.TerraformType(ctx).Is(tftypes.Bool) {
diags = state.SetAttribute(ctx, path, false)
} else if t.TerraformType(ctx).Is(tftypes.Set{}) || t.TerraformType(ctx).Is(tftypes.List{}) || t.TerraformType(ctx).Is(tftypes.Tuple{}) {
diags = state.SetAttribute(ctx, path, []interface{}{})
} else if t.TerraformType(ctx).Is(tftypes.Map{}) || t.TerraformType(ctx).Is(tftypes.Object{}) {
diags = state.SetAttribute(ctx, path, map[string]interface{}{})
}
} else if attr.Attributes != nil { // attribute is a not a "tftype" e.g. providertypes.SetNestedAttributes
diags = state.SetAttribute(ctx, path, []interface{}{})
} else if t.TerraformType(ctx).Is(tftypes.Map{}) || t.TerraformType(ctx).Is(tftypes.Object{}) {
diags = state.SetAttribute(ctx, path, map[string]interface{}{})
} else {
diags.Append(diag.NewErrorDiagnostic(
"Unknown Terraform Type for Attribute",
fmt.Sprintf("unknown terraform type for attribute (%s): %T", name, t),
))
}
} else if attr.Attributes != nil { // attribute is a not a "tftype" e.g. providertypes.SetNestedAttributes
diags = state.SetAttribute(ctx, path, []interface{}{})
} else {
diags.Append(diag.NewErrorDiagnostic(
"Unknown Terraform Type for Attribute",
fmt.Sprintf("unknown terraform type for attribute (%s): %T", name, t),
))
}

if diags.HasError() {
return tfresource.DiagsError(diags)
if diags.HasError() {
return tfresource.DiagsError(diags)
}
}

return nil
Expand Down

0 comments on commit f871646

Please sign in to comment.