List connections (v2)

List connections in a specified 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();

/**
 * Use the listConnections method
 *
 * @param {string} projectId - The Google Cloud project ID.
 * @param {string} locationId - The Google Cloud region or location for the connection (for example, 'us-central1' or 'global').
 */
async function listConnections(projectId, locationId) {
  try {
    const parent = `projects/${projectId}/locations/${locationId}`;
    const request = {
      parent,
    };

    const [connections] = await client.listConnections(request);

    console.log(`Retrieved ${connections.length} connection(s).`);
    for (const connection of connections) {
      console.log(`Connection Name: ${connection.name}`);
      console.log(`State: ${connection.state}`);
    }
  } 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 === status.INVALID_ARGUMENT) {
      console.error(`Invalid argument provided: ${err.message}`);
    } else {
      console.error(`Google Cloud API error [Code ${err.code}]: ${err.message}`);
    }
  }
}

module.exports = { listConnections };

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 list_connections(project_id: str, location_id: str) -> None:
    """Lists connections in a specified project and location.

    Args:
        project_id: The Google Cloud project ID.
        location_id: The Google Cloud region or location for the connections
          (for example, 'us-central1' or 'global').
    """
    parent = f"projects/{project_id}/locations/{location_id}"

    request = dlp_v2.ListConnectionsRequest(
        parent=parent,
    )

    try:
        response = client.list_connections(request=request)
        for connection in response:
            print(f"Connection name: {connection.name}")
            print(f"Connection state: {connection.state.name}")
    except google.api_core.exceptions.GoogleAPICallError as error:
        print(f"Failed to list connections: {error}")

What's next

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