Search in sources :

Example 1 with CloudResourceUid

use of bio.terra.buffer.generated.model.CloudResourceUid in project terra-resource-buffer by DataBiosphere.

the class CleanupSchedulerTest method testScheduleCleanup.

@Test
public void testScheduleCleanup() throws Exception {
    Pool pool = Pool.builder().creation(CREATION).id(PoolId.create("poolId")).resourceType(ResourceType.GOOGLE_PROJECT).size(1).resourceConfig(new ResourceConfig()).status(PoolStatus.ACTIVE).build();
    CloudResourceUid cloudResourceUid = new CloudResourceUid().googleProjectUid(new GoogleProjectUid().projectId("p1"));
    Resource resource = Resource.builder().id(ResourceId.create(UUID.randomUUID())).poolId(pool.id()).creation(Instant.now()).state(ResourceState.CREATING).build();
    bufferDao.createPools(ImmutableList.of(pool));
    bufferDao.createResource(resource);
    bufferDao.updateResourceAsReady(resource.id(), cloudResourceUid);
    bufferDao.updateOneReadyResourceToHandedOut(pool.id(), RequestHandoutId.create("1111"));
    assertEquals(1, bufferDao.retrieveResourceToCleanup(10).size());
    cleanupScheduler.initialize();
    Thread.sleep(1000);
    verify(mockPublisher).publish(messageArgumentCaptor.capture());
    assertThat(messageArgumentCaptor.getAllValues().stream().map(m -> m.getData().toStringUtf8()).collect(Collectors.toList()), Matchers.containsInAnyOrder(objectMapper.writeValueAsString(new CreateResourceRequestBody().creation(CREATION.atOffset(ZoneOffset.UTC)).expiration(CREATION.plus(crlConfiguration.getTestResourceTimeToLive()).atOffset(ZoneOffset.UTC)).putLabelsItem("client", CLIENT_NAME).resourceUid(new bio.terra.janitor.model.CloudResourceUid().googleProjectUid(new bio.terra.janitor.model.GoogleProjectUid().projectId("p1"))))));
    assertTrue(bufferDao.retrieveResourceToCleanup(10).isEmpty());
}
Also used : CloudResourceUid(bio.terra.buffer.generated.model.CloudResourceUid) CreateResourceRequestBody(bio.terra.janitor.model.CreateResourceRequestBody) Resource(bio.terra.buffer.common.Resource) Pool(bio.terra.buffer.common.Pool) ResourceConfig(bio.terra.buffer.generated.model.ResourceConfig) GoogleProjectUid(bio.terra.buffer.generated.model.GoogleProjectUid) BaseUnitTest(bio.terra.buffer.common.BaseUnitTest) Test(org.junit.jupiter.api.Test)

Example 2 with CloudResourceUid

use of bio.terra.buffer.generated.model.CloudResourceUid in project terra-resource-buffer by DataBiosphere.

the class PoolServiceTest method handoutResource_success.

@Test
public void handoutResource_success() throws Exception {
    PoolId poolId = PoolId.create("poolId");
    RequestHandoutId requestHandoutId = RequestHandoutId.create("handoutId");
    newReadyPool(poolId, 2);
    List<CloudResourceUid> resourceUids = bufferDao.retrieveResourcesRandomly(poolId, ResourceState.READY, 2).stream().map(Resource::cloudResourceUid).collect(Collectors.toList());
    ResourceInfo resourceInfo = poolService.handoutResource(poolId, requestHandoutId);
    assertEquals(poolId.id(), resourceInfo.getPoolId());
    assertEquals(requestHandoutId.id(), resourceInfo.getRequestHandoutId());
    // CloudResource may be either of the two resources.
    assertTrue(resourceInfo.getCloudResourceUid().equals(resourceUids.get(0)) || resourceInfo.getCloudResourceUid().equals(resourceUids.get(1)));
    // Use the same requestHandoutId, expect to get the same resource back.
    ResourceInfo secondResourceInfo = poolService.handoutResource(poolId, requestHandoutId);
    assertEquals(resourceInfo, secondResourceInfo);
}
Also used : CloudResourceUid(bio.terra.buffer.generated.model.CloudResourceUid) ResourceInfo(bio.terra.buffer.generated.model.ResourceInfo) PoolId(bio.terra.buffer.common.PoolId) RequestHandoutId(bio.terra.buffer.common.RequestHandoutId) BaseUnitTest(bio.terra.buffer.common.BaseUnitTest) Test(org.junit.jupiter.api.Test)

Example 3 with CloudResourceUid

use of bio.terra.buffer.generated.model.CloudResourceUid in project terra-resource-buffer by DataBiosphere.

the class GenerateProjectIdStep method doStep.

@Override
public StepResult doStep(FlightContext flightContext) {
    FlightMap workingMap = flightContext.getWorkingMap();
    try {
        String projectId = projectIdGenerator.generateIdWithRetries(gcpProjectConfig.getProjectIdSchema());
        // Terra. in that case, the project creation step will fail.
        if (retrieveProject(rmCow, projectId).isPresent()) {
            logger.info("Generated GCP project is already in use: {}", projectId);
            // without hitting project get quota issues.
            return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY);
        }
        flightContext.getWorkingMap().put(GOOGLE_PROJECT_ID, projectId);
        workingMap.put(CLOUD_RESOURCE_UID, new CloudResourceUid().googleProjectUid(new GoogleProjectUid().projectId(projectId)));
        return StepResult.getStepResultSuccess();
    } catch (IOException | InterruptedException ex) {
        logger.info("Error when generating GCP project id.", ex);
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, ex);
    }
}
Also used : CloudResourceUid(bio.terra.buffer.generated.model.CloudResourceUid) IOException(java.io.IOException) GoogleProjectUid(bio.terra.buffer.generated.model.GoogleProjectUid)

Example 4 with CloudResourceUid

use of bio.terra.buffer.generated.model.CloudResourceUid in project terra-resource-buffer by DataBiosphere.

the class BufferDaoTest method updateResourceAsReady.

@Test
public void updateResourceAsReady() {
    PoolId poolId = PoolId.create("poolId");
    Pool pool = newPool(poolId);
    bufferDao.createPools(ImmutableList.of(pool));
    Resource resource = newResource(poolId, ResourceState.CREATING);
    CloudResourceUid resourceUid = new CloudResourceUid().googleProjectUid(new GoogleProjectUid().projectId("p-123"));
    bufferDao.createResource(resource);
    bufferDao.updateResourceAsReady(resource.id(), resourceUid);
    Resource updatedResource = bufferDao.retrieveResource(resource.id()).get();
    assertEquals(ResourceState.READY, updatedResource.state());
    assertEquals(resourceUid, updatedResource.cloudResourceUid());
}
Also used : CloudResourceUid(bio.terra.buffer.generated.model.CloudResourceUid) PoolId(bio.terra.buffer.common.PoolId) Resource(bio.terra.buffer.common.Resource) Pool(bio.terra.buffer.common.Pool) GoogleProjectUid(bio.terra.buffer.generated.model.GoogleProjectUid) BaseUnitTest(bio.terra.buffer.common.BaseUnitTest) Test(org.junit.jupiter.api.Test)

Aggregations

CloudResourceUid (bio.terra.buffer.generated.model.CloudResourceUid)4 BaseUnitTest (bio.terra.buffer.common.BaseUnitTest)3 GoogleProjectUid (bio.terra.buffer.generated.model.GoogleProjectUid)3 Test (org.junit.jupiter.api.Test)3 Pool (bio.terra.buffer.common.Pool)2 PoolId (bio.terra.buffer.common.PoolId)2 Resource (bio.terra.buffer.common.Resource)2 RequestHandoutId (bio.terra.buffer.common.RequestHandoutId)1 ResourceConfig (bio.terra.buffer.generated.model.ResourceConfig)1 ResourceInfo (bio.terra.buffer.generated.model.ResourceInfo)1 CreateResourceRequestBody (bio.terra.janitor.model.CreateResourceRequestBody)1 IOException (java.io.IOException)1