Search in sources :

Example 6 with PoolId

use of bio.terra.buffer.common.PoolId in project terra-resource-buffer by DataBiosphere.

the class PoolServiceTest method updateFromConfig_updateResourceConfigOnExistingPool_throwsException.

@Test
public void updateFromConfig_updateResourceConfigOnExistingPool_throwsException() throws Exception {
    PoolId poolId = PoolId.create("poolId");
    PoolConfig poolConfig = new PoolConfig().poolId(poolId.toString()).size(1).resourceConfigName(RESOURCE_CONFIG_NAME);
    PoolWithResourceConfig parsedPoolConfig = PoolWithResourceConfig.create(poolConfig, newResourceConfig());
    poolService.updateFromConfig(ImmutableList.of(parsedPoolConfig));
    // Sets ResourceConfig's GCP project id prefix to newer value.
    PoolWithResourceConfig updatedPoolConfig = PoolWithResourceConfig.create(poolConfig, newResourceConfig(new GcpProjectConfig().projectIdSchema(new ProjectIdSchema().prefix("aou-rw-test2").scheme(ProjectIdSchema.SchemeEnum.RANDOM_CHAR))));
    assertThrows(RuntimeException.class, () -> poolService.updateFromConfig(ImmutableList.of(updatedPoolConfig)));
    assertThat(bufferDao.retrievePools().stream().map(Pool::resourceConfig).collect(Collectors.toList()), Matchers.containsInAnyOrder(parsedPoolConfig.resourceConfig()));
}
Also used : PoolId(bio.terra.buffer.common.PoolId) ProjectIdSchema(bio.terra.buffer.generated.model.ProjectIdSchema) GcpProjectConfig(bio.terra.buffer.generated.model.GcpProjectConfig) PoolConfig(bio.terra.buffer.generated.model.PoolConfig) Pool(bio.terra.buffer.common.Pool) BaseUnitTest(bio.terra.buffer.common.BaseUnitTest) Test(org.junit.jupiter.api.Test)

Example 7 with PoolId

use of bio.terra.buffer.common.PoolId in project terra-resource-buffer by DataBiosphere.

the class PoolServiceTest method updateFromConfig_deactivatePool_updatePoolStatusSuccess.

@Test
public void updateFromConfig_deactivatePool_updatePoolStatusSuccess() throws Exception {
    PoolId poolId = PoolId.create("poolId");
    PoolWithResourceConfig parsedPoolConfig = PoolWithResourceConfig.create(new PoolConfig().poolId(poolId.toString()).size(10).resourceConfigName(RESOURCE_CONFIG_NAME), newResourceConfig());
    poolService.updateFromConfig(ImmutableList.of(parsedPoolConfig));
    List<Pool> pools = bufferDao.retrievePools();
    Pool createdPool = pools.get(0);
    assertEquals(poolId, createdPool.id());
    assertEquals(PoolStatus.ACTIVE, createdPool.status());
    poolService.updateFromConfig(ImmutableList.of());
    Pool resizedPool = bufferDao.retrievePools().get(0);
    assertEquals(poolId, resizedPool.id());
    assertEquals(PoolStatus.DEACTIVATED, resizedPool.status());
}
Also used : PoolId(bio.terra.buffer.common.PoolId) PoolConfig(bio.terra.buffer.generated.model.PoolConfig) Pool(bio.terra.buffer.common.Pool) BaseUnitTest(bio.terra.buffer.common.BaseUnitTest) Test(org.junit.jupiter.api.Test)

Example 8 with PoolId

use of bio.terra.buffer.common.PoolId in project terra-resource-buffer by DataBiosphere.

the class PoolServiceTest method updateFromConfig_createDeactivateReactivate_success.

@Test
public void updateFromConfig_createDeactivateReactivate_success() throws Exception {
    // Create two pools so we fully exercise the batch methods in the DAO.
    PoolId poolId1 = PoolId.create("poolId1");
    PoolWithResourceConfig parsedPoolConfig1 = PoolWithResourceConfig.create(new PoolConfig().poolId(poolId1.toString()).size(10).resourceConfigName(RESOURCE_CONFIG_NAME), newResourceConfig());
    PoolId poolId2 = PoolId.create("poolId2");
    PoolWithResourceConfig parsedPoolConfig2 = PoolWithResourceConfig.create(new PoolConfig().poolId(poolId2.toString()).size(50).resourceConfigName(RESOURCE_CONFIG_NAME), newResourceConfig());
    poolService.updateFromConfig(ImmutableList.of(parsedPoolConfig1, parsedPoolConfig2));
    List<Pool> pools = bufferDao.retrievePools();
    assertEquals(2, pools.size());
    Pool createdPool1 = pools.stream().filter(p -> p.id().equals(poolId1)).findFirst().orElseThrow();
    assertEquals(poolId1, createdPool1.id());
    assertEquals(ResourceType.GOOGLE_PROJECT, createdPool1.resourceType());
    assertEquals(PoolStatus.ACTIVE, createdPool1.status());
    assertEquals(parsedPoolConfig1.resourceConfig(), createdPool1.resourceConfig());
    Pool createdPool2 = pools.stream().filter(p -> p.id().equals(poolId2)).findFirst().orElseThrow();
    ;
    assertEquals(poolId2, createdPool2.id());
    assertEquals(ResourceType.GOOGLE_PROJECT, createdPool2.resourceType());
    assertEquals(PoolStatus.ACTIVE, createdPool2.status());
    assertEquals(parsedPoolConfig1.resourceConfig(), createdPool1.resourceConfig());
    // Deactivate both pools
    poolService.updateFromConfig(Collections.emptyList());
    List<Pool> deactivatedPools = bufferDao.retrievePools();
    assertEquals(2, deactivatedPools.size());
    Pool deactivatedPool1 = deactivatedPools.stream().filter(p -> p.id().equals(poolId1)).findFirst().orElseThrow();
    assertEquals(ResourceType.GOOGLE_PROJECT, deactivatedPool1.resourceType());
    assertEquals(PoolStatus.DEACTIVATED, deactivatedPool1.status());
    Pool deactivatedPool2 = deactivatedPools.stream().filter(p -> p.id().equals(poolId2)).findFirst().orElseThrow();
    assertEquals(ResourceType.GOOGLE_PROJECT, deactivatedPool2.resourceType());
    assertEquals(PoolStatus.DEACTIVATED, deactivatedPool2.status());
    // Re-activate pools
    // Resize pool 2
    PoolWithResourceConfig resizedPoolConfig2 = PoolWithResourceConfig.create(new PoolConfig().poolId(poolId2.toString()).size(25).resourceConfigName(RESOURCE_CONFIG_NAME), newResourceConfig());
    poolService.updateFromConfig(ImmutableList.of(parsedPoolConfig1, resizedPoolConfig2));
    List<Pool> reactivatedPools = bufferDao.retrievePools();
    Pool reactivatedPool1 = reactivatedPools.stream().filter(p -> p.id().equals(poolId1)).findFirst().orElseThrow();
    assertEquals(ResourceType.GOOGLE_PROJECT, reactivatedPool1.resourceType());
    assertEquals(PoolStatus.ACTIVE, reactivatedPool1.status());
    assertEquals(10, reactivatedPool1.size());
    Pool reactivatedPool2 = reactivatedPools.stream().filter(p -> p.id().equals(poolId2)).findFirst().orElseThrow();
    assertEquals(ResourceType.GOOGLE_PROJECT, reactivatedPool2.resourceType());
    assertEquals(PoolStatus.ACTIVE, reactivatedPool2.status());
    assertEquals(25, reactivatedPool2.size());
}
Also used : PoolId(bio.terra.buffer.common.PoolId) PoolConfig(bio.terra.buffer.generated.model.PoolConfig) Pool(bio.terra.buffer.common.Pool) BaseUnitTest(bio.terra.buffer.common.BaseUnitTest) Test(org.junit.jupiter.api.Test)

Example 9 with PoolId

use of bio.terra.buffer.common.PoolId in project terra-resource-buffer by DataBiosphere.

the class FlightSchedulerTest method newPoolWithResourceCount.

/**
 * Creates a pool with resources with given {@code resourceStates}.
 */
private Pool newPoolWithResourceCount(int poolSize, Multiset<ResourceState> resourceStates) {
    PoolId poolId = PoolId.create(UUID.randomUUID().toString());
    Pool pool = Pool.builder().creation(Instant.now()).id(poolId).resourceType(ResourceType.GOOGLE_PROJECT).size(poolSize).resourceConfig(new ResourceConfig().configName("resourceName")).status(PoolStatus.ACTIVE).build();
    bufferDao.createPools(ImmutableList.of(pool));
    for (ResourceState state : resourceStates) {
        bufferDao.createResource(Resource.builder().id(ResourceId.create(UUID.randomUUID())).poolId(poolId).creation(Instant.now()).state(state).build());
    }
    return pool;
}
Also used : PoolId(bio.terra.buffer.common.PoolId) Pool(bio.terra.buffer.common.Pool) ResourceState(bio.terra.buffer.common.ResourceState) ResourceConfig(bio.terra.buffer.generated.model.ResourceConfig)

Example 10 with PoolId

use of bio.terra.buffer.common.PoolId in project terra-resource-buffer by DataBiosphere.

the class BufferDao method deactivatePools.

/**
 * Updates list of pools' status to DEACTIVATED.
 */
// TODO: consider delaying expiration so pool can be recovered
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE)
public void deactivatePools(List<PoolId> poolIds) {
    String sql = "UPDATE pool SET status = :status, expiration = :expiration WHERE id = :id ";
    MapSqlParameterSource[] sqlParameterSourceList = poolIds.stream().map(poolId -> new MapSqlParameterSource().addValue("id", poolId.id()).addValue("status", PoolStatus.DEACTIVATED.toString()).addValue("expiration", OffsetDateTime.now(ZoneOffset.UTC))).toArray(MapSqlParameterSource[]::new);
    jdbcTemplate.batchUpdate(sql, sqlParameterSourceList);
}
Also used : DataAccessException(org.springframework.dao.DataAccessException) OBJECT_MAPPER(bio.terra.buffer.app.configuration.BeanNames.OBJECT_MAPPER) LoggerFactory(org.slf4j.LoggerFactory) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) Autowired(org.springframework.beans.factory.annotation.Autowired) InternalServerErrorException(bio.terra.common.exception.InternalServerErrorException) HashMap(java.util.HashMap) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ResourceId(bio.terra.buffer.common.ResourceId) PoolId(bio.terra.buffer.common.PoolId) PoolAndResourceStates(bio.terra.buffer.common.PoolAndResourceStates) SQLException(java.sql.SQLException) ResourceState(bio.terra.buffer.common.ResourceState) Propagation(org.springframework.transaction.annotation.Propagation) ResultSet(java.sql.ResultSet) Map(java.util.Map) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ZoneOffset(java.time.ZoneOffset) DataAccessUtils(org.springframework.dao.support.DataAccessUtils) ResourceType(bio.terra.buffer.common.ResourceType) Resource(bio.terra.buffer.common.Resource) Logger(org.slf4j.Logger) CloudResourceUid(bio.terra.buffer.generated.model.CloudResourceUid) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Pool(bio.terra.buffer.common.Pool) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) UUID(java.util.UUID) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) PoolStatus(bio.terra.buffer.common.PoolStatus) CheckReturnValue(javax.annotation.CheckReturnValue) List(java.util.List) Component(org.springframework.stereotype.Component) RequestHandoutId(bio.terra.buffer.common.RequestHandoutId) ResourceConfig(bio.terra.buffer.generated.model.ResourceConfig) OffsetDateTime(java.time.OffsetDateTime) BUFFER_JDBC_TEMPLATE(bio.terra.buffer.app.configuration.BeanNames.BUFFER_JDBC_TEMPLATE) CreateNetworkStep(bio.terra.buffer.service.resource.flight.CreateNetworkStep) RowMapper(org.springframework.jdbc.core.RowMapper) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) Optional(java.util.Optional) Isolation(org.springframework.transaction.annotation.Isolation) ResultSetExtractor(org.springframework.jdbc.core.ResultSetExtractor) Transactional(org.springframework.transaction.annotation.Transactional) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

PoolId (bio.terra.buffer.common.PoolId)19 Test (org.junit.jupiter.api.Test)16 BaseUnitTest (bio.terra.buffer.common.BaseUnitTest)15 Pool (bio.terra.buffer.common.Pool)13 PoolConfig (bio.terra.buffer.generated.model.PoolConfig)7 RequestHandoutId (bio.terra.buffer.common.RequestHandoutId)4 Resource (bio.terra.buffer.common.Resource)4 CloudResourceUid (bio.terra.buffer.generated.model.CloudResourceUid)3 ResourceState (bio.terra.buffer.common.ResourceState)2 ResourceConfig (bio.terra.buffer.generated.model.ResourceConfig)2 HashMap (java.util.HashMap)2 BUFFER_JDBC_TEMPLATE (bio.terra.buffer.app.configuration.BeanNames.BUFFER_JDBC_TEMPLATE)1 OBJECT_MAPPER (bio.terra.buffer.app.configuration.BeanNames.OBJECT_MAPPER)1 BaseIntegrationTest (bio.terra.buffer.common.BaseIntegrationTest)1 PoolAndResourceStates (bio.terra.buffer.common.PoolAndResourceStates)1 PoolStatus (bio.terra.buffer.common.PoolStatus)1 ResourceId (bio.terra.buffer.common.ResourceId)1 ResourceType (bio.terra.buffer.common.ResourceType)1 GcpProjectConfig (bio.terra.buffer.generated.model.GcpProjectConfig)1 GoogleProjectUid (bio.terra.buffer.generated.model.GoogleProjectUid)1