alignak_backend_client/backend_client.py
def get_resource_list(self, resource_name, name=''):
        # pylint: disable=too-many-locals, too-many-nested-blocks
        """Get a specific resource list

        If name is not None, it may be a request to get the list of the services of an host.
        """
        try:
            logger.info("Trying to get %s list", resource_name)

            params = {}
            if resource_name in ['host', 'service', 'user']:
                params = {'where': json.dumps({'_is_template': self.model})}

            if resource_name == 'service' and name and '/' in name:
                splitted_name = name.split('/')

                # Get host from name
                response2 = self.backend.get(
                    'host', params={'where': json.dumps({'name': splitted_name[0],
                                                         '_is_template': self.model})})
                if response2['_items']:
                    host = response2['_items'][0]
                    logger.info("Got host '%s' for the service '%s'",
                                splitted_name[0], splitted_name[1])
                else:
                    logger.warning("Not found host '%s'!", splitted_name[0])
                    return False

                params = {'where': json.dumps({'host': host['_id']})}

            if self.embedded and resource_name in self.embedded_resources:
                params.update({'embedded': json.dumps(self.embedded_resources[resource_name])})

            rsp = self.backend.get_all(resource_name, params=params)
            if rsp['_items'] and rsp['_status'] == 'OK':
                response = rsp['_items']

                logger.info("-> found %ss", resource_name)

                # Exists in the backend, we got the element
                if not self.dry_run:
                    logger.info("-> dumping %ss list", resource_name)
                    for item in response:
                        # Filter fields prefixed with an _ (internal backend fields)
                        for field in list(item):
                            if field in ['_created', '_updated', '_etag', '_links', '_status']:
                                item.pop(field)
                                continue

                            # Filter fields prefixed with an _ in embedded items
                            if self.embedded and resource_name in self.embedded_resources and \
                                    field in self.embedded_resources[resource_name]:
                                # Embedded items may be a list or a simple dictionary,
                                # always make it a list
                                embedded_items = item[field]
                                if not isinstance(item[field], list):
                                    embedded_items = [item[field]]
                                # Filter fields in each embedded item
                                for embedded_item in embedded_items:
                                    if not embedded_item:
                                        continue
                                    for embedded_field in list(embedded_item):
                                        if embedded_field.startswith('_'):
                                            embedded_item.pop(embedded_field)

                    filename = self.file_dump(response, 'alignak-%s-list-%ss.json'
                                              % ('model' if self.model else 'object',
                                                 resource_name))
                    if filename:
                        logger.info("-> dumped %ss list to %s", resource_name, filename)
                else:
                    logger.info("Dry-run mode: should have dumped an %s list", resource_name)

                return True
            else:
                logger.warning("-> %s list is empty", resource_name)
                if not self.dry_run:
                    logger.info("-> dumping %ss list", resource_name)
                    filename = self.file_dump([], 'alignak-%s-list-%ss.json'
                                              % ('model' if self.model else 'object',
                                                 resource_name))
                    if filename:
                        logger.info("-> dumped %ss list to %s", resource_name, filename)

                return True

        except BackendException as exp:  # pragma: no cover, should never happen
            logger.exception("Exception: %s", exp)
            logger.error("Response: %s", exp.response)
            print("Get error for '%s' list" % (resource_name))
            print("~~~~~~~~~~~~~~~~~~~~~~~~~~")
            print("Exiting with error code: 5")
            return False
def get_resource_list(self, resource_name, name=''):
        # pylint: disable=too-many-locals, too-many-nested-blocks
        """Get a specific resource list

        If name is not None, it may be a request to get the list of the services of an host.
        """
        try:
            logger.info("Trying to get %s list", resource_name)

            params = {}
            if resource_name in ['host', 'service', 'user']:
                params = {'where': json.dumps({'_is_template': self.model})}

            if resource_name == 'service' and name and '/' in name:
                splitted_name = name.split('/')

                # Get host from name
                response2 = self.backend.get(
                    'host', params={'where': json.dumps({'name': splitted_name[0],
                                                         '_is_template': self.model})})
                if response2['_items']:
                    host = response2['_items'][0]
                    logger.info("Got host '%s' for the service '%s'",
                                splitted_name[0], splitted_name[1])
                else:
                    logger.warning("Not found host '%s'!", splitted_name[0])
                    return False

                params = {'where': json.dumps({'host': host['_id']})}

            if self.embedded and resource_name in self.embedded_resources:
                params.update({'embedded': json.dumps(self.embedded_resources[resource_name])})

            rsp = self.backend.get_all(resource_name, params=params)
            if rsp['_items'] and rsp['_status'] == 'OK':
                response = rsp['_items']

                logger.info("-> found %ss", resource_name)

                # Exists in the backend, we got the element
                if not self.dry_run:
                    logger.info("-> dumping %ss list", resource_name)
                    for item in response:
                        # Filter fields prefixed with an _ (internal backend fields)
                        for field in list(item):
                            if field in ['_created', '_updated', '_etag', '_links', '_status']:
                                item.pop(field)
                                continue

                            # Filter fields prefixed with an _ in embedded items
                            if self.embedded and resource_name in self.embedded_resources and \
                                    field in self.embedded_resources[resource_name]:
                                # Embedded items may be a list or a simple dictionary,
                                # always make it a list
                                embedded_items = item[field]
                                if not isinstance(item[field], list):
                                    embedded_items = [item[field]]
                                # Filter fields in each embedded item
                                for embedded_item in embedded_items:
                                    if not embedded_item:
                                        continue
                                    for embedded_field in list(embedded_item):
                                        if embedded_field.startswith('_'):
                                            embedded_item.pop(embedded_field)

                    filename = self.file_dump(response, 'alignak-%s-list-%ss.json'
                                              % ('model' if self.model else 'object',
                                                 resource_name))
                    if filename:
                        logger.info("-> dumped %ss list to %s", resource_name, filename)
                else:
                    logger.info("Dry-run mode: should have dumped an %s list", resource_name)

                return True
            else:
                logger.warning("-> %s list is empty", resource_name)
                if not self.dry_run:
                    logger.info("-> dumping %ss list", resource_name)
                    filename = self.file_dump([], 'alignak-%s-list-%ss.json'
                                              % ('model' if self.model else 'object',
                                                 resource_name))
                    if filename:
                        logger.info("-> dumped %ss list to %s", resource_name, filename)

                return True

        except BackendException as exp:  # pragma: no cover, should never happen
            logger.exception("Exception: %s", exp)
            logger.error("Response: %s", exp.response)
            print("Get error for '%s' list" % (resource_name))
            print("~~~~~~~~~~~~~~~~~~~~~~~~~~")
            print("Exiting with error code: 5")
            return False
Get a specific resource list

        If name is not None, it may be a request to get the list of the services of an host.