Search in sources :

Example 1 with UserActionableException

use of bio.terra.cli.exception.UserActionableException in project terra-cli by DataBiosphere.

the class BqDataset method execute.

/**
 * Update a BigQuery dataset in the workspace.
 */
@Override
protected void execute() {
    workspaceOption.overrideIfSpecified();
    // all update parameters are optional, but make sure at least one is specified
    if (!resourceUpdateOptions.isDefined() && !bqDatasetLifetimeOptions.isDefined() && !bqDatasetNewIds.isDefined()) {
        throw new UserActionableException("Specify at least one property to update.");
    }
    // get the resource and make sure it's the right type
    bio.terra.cli.businessobject.resource.BqDataset resource = Context.requireWorkspace().getResource(resourceUpdateOptions.resourceNameOption.name).castToType(Resource.Type.BQ_DATASET);
    if (resource.getStewardshipType().equals(StewardshipType.REFERENCED)) {
        if (bqDatasetLifetimeOptions.isDefined()) {
            throw new UserActionableException("Default lifetime can only be updated for controlled resources.");
        }
        UpdateReferencedBqDatasetParams.Builder updateParams = new UpdateReferencedBqDatasetParams.Builder().resourceParams(resourceUpdateOptions.populateMetadataFields().build()).datasetId(bqDatasetNewIds.getNewBqDatasetId()).projectId(bqDatasetNewIds.getNewGcpProjectId());
        resource.updateReferenced(updateParams.build());
    } else {
        resource.updateControlled(new UpdateControlledBqDatasetParams.Builder().resourceFields(resourceUpdateOptions.populateMetadataFields().build()).defaultPartitionLifetimeSeconds(bqDatasetLifetimeOptions.getDefaultPartitionLifetimeSeconds()).defaultTableLifetimeSeconds(bqDatasetLifetimeOptions.getDefaultTableLifetimeSeconds()).build());
    }
    formatOption.printReturnValue(new UFBqDataset(resource), BqDataset::printText);
}
Also used : UFBqDataset(bio.terra.cli.serialization.userfacing.resource.UFBqDataset) UpdateReferencedBqDatasetParams(bio.terra.cli.serialization.userfacing.input.UpdateReferencedBqDatasetParams) UserActionableException(bio.terra.cli.exception.UserActionableException) UFBqDataset(bio.terra.cli.serialization.userfacing.resource.UFBqDataset) UpdateControlledBqDatasetParams(bio.terra.cli.serialization.userfacing.input.UpdateControlledBqDatasetParams) WorkspaceOverride(bio.terra.cli.command.shared.options.WorkspaceOverride)

Example 2 with UserActionableException

use of bio.terra.cli.exception.UserActionableException in project terra-cli by DataBiosphere.

the class BqTable method execute.

/**
 * Update a BigQuery dataset in the workspace.
 */
@Override
protected void execute() {
    workspaceOption.overrideIfSpecified();
    // all update parameters are optional, but make sure at least one is specified
    if (!resourceUpdateOptions.isDefined() && !bqDatasetNewIds.isDefined() && newBqTableId == null) {
        throw new UserActionableException("Specify at least one property to update.");
    }
    // get the resource and make sure it's the right type
    bio.terra.cli.businessobject.resource.BqTable resource = Context.requireWorkspace().getResource(resourceUpdateOptions.resourceNameOption.name).castToType(Resource.Type.BQ_TABLE);
    UpdateReferencedBqTableParams.Builder bqTableParams = new UpdateReferencedBqTableParams.Builder().resourceParams(resourceUpdateOptions.populateMetadataFields().build()).tableId(newBqTableId).datasetId(bqDatasetNewIds.getNewBqDatasetId()).projectId(bqDatasetNewIds.getNewGcpProjectId());
    resource.updateReferenced(bqTableParams.build());
    formatOption.printReturnValue(new UFBqTable(resource), BqTable::printText);
}
Also used : UFBqTable(bio.terra.cli.serialization.userfacing.resource.UFBqTable) UserActionableException(bio.terra.cli.exception.UserActionableException) UFBqTable(bio.terra.cli.serialization.userfacing.resource.UFBqTable) UpdateReferencedBqTableParams(bio.terra.cli.serialization.userfacing.input.UpdateReferencedBqTableParams) WorkspaceOverride(bio.terra.cli.command.shared.options.WorkspaceOverride)

Example 3 with UserActionableException

use of bio.terra.cli.exception.UserActionableException in project terra-cli by DataBiosphere.

the class GcsBucket method execute.

/**
 * Update a bucket in the workspace.
 */
@Override
protected void execute() {
    workspaceOption.overrideIfSpecified();
    // all update parameters are optional, but make sure at least one is specified
    if (!resourceUpdateOptions.isDefined() && !storageClassOption.isDefined() && !lifecycleOptions.isDefined() && newBucketName.getNewBucketName() == null) {
        throw new UserActionableException("Specify at least one property to update.");
    }
    // get the resource and make sure it's the right type
    bio.terra.cli.businessobject.resource.GcsBucket resource = Context.requireWorkspace().getResource(resourceUpdateOptions.resourceNameOption.name).castToType(Resource.Type.GCS_BUCKET);
    if (resource.getStewardshipType().equals(StewardshipType.REFERENCED)) {
        // some options only apply to controlled resources
        if (storageClassOption.isDefined() || lifecycleOptions.isDefined()) {
            throw new UserActionableException("Storage and lifecycle options can only be updated for controlled resources.");
        }
        UpdateReferencedGcsBucketParams.Builder gcsBucketParams = new UpdateReferencedGcsBucketParams.Builder().resourceParams(resourceUpdateOptions.populateMetadataFields().build()).bucketName(newBucketName.getNewBucketName());
        resource.updateReferenced(gcsBucketParams.build());
    } else {
        resource.updateControlled(new UpdateControlledGcsBucketParams.Builder().resourceFields(resourceUpdateOptions.populateMetadataFields().build()).defaultStorageClass(storageClassOption.storageClass).lifecycle(lifecycleOptions.buildLifecycleObject()).build());
    }
    formatOption.printReturnValue(new UFGcsBucket(resource), GcsBucket::printText);
}
Also used : UpdateReferencedGcsBucketParams(bio.terra.cli.serialization.userfacing.input.UpdateReferencedGcsBucketParams) UserActionableException(bio.terra.cli.exception.UserActionableException) UFGcsBucket(bio.terra.cli.serialization.userfacing.resource.UFGcsBucket) UpdateControlledGcsBucketParams(bio.terra.cli.serialization.userfacing.input.UpdateControlledGcsBucketParams) UFGcsBucket(bio.terra.cli.serialization.userfacing.resource.UFGcsBucket) WorkspaceOverride(bio.terra.cli.command.shared.options.WorkspaceOverride)

Example 4 with UserActionableException

use of bio.terra.cli.exception.UserActionableException in project terra-cli by DataBiosphere.

the class GcsObject method execute.

/**
 * Update a bucket object in the workspace.
 */
@Override
protected void execute() {
    workspaceOption.overrideIfSpecified();
    // all update parameters are optional, but make sure at least one is specified
    if (!resourceUpdateOptions.isDefined() && newObjectName == null && newBucketName.getNewBucketName() == null) {
        throw new UserActionableException("Specify at least one property to update.");
    }
    // get the resource and make sure it's the right type
    bio.terra.cli.businessobject.resource.GcsObject resource = Context.requireWorkspace().getResource(resourceUpdateOptions.resourceNameOption.name).castToType(Resource.Type.GCS_OBJECT);
    UpdateResourceParams updateResourceParams = resourceUpdateOptions.populateMetadataFields().build();
    UpdateReferencedGcsObjectParams gcsObjectParams = new UpdateReferencedGcsObjectParams.Builder().resourceFields(updateResourceParams).bucketName(newBucketName.getNewBucketName()).objectName(newObjectName).build();
    resource.updateReferenced(gcsObjectParams);
    formatOption.printReturnValue(new UFGcsObject(resource), GcsObject::printText);
}
Also used : UFGcsObject(bio.terra.cli.serialization.userfacing.resource.UFGcsObject) UFGcsObject(bio.terra.cli.serialization.userfacing.resource.UFGcsObject) UserActionableException(bio.terra.cli.exception.UserActionableException) UpdateReferencedGcsObjectParams(bio.terra.cli.serialization.userfacing.input.UpdateReferencedGcsObjectParams) UpdateResourceParams(bio.terra.cli.serialization.userfacing.input.UpdateResourceParams) WorkspaceOverride(bio.terra.cli.command.shared.options.WorkspaceOverride)

Example 5 with UserActionableException

use of bio.terra.cli.exception.UserActionableException in project terra-cli by DataBiosphere.

the class BreakGlass method execute.

/**
 * Grant break-glass access to the workspace.
 */
@Override
protected void execute() {
    workspaceOption.overrideIfSpecified();
    // check that the SA key files exist and are valid
    ServiceAccountCredentials userProjectsAdminCredentials;
    ServiceAccountCredentials bigQueryCredentials;
    try {
        final List<String> SA_SCOPES = ImmutableList.of("https://www.googleapis.com/auth/cloud-platform");
        userProjectsAdminCredentials = GoogleOauth.getServiceAccountCredential(Path.of(userProjectAdminSAKeyFile).toFile(), SA_SCOPES);
        bigQueryCredentials = GoogleOauth.getServiceAccountCredential(Path.of(bigQuerySAKeyFile).toFile(), SA_SCOPES);
    } catch (IOException ioEx) {
        throw new UserActionableException("Error reading break-glass SA key files.", ioEx);
    }
    // require that the requester is a workspace owner
    Workspace currentWorkspace = Context.requireWorkspace();
    Optional<WorkspaceUser> granteeWorkspaceUser = WorkspaceUser.list(currentWorkspace).stream().filter(user -> user.getEmail().equalsIgnoreCase(granteeEmail)).findAny();
    if (granteeWorkspaceUser.isEmpty() || !granteeWorkspaceUser.get().getRoles().contains(WorkspaceUser.Role.OWNER)) {
        updateRequestsCatalogWithFailure(bigQueryCredentials, "Requestor is not a workspace owner.");
        throw new UserActionableException("The break-glass requester must be an owner of the workspace.");
    }
    // grant the user's proxy group the Editor role on the workspace project
    String granteeProxyGroupEmail = currentWorkspace.grantBreakGlass(granteeEmail, userProjectsAdminCredentials);
    // update the central BigQuery dataset with details of this request
    updateRequestsCatalogWithSuccess(bigQueryCredentials, granteeProxyGroupEmail);
    OUT.println("Break-glass access successfully granted to: " + granteeEmail);
}
Also used : Context(bio.terra.cli.businessobject.Context) BigQueryError(com.google.cloud.bigquery.BigQueryError) Workspace(bio.terra.cli.businessobject.Workspace) WorkspaceUser(bio.terra.cli.businessobject.WorkspaceUser) Date(java.util.Date) TableId(com.google.cloud.bigquery.TableId) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) BigQuery(com.google.cloud.bigquery.BigQuery) BigQueryOptions(com.google.cloud.bigquery.BigQueryOptions) DateTime(com.google.api.client.util.DateTime) BaseCommand(bio.terra.cli.command.shared.BaseCommand) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Command(picocli.CommandLine.Command) Path(java.nio.file.Path) CommandLine(picocli.CommandLine) Logger(org.slf4j.Logger) UserActionableException(bio.terra.cli.exception.UserActionableException) InsertAllRequest(com.google.cloud.bigquery.InsertAllRequest) IOException(java.io.IOException) UUID(java.util.UUID) List(java.util.List) GoogleOauth(bio.terra.cli.service.GoogleOauth) WorkspaceOverride(bio.terra.cli.command.shared.options.WorkspaceOverride) InsertAllResponse(com.google.cloud.bigquery.InsertAllResponse) Optional(java.util.Optional) ServiceAccountCredentials(com.google.auth.oauth2.ServiceAccountCredentials) SystemException(bio.terra.cli.exception.SystemException) UserActionableException(bio.terra.cli.exception.UserActionableException) ServiceAccountCredentials(com.google.auth.oauth2.ServiceAccountCredentials) IOException(java.io.IOException) WorkspaceUser(bio.terra.cli.businessobject.WorkspaceUser) Workspace(bio.terra.cli.businessobject.Workspace) WorkspaceOverride(bio.terra.cli.command.shared.options.WorkspaceOverride)

Aggregations

UserActionableException (bio.terra.cli.exception.UserActionableException)13 WorkspaceOverride (bio.terra.cli.command.shared.options.WorkspaceOverride)6 Workspace (bio.terra.cli.businessobject.Workspace)2 Path (java.nio.file.Path)2 UUID (java.util.UUID)2 Config (bio.terra.cli.businessobject.Config)1 Context (bio.terra.cli.businessobject.Context)1 Resource (bio.terra.cli.businessobject.Resource)1 WorkspaceUser (bio.terra.cli.businessobject.WorkspaceUser)1 GcpNotebook (bio.terra.cli.businessobject.resource.GcpNotebook)1 BaseCommand (bio.terra.cli.command.shared.BaseCommand)1 SystemException (bio.terra.cli.exception.SystemException)1 UpdateControlledBqDatasetParams (bio.terra.cli.serialization.userfacing.input.UpdateControlledBqDatasetParams)1 UpdateControlledGcsBucketParams (bio.terra.cli.serialization.userfacing.input.UpdateControlledGcsBucketParams)1 UpdateReferencedBqDatasetParams (bio.terra.cli.serialization.userfacing.input.UpdateReferencedBqDatasetParams)1 UpdateReferencedBqTableParams (bio.terra.cli.serialization.userfacing.input.UpdateReferencedBqTableParams)1 UpdateReferencedGcsBucketParams (bio.terra.cli.serialization.userfacing.input.UpdateReferencedGcsBucketParams)1 UpdateReferencedGcsObjectParams (bio.terra.cli.serialization.userfacing.input.UpdateReferencedGcsObjectParams)1 UpdateReferencedGitRepoParams (bio.terra.cli.serialization.userfacing.input.UpdateReferencedGitRepoParams)1 UpdateResourceParams (bio.terra.cli.serialization.userfacing.input.UpdateResourceParams)1