List content policies (v2)

List Data Loss Prevention content policies in a Google Cloud project and location.

Code sample

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();

/**
 * Lists Data Loss Prevention content policies in a project and location.
 *
 * @param {string} projectId - The Google Cloud project ID hosting the content policies.
 * @param {string} locationId - The Google Cloud region or location (for example, 'global', 'us-central1').
 */
async function listContentPolicies(projectId, locationId) {
  try {
    const parent = `projects/${projectId}/locations/${locationId}`;
    const request = {
      parent,
    };

    const [contentPolicies] = await client.listContentPolicies(request);

    if (!contentPolicies || contentPolicies.length === 0) {
      console.log('No content policies found.');
      return;
    }

    for (const policy of contentPolicies) {
      console.log(`Policy: ${policy.name}`);
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Resource not found: ${err.message}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied: ${err.message}`);
    } else if (err.code) {
      console.error(`API error (${err.code}): ${err.message}`);
    } else {
      throw err;
    }
  }
}

module.exports = { listContentPolicies };

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


def list_content_policies(project_id: str, location_id: str) -> None:
    """Lists content policies defined in a Google Cloud project and location.

    Args:
        project_id: The Google Cloud project ID hosting the content policies.
        location_id: The Google Cloud location where the content policies are defined (for example, 'global' or 'us-central1').
    """
    client = dlp_v2.DlpServiceClient()
    parent = f"projects/{project_id}/locations/{location_id}"

    request = dlp_v2.ListContentPoliciesRequest(
        parent=parent,
    )

    try:
        response = client.list_content_policies(request=request)
        found_policy = False
        for policy in response:
            print(f"Policy: {policy.name}")
            found_policy = True

        if not found_policy:
            print(f"No content policies found in {parent}.")
    except google.api_core.exceptions.GoogleAPIError as err:
        print(f"API error encountered: {err}")

What's next

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