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: Avoids error when removing project api key assignment for deleted project #1641

Merged
merged 1 commit into from
Nov 21, 2023

Conversation

AgustinBettati
Copy link
Member

Description

Link to any related issue(s): INTMDB-1297

Enables deleting a project and is project_assignment in project_api_key resource without encountering errors. This is only one part of HELP-52313.

Type of change:

  • Bug fix (non-breaking change which fixes an issue). Please, add the "bug" label to the PR.
  • New feature (non-breaking change which adds functionality). Please, add the "enhancement" label to the PR.
  • Breaking change (fix or feature that would cause existing functionality to not work as expected). Please, add the "breaking change" label to the PR.
  • This change requires a documentation update
  • Documentation fix/enhancement

Required Checklist:

  • I have signed the MongoDB CLA
  • I have read the contribution guidelines
  • I have checked that this change does not generate any credentials and that they are NOT accidentally logged anywhere.
  • I have added tests that prove my fix is effective or that my feature works per HashiCorp requirements
  • I have added any necessary documentation (if appropriate)
  • I have run make fmt and formatted my code
  • If changes include deprecations or removals, I defined an isolated PR with a relevant title as it will be used in the auto-generated changelog.

Further comments

@AgustinBettati AgustinBettati requested a review from a team as a code owner November 20, 2023 09:56
@github-actions github-actions bot added the bug label Nov 20, 2023
@@ -396,30 +399,30 @@ func newProjectAssignment(ctx context.Context, conn *matlas.Client, apiKeyID str
return results, nil
}

func getStateProjectAssignmentAPIKeys(d *schema.ResourceData) (newAPIKeys, changedAPIKeys, removedAPIKeys []any) {
Copy link
Member Author

Choose a reason for hiding this comment

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

changes in this function are only renaming variables

Copy link
Contributor

Code Coverage

Package Line Rate Health
github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas 2%
github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/framework/validator 68%
github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/testutils 46%
github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/util 17%
Summary 3% (284 / 10543)

projectID := apiKey.(map[string]any)["project_id"].(string)
_, err := conn.ProjectAPIKeys.Unassign(ctx, projectID, apiKeyID)
if err != nil && strings.Contains(err.Error(), "GROUP_NOT_FOUND") {
continue // allows removing assignment for a project that has been deleted
Copy link
Member

Choose a reason for hiding this comment

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

i would normally avoid continue in this situation, although your solution is probably more clear than:

if err != nil {
if !strings.Contains(err.Error(), "GROUP_NOT_FOUND") { // allows removing assignment for a project that has been deleted
return diag.Errorf("error removing api_key(%s) from the project(%s): %s", apiKeyID, projectID, err)
}
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, went for this approach to make this case explicit and easy to read

Copy link
Member

@lantoli lantoli left a comment

Choose a reason for hiding this comment

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

knitpit for changelog in PR title:

fix: avoid error when removing project api key... =>

fix: Avoids error when removing project api key...

@AgustinBettati AgustinBettati changed the title fix: avoid error when removing project api key assignment for deleted project fix: Avoids error when removing project api key assignment for deleted project Nov 21, 2023
@AgustinBettati AgustinBettati merged commit 58e939d into master Nov 21, 2023
@AgustinBettati AgustinBettati deleted the INTMDB-1297 branch November 21, 2023 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants