Ejemplo n.º 1
0
 def test_default_tenant_id(self):
     self.mox.ReplayAll()
     with self.app.test_request_context():
         self.install_fake_auth()
         result = auth.default_tenant_id()
     # look at tests/mocked.py, near line 100
     self.assertEquals(result, u'SYSTENANT_ID')
Ejemplo n.º 2
0
 def test_default_tenant_id(self):
     self.mox.ReplayAll()
     with self.app.test_request_context():
         self.install_fake_auth()
         result = auth.default_tenant_id()
     # look at tests/mocked.py, near line 100
     self.assertEquals(result, u'SYSTENANT_ID')
Ejemplo n.º 3
0
def _image_to_view(image, tenant=None):
    result = link_for_image(image.id, image.name)
    result.update({
        u'status':
        image.status,
        u'disk-format':
        image.disk_format,
        u'container-format':
        image.container_format,
        u'created':
        timestamp_from_openstack(image.created_at),
        u'md5sum':
        image.checksum,
        u'size':
        image.size,
        u'data-href':
        url_for('images.get_image_data', image_id=image.id)
    })
    if image.owner == auth.default_tenant_id():
        result[u'global'] = True
    else:
        result[u'global'] = False
        result[u'project'] = link_for_project(image.owner,
                                              tenant.name if tenant else None)

    if 'kernel_id' in image.properties:
        result['kernel'] = link_for_image(image.properties['kernel_id'])
    if 'ramdisk_id' in image.properties:
        result['ramdisk'] = link_for_image(image.properties['ramdisk_id'])

    return result
Ejemplo n.º 4
0
def _project_users_list(project_id):
    assert_admin_or_project_user(project_id, eperm_status=404)
    if project_id == default_tenant_id():
        abort(404)
    try:
        return admin_client_set().identity_admin.tenants.list_users(project_id)
    except osc_exc.NotFound:
        abort(404)
Ejemplo n.º 5
0
def _project_users_list(project_id):
    assert_admin_or_project_user(project_id, eperm_status=404)
    if project_id == default_tenant_id():
        abort(404)
    try:
        return admin_client_set().identity_admin.tenants.list_users(project_id)
    except osc_exc.NotFound:
        abort(404)
Ejemplo n.º 6
0
def _grant_admin(user_id):
    """Grant admin permission.

    Add admin role with in admin tenant (aka systenant).

    """
    auth.assert_admin()
    g.client_set.identity_admin.roles.add_user_role(
        user_id, auth.admin_role_id(), auth.default_tenant_id())
Ejemplo n.º 7
0
def _grant_admin(user_id):
    """Grant admin permission.

    Add admin role with in admin tenant (aka systenant).

    """
    auth.assert_admin()
    g.client_set.identity_admin.roles.add_user_role(user_id,
                                                    auth.admin_role_id(),
                                                    auth.default_tenant_id())
Ejemplo n.º 8
0
def _revoke_admin(user_id):
    """Revoke admin permission.

    Remove admin role in admin tenant (aka systenant).

    """
    auth.assert_admin()
    try:
        g.client_set.identity_admin.roles.remove_user_role(
            user_id, auth.admin_role_id(), auth.default_tenant_id())
    except osc_exc.NotFound:
        pass  # user was not admin
Ejemplo n.º 9
0
def _revoke_admin(user_id):
    """Revoke admin permission.

    Remove admin role in admin tenant (aka systenant).

    """
    auth.assert_admin()
    try:
        g.client_set.identity_admin.roles.remove_user_role(
            user_id, auth.admin_role_id(), auth.default_tenant_id())
    except osc_exc.NotFound:
        pass  # user was not admin
Ejemplo n.º 10
0
def _images_for_all_tenants():
    if g.my_projects:
        tenants = g.client_set.identity_public.tenants.list()
    else:
        tenants = g.client_set.identity_admin.tenants.list()

    tenant_dict = dict(((tenant.id, tenant) for tenant in tenants))
    tenant_dict[auth.default_tenant_id()] = None

    return [_image_to_view(image, tenant_dict.get(image.owner))
            for image in list_all_images(auth.admin_client_set().image.images)
            if not g.my_projects or image.owner in tenant_dict]
Ejemplo n.º 11
0
def _fetch_image(image_id, to_modify):
    try:
        image = auth.admin_client_set().image.images.get(image_id)
    except osc_exc.NotFound:
        abort(404)
    # NOTE(imelnikov): yes, glance may return False as string
    if image.deleted and image.deleted != 'False':
        abort(404)
    if image.owner == auth.default_tenant_id():
        if to_modify:
            auth.assert_admin()
    else:
        auth.assert_admin_or_project_user(image.owner)
    return image
Ejemplo n.º 12
0
def _images_for_all_tenants():
    if g.my_projects:
        tenants = g.client_set.identity_public.tenants.list()
    else:
        tenants = g.client_set.identity_admin.tenants.list()

    tenant_dict = dict(((tenant.id, tenant) for tenant in tenants))
    tenant_dict[auth.default_tenant_id()] = None

    return [
        _image_to_view(image, tenant_dict.get(image.owner))
        for image in list_all_images(auth.admin_client_set().image.images)
        if not g.my_projects or image.owner in tenant_dict
    ]
Ejemplo n.º 13
0
def _fetch_image(image_id, to_modify):
    try:
        image = auth.admin_client_set().image.images.get(image_id)
    except osc_exc.NotFound:
        abort(404)
    # NOTE(imelnikov): yes, glance may return False as string
    if image.deleted and image.deleted != 'False':
        abort(404)
    if image.owner == auth.default_tenant_id():
        if to_modify:
            auth.assert_admin()
    else:
        auth.assert_admin_or_project_user(image.owner)
    return image
Ejemplo n.º 14
0
def list_stats_by_project():
    parse_collection_request(_SCHEMA)
    cs = auth.admin_client_set()

    if g.my_projects:
        tenants = g.client_set.identity_public.tenants.list()
    else:
        tenants = cs.identity_admin.tenants.list()

    result = {}
    for tenant in tenants:
        if tenant.id != auth.default_tenant_id():
            users = cs.identity_admin.tenants.list_users(tenant.id)
            result[tenant.id] = {
                'project': link_for_tenant(tenant),
                'members': len(users),
                'instances': 0,
                'local-images': 0,
                'total-images': 0,
                'href': url_for('stats.get_project_stats',
                                project_id=tenant.id)
            }

    for server in cs.compute.servers.list(search_opts={'all_tenants': 1}):
        try:
            result[server.tenant_id]['instances'] += 1
        except KeyError:
            pass

    global_images = 0
    for image in list_all_images(cs.image.images):
        if image.is_public:
            global_images += 1
        if image.owner in result:
            result[image.owner]['local-images'] += 1
            if not image.is_public:
                result[image.owner]['total-images'] += 1

    for value in result.itervalues():
        value['total-images'] += global_images
    data = sorted(result.itervalues(), key=lambda s: s['href'])
    return make_collection_response(u'stats',
                                    data,
                                    parent_href=url_for('stats.altai_stats'))
Ejemplo n.º 15
0
def list_stats_by_project():
    parse_collection_request(_SCHEMA)
    cs = auth.admin_client_set()

    if g.my_projects:
        tenants = g.client_set.identity_public.tenants.list()
    else:
        tenants = cs.identity_admin.tenants.list()

    result = {}
    for tenant in tenants:
        if tenant.id != auth.default_tenant_id():
            users = cs.identity_admin.tenants.list_users(tenant.id)
            result[tenant.id] = {
                'project': link_for_tenant(tenant),
                'members': len(users),
                'instances': 0,
                'local-images': 0,
                'total-images': 0,
                'href': url_for('stats.get_project_stats',
                                project_id=tenant.id)
            }

    for server in cs.compute.servers.list(search_opts={'all_tenants': 1}):
        try:
            result[server.tenant_id]['instances'] += 1
        except KeyError:
            pass

    global_images = 0
    for image in list_all_images(cs.image.images):
        if image.is_public:
            global_images += 1
        if image.owner in result:
            result[image.owner]['local-images'] += 1
            if not image.is_public:
                result[image.owner]['total-images'] += 1

    for value in result.itervalues():
        value['total-images'] += global_images
    data = sorted(result.itervalues(), key=lambda s: s['href'])
    return make_collection_response(u'stats', data,
                                    parent_href=url_for('stats.altai_stats'))
Ejemplo n.º 16
0
def _image_to_view(image, tenant=None):
    result = link_for_image(image.id, image.name)
    result.update({
        u'status': image.status,
        u'disk-format': image.disk_format,
        u'container-format': image.container_format,
        u'created': timestamp_from_openstack(image.created_at),
        u'md5sum': image.checksum,
        u'size': image.size,
        u'data-href': url_for('images.get_image_data', image_id=image.id)
    })
    if image.owner == auth.default_tenant_id():
        result[u'global'] = True
    else:
        result[u'global'] = False
        result[u'project'] = link_for_project(image.owner,
                                              tenant.name if tenant else None)

    if 'kernel_id' in image.properties:
        result['kernel'] = link_for_image(image.properties['kernel_id'])
    if 'ramdisk_id' in image.properties:
        result['ramdisk'] = link_for_image(image.properties['ramdisk_id'])

    return result
Ejemplo n.º 17
0
    def test_list_works(self):
        tenants = [
            doubles.make(self.mox, doubles.Tenant, **kwargs)
            for kwargs in (dict(id=u'p1', name=u'test1'),
                           dict(id=u'p2', name=u'test2'),
                           dict(id=u'SYS', name=u'systenant'))
        ]
        servers = [
            doubles.make(self.mox, doubles.Server, tenant_id='p2'),
            doubles.make(self.mox, doubles.Server, tenant_id='SYS'),
        ]
        images = [
            doubles.make(self.mox, doubles.Image, **kwargs)
            for kwargs in (dict(id='x', is_public=True),
                           dict(id='y', is_public=False, owner='p2'),
                           dict(id='z', is_public=True, owner='p1'))
        ]

        self.fake_client_set.identity_admin.tenants.list()\
                .AndReturn(tenants)
        auth.default_tenant_id().AndReturn('SYS')
        self.fake_client_set.identity_admin.tenants.list_users('p1')\
                .AndReturn([])
        auth.default_tenant_id().AndReturn('SYS')
        self.fake_client_set.identity_admin.tenants.list_users('p2')\
                .AndReturn([])
        auth.default_tenant_id().AndReturn('SYS')

        self.fake_client_set.compute.servers\
                .list(search_opts={'all_tenants': 1})\
                .AndReturn(servers)
        stats.list_all_images(self.fake_client_set.image.images)\
                .AndReturn(images)

        expected = [
            {
                'href': '/v1/stats/by-project/p1',
                'project': {
                    'href': '/v1/projects/p1',
                    'id': 'p1',
                    'name': 'test1'
                },
                'instances': 0,
                'members': 0,
                'local-images': 1,
                'total-images': 2
            },
            {
                'href': '/v1/stats/by-project/p2',
                'project': {
                    'href': '/v1/projects/p2',
                    'id': 'p2',
                    'name': 'test2'
                },
                'instances': 1,
                'members': 0,
                'local-images': 1,
                'total-images': 3
            },
        ]

        self.mox.ReplayAll()
        rv = self.client.get('/v1/stats/by-project/')
        data = self.check_and_parse_response(rv)
        self.assertEquals(data['stats'], expected)
Ejemplo n.º 18
0
    def test_list_works(self):
        tenants = [doubles.make(self.mox, doubles.Tenant, **kwargs)
                   for kwargs in (dict(id=u'p1', name=u'test1'),
                                  dict(id=u'p2', name=u'test2'),
                                  dict(id=u'SYS', name=u'systenant'))]
        servers = [
            doubles.make(self.mox, doubles.Server, tenant_id='p2'),
            doubles.make(self.mox, doubles.Server, tenant_id='SYS'),
        ]
        images = [doubles.make(self.mox, doubles.Image, **kwargs)
                  for kwargs in (dict(id='x', is_public=True),
                                 dict(id='y', is_public=False, owner='p2'),
                                 dict(id='z', is_public=True, owner='p1'))]

        self.fake_client_set.identity_admin.tenants.list()\
                .AndReturn(tenants)
        auth.default_tenant_id().AndReturn('SYS')
        self.fake_client_set.identity_admin.tenants.list_users('p1')\
                .AndReturn([])
        auth.default_tenant_id().AndReturn('SYS')
        self.fake_client_set.identity_admin.tenants.list_users('p2')\
                .AndReturn([])
        auth.default_tenant_id().AndReturn('SYS')

        self.fake_client_set.compute.servers\
                .list(search_opts={'all_tenants': 1})\
                .AndReturn(servers)
        stats.list_all_images(self.fake_client_set.image.images)\
                .AndReturn(images)

        expected = [
            {
                'href': '/v1/stats/by-project/p1',
                'project': {
                    'href': '/v1/projects/p1',
                    'id': 'p1',
                    'name': 'test1'
                },
                'instances': 0,
                'members': 0,
                'local-images': 1,
                'total-images': 2
            },
            {
                'href': '/v1/stats/by-project/p2',
                'project': {
                    'href': '/v1/projects/p2',
                    'id': 'p2',
                    'name': 'test2'
                },
                'instances': 1,
                'members': 0,
                'local-images': 1,
                'total-images': 3
            },
        ]

        self.mox.ReplayAll()
        rv = self.client.get('/v1/stats/by-project/')
        data = self.check_and_parse_response(rv)
        self.assertEquals(data['stats'], expected)