def _validate_dimension_value(value): try: value[0] if len(value) > DIMENSION_VALUE_CONSTRAINTS['MAX_LENGTH']: raise exceptions.HTTPUnprocessableEntity( 'Dimension value %s must be 255 characters or less' % value ) except (TypeError, IndexError): raise exceptions.HTTPUnprocessableEntity( 'Dimension value cannot be empty' )
def validate_match(): if (not APPLICATION_TYPE_CONSTRAINTS['PATTERN'] .match(application_type)): raise exceptions.HTTPUnprocessableEntity( 'Application type %s may only contain: "a-z A-Z 0-9 _ - ."' % application_type )
def test_monitor_bulk_rejected(self, __, _): res = _init_resource(self) in_counter = res._logs_in_counter.increment = mock.Mock() bulk_counter = res._bulks_rejected_counter.increment = mock.Mock() rejected_counter = res._logs_rejected_counter.increment = mock.Mock() size_gauge = res._logs_size_gauge.send = mock.Mock() res._get_logs = mock.Mock( side_effect=log_api_exceptions.HTTPUnprocessableEntity('')) log_count = 1 v3_body, _ = _generate_v3_payload(log_count) payload = json.dumps(v3_body) content_length = len(payload) self.simulate_request(ENDPOINT, method='POST', headers={ headers.X_ROLES.name: logs_api.MONITORING_DELEGATE_ROLE, headers.X_TENANT_ID.name: TENANT_ID, 'Content-Type': 'application/json', 'Content-Length': str(content_length) }, body=payload) self.assertEqual(1, bulk_counter.call_count) self.assertEqual(0, in_counter.call_count) self.assertEqual(0, rejected_counter.call_count) self.assertEqual(0, size_gauge.call_count)
def _validate_dimension_name(name): try: if len(name) > DIMENSION_NAME_CONSTRAINTS['MAX_LENGTH']: raise exceptions.HTTPUnprocessableEntity( 'Dimension name %s must be 255 characters or less' % name ) if name[0] == '_': raise exceptions.HTTPUnprocessableEntity( 'Dimension name %s cannot start with underscore (_)' % name ) if not DIMENSION_NAME_CONSTRAINTS['PATTERN'].match(name): raise exceptions.HTTPUnprocessableEntity( 'Dimension name %s may not contain: %s' % (name, '> < = { } ( ) \' " , ; &') ) except (TypeError, IndexError): raise exceptions.HTTPUnprocessableEntity( 'Dimension name cannot be empty' )
def validate_length(): if (len(application_type) > APPLICATION_TYPE_CONSTRAINTS['MAX_LENGTH']): msg = ('Application type {type} must be ' '{length} characters or less') raise exceptions.HTTPUnprocessableEntity( msg.format( type=application_type, length=APPLICATION_TYPE_CONSTRAINTS[ 'MAX_LENGTH'] ) )
def validate_log_message(log_object): """Validates log property. Log property should have message property. Args: log_object (dict): log property """ if 'message' not in log_object: raise exceptions.HTTPUnprocessableEntity( 'Log property should have message' )
def parse_dimensions(dimensions): if not dimensions: raise exceptions.HTTPUnprocessableEntity('Dimension are required') new_dimensions = {} dimensions = map(str.strip, dimensions.split(',')) for dim in dimensions: if not dim: raise exceptions.HTTPUnprocessableEntity( 'Dimension cannot be empty') elif ':' not in dim: raise exceptions.HTTPUnprocessableEntity( '%s is not a valid dimension' % dim) dim = dim.split(':') name = str(dim[0].strip()) if dim[0] else None value = str(dim[1].strip()) if dim[1] else None if name and value: new_dimensions.update({name: value}) return new_dimensions
def validate_dimensions(dimensions): """Validates dimensions type. Empty dimensions are not being validated. For details see: :param dict dimensions: dimensions to validate * :py:data:`DIMENSION_NAME_CONSTRAINTS` * :py:data:`DIMENSION_VALUE_CONSTRAINTS` """ try: for dim_name, dim_value in six.iteritems(dimensions): _validate_dimension_name(dim_name) _validate_dimension_value(dim_value) except AttributeError: raise exceptions.HTTPUnprocessableEntity( 'Dimensions %s must be a dictionary (map)' % dimensions)
def test_monitor_not_all_logs_ok(self, __, _): res = _init_resource(self) in_counter = res._logs_in_counter.increment = mock.Mock() bulk_counter = res._bulks_rejected_counter.increment = mock.Mock() rejected_counter = res._logs_rejected_counter.increment = mock.Mock() size_gauge = res._logs_size_gauge.send = mock.Mock() log_count = 5 reject_logs = 1 v3_body, _ = _generate_v3_payload(log_count) payload = json.dumps(v3_body) content_length = len(payload) side_effects = [{} for ___ in range(log_count - reject_logs)] side_effects.append(log_api_exceptions.HTTPUnprocessableEntity('')) res._processor._get_dimensions = mock.Mock(side_effect=side_effects) self.simulate_request(ENDPOINT, method='POST', headers={ headers.X_ROLES.name: logs_api.MONITORING_DELEGATE_ROLE, headers.X_TENANT_ID.name: TENANT_ID, 'Content-Type': 'application/json', 'Content-Length': str(content_length) }, body=payload) self.assertEqual(1, bulk_counter.call_count) self.assertEqual(0, bulk_counter.mock_calls[0][2]['value']) self.assertEqual(1, in_counter.call_count) self.assertEqual(log_count - reject_logs, in_counter.mock_calls[0][2]['value']) self.assertEqual(1, rejected_counter.call_count) self.assertEqual(reject_logs, rejected_counter.mock_calls[0][2]['value']) self.assertEqual(1, size_gauge.call_count) self.assertEqual(content_length, size_gauge.mock_calls[0][2]['value'])
def _get_logs(request_body): """Get the logs in the HTTP request body.""" if 'logs' not in request_body: raise exceptions.HTTPUnprocessableEntity( 'Unprocessable Entity Logs not found') return request_body['logs']