Search in sources :

Example 1 with CreateResourceRequestBody

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);
    }
}
Also used : CreateResourceRequestBody(bio.terra.janitor.model.CreateResourceRequestBody) ByteString(com.google.protobuf.ByteString) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) IOException(java.io.IOException) ByteString(com.google.protobuf.ByteString) TopicName(com.google.pubsub.v1.TopicName) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) OffsetDateTime(java.time.OffsetDateTime) JanitorException(bio.terra.cloudres.common.JanitorException) ExecutionException(java.util.concurrent.ExecutionException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with CreateResourceRequestBody

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);
    }
}
Also used : CreateResourceRequestBody(bio.terra.janitor.model.CreateResourceRequestBody) OffsetDateTime(java.time.OffsetDateTime) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) ByteString(com.google.protobuf.ByteString) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with CreateResourceRequestBody

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());
}
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)

Aggregations

CreateResourceRequestBody (bio.terra.janitor.model.CreateResourceRequestBody)3 ByteString (com.google.protobuf.ByteString)2 IOException (java.io.IOException)2 OffsetDateTime (java.time.OffsetDateTime)2 ExecutionException (java.util.concurrent.ExecutionException)2 BaseUnitTest (bio.terra.buffer.common.BaseUnitTest)1 Pool (bio.terra.buffer.common.Pool)1 Resource (bio.terra.buffer.common.Resource)1 CloudResourceUid (bio.terra.buffer.generated.model.CloudResourceUid)1 GoogleProjectUid (bio.terra.buffer.generated.model.GoogleProjectUid)1 ResourceConfig (bio.terra.buffer.generated.model.ResourceConfig)1 JanitorException (bio.terra.cloudres.common.JanitorException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)1 JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)1 TopicName (com.google.pubsub.v1.TopicName)1 Test (org.junit.jupiter.api.Test)1