def _process_operation(operation_pb): """Processes a create protobuf response. :type operation_pb: :class:`google.longrunning.operations_pb2.Operation` :param operation_pb: The long-running operation response from a Create/Update/Undelete cluster request. :rtype: tuple :returns: A pair of an integer and datetime stamp. The integer is the ID of the operation (``operation_id``) and the timestamp when the create operation began (``operation_begin``). :raises: :class:`ValueError <exceptions.ValueError>` if the operation name doesn't match the :data:`_OPERATION_NAME_RE` regex. """ match = _OPERATION_NAME_RE.match(operation_pb.name) if match is None: raise ValueError( 'Operation name was not in the expected ' 'format after a cluster modification.', operation_pb.name) operation_id = int(match.group('operation_id')) request_metadata = _parse_pb_any_to_native(operation_pb.metadata) operation_begin = _pb_timestamp_to_datetime(request_metadata.request_time) return operation_id, operation_begin
def _get_value_from_value_pb(value_pb): """Given a protobuf for a Value, get the correct value. The Cloud Datastore Protobuf API returns a Property Protobuf which has one value set and the rest blank. This function retrieves the the one value provided. Some work is done to coerce the return value into a more useful type (particularly in the case of a timestamp value, or a key value). :type value_pb: :class:`gcloud.datastore._generated.entity_pb2.Value` :param value_pb: The Value Protobuf. :returns: The value provided by the Protobuf. :raises: :class:`ValueError <exceptions.ValueError>` if no value type has been set. """ value_type = value_pb.WhichOneof('value_type') if value_type == 'timestamp_value': result = _pb_timestamp_to_datetime(value_pb.timestamp_value) elif value_type == 'key_value': result = key_from_protobuf(value_pb.key_value) elif value_type == 'boolean_value': result = value_pb.boolean_value elif value_type == 'double_value': result = value_pb.double_value elif value_type == 'integer_value': result = value_pb.integer_value elif value_type == 'string_value': result = value_pb.string_value elif value_type == 'blob_value': result = value_pb.blob_value elif value_type == 'entity_value': result = entity_from_protobuf(value_pb.entity_value) elif value_type == 'array_value': result = [ _get_value_from_value_pb(value) for value in value_pb.array_value.values ] elif value_type == 'geo_point_value': result = GeoPoint(value_pb.geo_point_value.latitude, value_pb.geo_point_value.longitude) elif value_type == 'null_value': result = None else: raise ValueError('Value protobuf did not have any value set') return result
def _process_operation(operation_pb): """Processes a create protobuf response. :type operation_pb: :class:`google.longrunning.operations_pb2.Operation` :param operation_pb: The long-running operation response from a Create/Update/Undelete cluster request. :rtype: tuple :returns: A pair of an integer and datetime stamp. The integer is the ID of the operation (``operation_id``) and the timestamp when the create operation began (``operation_begin``). :raises: :class:`ValueError <exceptions.ValueError>` if the operation name doesn't match the :data:`_OPERATION_NAME_RE` regex. """ match = _OPERATION_NAME_RE.match(operation_pb.name) if match is None: raise ValueError('Operation name was not in the expected ' 'format after a cluster modification.', operation_pb.name) operation_id = int(match.group('operation_id')) request_metadata = _parse_pb_any_to_native(operation_pb.metadata) operation_begin = _pb_timestamp_to_datetime( request_metadata.request_time) return operation_id, operation_begin
def _process_operation(operation_pb): """Processes a create protobuf response. :type operation_pb: :class:`google.longrunning.operations_pb2.Operation` :param operation_pb: The long-running operation response from a Create/Update/Undelete instance request. :rtype: (int, str, datetime) :returns: (operation_id, location_id, operation_begin). :raises: :class:`ValueError <exceptions.ValueError>` if the operation name doesn't match the :data:`_OPERATION_NAME_RE` regex. """ match = _OPERATION_NAME_RE.match(operation_pb.name) if match is None: raise ValueError('Operation name was not in the expected ' 'format after instance creation.', operation_pb.name) location_id = match.group('location_id') operation_id = int(match.group('operation_id')) request_metadata = _parse_pb_any_to_native(operation_pb.metadata) operation_begin = _pb_timestamp_to_datetime( request_metadata.request_time) return operation_id, location_id, operation_begin
def _get_value_from_value_pb(value_pb): """Given a protobuf for a Value, get the correct value. The Cloud Datastore Protobuf API returns a Property Protobuf which has one value set and the rest blank. This function retrieves the the one value provided. Some work is done to coerce the return value into a more useful type (particularly in the case of a timestamp value, or a key value). :type value_pb: :class:`gcloud.datastore._generated.entity_pb2.Value` :param value_pb: The Value Protobuf. :rtype: object :returns: The value provided by the Protobuf. :raises: :class:`ValueError <exceptions.ValueError>` if no value type has been set. """ value_type = value_pb.WhichOneof('value_type') if value_type == 'timestamp_value': result = _pb_timestamp_to_datetime(value_pb.timestamp_value) elif value_type == 'key_value': result = key_from_protobuf(value_pb.key_value) elif value_type == 'boolean_value': result = value_pb.boolean_value elif value_type == 'double_value': result = value_pb.double_value elif value_type == 'integer_value': result = value_pb.integer_value elif value_type == 'string_value': result = value_pb.string_value elif value_type == 'blob_value': result = value_pb.blob_value elif value_type == 'entity_value': result = entity_from_protobuf(value_pb.entity_value) elif value_type == 'array_value': result = [_get_value_from_value_pb(value) for value in value_pb.array_value.values] elif value_type == 'geo_point_value': result = GeoPoint(value_pb.geo_point_value.latitude, value_pb.geo_point_value.longitude) elif value_type == 'null_value': result = None else: raise ValueError('Value protobuf did not have any value set') return result
def _process_operation(operation_pb): """Processes a create protobuf response. :type operation_pb: :class:`google.longrunning.operations_pb2.Operation` :param operation_pb: The long-running operation response from a Create/Update/Undelete instance request. :rtype: (int, str, datetime) :returns: (operation_id, location_id, operation_begin). :raises: :class:`ValueError <exceptions.ValueError>` if the operation name doesn't match the :data:`_OPERATION_NAME_RE` regex. """ match = _OPERATION_NAME_RE.match(operation_pb.name) if match is None: raise ValueError( 'Operation name was not in the expected ' 'format after instance creation.', operation_pb.name) location_id = match.group('location_id') operation_id = int(match.group('operation_id')) request_metadata = _parse_pb_any_to_native(operation_pb.metadata) operation_begin = _pb_timestamp_to_datetime(request_metadata.request_time) return operation_id, location_id, operation_begin
def _callFUT(self, timestamp): from gcloud._helpers import _pb_timestamp_to_datetime return _pb_timestamp_to_datetime(timestamp)
def test_write_entries_w_extra_properties(self): # pylint: disable=too-many-statements from datetime import datetime from google.logging.type.log_severity_pb2 import WARNING from google.logging.v2.log_entry_pb2 import LogEntry from gcloud._helpers import UTC, _pb_timestamp_to_datetime NOW = datetime.utcnow().replace(tzinfo=UTC) TEXT = 'TEXT' LOG_PATH = 'projects/%s/logs/%s' % (self.PROJECT, self.LOG_NAME) SEVERITY = 'WARNING' LABELS = { 'foo': 'bar', } IID = 'IID' REQUEST_METHOD = 'GET' REQUEST_URL = 'http://example.com/requested' STATUS = 200 REQUEST_SIZE = 256 RESPONSE_SIZE = 1024 REFERRER_URL = 'http://example.com/referer' USER_AGENT = 'Agent/1.0' REMOTE_IP = '1.2.3.4' REQUEST = { 'requestMethod': REQUEST_METHOD, 'requestUrl': REQUEST_URL, 'status': STATUS, 'requestSize': REQUEST_SIZE, 'responseSize': RESPONSE_SIZE, 'referer': REFERRER_URL, 'userAgent': USER_AGENT, 'remoteIp': REMOTE_IP, 'cacheHit': False, } PRODUCER = 'PRODUCER' OPID = 'OPID' OPERATION = { 'producer': PRODUCER, 'id': OPID, 'first': False, 'last': True, } ENTRY = { 'logName': LOG_PATH, 'resource': {'type': 'global'}, 'textPayload': TEXT, 'severity': SEVERITY, 'labels': LABELS, 'insertId': IID, 'timestamp': NOW, 'httpRequest': REQUEST, 'operation': OPERATION, } gax_api = _GAXLoggingAPI() api = self._makeOne(gax_api) api.write_entries([ENTRY]) entries, log_name, resource, labels, partial_success, options = ( gax_api._write_log_entries_called_with) self.assertEqual(len(entries), 1) entry = entries[0] self.assertTrue(isinstance(entry, LogEntry)) self.assertEqual(entry.log_name, LOG_PATH) self.assertEqual(entry.resource.type, 'global') self.assertEqual(entry.text_payload, TEXT) self.assertEqual(entry.severity, WARNING) self.assertEqual(entry.labels, LABELS) self.assertEqual(entry.insert_id, IID) stamp = _pb_timestamp_to_datetime(entry.timestamp) self.assertEqual(stamp, NOW) request = entry.http_request self.assertEqual(request.request_method, REQUEST_METHOD) self.assertEqual(request.request_url, REQUEST_URL) self.assertEqual(request.status, STATUS) self.assertEqual(request.request_size, REQUEST_SIZE) self.assertEqual(request.response_size, RESPONSE_SIZE) self.assertEqual(request.referer, REFERRER_URL) self.assertEqual(request.user_agent, USER_AGENT) self.assertEqual(request.remote_ip, REMOTE_IP) self.assertEqual(request.cache_hit, False) operation = entry.operation self.assertEqual(operation.producer, PRODUCER) self.assertEqual(operation.id, OPID) self.assertFalse(operation.first) self.assertTrue(operation.last) self.assertEqual(log_name, None) self.assertEqual(resource, None) self.assertEqual(labels, None) self.assertEqual(partial_success, False) self.assertEqual(options, None)
def _pb_timestamp_to_rfc3339(timestamp_pb): """Helper for :func:_log_entry_pb_to_mapping""" timestamp = _pb_timestamp_to_datetime(timestamp_pb) return _datetime_to_rfc3339(timestamp)