Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: project delete function uses correct string representation of id #1733

Merged
merged 5 commits into from
Dec 11, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 13 additions & 18 deletions internal/service/project/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (r *projectRS) Create(ctx context.Context, req resource.CreateRequest, resp

_, _, err := connV2.TeamsApi.AddAllTeamsToProject(ctx, project.GetId(), NewTeamRoleList(ctx, teams)).Execute()
if err != nil {
errd := deleteProject(ctx, r.Client.AtlasV2, project.Id)
errd := deleteProject(ctx, r.Client.AtlasV2, project.GetId())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we have a test that fails before this change and passes after?

Copy link
Collaborator Author

@oarbusi oarbusi Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we now have. The call we mock in the test is only mocked with correct projectId, so in case the string has extra quotes, tests fail. See image
Screenshot 2023-12-11 at 11 14 26

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great, thx!

if errd != nil {
resp.Diagnostics.AddError("error during project deletion when adding teams", fmt.Sprintf(errorProjectDelete, project.GetId(), err.Error()))
return
Expand All @@ -290,7 +290,7 @@ func (r *projectRS) Create(ctx context.Context, req resource.CreateRequest, resp
}
_, _, err := connV2.ProjectsApi.SetProjectLimit(ctx, limit.Name.ValueString(), project.GetId(), dataFederationLimit).Execute()
if err != nil {
errd := deleteProject(ctx, r.Client.AtlasV2, project.Id)
errd := deleteProject(ctx, r.Client.AtlasV2, project.GetId())
if errd != nil {
resp.Diagnostics.AddError("error during project deletion when adding limits", fmt.Sprintf(errorProjectDelete, project.GetId(), err.Error()))
return
Expand All @@ -304,7 +304,7 @@ func (r *projectRS) Create(ctx context.Context, req resource.CreateRequest, resp
// add settings
projectSettings, _, err := connV2.ProjectsApi.GetProjectSettings(ctx, *project.Id).Execute()
if err != nil {
errd := deleteProject(ctx, r.Client.AtlasV2, project.Id)
errd := deleteProject(ctx, r.Client.AtlasV2, project.GetId())
if errd != nil {
resp.Diagnostics.AddError("error during project deletion when getting project settings", fmt.Sprintf(errorProjectDelete, project.GetId(), err.Error()))
return
Expand Down Expand Up @@ -333,7 +333,7 @@ func (r *projectRS) Create(ctx context.Context, req resource.CreateRequest, resp
}

if _, _, err = connV2.ProjectsApi.UpdateProjectSettings(ctx, project.GetId(), projectSettings).Execute(); err != nil {
errd := deleteProject(ctx, r.Client.AtlasV2, project.Id)
errd := deleteProject(ctx, r.Client.AtlasV2, project.GetId())
if errd != nil {
resp.Diagnostics.AddError("error during project deletion when updating project settings", fmt.Sprintf(errorProjectDelete, project.GetId(), err.Error()))
return
Expand Down Expand Up @@ -494,7 +494,7 @@ func (r *projectRS) Delete(ctx context.Context, req resource.DeleteRequest, resp
}

projectID := project.ID.ValueString()
err := deleteProject(ctx, r.Client.AtlasV2, &projectID)
err := deleteProject(ctx, r.Client.AtlasV2, projectID)

if err != nil {
resp.Diagnostics.AddError("error when destroying resource", fmt.Sprintf(errorProjectDelete, projectID, err.Error()))
Expand Down Expand Up @@ -725,7 +725,7 @@ func UpdateProject(ctx context.Context, client GroupProjectService, projectState
return nil
}

func deleteProject(ctx context.Context, connV2 *admin.APIClient, projectID *string) error {
func deleteProject(ctx context.Context, connV2 *admin.APIClient, projectID string) error {
stateConf := &retry.StateChangeConf{
Pending: []string{projectDependentsStateDeleting, projectDependentsStateRetry},
Target: []string{projectDependentsStateIdle},
Expand All @@ -738,10 +738,10 @@ func deleteProject(ctx context.Context, connV2 *admin.APIClient, projectID *stri
_, err := stateConf.WaitForStateContext(ctx)

if err != nil {
tflog.Info(ctx, fmt.Sprintf("[ERROR] could not determine MongoDB project %s dependents status: %s", *projectID, err.Error()))
tflog.Info(ctx, fmt.Sprintf("[ERROR] could not determine MongoDB project %s dependents status: %s", projectID, err.Error()))
}

_, _, err = connV2.ProjectsApi.DeleteProject(ctx, *projectID).Execute()
_, _, err = connV2.ProjectsApi.DeleteProject(ctx, projectID).Execute()

return err
}
Expand All @@ -755,18 +755,13 @@ Else consider the aggregate dependents idle.
If we get a defined error response, return that right away
Else retry
*/
func resourceProjectDependentsDeletingRefreshFunc(ctx context.Context, projectID *string, connV2 *admin.APIClient) retry.StateRefreshFunc {
func resourceProjectDependentsDeletingRefreshFunc(ctx context.Context, projectID string, connV2 *admin.APIClient) retry.StateRefreshFunc {
return func() (any, string, error) {
nonNullProjectID := conversion.StringPtrNullIfEmpty(projectID)
clusters, _, err := connV2.ClustersApi.ListClusters(ctx, nonNullProjectID.String()).Execute()
clusters, _, listClustersErr := connV2.ClustersApi.ListClusters(ctx, projectID).Execute()
dependents := AtlasProjectDependants{AdvancedClusters: clusters}

if _, ok := admin.AsError(err); ok {
return nil, "", err
}

if err != nil {
return nil, projectDependentsStateRetry, nil
if listClustersErr != nil {
return nil, "", listClustersErr
}

if *dependents.AdvancedClusters.TotalCount == 0 {
Expand All @@ -779,7 +774,7 @@ func resourceProjectDependentsDeletingRefreshFunc(ctx context.Context, projectID
}
}

log.Printf("[DEBUG] status for MongoDB project %s dependents: %s", nonNullProjectID, projectDependentsStateDeleting)
log.Printf("[DEBUG] status for MongoDB project %s dependents: %s", projectID, projectDependentsStateDeleting)

return dependents, projectDependentsStateDeleting, nil
}
Expand Down
Loading