Search in sources :

Example 6 with WsmApplication

use of bio.terra.workspace.service.workspace.model.WsmApplication in project terra-workspace-manager by DataBiosphere.

the class ControlledResourceService method getAssociatedApp.

/**
 * When creating application-owned resources, we need to hold the UUID of the application in the
 * ControlledResource object. On the one hand, maybe this should be done as part of the create
 * flight. On the other hand, the pattern we have is to assemble the complete ControlledResource
 * in the controller and have that class be immutable. So we do this lookup and error check early
 * on. Throws ApplicationNotFound if there is no matching application record.
 *
 * @param managedBy the managed by type
 * @param userRequest the user request
 * @return null if not an application managed resource; application UUID otherwise
 */
@Nullable
public UUID getAssociatedApp(ManagedByType managedBy, AuthenticatedUserRequest userRequest) {
    if (managedBy != ManagedByType.MANAGED_BY_APPLICATION) {
        return null;
    }
    String applicationEmail = SamRethrow.onInterrupted(() -> samService.getUserEmailFromSam(userRequest), "get application email");
    WsmApplication application = applicationDao.getApplicationByEmail(applicationEmail);
    return application.getApplicationId();
}
Also used : WsmApplication(bio.terra.workspace.service.workspace.model.WsmApplication) Nullable(javax.annotation.Nullable)

Example 7 with WsmApplication

use of bio.terra.workspace.service.workspace.model.WsmApplication in project terra-workspace-manager by DataBiosphere.

the class WsmApplicationService method appFromConfig.

@VisibleForTesting
Optional<WsmApplication> appFromConfig(App config) {
    try {
        if (config.getIdentifier() == null || config.getServiceAccount() == null || config.getState() == null) {
            logError("Invalid application configuration: missing some required fields (identifier, service-account, state)");
            return Optional.empty();
        }
        UUID applicationId = UUID.fromString(config.getIdentifier());
        WsmApplicationState state = WsmApplicationState.fromString(config.getState());
        if (!EmailValidator.getInstance(false, true).isValid(config.getServiceAccount())) {
            logError("Invalid application configuration: service account is not a valid email address");
            return Optional.empty();
        }
        // We keep everything homogeneously lowercase
        String serviceAccount = StringUtils.lowerCase(config.getServiceAccount());
        return Optional.of(new WsmApplication().applicationId(applicationId).displayName(config.getName()).description(config.getDescription()).serviceAccount(serviceAccount).state(state));
    } catch (IllegalArgumentException e) {
        logError("Invalid application configuration: invalid UUID format", e);
    } catch (InvalidApplicationConfigException e) {
        logError("Invalid application configuration: state must be operating, deprecated, or decommissioned", e);
    }
    return Optional.empty();
}
Also used : WsmApplication(bio.terra.workspace.service.workspace.model.WsmApplication) WsmApplicationState(bio.terra.workspace.service.workspace.model.WsmApplicationState) UUID(java.util.UUID) InvalidApplicationConfigException(bio.terra.workspace.service.workspace.exceptions.InvalidApplicationConfigException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 8 with WsmApplication

use of bio.terra.workspace.service.workspace.model.WsmApplication in project terra-workspace-manager by DataBiosphere.

the class WsmApplicationService method buildAppMap.

@VisibleForTesting
Map<UUID, WsmDbApplication> buildAppMap() {
    List<WsmApplication> dbApps = applicationDao.listApplications();
    Map<UUID, WsmDbApplication> dbAppMap = new HashMap<>();
    for (WsmApplication app : dbApps) {
        dbAppMap.put(app.getApplicationId(), new WsmDbApplication(app));
    }
    return dbAppMap;
}
Also used : WsmApplication(bio.terra.workspace.service.workspace.model.WsmApplication) HashMap(java.util.HashMap) UUID(java.util.UUID) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 9 with WsmApplication

use of bio.terra.workspace.service.workspace.model.WsmApplication in project terra-workspace-manager by DataBiosphere.

the class ApplicationUnitTest method processAppTest.

// This test writes to the database, so conflicts with the missingConfigTest
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
@Test
public void processAppTest() {
    // Each "pass" in the test represents starting up with a different configuration.
    // Start with an empty map == no data in the database
    Map<UUID, WsmDbApplication> dbAppMap = new HashMap<>();
    WsmApplication wsmApp = makeWsmApp();
    // -- First Pass --
    // Create a new application in the database
    appService.enableTestMode();
    appService.processApp(wsmApp, dbAppMap);
    assertMessage(0, INFO_CREATED);
    // Retrieve apps from the database and validate the data round-trip
    // There can be stray applications in the database, so we use greater/equal assert
    List<WsmApplication> wsmApps = appDao.listApplications();
    assertThat(wsmApps.size(), greaterThanOrEqualTo(1));
    assertThat(wsmApps, hasItem(wsmApp));
    // Trying to create a duplicate new application. This is not caught in the map, but
    // the database create fails on a PK constraint.
    appService.processApp(wsmApp, dbAppMap);
    assertMessage(1, ERROR_CREATE_FAILED);
    // -- Second pass --
    // Rebuild the db app map with wsmApp in it
    dbAppMap = appService.buildAppMap();
    appService.enableTestMode();
    appService.processApp(wsmApp, dbAppMap);
    assertMessage(0, INFO_NOCHANGE);
    // Process it again with the same app map; should flag it as a duplicate
    appService.processApp(wsmApp, dbAppMap);
    assertMessage(1, ERROR_DUPLICATE);
    // -- 3rd pass -- update name and desc
    dbAppMap = appService.buildAppMap();
    appService.enableTestMode();
    wsmApp.displayName(GOOD_NAME);
    wsmApp.description(GOOD_DESC);
    appService.processApp(wsmApp, dbAppMap);
    assertMessage(0, INFO_UPDATED);
    // -- 4th pass -- State transition to deprecated
    stateTransition(WsmApplicationState.DEPRECATED, INFO_UPDATED);
    // -- 5th pass -- State transition back to operating
    stateTransition(WsmApplicationState.OPERATING, INFO_UPDATED);
    // -- 6th pass -- State transition to decommissioned
    stateTransition(WsmApplicationState.DECOMMISSIONED, INFO_UPDATED);
    // -- 7th pass -- Cannot transition from decommissioned
    stateTransition(WsmApplicationState.OPERATING, ERROR_DECOMMISSIONED);
    // -- 8th pass -- Cannot transition from decommissioned
    stateTransition(WsmApplicationState.DEPRECATED, ERROR_DECOMMISSIONED);
}
Also used : WsmApplication(bio.terra.workspace.service.workspace.model.WsmApplication) HashMap(java.util.HashMap) UUID(java.util.UUID) WsmDbApplication(bio.terra.workspace.service.workspace.WsmApplicationService.WsmDbApplication) DirtiesContext(org.springframework.test.annotation.DirtiesContext) Test(org.junit.jupiter.api.Test) BaseUnitTest(bio.terra.workspace.common.BaseUnitTest)

Example 10 with WsmApplication

use of bio.terra.workspace.service.workspace.model.WsmApplication in project terra-workspace-manager by DataBiosphere.

the class ApplicationConfigurationTest method configurationTest.

@Test
public void configurationTest() {
    // This test has to be in sync with the contents of application-configuration-test.yml
    List<WsmApplication> wsmApps = appDao.listApplications();
    assertEquals(2, wsmApps.size());
    for (WsmApplication wsmApp : wsmApps) {
        if (wsmApp.getApplicationId().equals(LEO_UUID)) {
            checkLeo(wsmApp);
        } else if (wsmApp.getApplicationId().equals(CARMEN_UUID)) {
            checkCarmen(wsmApp);
        } else {
            fail();
        }
    }
}
Also used : WsmApplication(bio.terra.workspace.service.workspace.model.WsmApplication) BaseTest(bio.terra.workspace.common.BaseTest) Test(org.junit.jupiter.api.Test)

Aggregations

WsmApplication (bio.terra.workspace.service.workspace.model.WsmApplication)12 UUID (java.util.UUID)5 Test (org.junit.jupiter.api.Test)4 FlightMap (bio.terra.stairway.FlightMap)3 BaseUnitTest (bio.terra.workspace.common.BaseUnitTest)3 WsmDbApplication (bio.terra.workspace.service.workspace.WsmApplicationService.WsmDbApplication)3 HashMap (java.util.HashMap)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 DirtiesContext (org.springframework.test.annotation.DirtiesContext)2 App (bio.terra.workspace.app.configuration.external.WsmApplicationConfiguration.App)1 BaseTest (bio.terra.workspace.common.BaseTest)1 ApplicationNotFoundException (bio.terra.workspace.db.exception.ApplicationNotFoundException)1 InvalidApplicationStateException (bio.terra.workspace.db.exception.InvalidApplicationStateException)1 InvalidApplicationConfigException (bio.terra.workspace.service.workspace.exceptions.InvalidApplicationConfigException)1 WsmApplicationState (bio.terra.workspace.service.workspace.model.WsmApplicationState)1 WsmWorkspaceApplication (bio.terra.workspace.service.workspace.model.WsmWorkspaceApplication)1 Nullable (javax.annotation.Nullable)1