use of bio.terra.janitor.model.CreateResourceRequestBody in project terra-cloud-resource-lib by DataBiosphere.
the class CleanupRecorder method publish.
private static void publish(CloudResourceUid resource, @Nullable ResourceMetadata metadata, ClientConfig clientConfig) {
CleanupConfig cleanupConfig = clientConfig.getCleanupConfig().get();
if (publisher == null) {
// Provide a new publisher if not present.
TopicName topicName = TopicName.of(cleanupConfig.janitorProjectId(), cleanupConfig.janitorTopicName());
try {
providePublisher(Publisher.newBuilder(topicName).setCredentialsProvider(FixedCredentialsProvider.create(cleanupConfig.credentials())).build());
} catch (IOException e) {
throw new JanitorException("Failed to initialize Janitor pubsub publisher.", e);
}
}
ObjectMapper objectMapper = new ObjectMapper().registerModule(new Jdk8Module()).registerModule(new JavaTimeModule()).configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
OffsetDateTime now = OffsetDateTime.now(clock);
CreateResourceRequestBody body = new CreateResourceRequestBody().resourceUid(resource).resourceMetadata(metadata).creation(now).expiration(now.plus(cleanupConfig.timeToLive())).putLabelsItem("client", clientConfig.getClientName()).putLabelsItem("cleanupId", cleanupConfig.cleanupId());
ByteString data;
try {
data = ByteString.copyFromUtf8(objectMapper.writeValueAsString(body));
} catch (IOException e) {
throw new JanitorException(String.format("Failed to serialize CreateResourceRequestBody: [%s]", body), e);
}
ApiFuture<String> messageIdFuture = publisher.publish(PubsubMessage.newBuilder().setData(data).build());
try {
String messageId = messageIdFuture.get();
logger.debug("Publish message to Janitor track resource " + messageId);
} catch (InterruptedException | ExecutionException e) {
throw new JanitorException(String.format("Failed to publish message: [%s] ", data.toString()), e);
}
}
use of bio.terra.janitor.model.CreateResourceRequestBody in project terra-resource-buffer by DataBiosphere.
the class CleanupScheduler method publish.
private void publish(CloudResourceUid cloudResourceUid) {
ByteString data;
try {
OffsetDateTime now = OffsetDateTime.now(clock);
CreateResourceRequestBody body = new CreateResourceRequestBody().resourceUid(objectMapper.readValue(objectMapper.writeValueAsString(cloudResourceUid), bio.terra.janitor.model.CloudResourceUid.class)).creation(now).expiration(now.plus(crlConfiguration.getTestResourceTimeToLive())).putLabelsItem("client", CLIENT_NAME);
data = ByteString.copyFromUtf8(objectMapper.writeValueAsString(body));
} catch (IOException e) {
throw new RuntimeException(String.format("Failed to build CreateResourceRequestBody for CloudResourceUid: [%s]", cloudResourceUid), e);
}
ApiFuture<String> messageIdFuture = publisher.publish(PubsubMessage.newBuilder().setData(data).build());
try {
String messageId = messageIdFuture.get();
logger.debug("Publish message to Janitor track resource " + messageId);
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(String.format("Failed to publish message: [%s] ", data.toString()), e);
}
}
use of bio.terra.janitor.model.CreateResourceRequestBody 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());
}
Aggregations