Exemple #1
0
 def list_keypairs(self, req, tenant_id):
     '''
     list key pairs
     support all_tenants=True
     '''
     result, headers = self._nova_request(req)
     auth_token = req.headers.get("x-auth-token")
     try:
         keypairs = result['keypairs']
         keypair_list_ret = []
         for item in keypairs:
             keypair = item['keypair']
             user = ops_api.get_user(keypair['user_id'], auth_token)
             keypair.update(user_name=user['name'])
             if 'tenantName' in user:
                 keypair.update(
                         tenant_name=user['tenantName'],
                         tenant_id=user['tenantId'])
                 fingerprint = keypair.get('fingerprint', '').\
                                         replace('.create', '')
                 keypair.update(fingerprint=fingerprint)
             keypair_list_ret.append(keypair)
         keypair_list_ret = list_filter.filter_keypairs(req,
                                                        keypair_list_ret)
         keypair_list_ret = list_sort.sort_keypairs(req, keypair_list_ret)
     except KeyError:
         LOG.exception(_("repack keypair data error."))
         raise exc.HTTPFailedDependency(_("Nova method deprecated."))
     return dict(keypairs=keypair_list_ret)
Exemple #2
0
    def list_all_quotas(self, req, tenant_id):
        '''
        get all quota sets
        '''
        # Request quota and usage
        result, headers = self._nova_request(req)
        converted_result = []
        auth_token = req.headers.get("x-auth-token")

        # Request keypair list and calculate usage
        keypairs = ops_api.get_keypairs(tenant_id, auth_token)
        for keypair in keypairs:
            userid = keypair['keypair'].get('user_id', None)
            if userid is None:
                continue
            tenant = ops_api.get_user(userid, auth_token)
            keypair_owner = tenant.get('tenantId', None)
            if keypair_owner is None:
                continue
            target_usage = result.get(keypair_owner, None)
            if target_usage is None:
                # no tenant in quota usage id for this keypair_owner
                continue
            target_keypair_usage = target_usage.get('key_pairs', {})
            target_keypair_in_use = target_keypair_usage.get('in_use', 0)
            in_use = int(target_keypair_in_use) + 1
            target_keypair_usage.update(in_use=in_use)
        # Insert tenant_id and tenant name in each item
        for tenantid in result:
            item = result[tenantid]
            item.update({"tenant_id": tenantid})
            tenant = ops_api.get_tenant(tenantid, auth_token)
            item.update(tenant_name=tenant['name'])
            converted_result.append(item)
        filter_result = list_filter.filter_quotas(req, tenant_id,
                                                  converted_result)
        sort_result = list_sort.sort_quotas(req, filter_result)
        sort_result = [re for re in sort_result if re.get('tenant_id', '')]
        return dict(quotas=sort_result)