Example #1
0
    def test_create_subclasses(self):
        resource_defs = {
            'Frob': {},
            'Chain': {
                'hasMany': {
                    'Frobs': {
                        'request': {
                            'operation': 'GetFrobs'
                        },
                        'resource': {
                            'type': 'Frob'
                        }
                    }
                }
            }
        }
        collection_model = Collection(
            'Frobs', resource_defs['Chain']['hasMany']['Frobs'], resource_defs)

        service_context = ServiceContext(
            service_name='test',
            resource_json_definitions=resource_defs,
            service_model=self.service_model,
            service_waiter_model=None)
        collection_cls = self.load(resource_name='Chain',
                                   collection_model=collection_model,
                                   service_context=service_context,
                                   event_emitter=self.event_emitter)
        collection = collection_cls(collection_model=collection_model,
                                    parent=self.parent,
                                    factory=self.resource_factory,
                                    service_context=service_context)

        assert collection_cls.__name__ == 'test.Chain.FrobsCollectionManager'
        assert isinstance(collection, CollectionManager)

        # Make sure that collection manager created from the factory
        # returns a ResourceCollection.
        assert isinstance(collection.all(), ResourceCollection)

        # Make sure that the collection returned from the collection
        # manager can be chained and return a ResourceCollection as well.
        assert isinstance(collection.all().all(), ResourceCollection)
Example #2
0
def get_sub_resources(
    session: Session, service_name: ServiceName, resource: Boto3ServiceResource
) -> List[Boto3ServiceResource]:
    """
    Initialize ServiceResource sub-resources with fake data.

    Arguments:
        session -- boto3 session.
        service_name -- Target service name.
        resource -- Parent ServiceResource.

    Returns:
        A list of initialized `Boto3ServiceResource`.
    """
    result: List[Boto3ServiceResource] = []
    session_session = session._session  # pylint: disable=protected-access
    loader = session_session.get_component("data_loader")
    assert resource.meta.service_name == service_name.boto3_name
    json_resource_model = loader.load_service_model(service_name.boto3_name, "resources-1")
    service_model = resource.meta.client.meta.service_model
    assert service_model.service_name == service_name.boto3_name
    try:
        service_waiter_model = session_session.get_waiter_model(service_name.boto3_name)
    except UnknownServiceError:
        service_waiter_model = None
    for name in json_resource_model["resources"]:
        resource_model = json_resource_model["resources"][name]
        resource_class = session.resource_factory.load_from_definition(
            resource_name=name,
            single_resource_json_definition=resource_model,
            service_context=ServiceContext(
                service_name=service_name.boto3_name,
                resource_json_definitions=json_resource_model["resources"],
                service_model=service_model,
                service_waiter_model=service_waiter_model,
            ),
        )
        identifiers = resource_class.meta.resource_model.identifiers
        args = ["foo"] * len(identifiers)
        result.append(resource_class(*args, client=get_boto3_client(session, service_name)))

    return result
Example #3
0
    def test_service_action_calls_resource_handler(self, handler_mock, params_mock):
        self.action_def['resource'] = {
            'type': 'Frob',
            'path': 'Container'
        }

        resource = mock.Mock()
        resource.meta = ResourceMeta('test', client=mock.Mock())
        operation = resource.meta.client.get_frobs
        operation.return_value = 'response'

        factory = mock.Mock()
        resource_defs = {}
        service_model = mock.Mock()

        action_model = self.action

        service_context = ServiceContext(
            service_name='test',
            service_model=service_model,
            resource_json_definitions=resource_defs,
            service_waiter_model=None
        )

        action = ServiceAction(
            action_model=action_model, factory=factory,
            service_context=service_context
        )

        handler_mock.return_value.return_value = 'response'

        action(resource)

        handler_mock.assert_called_with(
            search_path='Container', factory=factory,
            resource_model=action_model.resource,
            service_context=service_context,
            operation_name='GetFrobs'
        )
Example #4
0
    def load(
        self,
        resource_name,
        resource_json_definition=None,
        resource_json_definitions=None,
        service_model=None,
    ):
        if resource_json_definition is None:
            resource_json_definition = {}
        if resource_json_definitions is None:
            resource_json_definitions = {}
        service_context = ServiceContext(
            service_name='test',
            resource_json_definitions=resource_json_definitions,
            service_model=service_model,
            service_waiter_model=None,
        )

        return self.factory.load_from_definition(
            resource_name=resource_name,
            single_resource_json_definition=resource_json_definition,
            service_context=service_context,
        )
Example #5
0
 def _document_resources(self, section):
     temp_identifier_value = 'foo'
     loader = self._botocore_session.get_component('data_loader')
     json_resource_model = loader.load_service_model(
         self._service_name, 'resources-1')
     service_model = self._service_resource.meta.client.meta.service_model
     for resource_name in json_resource_model['resources']:
         resource_model = json_resource_model['resources'][resource_name]
         resource_cls = self._session.resource_factory.load_from_definition(
             resource_name=resource_name,
             single_resource_json_definition=resource_model,
             service_context=ServiceContext(
                 service_name=self._service_name,
                 resource_json_definitions=json_resource_model['resources'],
                 service_model=service_model,
                 service_waiter_model=None))
         identifiers = resource_cls.meta.resource_model.identifiers
         args = []
         for _ in identifiers:
             args.append(temp_identifier_value)
         resource = resource_cls(*args, client=self._client)
         ResourceDocumenter(
             resource, self._botocore_session).document_resource(
                 section.add_new_section(resource.meta.resource_model.name))