diff --git a/src/main/java/bio/terra/service/profile/ProfileDao.java b/src/main/java/bio/terra/service/profile/ProfileDao.java index a5cd549026..4914f41edc 100644 --- a/src/main/java/bio/terra/service/profile/ProfileDao.java +++ b/src/main/java/bio/terra/service/profile/ProfileDao.java @@ -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; @@ -65,8 +66,7 @@ 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)) @@ -74,10 +74,8 @@ public BillingProfileModel createBillingProfile( .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() @@ -192,6 +190,25 @@ public boolean deleteBillingProfileById(UUID id) { } } + @Transactional(propagation = Propagation.REQUIRED, readOnly = true) + public List 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. diff --git a/src/main/java/bio/terra/service/profile/ProfileOwnedResource.java b/src/main/java/bio/terra/service/profile/ProfileOwnedResource.java new file mode 100644 index 0000000000..d1051a6fad --- /dev/null +++ b/src/main/java/bio/terra/service/profile/ProfileOwnedResource.java @@ -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, + } +} diff --git a/src/test/java/bio/terra/common/fixtures/DaoOperations.java b/src/test/java/bio/terra/common/fixtures/DaoOperations.java index 949bcf7842..f023859c70 100644 --- a/src/test/java/bio/terra/common/fixtures/DaoOperations.java +++ b/src/test/java/bio/terra/common/fixtures/DaoOperations.java @@ -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) diff --git a/src/test/java/bio/terra/service/resourcemanagement/ProfileDaoTest.java b/src/test/java/bio/terra/service/resourcemanagement/ProfileDaoTest.java index d3126d1b39..3620bd9b4f 100644 --- a/src/test/java/bio/terra/service/resourcemanagement/ProfileDaoTest.java +++ b/src/test/java/bio/terra/service/resourcemanagement/ProfileDaoTest.java @@ -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; @@ -11,6 +13,7 @@ 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; @@ -18,6 +21,7 @@ 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; @@ -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(); @@ -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))); + } }