Ejemplo n.º 1
0
def _process_rpo_update(rule_id, retention_period):
    """Makes a request to update an existing retention rule."""
    # Build the URL for the PUT to update the retention rule
    url = '{}/{}'.format(RETENTION_RULES_ENDPOINT, rule_id)
    body = {'retentionPeriod': retention_period}
    LOGGER.debug('PUT: %s', url)
    LOGGER.debug('Body: %s', body)
    response = requests.put(url, json=body, headers=utils.get_auth_header())
    LOGGER.debug('Response: %s', response.text)
Ejemplo n.º 2
0
def _process_rpo(re_match, event_attributes, object_id):
    """Makes a request to delete a retention rule"""
    sdrs_request = utils.parse_rpo_request(re_match, event_attributes,
                                           object_id)

    url = '{}?projectId={}&dataStorageName={}&type=DATASET'.format(
        RETENTION_RULES_ENDPOINT, sdrs_request.project_id,
        urllib.parse.quote_plus(sdrs_request.data_storage_name))
    LOGGER.debug('DELETE: %s', url)
    response = requests.delete(url, headers=utils.get_auth_header())
    LOGGER.debug('Response: %s', response.text)
Ejemplo n.º 3
0
def handler(event, context):
    """Takes in a pubsub message and invokes a POST based on the message"""
    pub_sub_message = base64.b64decode(event['data']).decode('utf-8')

    if pub_sub_message == 'executor':
        LOGGER.debug('POST: %s', EVENTS_EXECUTION_ENDPOINT)
        response = requests.post(EVENTS_EXECUTION_ENDPOINT,
                                 json={'type': 'POLICY'},
                                 headers=utils.get_auth_header())
        LOGGER.debug('Response: %s', response.text)

    elif pub_sub_message == 'validator':
        LOGGER.debug('POST: %s', EVENTS_VALIDATION_ENDPOINT)
        response = requests.post(EVENTS_VALIDATION_ENDPOINT,
                                 headers=utils.get_auth_header())
        LOGGER.debug('Response: %s', response.text)

    else:
        LOGGER.warn('Unexpected message from PubSub: %s', pub_sub_message)
    return
Ejemplo n.º 4
0
def _process_delete(re_match, event_attributes, object_id):
    """Makes a request to create an immediate USER type retention job"""
    sdrs_request = utils.parse_delete_request(re_match, event_attributes,
                                              object_id)
    url = '{}/execution'.format(EVENTS_ENDPOINT)
    body = {
        'target': sdrs_request.data_storage_name,
        'projectId': sdrs_request.project_id,
        'type': 'USER'
    }
    LOGGER.debug('POST: %s', url)
    LOGGER.debug('Body: %s', body)
    response = requests.post(url, json=body, headers=utils.get_auth_header())
    LOGGER.debug('Response: %s', response.text)
Ejemplo n.º 5
0
def _process_rpo_create(sdrs_request):
    """Makes a request to create a retention rule."""
    body = {
        'dataStorageName': sdrs_request.data_storage_name,
        'projectId': sdrs_request.project_id,
        'retentionPeriod': sdrs_request.retention_period,
        'type': 'DATASET'
    }
    LOGGER.debug('POST: %s', RETENTION_RULES_ENDPOINT)
    LOGGER.debug('Body: %s', body)
    response = requests.post(RETENTION_RULES_ENDPOINT,
                             json=body,
                             headers=utils.get_auth_header())
    LOGGER.debug('Response: %s', response.text)
Ejemplo n.º 6
0
def _process_delete_success(re_match, event_attributes, object_id):
    """Makes a request to notify about the deletion of a directory or dataset"""
    body = {
        'deletedObject':
        'gs://{}/{}'.format(event_attributes['bucketId'], object_id),
        'projectId':
        PROJECT_ID,
        'deletedAt':
        event_attributes['eventTime']
    }
    LOGGER.debug('POST: %s', EVENTS_NOTIFICATION_ENDPOINT)
    LOGGER.debug('Body: %s', body)
    response = requests.post(EVENTS_NOTIFICATION_ENDPOINT,
                             json=body,
                             headers=utils.get_auth_header())
    LOGGER.debug('Response: %s', response.text)
Ejemplo n.º 7
0
def _process_rpo(re_match, event_attributes, object_id):
    """Find outs if the retention rule needs to be created or updated."""
    sdrs_request = utils.parse_rpo_request(re_match, event_attributes,
                                           object_id)

    # Check to see if the retention rule already exists
    url = '{}?projectId={}&dataStorageName={}&type=DATASET'.format(
        RETENTION_RULES_ENDPOINT, sdrs_request.project_id,
        urllib.parse.quote_plus(sdrs_request.data_storage_name))
    LOGGER.debug('GET: %s', url)
    response = requests.get(url, headers=utils.get_auth_header())

    if response.status_code == requests.codes.ok:
        # A 200 response means the rule already exists, so update it
        rule_id = response.json().get('ruleId')
        _process_rpo_update(rule_id, sdrs_request.retention_period)
    elif response.status_code == 404:
        # A 404 response means the rule doesn't exist, so create it
        _process_rpo_create(sdrs_request)
    else:
        LOGGER.error('Unexpected response code %s returned: %s',
                     response.status_code, response.text)