use of bio.terra.workspace.model.ResourceDescription in project terra-cli by DataBiosphere.
the class WorkspaceManagerService method enumerateAllResources.
/**
* Call the Workspace Manager GET "/api/workspaces/v1/{workspaceId}/resources" endpoint, possibly
* multiple times, to get a list of all resources (controlled and referenced) in the workspace.
* Throw an exception if the number of resources in the workspace is greater than the specified
* limit.
*
* @param workspaceId the workspace to query
* @param limit the maximum number of resources to return
* @return a list of resources
* @throws SystemException if the number of resources in the workspace > the specified limit
*/
public List<ResourceDescription> enumerateAllResources(UUID workspaceId, int limit) {
return handleClientExceptions(() -> {
// poll the enumerate endpoint until no results are returned, or we hit the limit
List<ResourceDescription> allResources = new ArrayList<>();
int numResultsReturned = 0;
do {
int offset = allResources.size();
ResourceList result = HttpUtils.callWithRetries(() -> new ResourceApi(apiClient).enumerateResources(workspaceId, offset, MAX_RESOURCES_PER_ENUMERATE_REQUEST, null, null), WorkspaceManagerService::isRetryable);
// add all fetched resources to the running list
numResultsReturned = result.getResources().size();
logger.debug("Called enumerate endpoints, fetched {} resources", numResultsReturned);
allResources.addAll(result.getResources());
// if we have fetched more than the limit, then throw an exception
if (allResources.size() > limit) {
throw new SystemException("Total number of resources (" + allResources.size() + ") exceeds the CLI limit (" + limit + ")");
}
// if this fetch returned less than the maximum allowed per request, then that indicates
// there are no more
} while (numResultsReturned >= MAX_RESOURCES_PER_ENUMERATE_REQUEST);
logger.debug("Fetched total number of resources: {}", allResources.size());
return allResources;
}, "Error enumerating resources in the workspace.");
}
use of bio.terra.workspace.model.ResourceDescription in project terra-workspace-manager by DataBiosphere.
the class EnumerateResources method doUserJourney.
@Override
public void doUserJourney(TestUserSpecification testUser, WorkspaceApi workspaceApi) throws Exception {
// Add second user to the workspace as a reader
workspaceApi.grantRole(new GrantRoleRequestBody().memberEmail(workspaceReader.userEmail), getWorkspaceId(), IamRole.READER);
// Case 1: fetch all
ResourceList enumList = ownerResourceApi.enumerateResources(getWorkspaceId(), 0, RESOURCE_COUNT, null, null);
logResult("fetchall", enumList);
// Make sure we got all of the expected ids
matchFullResourceList(enumList.getResources());
// Repeat case 1 as the workspace reader.
// As this is the first operation after modifying workspace IAM groups, retry here to compensate
// for the delay in GCP IAM propagation.
ResourceList readerEnumList = ClientTestUtils.getWithRetryOnException(() -> readerResourceApi.enumerateResources(getWorkspaceId(), 0, RESOURCE_COUNT, null, null));
logResult("fetchall reader", readerEnumList);
matchFullResourceList(readerEnumList.getResources());
// Case 2: fetch by pages
ResourceList page1List = ownerResourceApi.enumerateResources(getWorkspaceId(), 0, PAGE_SIZE, null, null);
logResult("page1", page1List);
assertThat(page1List.getResources().size(), equalTo(PAGE_SIZE));
ResourceList page2List = ownerResourceApi.enumerateResources(getWorkspaceId(), PAGE_SIZE, PAGE_SIZE, null, null);
logResult("page2", page2List);
assertThat(page2List.getResources().size(), equalTo(PAGE_SIZE));
ResourceList page3List = ownerResourceApi.enumerateResources(getWorkspaceId(), 2 * PAGE_SIZE, PAGE_SIZE, null, null);
logResult("page3", page3List);
assertThat(page3List.getResources().size(), lessThan(PAGE_SIZE));
List<ResourceDescription> descriptionList = new ArrayList<>();
descriptionList.addAll(page1List.getResources());
descriptionList.addAll(page2List.getResources());
descriptionList.addAll(page3List.getResources());
matchFullResourceList(descriptionList);
// Case 3: no results if offset is too high
ResourceList enumEmptyList = ownerResourceApi.enumerateResources(getWorkspaceId(), 10 * PAGE_SIZE, PAGE_SIZE, null, null);
assertThat(enumEmptyList.getResources().size(), equalTo(0));
// Case 4: filter by resource type
ResourceList buckets = ownerResourceApi.enumerateResources(getWorkspaceId(), 0, RESOURCE_COUNT, ResourceType.GCS_BUCKET, null);
logResult("buckets", buckets);
long expectedBuckets = resourceList.stream().filter(m -> m.getResourceType() == ResourceType.GCS_BUCKET).count();
logger.info("Counted {} buckets created", expectedBuckets);
// Note - assertThat exits out on an int -> long compare, so just don't do that.
long actualBuckets = buckets.getResources().size();
assertThat(actualBuckets, equalTo(expectedBuckets));
// Case 5: filter by stewardship type
ResourceList referencedList = ownerResourceApi.enumerateResources(getWorkspaceId(), 0, RESOURCE_COUNT, null, StewardshipType.REFERENCED);
logResult("referenced", referencedList);
long expectedReferenced = resourceList.stream().filter(m -> m.getStewardshipType() == StewardshipType.REFERENCED).count();
logger.info("Counted {} referenced created", expectedReferenced);
long actualReferenced = referencedList.getResources().size();
assertThat(actualReferenced, equalTo(expectedReferenced));
// Case 6: filter by resource and stewardship
ResourceList controlledBucketList = ownerResourceApi.enumerateResources(getWorkspaceId(), 0, RESOURCE_COUNT, ResourceType.GCS_BUCKET, StewardshipType.CONTROLLED);
logResult("controlledBucket", controlledBucketList);
long expectedControlledBuckets = resourceList.stream().filter(m -> (m.getStewardshipType() == StewardshipType.CONTROLLED && m.getResourceType() == ResourceType.GCS_BUCKET)).count();
logger.info("Counted {} controlled buckets created", expectedControlledBuckets);
long actualControlledBuckets = controlledBucketList.getResources().size();
assertThat(actualControlledBuckets, equalTo(expectedControlledBuckets));
// Case 7: validate error on invalid pagination params
ApiException invalidPaginationException = assertThrows(ApiException.class, () -> ownerResourceApi.enumerateResources(getWorkspaceId(), -11, 2, ResourceType.GCS_BUCKET, StewardshipType.CONTROLLED));
assertThat(invalidPaginationException.getMessage(), containsString("Invalid pagination"));
invalidPaginationException = assertThrows(ApiException.class, () -> ownerResourceApi.enumerateResources(getWorkspaceId(), 0, 0, ResourceType.GCS_BUCKET, StewardshipType.CONTROLLED));
assertThat(invalidPaginationException.getMessage(), containsString("Invalid pagination"));
}
use of bio.terra.workspace.model.ResourceDescription in project terra-workspace-manager by DataBiosphere.
the class EnumerateResources method logResult.
private void logResult(String tag, ResourceList resourceList) {
List<ResourceDescription> descList = resourceList.getResources();
logger.info("Enumeration results for {} - {} resources", tag, descList.size());
for (ResourceDescription desc : descList) {
ResourceMetadata metadata = desc.getMetadata();
String access = "<null>";
String managed = "<null>";
String user = "<null>";
if (metadata.getStewardshipType() == StewardshipType.CONTROLLED && metadata.getControlledResourceMetadata() != null) {
ControlledResourceMetadata controlled = metadata.getControlledResourceMetadata();
access = controlled.getAccessScope().toString();
managed = controlled.getManagedBy().toString();
user = controlled.getPrivateResourceUser().getUserName();
}
logger.info(" {}: id={} type={} stew={} cloud={} access={} managed={} user={}", metadata.getName(), metadata.getResourceId(), metadata.getResourceType(), metadata.getStewardshipType(), metadata.getCloudPlatform(), access, managed, user);
}
}
Aggregations