Example #1
0
    def _parse_homepage(self, json_body):
        py_obj = json.loads(json_body)

        srv_root_url = py_obj['href']
        self._srv_root_url = srv_root_url

        for link in py_obj['links']:
            # strip base from *_url to get *_uri
            uri = link['link']['href'].replace(srv_root_url, '')
            if link['link']['rel'] == 'collection':
                class_name = "%s" % (utils.CamelCase(link['link']['name']))
                cls = str_to_class(class_name)
                if not cls:
                    continue
                cls.create_uri = uri
            elif link['link']['rel'] == 'resource-base':
                class_name = "%s" % (utils.CamelCase(link['link']['name']))
                cls = str_to_class(class_name)
                if not cls:
                    continue
                resource_type = link['link']['name']
                cls.resource_uri_base[resource_type] = uri
            elif link['link']['rel'] == 'action':
                act_type = link['link']['name']
                self._action_uri[act_type] = uri
Example #2
0
    def restore_config(self, create, resource, json_body):
        class_name = "%s" % (utils.CamelCase(resource))
        cls = str_to_class(class_name)
        if not cls:
            return None

        if create:
            uri = cls.create_uri
            content = self._request_server(rest.OP_POST, uri, data=json_body)
        else:
            obj_dict = json.loads(json_body)
            uri = cls.resource_uri_base[resource] + '/'
            uri += obj_dict[resource]['uuid']
            content = self._request_server(rest.OP_PUT, uri, data=json_body)

        return json.loads(content)
Example #3
0
def get_object_class(res_type):
    cls_name = '%s' %(utils.CamelCase(res_type))
    return utils.str_to_class(cls_name, __name__)
Example #4
0
def get_object_class(obj_type):
    cls_name = '%s' % (utils.CamelCase(obj_type.replace('-', '_')))
    return utils.str_to_class(cls_name, __name__)
Example #5
0
    def resource_list(self,
                      obj_type,
                      parent_id=None,
                      parent_fq_name=None,
                      back_ref_id=None,
                      obj_uuids=None,
                      fields=None,
                      detail=False,
                      count=False,
                      filters=None):
        if not obj_type:
            raise ResourceTypeUnknownError(obj_type)

        class_name = "%s" % (utils.CamelCase(obj_type))
        obj_class = str_to_class(class_name)
        if not obj_class:
            raise ResourceTypeUnknownError(obj_type)

        query_params = {}
        do_post_for_list = False

        if parent_fq_name:
            parent_fq_name_str = ':'.join(parent_fq_name)
            query_params['parent_fq_name_str'] = parent_fq_name_str
        elif parent_id:
            if isinstance(parent_id, list):
                query_params['parent_id'] = ','.join(parent_id)
                if len(parent_id) > self.POST_FOR_LIST_THRESHOLD:
                    do_post_for_list = True
            else:
                query_params['parent_id'] = parent_id

        if back_ref_id:
            if isinstance(back_ref_id, list):
                query_params['back_ref_id'] = ','.join(back_ref_id)
                if len(back_ref_id) > self.POST_FOR_LIST_THRESHOLD:
                    do_post_for_list = True
            else:
                query_params['back_ref_id'] = back_ref_id

        if obj_uuids:
            comma_sep_obj_uuids = ','.join(u for u in obj_uuids)
            query_params['obj_uuids'] = comma_sep_obj_uuids
            if len(obj_uuids) > self.POST_FOR_LIST_THRESHOLD:
                do_post_for_list = True

        if fields:
            comma_sep_fields = ','.join(f for f in fields)
            query_params['fields'] = comma_sep_fields

        query_params['detail'] = detail

        query_params['count'] = count

        if filters:
            query_params['filters'] = ','.join('%s==%s' % (k, json.dumps(v))
                                               for k, v in filters.items())

        if do_post_for_list:
            uri = self._action_uri.get('list-bulk-collection')
            if not uri:
                raise

            # use same keys as in GET with additional 'type'
            query_params['type'] = obj_type
            json_body = json.dumps(query_params)
            content = self._request_server(rest.OP_POST, uri, json_body)
        else:  # GET /<collection>
            content = self._request_server(rest.OP_GET,
                                           obj_class.create_uri,
                                           data=query_params)

        if not detail:
            return json.loads(content)

        resource_dicts = json.loads(content)['%ss' % (obj_type)]
        resource_objs = []
        for resource_dict in resource_dicts:
            obj_dict = resource_dict['%s' % (obj_type)]
            resource_obj = obj_class.from_dict(**obj_dict)
            resource_obj.clear_pending_updates()
            resource_obj.set_server_conn(self)
            resource_objs.append(resource_obj)

        return resource_objs