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

Sc 10284/update handler #73

Merged
merged 20 commits into from Dec 14, 2022
Merged

Sc 10284/update handler #73

merged 20 commits into from Dec 14, 2022

Conversation

daniellemaxwell
Copy link
Contributor

@daniellemaxwell daniellemaxwell commented Dec 9, 2022

Scope of changes

Adds TenantUpdate server-side handler that will update tenant records in the Tenant API along with corresponding tests.

Fixes SC-10284

Type of change

  • new feature
  • bug fix
  • documentation
  • testing
  • technical debt
  • other (describe)

Acceptance criteria

Describe how reviewers can test this change to be sure that it works correctly. Add a checklist if possible.

Author checklist

  • I have manually tested the change and/or added automation in the form of unit tests or integration tests
  • I have updated the dependencies list
  • I have recompiled and included new protocol buffers to reflect changes I made
  • I have added new test fixtures as needed to support added tests
  • Check this box if a reviewer can merge this pull request after approval (leave it unchecked if you want to do it yourself)
  • I have moved the associated Shortcut story to "Ready for Review"

Reviewer(s) checklist

  • Any new user-facing content that has been added for this PR has been QA'ed to ensure correct grammar, spelling, and understandability.

@shortcut-integration
Copy link

This pull request has been linked to Shortcut Story #10284: Implement Tenant Resource Update Handlers.

Copy link
Contributor

@pdeziel pdeziel left a comment

Choose a reason for hiding this comment

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

I had one suggestion to make sure we're not overwriting data on the tenant objects in the database but other than that it's looking good! Thanks for continuing to add the tests as we create the endpoints!

}

// Get the tenant ID from the URL and return a 400 if the tenant
// does not exist.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// does not exist.
// ID is not a ULID

ID: tenantID,
Name: tenant.Name,
EnvironmentType: tenant.EnvironmentType,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The pattern we've mostly used in GDS for Update handlers is to first try to retrieve the original record, then update the record that was retrieved and store it back to the database.

I think this would go a long way to make this endpoint more resilient to future changes, since it prevents us from accidentally overwriting internal data on the tenant in the database. We may also have metadata fields like created_at, modified_at timestamps that we don't want to override. This would also allow us to handle the "tenant not found" and "error calling the database" errors separately to give the caller more information about what went wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pdeziel I made an update to retrieve the tenant from the db once the tenant ID is parsed from the URL. However, if the change isn't in line with what you had in mind please let me know.

Copy link
Contributor

Choose a reason for hiding this comment

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

Great, now that we are retrieving the tenant from the db can we use that instead of the req (so modifying the model directly) when storing to the database?

Also, I think it's probably better to put the db.RetrieveTenant after the 400 checks, so that we don't have to make a request to the database unless the request is formatted correctly (this will save a bit of time on the error paths).

suite.requireError(err, http.StatusBadRequest, "tenant environment type is required", "expected error when tenant environent type does not exist")

req := &api.Tenant{
ID: "01ARZ3NDEKTSV4RRFFQ69G5FAV",
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we change this to a different ID so that we're testing that the ID in the request does not override the ID on the tenant object?

Copy link
Contributor

@pdeziel pdeziel left a comment

Choose a reason for hiding this comment

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

Thanks, I had a few more suggestions to shore up the endpoint but after that looks good!

ID: tenantID,
Name: tenant.Name,
EnvironmentType: tenant.EnvironmentType,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Great, now that we are retrieving the tenant from the db can we use that instead of the req (so modifying the model directly) when storing to the database?

Also, I think it's probably better to put the db.RetrieveTenant after the 400 checks, so that we don't have to make a request to the database unless the request is formatted correctly (this will save a bit of time on the error paths).

// tenant record cannot be updated.
if err := db.UpdateTenant(c.Request.Context(), req); err != nil {
log.Error().Err(err).Msg("could not save tenant")
c.JSON(http.StatusNotFound, api.ErrorResponse("could not update tenant"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
c.JSON(http.StatusNotFound, api.ErrorResponse("could not update tenant"))
c.JSON(http.StatusInternalServerError, api.ErrorResponse("could not update tenant"))

Copy link
Contributor

@pdeziel pdeziel Dec 14, 2022

Choose a reason for hiding this comment

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

At this point we have already verified that the tenant exists so if we get an error here then it's probably a server side error.

@daniellemaxwell daniellemaxwell merged commit 4c4844c into main Dec 14, 2022
@daniellemaxwell daniellemaxwell deleted the sc-10284/update-handler branch December 14, 2022 20:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants