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

Css 9402/resources #1347

Merged

Conversation

SimoneDutto
Copy link
Contributor

Description

This pr adds the resources endpoint.
The expected response is a paginated list of Resources (clouds, controllers, models, service_accounts, application offers).

We implemented this with a raw query with multiple union for the different tables.
The reason behind a raw query instead of a view is that view doesn't provide any cons in our situation.

Fly by update of the db client to enable prepared statements, it should improve the performance caching request and it was a simple flag in the gorm client Doc

Fixes JIRA/GitHub issue number

Engineering checklist

Check only items that apply

  • Documentation updated
  • Covered by unit tests
  • Covered by integration tests

Test instructions

Notes for code reviewers

@SimoneDutto SimoneDutto requested a review from a team as a code owner September 4, 2024 11:08
Copy link
Contributor

@ale8k ale8k left a comment

Choose a reason for hiding this comment

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

Initial run over, will go over again, looking more closely at query and pagination. Not keen on having test with integration in the name... You can definitely unit test just the handler with mocks to test the handlers logic. This probably something to discuss at standup.

internal/db/resources.go Outdated Show resolved Hide resolved
internal/db/resources.go Outdated Show resolved Hide resolved
internal/db/resources_test.go Outdated Show resolved Hide resolved
internal/db/resources_test.go Outdated Show resolved Hide resolved
internal/jimm/resource.go Outdated Show resolved Hide resolved
internal/rebac_admin/resources_integration_test.go Outdated Show resolved Hide resolved
internal/rebac_admin/resources_integration_test.go Outdated Show resolved Hide resolved
internal/rebac_admin/resources_integration_test.go Outdated Show resolved Hide resolved
@SimoneDutto
Copy link
Contributor Author

SimoneDutto commented Sep 4, 2024

Initial run over, will go over again, looking more closely at query and pagination. Not keen on having test with integration in the name... You can definitely unit test just the handler with mocks to test the handlers logic. This probably something to discuss at standup.

In the rebac admin folder we use this approach of having <name>_test.go and <name>_integration_test.go.
Yeah, you can totally mock out Jimm methods and we did for some endpoints and these tests are usually in the <name>_test.go, but for this one I think it's best to have the integration tests directly

kian99
kian99 previously approved these changes Sep 4, 2024
Copy link
Contributor

@kian99 kian99 left a comment

Choose a reason for hiding this comment

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

Very nice, I think this is pretty much good to go, just left some comments where things were a little unclear.

@@ -62,6 +62,22 @@ func CreatePagination(sizeP, pageP *int, total int) (int, *int, LimitOffsetPagin
return page, nextPage, NewOffsetFilter(pageSize, offset)
}

// CreatePagination returns the current page, the page size, and the pagination.LimitOffsetPagination.
// This method is different approach to the method `CreatePagination` when we don't have the total number of records.
func CreatePaginationWithoutTotal(sizeP, pageP *int) (int, int, LimitOffsetPagination) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably return a struct here and in CreatePagination because I'm very confused looking at this function and even the test, what int, int represent.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or I like the idea of named returns here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, you are right on this. It's pretty confusing without names

Copy link
Contributor Author

@SimoneDutto SimoneDutto Sep 4, 2024

Choose a reason for hiding this comment

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

changed to named returns and improved godoc

c := qt.New(t)
pPage := utils.IntToPointer(0)
pSize := utils.IntToPointer(10)
page, size, pag := pagination.CreatePaginationWithoutTotal(pSize, pPage)
Copy link
Contributor

Choose a reason for hiding this comment

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

page is the offset?
size is the number of returned elements? Why is it the page size + 1?
And pag if the filter, maybe the name could be changed.

Comment on lines 21 to 22
FROM application_offers
JOIN models ON application_offers.model_id = models.id
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need the parent objects? Does the rebac-admin spec require that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, you can find in the issue on Jira the entities who need the parent as well https://warthogs.atlassian.net/browse/CSS-9402

internal/rebac_admin/resources.go Outdated Show resolved Hide resolved
Comment on lines 39 to 43
if len(res) == pageSize {
nPage := page + 1
nextPage = &nPage
res = res[:len(res)-1]
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I get it, but it's confusing to read this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just make a func maybe call geTnextPageAndResponse or something

Copy link
Contributor Author

Choose a reason for hiding this comment

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

did it, thank you for the suggestion

Comment on lines +56 to +58
Next: resources.Next{
Page: nextPage,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this whole object be nil if nextPage is nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it could, but i think it complicates the code to add an if nextPage == nil to exclude the Next argument.
What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, we used the same approach for all the other endpoints

internal/rebac_admin/resources_integration_test.go Outdated Show resolved Hide resolved
ale8k
ale8k previously approved these changes Sep 4, 2024
Copy link
Contributor

@ale8k ale8k left a comment

Choose a reason for hiding this comment

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

lgtm

@SimoneDutto SimoneDutto dismissed stale reviews from ale8k and kian99 via 15ca509 September 4, 2024 13:44
@SimoneDutto SimoneDutto requested review from ale8k and kian99 September 4, 2024 13:46
internal/rebac_admin/resources.go Outdated Show resolved Hide resolved
internal/rebac_admin/resources_integration_test.go Outdated Show resolved Hide resolved
Comment on lines 57 to 58
// Then, we set the next page if we have this many records.
// If it does we return the records minus 1 and advide the consumer there is another page.
Copy link
Contributor

Choose a reason for hiding this comment

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

The wording is confusing here,
Reading this I see

  • If we have as many records as requested, we set the next page.
  • If we don't have as many records as request, we return records minus 1 and tell client there is another page.
  • Else we return the records and set the next page as empty.

Aren't points 1 and 3 saying the same thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you are right, i made a mess with c/p. Now it's updated.

@SimoneDutto SimoneDutto requested a review from kian99 September 4, 2024 14:34
@SimoneDutto SimoneDutto merged commit f0c184b into canonical:feature-rebac-admin Sep 4, 2024
4 checks passed
@SimoneDutto SimoneDutto mentioned this pull request Sep 5, 2024
3 tasks
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.

3 participants