def _load_service_model(self, service_name, api_version=None): json_model = self._loader.load_service_model(service_name, 'service-2', api_version=api_version) service_model = ServiceModel(json_model, service_name=service_name) self._register_retries(service_model) return service_model
def test_resource_loads_collections(self, mock_model): model = { 'hasMany': { u'Queues': { 'request': { 'operation': 'ListQueues' }, 'resource': { 'type': 'Queue' } } } } defs = { 'Queue': {} } service_model = ServiceModel({}) mock_model.return_value.name = 'queues' resource = self.load('test', 'test', model, defs, service_model)() self.assertTrue(hasattr(resource, 'queues'), 'Resource should expose queues collection') self.assertIsInstance(resource.queues, CollectionManager, 'Queues collection should be a collection manager')
def setUp(self): self.model = { 'metadata': { 'protocol': 'query', 'apiVersion': '2014-01-01' }, 'documentation': '', 'operations': { 'TestOperation': { 'name': 'TestOperation', 'http': { 'method': 'POST', 'requestUri': '/', }, 'input': { 'shape': 'InputShape' }, } }, 'shapes': { 'InputShape': { 'type': 'structure', 'members': { 'Timestamp': { 'shape': 'StringTestType' }, } }, 'StringTestType': { 'type': 'string', 'min': 15 } } } self.service_model = ServiceModel(self.model)
def get_service_model(self, service, api_version=None): """Get the service model for the service.""" with mock.patch('botocore.loaders.Loader.list_available_services', return_value=[service]): return ServiceModel(self.loader.load_service_model( service, type_name='service-2', api_version=api_version), service_name=service)
def get_model_location(session, service_definition, service_name=None): """Gets the path of where a service-2.json file should go in ~/.aws/models :type session: botocore.session.Session :param session: A session object :type service_definition: dict :param service_definition: The json loaded service definition :type service_name: str :param service_name: The service name to use. If this not provided, this will be determined from a combination of available services and the service definition. :returns: The path to where are model should be placed based on the service defintion and the current services in botocore. """ # Add the ServiceModel abstraction over the service json definition to # make it easier to work with. service_model = ServiceModel(service_definition) # Determine the service_name if not provided if service_name is None: endpoint_prefix = service_model.endpoint_prefix service_name = _get_service_name(session, endpoint_prefix) api_version = service_model.api_version # For the model location we only want the custom data path (~/.aws/models # not the one set by AWS_DATA_PATH) data_path = session.get_component('data_loader').CUSTOMER_DATA_PATH # Use the version of the model to determine the file's naming convention. service_model_name = ('service-%d.json' % int(float(service_definition.get('version', '2.0')))) return os.path.join(data_path, service_name, api_version, service_model_name)
def test_validate_ignores_response_metadata(self): service_response = {'ResponseMetadata': {'foo': 'bar'}} service_model = ServiceModel({ 'documentation': '', 'operations': { 'foo': { 'name': 'foo', 'input': { 'shape': 'StringShape' }, 'output': { 'shape': 'StringShape' } } }, 'shapes': { 'StringShape': { 'type': 'string' } } }) op_name = service_model.operation_names[0] output_shape = service_model.operation_model(op_name).output_shape self.client.meta.service_model = service_model self.stubber.add_response('TestOperation', service_response) self.validate_parameters_mock.assert_called_with({}, output_shape) # Make sure service response hasn't been mutated self.assertEqual(service_response, {'ResponseMetadata': { 'foo': 'bar' }})
def setUp(self): self.service_name = 'baz' self.service_model = ServiceModel( { 'metadata': { 'endpointPrefix': 'bad', }, 'operations': { 'SampleOperation': { 'name': 'SampleOperation', 'input': { 'shape': 'Input' } } }, 'shapes': { 'StringShape': { 'type': 'string' }, 'Input': { 'type': 'structure', 'members': { 'Foo': { 'shape': 'StringShape' } } } } }, self.service_name) self.operation_model = self.service_model.operation_model( 'SampleOperation') self.argument_model = self.operation_model.input_shape.members['Foo'] self.event_emitter = mock.Mock()
def __init__(self, session: Session, service_name: ServiceName): loader = session._loader # pylint: disable=protected-access botocore_session: BotocoreSession = session._session # pylint: disable=protected-access service_data = botocore_session.get_service_data(service_name.boto3_name) self.service_name = service_name self.service_model = ServiceModel(service_data, service_name.boto3_name) self._typed_dict_map: Dict[str, TypeTypedDict] = {} self._waiters_shape: Shape = {} try: self._waiters_shape = loader.load_service_model( service_name.boto3_name, "waiters-2" ) except UnknownServiceError: pass self._paginators_shape: Shape = {} try: self._paginators_shape = loader.load_service_model( service_name.boto3_name, "paginators-1" ) except UnknownServiceError: pass self._resources_shape: Shape = {} try: self._resources_shape = loader.load_service_model( service_name.boto3_name, "resources-1" ) except UnknownServiceError: pass self.logger = get_logger()
def setUp(self): self.model = { 'metadata': {'protocol': 'json', 'apiVersion': '2014-01-01', 'jsonVersion': '1.1', 'targetPrefix': 'foo'}, 'documentation': '', 'operations': { 'TestOperation': { 'name': 'TestOperation', 'http': { 'method': 'POST', 'requestUri': '/', }, 'input': {'shape': 'InputShape'}, } }, 'shapes': { 'InputShape': { 'type': 'structure', 'members': { 'Timestamp': {'shape': 'TimestampType'}, } }, 'TimestampType': { 'type': 'timestamp', } } } self.service_model = ServiceModel(self.model)
def setUp(self): self.model = { 'metadata': {'protocol': 'rest-xml', 'apiVersion': '2014-01-01'}, 'documentation': '', 'operations': { 'TestOperation': { 'name': 'TestOperation', 'http': { 'method': 'POST', 'requestUri': '/', }, 'input': {'shape': 'InputShape'}, } }, 'shapes': { 'InputShape': { 'type': 'structure', 'members': { 'TimestampHeader': { 'shape': 'TimestampType', 'location': 'header', 'locationName': 'x-timestamp' }, } }, 'TimestampType': { 'type': 'timestamp', } } } self.service_model = ServiceModel(self.model)
def test_route53_resource_id(self): event = self.session.create_event('before-parameter-build', 'route53', 'GetHostedZone') params = { 'Id': '/hostedzone/ABC123', 'HostedZoneId': '/hostedzone/ABC123', 'ResourceId': '/hostedzone/DEF456', 'DelegationSetId': '/hostedzone/GHI789', 'Other': '/hostedzone/foo' } operation_def = { 'name': 'GetHostedZone', 'input': { 'shape': 'GetHostedZoneInput' } } service_def = { 'metadata': {}, 'shapes': { 'GetHostedZoneInput': { 'type': 'structure', 'members': { 'Id': { 'shape': 'ResourceId' }, 'HostedZoneId': { 'shape': 'ResourceId' }, 'ResourceId': { 'shape': 'ResourceId' }, 'DelegationSetId': { 'shape': 'DelegationSetId' }, 'Other': { 'shape': 'String' } } }, 'ResourceId': { 'type': 'string' }, 'DelegationSetId': { 'type': 'string' }, 'String': { 'type': 'string' } } } model = OperationModel(operation_def, ServiceModel(service_def)) self.session.emit(event, params=params, model=model) self.assertEqual(params['Id'], 'ABC123') self.assertEqual(params['HostedZoneId'], 'ABC123') self.assertEqual(params['ResourceId'], 'DEF456') self.assertEqual(params['DelegationSetId'], 'GHI789') # This one should have been left alone self.assertEqual(params['Other'], '/hostedzone/foo')
def test_no_output(self): service_model = ServiceModel({ 'operations': { 'SampleOperation': { 'name': 'SampleOperation', 'input': { 'shape': 'SampleOperationInputOutput' }, } }, 'shapes': { 'SampleOperationInput': { 'type': 'structure', 'members': {}, }, 'String': { 'type': 'string' }, }, }) operation_model = service_model.operation_model('SampleOperation') parsed = {} self.injector.inject_attribute_value_output(parsed=parsed, model=operation_model) assert parsed == {}
def setUp(self): self.model = { 'metadata': {'protocol': 'rest-xml', 'apiVersion': '2014-01-01'}, 'documentation': '', 'operations': { 'TestOperation': { 'name': 'TestOperation', 'http': { 'method': 'POST', 'requestUri': '/', }, 'input': {'shape': 'InputShape'}, } }, 'shapes': { 'InputShape': { 'type': 'structure', 'members': { 'Foo': { 'shape': 'FooShape', 'locationName': 'Foo' }, }, 'payload': 'Foo' }, 'FooShape': { 'type': 'list', 'member': {'shape': 'StringShape'} }, 'StringShape': { 'type': 'string', } } } self.service_model = ServiceModel(self.model)
def test_glacier_version_header_added(self): request_dict = { 'headers': {} } model = ServiceModel({'metadata': {'apiVersion': '2012-01-01'}}) handlers.add_glacier_version(model, request_dict) self.assertEqual(request_dict['headers']['x-amz-glacier-version'], '2012-01-01')
def test_ComputingSerializer(self): computing_model = { "metadata": self.computing_model_metadata, "operations": { "ComputingOperation": { "http": { "method": "POST", "requestUri": "/api/" }, "input": { "shape": "ComputingOperationRequest" }, "name": "ComputingOperation", "output": { "shape": "ComputingOperationResult" } } }, "shapes": { "ComputingOperationRequest": { "members": { "Parameter": { "locationName": "Parameter", "shape": "String" } }, "name": "ComputingOperationRequest", "type": "structure" }, "ComputingOperationResult": { "members": { "Response": { "locationName": "Response", "shape": "String" } }, "name": "ComputingOperationResult", "type": "structure" }, "String": { "name": "String", "type": "string" }, } } computing_service_model = ServiceModel(computing_model) params = { "Parameter": "test" } computing_serializer = serialize.ComputingSerializer() res = computing_serializer.serialize_to_request( params, computing_service_model.operation_model("ComputingOperation")) assert res["body"] == {"Action": "ComputingOperation", "Parameter": "test", "Version": "3.0"} assert res["headers"] == {"Content-Type": "application/x-www-form-urlencoded; charset=utf-8"} assert res["method"] == "POST" assert res["query_string"] == "" assert res["url_path"] == "/api/"
def test_RdbSerializer(self): rdb_model = { "metadata": self.rdb_model_metadata, "operations": { "RdbOperation": { "http": { "method": "POST", "requestUri": "/" }, "input": { "shape": "RdbOperationRequest" }, "name": "rdbOperation", "output": { "shape": "RdbOperationResult" } } }, "shapes": { "RdbOperationRequest": { "members": { "Parameter": { "locationName": "Parameter", "shape": "String" } }, "name": "RdbOperationRequest", "type": "structure" }, "RdbOperationResult": { "members": { "Response": { "locationName": "Response", "shape": "String" } }, "name": "RdbOperationResult", "type": "structure" }, "String": { "name": "String", "type": "string" }, } } rdb_service_model = ServiceModel(rdb_model) params = { "Parameter": "test" } rdb_serializer = serialize.RdbSerializer() res = rdb_serializer.serialize_to_request( params, rdb_service_model.operation_model("RdbOperation")) assert res["body"] == {"Action": "RdbOperation", "Parameter": "test", "Version": "2013-05-15N2013-12-16"} assert res["headers"] == {"Content-Type": "application/x-www-form-urlencoded; charset=utf-8"} assert res["method"] == "POST" assert res["query_string"] == "" assert res["url_path"] == "/"
def test_route53_resource_id_missing_input_shape(self): event = 'before-parameter-build.route53.GetHostedZone' params = {'HostedZoneId': '/hostedzone/ABC123'} operation_def = {'name': 'GetHostedZone'} service_def = {'metadata': {}, 'shapes': {}} model = OperationModel(operation_def, ServiceModel(service_def)) self.session.emit(event, params=params, model=model) self.assertEqual(params['HostedZoneId'], '/hostedzone/ABC123')
def load_service(service: str, version: str = None, model_type="service-2") -> ServiceModel: """ For example: load_service("sqs", "2012-11-05") """ service_description = loader.load_service_model(service, model_type, version) return ServiceModel(service_description, service)
def setUp(self): self.model = { "metadata": { 'endpointPrefix': 'myservice', 'serviceFullName': 'MyService', }, 'operations': { 'OperationName': { 'name': 'OperationName', 'errors': [ { 'shape': 'ExceptionMissingCode' }, { 'shape': 'ExceptionWithModeledCode' }, ], }, 'AnotherOperationName': { 'name': 'AnotherOperationName', 'errors': [ { 'shape': 'ExceptionForAnotherOperation' }, { 'shape': 'ExceptionWithModeledCode' }, ], } }, 'shapes': { 'ExceptionWithModeledCode': { 'type': 'structure', 'members': {}, 'error': { 'code': 'ModeledCode' }, 'exception': True, }, 'ExceptionMissingCode': { 'type': 'structure', 'members': {}, 'exception': True, }, 'ExceptionForAnotherOperation': { 'type': 'structure', 'members': {}, 'exception': True, } } } self.service_model = ServiceModel(self.model) self.exceptions_factory = ClientExceptionsFactory()
def setUp(self): super(TestCollectionFactory, self).setUp() self.client = mock.Mock() self.client.can_paginate.return_value = False self.parent = mock.Mock() self.parent.meta = ResourceMeta('test', client=self.client) self.resource_factory = ResourceFactory() self.service_model = ServiceModel({}) self.factory = CollectionFactory() self.load = self.factory.load_from_definition
def test_validates_on_empty_output_shape(self): service_model = ServiceModel({ 'documentation': '', 'operations': { 'foo': { 'name': 'foo' } } }) self.client.meta.service_model = service_model with self.assertRaises(ParamValidationError): self.stubber.add_response('TestOperation', {'foo': 'bar'})
def setUp(self): self.service_description = { 'metadata': { 'serviceFullName': 'AWS MyService', 'apiVersion': '2014-01-01', 'endpointPrefix': 'myservice', 'signatureVersion': 'v4', 'protocol': 'query' }, 'operations': {}, 'shapes': {}, } self.service_model = ServiceModel(self.service_description, 'myservice')
def get_service_model(self, service_name, api_version=None): """Get the service model object. :type service_name: string :param service_name: The service name :type api_version: string :param api_version: The API version of the service. If none is provided, then the latest API version will be used. :rtype: L{botocore.model.ServiceModel} :return: The botocore service model for the service. """ service_description = self.get_service_data(service_name, api_version) return ServiceModel(service_description, service_name=service_name)
def get_service_info(endpoint_resolver: EndpointResolver, service_name: str, region: str) -> ServiceInfo: service_model_json = create_loader().load_service_model( service_name, "service-2") service_data = ClientEndpointBridge(endpoint_resolver).resolve( service_name=ServiceModel(service_model_json, service_name=service_name).endpoint_prefix, region_name=region, ) return ServiceInfo( service_name, service_data["metadata"]["hostname"], service_data["endpoint_url"], service_data["metadata"].get("credentialScope"), )
def test_can_generate_operation_with_describe_prefix(self): model = deepcopy(BASIC_MODEL) # Swap ListCertificates for DescribeCertificates. # We should still be able to generate auto-completion data. model['operations']['DescribeCertificates'] = model['operations'].pop( 'ListCertificates') service_model = ServiceModel(model) completion_data = self.heuristic.generate_completion_descriptions( service_model) # Ensure we're using the swapped 'DescribeCertificates' operation. self.assertEqual( completion_data['resources']['Certificate']['operation'], 'DescribeCertificates') self.assertEqual(list(sorted(completion_data['operations'])), [ 'DeleteCertificate', 'DescribeCertificate', 'ExportCertificate', 'GetCertificate', 'ImportCertificate' ])
def setUp(self): self.waiter_config = { 'version': 2, 'waiters': { 'WaiterName': { 'operation': 'Foo', 'delay': 1, 'maxAttempts': 1, 'acceptors': [], }, }, } self.waiter_model = WaiterModel(self.waiter_config) self.service_json_model = { 'metadata': { 'serviceFullName': 'Amazon MyService' }, 'operations': { 'Foo': { 'name': 'Foo', 'input': { 'shape': 'FooInputOutput' }, 'output': { 'shape': 'FooInputOutput' } } }, 'shapes': { 'FooInputOutput': { 'type': 'structure', 'members': { 'bar': { 'shape': 'String', 'documentation': 'Documents bar' } } }, 'String': { 'type': 'string' } } } self.service_model = ServiceModel(self.service_json_model, 'myservice') self.client = mock.Mock() self.client.meta.service_model = self.service_model
def construct_manager(self, cache=None, time=None, side_effect=None): self.service_model = ServiceModel(self.service_description) self.meta = mock.Mock(spec=ClientMeta) self.meta.service_model = self.service_model self.client = mock.Mock() if side_effect is None: side_effect = [{ 'Endpoints': [{ 'Address': 'new.com', 'CachePeriodInMinutes': 2, }] }] self.client.describe_endpoints.side_effect = side_effect self.client.meta = self.meta self.manager = EndpointDiscoveryManager(self.client, cache=cache, current_time=time)
def setUp(self): super(TestResourceCollection, self).setUp() # Minimal definition so things like repr work self.collection_def = { 'request': { 'operation': 'TestOperation' }, 'resource': { 'type': 'Frob' } } self.client = mock.Mock() self.client.can_paginate.return_value = False self.parent = mock.Mock() self.parent.meta = ResourceMeta('test', client=self.client) self.factory = ResourceFactory(mock.Mock()) self.service_model = ServiceModel({})
def test_decode_json_policy(self): parsed = { 'Document': '{"foo": "foobarbaz"}', 'Other': 'bar', } service_def = { 'operations': { 'Foo': { 'output': { 'shape': 'PolicyOutput' }, } }, 'shapes': { 'PolicyOutput': { 'type': 'structure', 'members': { 'Document': { 'shape': 'policyDocumentType' }, 'Other': { 'shape': 'stringType' } } }, 'policyDocumentType': { 'type': 'string' }, 'stringType': { 'type': 'string' }, } } model = ServiceModel(service_def) op_model = model.operation_model('Foo') handlers.json_decode_policies(parsed, op_model) self.assertEqual(parsed['Document'], {'foo': 'foobarbaz'}) no_document = {'Other': 'bar'} handlers.json_decode_policies(no_document, op_model) self.assertEqual(no_document, {'Other': 'bar'})
def test_resource_loads_waiters(self): model = { "waiters": { "Exists": { "waiterName": "BucketExists", "params": [{ "target": "Bucket", "source": "identifier", "name": "Name" }] } } } defs = {'Bucket': {}} service_model = ServiceModel({}) resource = self.load('test', model, defs, service_model)() assert hasattr(resource, 'wait_until_exists')