Skip to content

Commit

Permalink
tfclient import raise error if resource read returns nil
Browse files Browse the repository at this point in the history
  • Loading branch information
magodo committed Sep 4, 2024
1 parent 4f99968 commit 1d2038a
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/meta/base_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,7 @@ func (meta *baseMeta) importItem_notf(ctx context.Context, item *ImportItem, imp
return
}
res := importResp.ImportedResources[0]

readResp, diags := meta.tfclient.ReadResource(ctx, typ.ReadResourceRequest{
TypeName: res.TypeName,
PriorState: res.State,
Expand All @@ -938,6 +939,15 @@ func (meta *baseMeta) importItem_notf(ctx context.Context, item *ImportItem, imp
return
}

// Ensure the state is not null
if readResp.NewState.IsNull() {
meta.Logger().Error("Cannot import an non-existent resource", "tf_addr", item.TFAddr)
meta.tc.Trace(telemetry.Error, fmt.Sprintf("Cannot import an non-existent resource: %s", item.AzureResourceID.TypeString()))
item.ImportError = fmt.Errorf("Cannot import non-existent remote object")
item.Imported = false
return
}

meta.Logger().Debug("Finish importing a resource", "tf_id", item.TFResourceId, "tf_addr", addr)
item.State = readResp.NewState
item.ImportError = nil
Expand Down

0 comments on commit 1d2038a

Please sign in to comment.