def list_dlp_jobs(project, filter_string=None, job_type=None): """Uses the Data Loss Prevention API to lists DLP jobs that match the specified filter in the request. Args: project: The project id to use as a parent resource. filter: (Optional) Allows filtering. Supported syntax: * Filter expressions are made up of one or more restrictions. * Restrictions can be combined by 'AND' or 'OR' logical operators. A sequence of restrictions implicitly uses 'AND'. * A restriction has the form of '<field> <operator> <value>'. * Supported fields/values for inspect jobs: - `state` - PENDING|RUNNING|CANCELED|FINISHED|FAILED - `inspected_storage` - DATASTORE|CLOUD_STORAGE|BIGQUERY - `trigger_name` - The resource name of the trigger that created job. * Supported fields for risk analysis jobs: - `state` - RUNNING|CANCELED|FINISHED|FAILED * The operator must be '=' or '!='. Examples: * inspected_storage = cloud_storage AND state = done * inspected_storage = cloud_storage OR inspected_storage = bigquery * inspected_storage = cloud_storage AND (state = done OR state = canceled) type: (Optional) The type of job. Defaults to 'INSPECT'. Choices: DLP_JOB_TYPE_UNSPECIFIED INSPECT_JOB: The job inspected content for sensitive data. RISK_ANALYSIS_JOB: The job executed a Risk Analysis computation. Returns: None; the response from the API is printed to the terminal. """ # Import the client library. import google.cloud.dlp # Instantiate a client. dlp = google.cloud.dlp_v2.DlpServiceClient() # Convert the project id into a full resource id. parent = dlp.project_path(project) # Job type dictionary job_type_to_int = { "DLP_JOB_TYPE_UNSPECIFIED": google.cloud.dlp.enums.DlpJobType.DLP_JOB_TYPE_UNSPECIFIED, "INSPECT_JOB": google.cloud.dlp.enums.DlpJobType.INSPECT_JOB, "RISK_ANALYSIS_JOB": google.cloud.dlp.enums.DlpJobType.RISK_ANALYSIS_JOB, } # If job type is specified, convert job type to number through enums. if job_type: job_type = job_type_to_int[job_type] # Call the API to get a list of jobs. response = dlp.list_dlp_jobs(parent, filter_=filter_string, type_=job_type) # Iterate over results. for job in response: print("Job: %s; status: %s" % (job.name, job.JobState.Name(job.state)))
def list_dlp_jobs(project, filter_string=None, job_type=None): """Uses the Data Loss Prevention API to lists DLP jobs that match the specified filter in the request. Args: project: The project id to use as a parent resource. filter: (Optional) Allows filtering. Supported syntax: * Filter expressions are made up of one or more restrictions. * Restrictions can be combined by 'AND' or 'OR' logical operators. A sequence of restrictions implicitly uses 'AND'. * A restriction has the form of '<field> <operator> <value>'. * Supported fields/values for inspect jobs: - `state` - PENDING|RUNNING|CANCELED|FINISHED|FAILED - `inspected_storage` - DATASTORE|CLOUD_STORAGE|BIGQUERY - `trigger_name` - The resource name of the trigger that created job. * Supported fields for risk analysis jobs: - `state` - RUNNING|CANCELED|FINISHED|FAILED * The operator must be '=' or '!='. Examples: * inspected_storage = cloud_storage AND state = done * inspected_storage = cloud_storage OR inspected_storage = bigquery * inspected_storage = cloud_storage AND (state = done OR state = canceled) type: (Optional) The type of job. Defaults to 'INSPECT'. Choices: DLP_JOB_TYPE_UNSPECIFIED INSPECT_JOB: The job inspected content for sensitive data. RISK_ANALYSIS_JOB: The job executed a Risk Analysis computation. Returns: None; the response from the API is printed to the terminal. """ # Import the client library. import google.cloud.dlp # Instantiate a client. dlp = google.cloud.dlp.DlpServiceClient() # Convert the project id into a full resource id. parent = dlp.project_path(project) # Job type dictionary job_type_to_int = { 'DLP_JOB_TYPE_UNSPECIFIED': google.cloud.dlp.enums.DlpJobType.DLP_JOB_TYPE_UNSPECIFIED, 'INSPECT_JOB': google.cloud.dlp.enums.DlpJobType.INSPECT_JOB, 'RISK_ANALYSIS_JOB': google.cloud.dlp.enums.DlpJobType.RISK_ANALYSIS_JOB } # If job type is specified, convert job type to number through enums. if job_type: job_type = job_type_to_int[job_type] # Call the API to get a list of jobs. response = dlp.list_dlp_jobs( parent, filter_=filter_string, type_=job_type) # Iterate over results. for job in response: print('Job: %s; status: %s' % (job.name, job.JobState.Name(job.state)))