Example #1
0
 def list_floating_ips(self, req, tenant_id):
     '''
     list all floating ip
     '''
     result, headers = self._nova_request(req)
     auth_token = req.context.auth_tok
     try:
         for floating_ip in result['floating_ips']:
             project_id = floating_ip['project_id']
             if project_id is not None:
                 tenant = ops_api.get_tenant(project_id, auth_token)
                 project_name = tenant['name']
                 floating_ip.update(project_name=project_name)
     except KeyError:
         LOG.exception(_("list floating ip error."))
         raise exc.HTTPFailedDependency(_("Nova method deprecated."))
     except exc.HTTPNotFound:
         LOG.error(_("project_id %s not found.") % project_id)
         raise exc.HTTPNotFound(_("project_id %s not found for"
             " floating ip %s.") % (project_id, floating_ip))
     pools = result.get('pools', None)
     ips = result.get('floating_ips', None)
     ips = list_filter.filter_floating_ips(req, ips)
     ips = list_sort.sort_floating_ips(req, ips)
     if pools is not None:
         return dict(pools=pools, floating_ips=ips)
     else:
         return dict(floating_ips=ips)
Example #2
0
    def _repack_server_data(self, server, req, tenant_id):
        server.update({"OS-EXT-STS:power_state":
                      self._get_power_state(server['OS-EXT-STS:power_state'])})
        auth_token = req.headers.get("x-auth-token")
        # get flavor info
        flavor_id = server['flavor']['id']
        flavor = ops_api.get_flavor(tenant_id,
                           auth_token,
                           flavor_id)
        if flavor is not None:
            for k, v in flavor['flavor'].iteritems():
                if k == 'links':
                    continue
                server.update({"flavor-%s" % k: v})
            server.pop('flavor')

        server_owner_id = server['tenant_id']
        # get tenant name
        tenant = ops_api.get_tenant(server_owner_id, auth_token)
        tenant_name = tenant['name']
        server.update(tenant_name=tenant_name)

        fixed_ips = []
        private_floating_ips = []
        public_floating_ips = []
        # recognize IP type
        for ip in server['addresses'].get('private', {}):
            is_floating_ip, ip_type = _recog_ip_type(ip['addr'])
            if is_floating_ip and ip_type == 'private':
                private_floating_ips.append(ip)
            elif is_floating_ip and ip_type == 'public':
                public_floating_ips.append(ip)
            else:
                fixed_ips.append(ip)
        server.update(fixed_ips=fixed_ips)
        server.update(private_floating_ips=private_floating_ips)
        server.update(public_floating_ips=public_floating_ips)

        # get image info
        image_id = server['image']['id']
        image = ops_api.get_image(auth_token, image_id)
        if image is not None:
            server.update(image)
            server.pop('image')

        # get running time (now - created_at)
        # FIXME(hzzhoushaoyu): created_at should transform to utc+0 datetime,
        # but as list show required nova return utc+8 datetime.
        # If nova return utc+0 datetime, running time may be ERROR data.
        created_at = server['created']
        created_datetime = timeutils.normalize_time(
                timeutils.local_to_utc(
                    timeutils.parse_strtime(created_at, '%Y-%m-%d %H:%M:%S')))
        running_time = timeutils.seconds_from_now(created_datetime)
        server.update({"running_seconds": running_time})
        return server
Example #3
0
 def show(self, req, image_id):
     '''
     show image info by specified image id
     '''
     result, headers = self._glance_request(req)
     temp = None
     for k, v in headers:
         if k == 'x-image-meta-owner':
             auth_token = req.context.auth_tok
             tenant = api.get_tenant(v, auth_token)
             temp = ('x-image-meta-owner_name', tenant['name'])
     if temp is not None:
         headers.append(temp)
     return headers
Example #4
0
 def _get_image(self, req, image, image_type=None):
     '''
     get more info for single image.
     '''
     if image_type == 'image' and \
             'image_type' in image.get('properties', {}) and \
             image.get('properties').get('image_type') == 'snapshot':
         return None
     elif image_type == 'snapshot' and \
             'image_type' not in image.get('properties', {}):
         return None
     image_owner_id = image['owner']
     auth_token = req.context.auth_tok
     tenant = api.get_tenant(image_owner_id, auth_token)
     tenant_name = tenant['name']
     image['owner_name'] = tenant_name
     return image
Example #5
0
 def list_security_groups(self, req, tenant_id):
     '''
     list all security groups
     '''
     result, headers = self._nova_request(req)
     try:
         groups = result['security_groups']
         auth_token = req.context.auth_tok
         for group in groups:
             group.pop("rules")
             tenant_id = group.get("tenant_id")
             tenant = ops_api.get_tenant(tenant_id, auth_token)
             tenant_name = tenant['name']
             group.update(tenant_name=tenant_name)
         groups = list_filter.filter_security_groups(req, groups)
         groups = list_sort.sort_security_groups(req, groups)
         return dict(security_groups=groups)
     except KeyError:
         LOG.exception(_("repack server data error."))
         raise exc.HTTPFailedDependency(_("Nova method deprecated."))
Example #6
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)