Skip to content

Commit

Permalink
feat: remove requirement to use imported resource when apply (#1260)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangndst authored Aug 8, 2024
1 parent 2876035 commit 106ac08
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
21 changes: 21 additions & 0 deletions pkg/engine/runtime/terraform/terraform_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ func (t *Runtime) Apply(ctx context.Context, request *runtime.ApplyRequest) *run

// dry run by terraform plan
if request.DryRun {
// the import resource should be import and return the resource from tfstate
if _, ok := plan.Extensions[tfops.ImportIDKey].(string); ok {
readResponse := t.Read(ctx, &runtime.ReadRequest{
PriorResource: request.PriorResource,
PlanResource: request.PlanResource,
Stack: request.Stack,
})
if readResponse.Status != nil {
return &runtime.ApplyResponse{Resource: &apiv1.Resource{}, Status: readResponse.Status}
}
return &runtime.ApplyResponse{
Resource: &apiv1.Resource{
ID: plan.ID,
Type: plan.Type,
Attributes: readResponse.Resource.Attributes,
DependsOn: plan.DependsOn,
Extensions: plan.Extensions,
},
Status: nil,
}
}
pr, err := ws.Plan(ctx)
if err != nil {
return &runtime.ApplyResponse{Resource: nil, Status: v1.NewErrorStatus(err)}
Expand Down
4 changes: 2 additions & 2 deletions pkg/modules/generators/app_configurations_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,8 @@ func patchImportedResources(resources v1.Resources, projectImportedResources map
for kusionID, importedID := range projectImportedResources {
if res, ok := resIndex[kusionID]; ok {
res.Extensions[tfops.ImportIDKey] = importedID
} else {
return fmt.Errorf("empty kusion id: %s", kusionID)
// remove the resource attribute to avoid update conflict when using terraform import
res.Attributes = make(map[string]interface{})
}
}

Expand Down

0 comments on commit 106ac08

Please sign in to comment.