def test_unicode_key_value_to_string(self):
     src = {u'key': u'\u70fd\u7231\u5a77'}
     expected = {'key': '\xe7\x83\xbd\xe7\x88\xb1\xe5\xa9\xb7'}
     if six.PY2:
         self.assertEqual(expected, utils.unicode_key_value_to_string(src))
     else:
         # u'xxxx' in PY3 is str, we will not get extra 'u' from cli
         # output in PY3
         self.assertEqual(src, utils.unicode_key_value_to_string(src))
 def test_unicode_key_value_to_string(self):
     src = {u'key': u'\u70fd\u7231\u5a77'}
     expected = {'key': '\xe7\x83\xbd\xe7\x88\xb1\xe5\xa9\xb7'}
     if six.PY2:
         self.assertEqual(expected, utils.unicode_key_value_to_string(src))
     else:
         # u'xxxx' in PY3 is str, we will not get extra 'u' from cli
         # output in PY3
         self.assertEqual(src, utils.unicode_key_value_to_string(src))
Example #3
0
    def _build_list_url(self,
                        resource_type,
                        detailed=True,
                        search_opts=None,
                        marker=None,
                        limit=None,
                        sort_key=None,
                        sort_dir=None,
                        sort=None,
                        offset=None):

        if search_opts is None:
            search_opts = {}

        query_params = {}
        for key, val in search_opts.items():
            if val:
                query_params[key] = val

        if marker:
            query_params['marker'] = marker

        if limit:
            query_params['limit'] = limit

        if sort:
            query_params['sort'] = self._format_sort_param(sort, resource_type)
        else:
            # sort_key and sort_dir deprecated in kilo, prefer sort
            if sort_key:
                query_params['sort_key'] = self._format_sort_key_param(
                    sort_key, resource_type)

            if sort_dir:
                query_params['sort_dir'] = self._format_sort_dir_param(
                    sort_dir)

        if offset:
            query_params['offset'] = offset
        query_params = utils.unicode_key_value_to_string(query_params)
        # Transform the dict to a sequence of two-element tuples in fixed
        # order, then the encoded string will be consistent in Python 2&3.
        query_string = ""
        if query_params:
            params = sorted(query_params.items(), key=lambda x: x[0])
            query_string = "?%s" % parse.urlencode(params)

        detail = ""
        if detailed:
            detail = "/detail"

        return ("/%(resource_type)s%(detail)s%(query_string)s" % {
            "resource_type": resource_type,
            "detail": detail,
            "query_string": query_string
        })
Example #4
0
    def get(self, group_id, **kwargs):
        """Get a group.

        :param group_id: The ID of the group to get.
        :rtype: :class:`Group`
        """
        query_params = utils.unicode_key_value_to_string(kwargs)
        query_string = utils.build_query_param(query_params, sort=True)

        return self._get("/groups/%s" % group_id + query_string, "group")
Example #5
0
    def get(self, group_id, **kwargs):
        """Get a group.

        :param group_id: The ID of the group to get.
        :rtype: :class:`Group`
        """
        query_params = utils.unicode_key_value_to_string(kwargs)
        query_string = utils.build_query_param(query_params, sort=True)

        return self._get("/groups/%s" % group_id + query_string,
                         "group")
def quota_show(quotas):
    quotas_info_dict = utils.unicode_key_value_to_string(quotas._info)
    quota_dict = {}
    for resource in quotas_info_dict.keys():
        good_name = False
        for name in _quota_resources:
            if resource.startswith(name):
                good_name = True
        if not good_name:
            continue
        quota_dict[resource] = getattr(quotas, resource, None)
    utils.print_dict(quota_dict)
def quota_show(quotas):
    quotas_info_dict = utils.unicode_key_value_to_string(quotas._info)
    quota_dict = {}
    for resource in quotas_info_dict.keys():
        good_name = False
        for name in _quota_resources:
            if resource.startswith(name):
                good_name = True
        if not good_name:
            continue
        quota_dict[resource] = getattr(quotas, resource, None)
    utils.print_dict(quota_dict)
Example #8
0
def bot_qoute_show(tenant_name):
    bot_answer = []
    txt = ""
    for reg in regions:
        logger.info(reg)
        try:
            sess = region_env(reg)
            keystone = keyclient.Client(session=sess)
            tenant_dict = {t.name: t.id for t in keystone.tenants.list()}
            # looking for a tenant in the DC
            if tenant_dict.get(tenant_name):
                id = tenant_dict.get(tenant_name)
            else:
                continue
            #  get information about tenant resources in nova
            nova = novaclient.Client(version="2.0", session=sess)
            nova_request = nova.quotas.get(id, detail=True)
            # preparing an answer
            for kk, vv in nova_request._info.items():
                if kk == 'id':
                    continue
                txt += "<code>" + kk + ":        "
                for res, chis in dict(vv).items():
                    if res == 'reserved':
                        continue
                    txt += "[" + res + "=" + str(chis) + "]  "
                txt += "</code>\n"
            bot_answer.append(txt)
            #  get information about tenant resources in cinder
            cinder = cinderclient.Client(version="2", session=sess)
            quota = cinder.quotas.get(id, usage=True)
            quotas_info_dict = cinderutils.unicode_key_value_to_string(
                quota._info)
            # this is we have dict in dict, for return an answer we need to deleting a key not containing dict
            del quotas_info_dict['id']
            for key, value in quotas_info_dict.items():
                bot_answer.append(f"<b>{key} </b>:      ")
                for res, chis in value.items():
                    if res == 'reserved' or res == 'allocated':
                        continue
                    else:
                        bot_answer.append(f"[{res} = {chis}]  ")
                bot_answer.append(f"\n")
            return bot_answer
        except Exception as ff:
            logger.exception(ff)
            continue
    else:
        bot_answer = "Search results: fail"
        return bot_answer
Example #9
0
    def get(self, group_id, **kwargs):
        """Get a group.

        :param group_id: The ID of the group to get.
        :rtype: :class:`Group`
        """
        query_params = utils.unicode_key_value_to_string(kwargs)

        query_string = ""
        if query_params:
            params = sorted(query_params.items(), key=lambda x: x[0])
            query_string = "?%s" % parse.urlencode(params)

        return self._get("/groups/%s" % group_id + query_string, "group")
def quota_usage_show(quotas):
    quota_list = []
    quotas_info_dict = utils.unicode_key_value_to_string(quotas._info)
    for resource in quotas_info_dict.keys():
        good_name = False
        for name in _quota_resources:
            if resource.startswith(name):
                good_name = True
        if not good_name:
            continue
        quota_info = getattr(quotas, resource, None)
        quota_info['Type'] = resource
        quota_info = dict((k.capitalize(), v) for k, v in quota_info.items())
        quota_list.append(quota_info)
    utils.print_list(quota_list, _quota_infos)
def quota_usage_show(quotas):
    quota_list = []
    quotas_info_dict = utils.unicode_key_value_to_string(quotas._info)
    for resource in quotas_info_dict.keys():
        good_name = False
        for name in _quota_resources:
            if resource.startswith(name):
                good_name = True
        if not good_name:
            continue
        quota_info = getattr(quotas, resource, None)
        quota_info['Type'] = resource
        quota_info = dict((k.capitalize(), v) for k, v in quota_info.items())
        quota_list.append(quota_info)
    utils.print_list(quota_list, _quota_infos)
Example #12
0
    def _build_list_url(self, resource_type, detailed=True, search_opts=None,
                        marker=None, limit=None, sort_key=None, sort_dir=None,
                        sort=None, offset=None):

        if search_opts is None:
            search_opts = {}

        query_params = {}
        for key, val in search_opts.items():
            if val:
                query_params[key] = val

        if marker:
            query_params['marker'] = marker

        if limit:
            query_params['limit'] = limit

        if sort:
            query_params['sort'] = self._format_sort_param(sort,
                                                           resource_type)
        else:
            # sort_key and sort_dir deprecated in kilo, prefer sort
            if sort_key:
                query_params['sort_key'] = self._format_sort_key_param(
                    sort_key,
                    resource_type)

            if sort_dir:
                query_params['sort_dir'] = self._format_sort_dir_param(
                    sort_dir)

        if offset:
            query_params['offset'] = offset
        query_params = utils.unicode_key_value_to_string(query_params)
        # Transform the dict to a sequence of two-element tuples in fixed
        # order, then the encoded string will be consistent in Python 2&3.

        query_string = utils.build_query_param(query_params, sort=True)

        detail = ""
        if detailed:
            detail = "/detail"

        return ("/%(resource_type)s%(detail)s%(query_string)s" %
                {"resource_type": resource_type, "detail": detail,
                 "query_string": query_string})
Example #13
0
    def _build_list_url(self, resource_type, detailed=True, search_opts=None,
                        marker=None, limit=None, sort=None, offset=None):

        if search_opts is None:
            search_opts = {}

        query_params = {}
        for key, val in search_opts.items():
            if val:
                query_params[key] = val

        if marker:
            query_params['marker'] = marker

        if limit:
            query_params['limit'] = limit

        if sort:
            query_params['sort'] = self._format_sort_param(sort,
                                                           resource_type)

        if offset:
            query_params['offset'] = offset
        query_params = utils.unicode_key_value_to_string(query_params)
        # Transform the dict to a sequence of two-element tuples in fixed
        # order, then the encoded string will be consistent in Python 2&3.

        query_string = utils.build_query_param(query_params, sort=True)

        detail = ""
        if detailed:
            detail = "/detail"

        return ("/%(resource_type)s%(detail)s%(query_string)s" %
                {"resource_type": resource_type, "detail": detail,
                 "query_string": query_string})
Example #14
0
 def test_unicode_key_value_to_string(self):
     expected = {u'key': u'\u043f\u043f\u043f\u043f\u043f'}
     self.assertEqual(expected, utils.unicode_key_value_to_string(expected))
 def test_unicode_key_value_to_string(self):
     expected = {u'key': u'\u043f\u043f\u043f\u043f\u043f'}
     self.assertEqual(expected, utils.unicode_key_value_to_string(expected))