Activate job trigger (v2)

Activate a job trigger to start a new job.

Code sample

Java

To learn how to install and use the client library for Sensitive Data Protection, see Sensitive Data Protection client libraries.

To authenticate to Sensitive Data Protection, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import com.google.cloud.dlp.v2.DlpServiceClient;
import com.google.privacy.dlp.v2.ActivateJobTriggerRequest;
import com.google.privacy.dlp.v2.DlpJob;
import java.io.IOException;

public class ActivateJobTrigger {

  public static void main(String[] args) throws Exception {
    String projectId = "example-project_id";
    String jobTriggerId = "example-job_trigger_id";
    activateJobTrigger(projectId, jobTriggerId);
  }

  public static void activateJobTrigger(String projectId, String jobTriggerId) throws Exception {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (DlpServiceClient client = DlpServiceClient.create()) {
      // Construct the resource name using the template projects/{project_id}/jobTriggers/{job_trigger_id}
      String name = String.format("projects/%s/jobTriggers/%s", projectId, jobTriggerId);

      ActivateJobTriggerRequest request =
          ActivateJobTriggerRequest.newBuilder().setName(name).build();

      DlpJob response = client.activateJobTrigger(request);
      System.out.println("Activated job trigger: " + response.getName());
      System.out.println("Job state: " + response.getState());
    }
  }
}

Node.js

To learn how to install and use the client library for Sensitive Data Protection, see Sensitive Data Protection client libraries.

To authenticate to Sensitive Data Protection, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

'use strict';

const {DlpServiceClient} = require('@google-cloud/dlp');
const {status} = require('@grpc/grpc-js');

const client = new DlpServiceClient();

/**
 * Activates a Cloud DLP job trigger.
 *
 * @param {string} projectId - Google Cloud project ID that owns the job trigger.
 * @param {string} jobTriggerId - Identifier of the JobTrigger to activate.
 */
async function activateJobTrigger(projectId, jobTriggerId) {
  const name = `projects/${projectId}/jobTriggers/${jobTriggerId}`;

  try {
    const request = {
      name,
    };

    const [dlpJob] = await client.activateJobTrigger(request);
    console.log(`Job trigger activated. Resulting job name: ${dlpJob.name}`);
    console.log(`Job state: ${dlpJob.state}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Resource not found for ${name}: ${err.message}`);
    } else if (err.code === status.INVALID_ARGUMENT) {
      console.error(`Invalid request arguments: ${err.message}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied accessing ${name}: ${err.message}`);
    } else {
      console.error(`Failed to activate job trigger: ${err.message}`);
    }
  }
}

module.exports = { activateJobTrigger };

Python

To learn how to install and use the client library for Sensitive Data Protection, see Sensitive Data Protection client libraries.

To authenticate to Sensitive Data Protection, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import google.api_core.exceptions
from google.cloud import dlp_v2

client = dlp_v2.DlpServiceClient()


def activate_job_trigger(project_id: str, job_trigger_id: str) -> None:
    """Activate an existing Cloud DLP job trigger to start scanning immediately.

    Args:
        project_id: The Google Cloud project ID containing the job trigger.
        job_trigger_id: The ID of the JobTrigger to activate. This may be
          caller-specified or server-generated; obtain it from an existing
          JobTrigger resource or ListJobTriggers response.
    """
    name = f"projects/{project_id}/jobTriggers/{job_trigger_id}"

    request = dlp_v2.ActivateJobTriggerRequest(
        name=name,
    )

    try:
        response = client.activate_job_trigger(request=request)
        print(f"Activated job trigger successfully: {response.name}")
        print(f"Job state: {response.state.name}")
    except google.api_core.exceptions.GoogleAPICallError as e:
        print(f"Error activating job trigger: {e.message}")

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.