Skip to content

Commit

Permalink
given a billing profile ID, return all snapshots and datasets that we…
Browse files Browse the repository at this point in the history
…re created using that ID
  • Loading branch information
pshapiro4broad committed Jan 8, 2025
1 parent 2796122 commit 81f1c4e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
29 changes: 23 additions & 6 deletions src/main/java/bio/terra/service/profile/ProfileDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -65,19 +66,16 @@ public BillingProfileModel createBillingProfile(
+ " (:id, :name, :biller, :billing_account_id, :description, :cloud_platform, "
+ " :tenant_id, :subscription_id, :resource_group_name, :application_deployment_name, :created_by)";

String billingAccountId =
Optional.ofNullable(profileRequest.getBillingAccountId()).orElse(null);
String billingAccountId = profileRequest.getBillingAccountId();
String cloudPlatform =
Optional.ofNullable(profileRequest.getCloudPlatform())
.or(() -> Optional.of(CloudPlatform.GCP))
.map(Enum::name)
.get();
UUID tenantId = profileRequest.getTenantId();
UUID subscriptionId = profileRequest.getSubscriptionId();
String resourceGroupName =
Optional.ofNullable(profileRequest.getResourceGroupName()).orElse(null);
String applicationDeploymentName =
Optional.ofNullable(profileRequest.getApplicationDeploymentName()).orElse(null);
String resourceGroupName = profileRequest.getResourceGroupName();
String applicationDeploymentName = profileRequest.getApplicationDeploymentName();

MapSqlParameterSource params =
new MapSqlParameterSource()
Expand Down Expand Up @@ -192,6 +190,25 @@ public boolean deleteBillingProfileById(UUID id) {
}
}

@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public List<ProfileOwnedResource> listProfileOwnedResources(UUID profileId) {
String sql =
"select dataset.id as id, dataset.name as name, dataset.description as description, 'DATASET' as type "
+ "from dataset where dataset.default_profile_id = :profile_id "
+ "union all "
+ "select snapshot.id as id, snapshot.name as name, snapshot.description as description, 'SNAPSHOT' as type "
+ "from snapshot where snapshot.profile_id = :profile_id";
return jdbcTemplate.query(
sql,
Map.of("profile_id", profileId),
(rs, rowNum) ->
new ProfileOwnedResource(
rs.getObject("id", UUID.class),
rs.getString("name"),
rs.getString("description"),
ProfileOwnedResource.Type.valueOf(rs.getString("type"))));
}

/**
* This method is made for use by upgrade, where we need to find all of the old billing profiles
* without regard to visibility.
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/bio/terra/service/profile/ProfileOwnedResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package bio.terra.service.profile;

import java.util.UUID;

public record ProfileOwnedResource(UUID id, String name, String description, Type type) {
public enum Type {
DATASET,
SNAPSHOT,
}
}
7 changes: 5 additions & 2 deletions src/test/java/bio/terra/common/fixtures/DaoOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,15 @@ public Dataset createDataset(String path) throws IOException {
BillingProfileRequestModel profileRequest = ProfileFixtures.randomBillingProfileRequest();
BillingProfileModel billingProfile =
profileDao.createBillingProfile(profileRequest, "testUser");
return createDataset(billingProfile.getId(), path);
}

public Dataset createDataset(UUID billingProfileId, String path) throws IOException {
BillingProfileModel billingProfile = profileDao.getBillingProfileById(billingProfileId);
GoogleProjectResource projectResource = ResourceFixtures.randomProjectResource(billingProfile);
UUID projectId = resourceDao.createProject(projectResource);
projectResource.id(projectId);

return createDataset(billingProfile.getId(), projectId, path);
return createDataset(billingProfileId, projectId, path);
}

public Dataset createDataset(UUID billingProfileId, UUID projectResourceId, String path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.emptyOrNullString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.everyItem;
Expand All @@ -11,13 +13,15 @@

import bio.terra.common.EmbeddedDatabaseTest;
import bio.terra.common.category.Unit;
import bio.terra.common.fixtures.DaoOperations;
import bio.terra.common.fixtures.ProfileFixtures;
import bio.terra.model.BillingProfileModel;
import bio.terra.model.BillingProfileRequestModel;
import bio.terra.model.BillingProfileUpdateModel;
import bio.terra.model.CloudPlatform;
import bio.terra.model.EnumerateBillingProfileModel;
import bio.terra.service.profile.ProfileDao;
import bio.terra.service.profile.ProfileOwnedResource;
import bio.terra.service.profile.exception.ProfileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -42,6 +46,8 @@ class ProfileDaoTest {

@Autowired private ProfileDao profileDao;

@Autowired private DaoOperations daoOperations;

// keeps track of the profiles that are made so they can be cleaned up
private BillingProfileModel makeProfile() {
BillingProfileRequestModel profileRequest = ProfileFixtures.randomBillingProfileRequest();
Expand Down Expand Up @@ -225,4 +231,26 @@ private void assertRequestMatchesResult(
result.getApplicationDeploymentName(),
equalTo(request.getApplicationDeploymentName()));
}

@Test
void listProfileOwnedResources() throws Exception {
UUID profileId = makeProfile().getId();
assertThat(profileDao.listProfileOwnedResources(profileId), empty());

var dataset = daoOperations.createDataset(profileId, "snapshot-test-dataset.json");
var snapshot = daoOperations.createAndIngestSnapshot(dataset, "snapshot-test-snapshot.json");
assertThat(
profileDao.listProfileOwnedResources(profileId),
containsInAnyOrder(
new ProfileOwnedResource(
dataset.getId(),
dataset.getName(),
dataset.getDescription(),
ProfileOwnedResource.Type.DATASET),
new ProfileOwnedResource(
snapshot.getId(),
snapshot.getName(),
snapshot.getDescription(),
ProfileOwnedResource.Type.SNAPSHOT)));
}
}

0 comments on commit 81f1c4e

Please sign in to comment.