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

TSPS-357 Add admin endpoint to get a user's quota and update their quota #162

Merged
merged 8 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 8 additions & 8 deletions common/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ info:
version: 1.0.0
paths:
# Admin APIs
/api/admin/v1/pipeline/{pipelineName}:
/api/admin/v1/pipelines/{pipelineName}:
Copy link
Collaborator

Choose a reason for hiding this comment

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

ah nice catch, thanks!

parameters:
- $ref: '#/components/parameters/PipelineName'
get:
Expand Down Expand Up @@ -48,14 +48,14 @@ paths:
500:
$ref: '#/components/responses/ServerError'

/api/admin/v1/quota/{pipelineName}/{userId}:
/api/admin/v1/quotas/{pipelineName}/{userId}:
parameters:
- $ref: '#/components/parameters/PipelineName'
- $ref: '#/components/parameters/UserId'
get:
summary: Get description for a given pipeline.
summary: Get quota for a given pipeline and user.
tags: [ admin ]
operationId: getQuota
operationId: getQuotaForPipelineAndUser
responses:
200:
description: Success
Expand All @@ -70,9 +70,9 @@ paths:
500:
$ref: '#/components/responses/ServerError'
patch:
summary: Update attributes for a given pipeline.
summary: Update quota limit for a given pipeline and user.
tags: [ admin ]
operationId: updateQuotaLimit
operationId: updateQuotaLimitForPipelineAndUser
requestBody:
required: true
content:
Expand Down Expand Up @@ -403,7 +403,7 @@ components:
UserId:
name: userId
in: path
description: A string identifier to used to identify a terra user
description: A string identifier to used to identify a Terra user
required: true
schema:
type: string
Expand Down Expand Up @@ -929,7 +929,7 @@ components:

UpdateQuotaLimitRequestBody:
description: |
json object containing the admin provided information to update a users quota limit with
json object containing the admin provided information used to update a user's quota limit
type: object
required: [ quotaLimit ]
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public ResponseEntity<ApiAdminPipeline> getPipeline(String pipelineName) {
}

@Override
public ResponseEntity<ApiAdminQuota> getQuota(String pipelineName, String userId) {
public ResponseEntity<ApiAdminQuota> getQuotaForPipelineAndUser(
String pipelineName, String userId) {
final SamUser authedUser = getAuthenticatedInfo();
samService.checkAdminAuthz(authedUser);
PipelinesEnum validatedPipelineName =
Expand All @@ -92,7 +93,7 @@ public ResponseEntity<ApiAdminPipeline> updatePipeline(
}

@Override
public ResponseEntity<ApiAdminQuota> updateQuotaLimit(
public ResponseEntity<ApiAdminQuota> updateQuotaLimitForPipelineAndUser(
String pipelineName, String userId, ApiUpdateQuotaLimitRequestBody body) {
final SamUser authedUser = getAuthenticatedInfo();
samService.checkAdminAuthz(authedUser);
Expand All @@ -109,7 +110,7 @@ public ResponseEntity<ApiAdminQuota> updateQuotaLimit(
}
int newQuotaLimit = body.getQuotaLimit();
UserQuota userQuota = quotasService.getQuotaForUserAndPipeline(userId, validatedPipelineName);
userQuota = quotasService.updateQuotaLimit(userQuota, newQuotaLimit);
userQuota = quotasService.adminUpdateQuotaLimit(userQuota, newQuotaLimit);
return new ResponseEntity<>(userQuotaToApiAdminQuota(userQuota), HttpStatus.OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public UserQuota updateQuotaConsumed(UserQuota userQuota, int newQuotaConsumed)
* @param newQuotaLimit - the new quota limit
* @return - the updated user quota
*/
public UserQuota updateQuotaLimit(UserQuota userQuota, int newQuotaLimit) {
public UserQuota adminUpdateQuotaLimit(UserQuota userQuota, int newQuotaLimit) {
if (newQuotaLimit < userQuota.getQuotaConsumed()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

so we would allow setting the newQuotaLimit to be equal to the user's current QuotaConsumed, thereby preventing any further activity, is that right? (sounds good to me)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

correct

throw new InternalServerErrorException(
String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ void updatePipelineWorkspaceOk() throws Exception {
.perform(
patch(
String.format(
"/api/admin/v1/pipeline/%s", PipelinesEnum.ARRAY_IMPUTATION.getValue()))
"/api/admin/v1/pipelines/%s",
PipelinesEnum.ARRAY_IMPUTATION.getValue()))
.contentType(MediaType.APPLICATION_JSON)
.content(
createTestJobPostBody(
Expand Down Expand Up @@ -104,7 +105,7 @@ void updatePipelineWorkspaceIdRequireWorkspaceName() throws Exception {
.perform(
patch(
String.format(
"/api/admin/v1/pipeline/%s", PipelinesEnum.ARRAY_IMPUTATION.getValue()))
"/api/admin/v1/pipelines/%s", PipelinesEnum.ARRAY_IMPUTATION.getValue()))
.contentType(MediaType.APPLICATION_JSON)
.content(
createTestJobPostBody(
Expand All @@ -118,7 +119,7 @@ void updatePipelineWorkspaceIdRequireWorkspaceProject() throws Exception {
.perform(
patch(
String.format(
"/api/admin/v1/pipeline/%s", PipelinesEnum.ARRAY_IMPUTATION.getValue()))
"/api/admin/v1/pipelines/%s", PipelinesEnum.ARRAY_IMPUTATION.getValue()))
.contentType(MediaType.APPLICATION_JSON)
.content(createTestJobPostBody(null, TEST_WORKSPACE_NAME, TEST_WDL_METHOD_VERSION)))
.andExpect(status().isBadRequest());
Expand All @@ -130,7 +131,7 @@ void updatePipelineWorkspaceIdRequireWdlMethodVersion() throws Exception {
.perform(
patch(
String.format(
"/api/admin/v1/pipeline/%s", PipelinesEnum.ARRAY_IMPUTATION.getValue()))
"/api/admin/v1/pipelines/%s", PipelinesEnum.ARRAY_IMPUTATION.getValue()))
.contentType(MediaType.APPLICATION_JSON)
.content(
createTestJobPostBody(
Expand All @@ -146,7 +147,7 @@ void updatePipelineWorkspaceIdNotAdminUser() throws Exception {
.perform(
patch(
String.format(
"/api/admin/v1/pipeline/%s", PipelinesEnum.ARRAY_IMPUTATION.getValue()))
"/api/admin/v1/pipelines/%s", PipelinesEnum.ARRAY_IMPUTATION.getValue()))
.contentType(MediaType.APPLICATION_JSON)
.content(
createTestJobPostBody(
Expand All @@ -165,7 +166,7 @@ void getAdminPipelineOk() throws Exception {
.perform(
get(
String.format(
"/api/admin/v1/pipeline/%s", PipelinesEnum.ARRAY_IMPUTATION.getValue())))
"/api/admin/v1/pipelines/%s", PipelinesEnum.ARRAY_IMPUTATION.getValue())))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
Expand All @@ -187,7 +188,7 @@ void getAdminPipelineNotAdminUser() throws Exception {
.perform(
get(
String.format(
"/api/admin/v1/pipeline/%s", PipelinesEnum.ARRAY_IMPUTATION.getValue())))
"/api/admin/v1/pipelines/%s", PipelinesEnum.ARRAY_IMPUTATION.getValue())))
.andExpect(status().isForbidden());
}

Expand All @@ -201,7 +202,7 @@ void getUserQuotaOk() throws Exception {
.perform(
get(
String.format(
"/api/admin/v1/quota/%s/%s",
"/api/admin/v1/quotas/%s/%s",
PipelinesEnum.ARRAY_IMPUTATION.getValue(), TEST_SAM_USER.getSubjectId())))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
Expand All @@ -227,7 +228,7 @@ void getAdminQuotaNotAdminUser() throws Exception {
.perform(
get(
String.format(
"/api/admin/v1/quota/%s/%s",
"/api/admin/v1/quotas/%s/%s",
PipelinesEnum.ARRAY_IMPUTATION.getValue(), TEST_SAM_USER.getSubjectId())))
.andExpect(status().isForbidden());
}
Expand All @@ -242,13 +243,14 @@ void updateAdminQuotaOk() throws Exception {
when(quotasServiceMock.isUserQuotaPresent(
TEST_SAM_USER.getSubjectId(), PipelinesEnum.ARRAY_IMPUTATION))
.thenReturn(true);
when(quotasServiceMock.updateQuotaLimit(TEST_USER_QUOTA_1, 800)).thenReturn(updatedUserQuota);
when(quotasServiceMock.adminUpdateQuotaLimit(TEST_USER_QUOTA_1, 800))
.thenReturn(updatedUserQuota);
MvcResult result =
mockMvc
.perform(
patch(
String.format(
"/api/admin/v1/quota/%s/%s",
"/api/admin/v1/quotas/%s/%s",
PipelinesEnum.ARRAY_IMPUTATION.getValue(),
TEST_SAM_USER.getSubjectId()))
.contentType(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -277,7 +279,7 @@ void updateAdminQuotaUserQuotaDoesntExist() throws Exception {
.perform(
patch(
String.format(
"/api/admin/v1/quota/%s/%s",
"/api/admin/v1/quotas/%s/%s",
PipelinesEnum.ARRAY_IMPUTATION.getValue(), TEST_SAM_USER.getSubjectId()))
.contentType(MediaType.APPLICATION_JSON)
.content(createTestJobPostBody(800)))
Expand All @@ -290,7 +292,7 @@ void updateAdminQuotaRequireQuotaLimit() throws Exception {
.perform(
patch(
String.format(
"/api/admin/v1/quota/%s/%s",
"/api/admin/v1/quotas/%s/%s",
PipelinesEnum.ARRAY_IMPUTATION.getValue(), TEST_SAM_USER.getSubjectId()))
.contentType(MediaType.APPLICATION_JSON)
.content("{}"))
Expand All @@ -305,7 +307,7 @@ void updateAdminQuotaIdNotAdminUser() throws Exception {
.perform(
patch(
String.format(
"/api/admin/v1/quota/%s/%s",
"/api/admin/v1/quotas/%s/%s",
PipelinesEnum.ARRAY_IMPUTATION.getValue(), TEST_SAM_USER.getSubjectId()))
.contentType(MediaType.APPLICATION_JSON)
.content(createTestJobPostBody(500)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void updateQuotaLimit() {

// call service to update quota limit
int newQuotaLimit = 150;
UserQuota updatedUserQuota = quotasService.updateQuotaLimit(userQuota, newQuotaLimit);
UserQuota updatedUserQuota = quotasService.adminUpdateQuotaLimit(userQuota, newQuotaLimit);

assertEquals(userQuota.getUserId(), updatedUserQuota.getUserId());
assertEquals(userQuota.getPipelineName(), updatedUserQuota.getPipelineName());
Expand All @@ -143,7 +143,7 @@ void updateQuotaLimitLessThanQuotaConsumed() {
int newQuotaLimit = 20;
assertThrows(
InternalServerErrorException.class,
() -> quotasService.updateQuotaLimit(userQuota, newQuotaLimit));
() -> quotasService.adminUpdateQuotaLimit(userQuota, newQuotaLimit));
}

@Test
Expand Down
Loading