use of org.pmiops.workbench.model.BillingProjectStatus in project workbench by all-of-us.
the class ProfileController method initializeUserIfNeeded.
private User initializeUserIfNeeded() {
UserAuthentication userAuthentication = userAuthenticationProvider.get();
User user = userAuthentication.getUser();
if (userAuthentication.getUserType() == UserType.SERVICE_ACCOUNT) {
// Service accounts don't need further initialization.
return user;
}
// On first sign-in, create a FC user, billing project, and set the first sign in time.
if (user.getFirstSignInTime() == null) {
// instead use the freeTierBillingProjectStatus.
if (user.getFreeTierBillingProjectName() == null) {
String billingProjectName = createFirecloudUserAndBillingProject(user);
user.setFreeTierBillingProjectName(billingProjectName);
user.setFreeTierBillingProjectStatus(BillingProjectStatus.PENDING);
}
user.setFirstSignInTime(new Timestamp(clock.instant().toEpochMilli()));
try {
return userDao.save(user);
} catch (ObjectOptimisticLockingFailureException e) {
log.log(Level.WARNING, "version conflict for user update", e);
throw new ConflictException("Failed due to concurrent modification");
}
}
// Free tier billing project setup is complete; nothing to do.
if (BillingProjectStatus.READY.equals(user.getFreeTierBillingProjectStatus())) {
return user;
}
// On subsequent sign-ins to the first, attempt to complete the setup of the FC billing project
// and mark the Workbench's project setup as completed. FC project creation is asynchronous, so
// first confirm whether Firecloud claims the project setup is complete.
BillingProjectStatus status = null;
try {
status = fireCloudService.getBillingProjectMemberships().stream().filter(m -> user.getFreeTierBillingProjectName().equals(m.getProjectName())).map(m -> fcToWorkbenchBillingMap.get(m.getCreationStatus())).findFirst().orElse(BillingProjectStatus.NONE);
} catch (ApiException e) {
log.log(Level.WARNING, "failed to retrieve billing projects, continuing", e);
return user;
}
switch(status) {
case NONE:
case PENDING:
log.log(Level.INFO, "free tier project is still initializing, continuing");
return user;
case ERROR:
log.log(Level.SEVERE, String.format("free tier project %s failed to be created", user.getFreeTierBillingProjectName()));
user.setFreeTierBillingProjectStatus(status);
return userDao.save(user);
case READY:
break;
default:
log.log(Level.SEVERE, String.format("unrecognized status '%s'", status));
return user;
}
// notebooks.
try {
fireCloudService.grantGoogleRoleToUser(user.getFreeTierBillingProjectName(), FireCloudService.BIGQUERY_JOB_USER_GOOGLE_ROLE, user.getEmail());
} catch (ApiException e) {
log.log(Level.WARNING, "granting BigQuery role on created free tier billing project failed", e);
// Allow the user to continue, as most workbench functionality will still be usable.
return user;
}
log.log(Level.INFO, "free tier project initialized and BigQuery role granted");
user.setFreeTierBillingProjectStatus(BillingProjectStatus.READY);
return userDao.save(user);
}
Aggregations