Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / lib / sqlalchemy / groups.py @ 3759eddb

History | View | Annotate | Download (6 kB)

1 2e662088 Antony Chazapis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 2715ade4 Sofia Papagiannaki
#
3 4f917833 Sofia Papagiannaki
# Redistribution and use in source and binary forms, with or
4 4f917833 Sofia Papagiannaki
# without modification, are permitted provided that the following
5 4f917833 Sofia Papagiannaki
# conditions are met:
6 2715ade4 Sofia Papagiannaki
#
7 4f917833 Sofia Papagiannaki
#   1. Redistributions of source code must retain the above
8 4f917833 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
9 4f917833 Sofia Papagiannaki
#      disclaimer.
10 2715ade4 Sofia Papagiannaki
#
11 4f917833 Sofia Papagiannaki
#   2. Redistributions in binary form must reproduce the above
12 4f917833 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
13 4f917833 Sofia Papagiannaki
#      disclaimer in the documentation and/or other materials
14 4f917833 Sofia Papagiannaki
#      provided with the distribution.
15 2715ade4 Sofia Papagiannaki
#
16 4f917833 Sofia Papagiannaki
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 4f917833 Sofia Papagiannaki
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 4f917833 Sofia Papagiannaki
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 4f917833 Sofia Papagiannaki
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 4f917833 Sofia Papagiannaki
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 4f917833 Sofia Papagiannaki
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 4f917833 Sofia Papagiannaki
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 4f917833 Sofia Papagiannaki
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 4f917833 Sofia Papagiannaki
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 4f917833 Sofia Papagiannaki
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 4f917833 Sofia Papagiannaki
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 4f917833 Sofia Papagiannaki
# POSSIBILITY OF SUCH DAMAGE.
28 2715ade4 Sofia Papagiannaki
#
29 4f917833 Sofia Papagiannaki
# The views and conclusions contained in the software and
30 4f917833 Sofia Papagiannaki
# documentation are those of the authors and should not be
31 4f917833 Sofia Papagiannaki
# interpreted as representing official policies, either expressed
32 4f917833 Sofia Papagiannaki
# or implied, of GRNET S.A.
33 4f917833 Sofia Papagiannaki
34 4f917833 Sofia Papagiannaki
from collections import defaultdict
35 4f917833 Sofia Papagiannaki
from sqlalchemy import Table, Column, String, MetaData
36 4f917833 Sofia Papagiannaki
from sqlalchemy.sql import select, and_
37 4f917833 Sofia Papagiannaki
from sqlalchemy.schema import Index
38 6a82f89f Sofia Papagiannaki
from sqlalchemy.exc import NoSuchTableError
39 6a82f89f Sofia Papagiannaki
40 4f917833 Sofia Papagiannaki
from dbworker import DBWorker
41 4f917833 Sofia Papagiannaki
42 2715ade4 Sofia Papagiannaki
43 6a82f89f Sofia Papagiannaki
def create_tables(engine):
44 6a82f89f Sofia Papagiannaki
    metadata = MetaData()
45 2715ade4 Sofia Papagiannaki
    columns = []
46 6a82f89f Sofia Papagiannaki
    columns.append(Column('owner', String(256), primary_key=True))
47 6a82f89f Sofia Papagiannaki
    columns.append(Column('name', String(256), primary_key=True))
48 6a82f89f Sofia Papagiannaki
    columns.append(Column('member', String(256), primary_key=True))
49 6a82f89f Sofia Papagiannaki
    groups = Table('groups', metadata, *columns, mysql_engine='InnoDB')
50 2715ade4 Sofia Papagiannaki
51 6a82f89f Sofia Papagiannaki
    # place an index on member
52 6a82f89f Sofia Papagiannaki
    Index('idx_groups_member', groups.c.member)
53 2715ade4 Sofia Papagiannaki
54 6a82f89f Sofia Papagiannaki
    metadata.create_all(engine)
55 6a82f89f Sofia Papagiannaki
    return metadata.sorted_tables
56 2715ade4 Sofia Papagiannaki
57 2715ade4 Sofia Papagiannaki
58 4f917833 Sofia Papagiannaki
class Groups(DBWorker):
59 4f917833 Sofia Papagiannaki
    """Groups are named collections of members, belonging to an owner."""
60 2715ade4 Sofia Papagiannaki
61 4f917833 Sofia Papagiannaki
    def __init__(self, **params):
62 4f917833 Sofia Papagiannaki
        DBWorker.__init__(self, **params)
63 6a82f89f Sofia Papagiannaki
        try:
64 6a82f89f Sofia Papagiannaki
            metadata = MetaData(self.engine)
65 6a82f89f Sofia Papagiannaki
            self.groups = Table('groups', metadata, autoload=True)
66 6a82f89f Sofia Papagiannaki
        except NoSuchTableError:
67 6a82f89f Sofia Papagiannaki
            tables = create_tables(self.engine)
68 6a82f89f Sofia Papagiannaki
            map(lambda t: self.__setattr__(t.name, t), tables)
69 2715ade4 Sofia Papagiannaki
70 4f917833 Sofia Papagiannaki
    def group_names(self, owner):
71 4f917833 Sofia Papagiannaki
        """List all group names belonging to owner."""
72 2715ade4 Sofia Papagiannaki
73 4f917833 Sofia Papagiannaki
        s = select([self.groups.c.name],
74 2715ade4 Sofia Papagiannaki
                   self.groups.c.owner == owner).distinct()
75 4f917833 Sofia Papagiannaki
        r = self.conn.execute(s)
76 4f917833 Sofia Papagiannaki
        l = [row[0] for row in r.fetchall()]
77 4f917833 Sofia Papagiannaki
        r.close()
78 4f917833 Sofia Papagiannaki
        return l
79 2715ade4 Sofia Papagiannaki
80 4f917833 Sofia Papagiannaki
    def group_dict(self, owner):
81 4f917833 Sofia Papagiannaki
        """Return a dict mapping group names to member lists for owner."""
82 2715ade4 Sofia Papagiannaki
83 4f917833 Sofia Papagiannaki
        s = select([self.groups.c.name, self.groups.c.member],
84 2715ade4 Sofia Papagiannaki
                   self.groups.c.owner == owner)
85 4f917833 Sofia Papagiannaki
        r = self.conn.execute(s)
86 4f917833 Sofia Papagiannaki
        d = defaultdict(list)
87 4f917833 Sofia Papagiannaki
        for group, member in r.fetchall():
88 4f917833 Sofia Papagiannaki
            d[group].append(member)
89 4f917833 Sofia Papagiannaki
        r.close()
90 4f917833 Sofia Papagiannaki
        return d
91 2715ade4 Sofia Papagiannaki
92 4f917833 Sofia Papagiannaki
    def group_add(self, owner, group, member):
93 4f917833 Sofia Papagiannaki
        """Add a member to a group."""
94 2715ade4 Sofia Papagiannaki
95 2ce96391 Sofia Papagiannaki
        s = self.groups.select()
96 2ce96391 Sofia Papagiannaki
        s = s.where(self.groups.c.owner == owner)
97 2ce96391 Sofia Papagiannaki
        s = s.where(self.groups.c.name == group)
98 2ce96391 Sofia Papagiannaki
        s = s.where(self.groups.c.member == member)
99 2ce96391 Sofia Papagiannaki
        r = self.conn.execute(s)
100 2ce96391 Sofia Papagiannaki
        groups = r.fetchall()
101 4f917833 Sofia Papagiannaki
        r.close()
102 2715ade4 Sofia Papagiannaki
        if len(groups) == 0:
103 2ce96391 Sofia Papagiannaki
            s = self.groups.insert()
104 2ce96391 Sofia Papagiannaki
            r = self.conn.execute(s, owner=owner, name=group, member=member)
105 2ce96391 Sofia Papagiannaki
            r.close()
106 2715ade4 Sofia Papagiannaki
107 4f917833 Sofia Papagiannaki
    def group_addmany(self, owner, group, members):
108 4f917833 Sofia Papagiannaki
        """Add members to a group."""
109 2715ade4 Sofia Papagiannaki
110 b045ca90 Sofia Papagiannaki
        ins = self.groups.insert()
111 b045ca90 Sofia Papagiannaki
        values = list({'owner': owner,
112 b045ca90 Sofia Papagiannaki
                       'name': group,
113 b045ca90 Sofia Papagiannaki
                       'member': m} for m in members)
114 b045ca90 Sofia Papagiannaki
        self.conn.execute(ins, values)
115 2715ade4 Sofia Papagiannaki
116 4f917833 Sofia Papagiannaki
    def group_remove(self, owner, group, member):
117 4f917833 Sofia Papagiannaki
        """Remove a member from a group."""
118 2715ade4 Sofia Papagiannaki
119 2715ade4 Sofia Papagiannaki
        s = self.groups.delete().where(and_(self.groups.c.owner == owner,
120 2715ade4 Sofia Papagiannaki
                                            self.groups.c.name == group,
121 2715ade4 Sofia Papagiannaki
                                            self.groups.c.member == member))
122 4f917833 Sofia Papagiannaki
        r = self.conn.execute(s)
123 4f917833 Sofia Papagiannaki
        r.close()
124 2715ade4 Sofia Papagiannaki
125 4f917833 Sofia Papagiannaki
    def group_delete(self, owner, group):
126 4f917833 Sofia Papagiannaki
        """Delete a group."""
127 2715ade4 Sofia Papagiannaki
128 2715ade4 Sofia Papagiannaki
        s = self.groups.delete().where(and_(self.groups.c.owner == owner,
129 2715ade4 Sofia Papagiannaki
                                            self.groups.c.name == group))
130 4f917833 Sofia Papagiannaki
        r = self.conn.execute(s)
131 4f917833 Sofia Papagiannaki
        r.close()
132 2715ade4 Sofia Papagiannaki
133 4f917833 Sofia Papagiannaki
    def group_destroy(self, owner):
134 4f917833 Sofia Papagiannaki
        """Delete all groups belonging to owner."""
135 2715ade4 Sofia Papagiannaki
136 2715ade4 Sofia Papagiannaki
        s = self.groups.delete().where(self.groups.c.owner == owner)
137 4f917833 Sofia Papagiannaki
        r = self.conn.execute(s)
138 4f917833 Sofia Papagiannaki
        r.close()
139 2715ade4 Sofia Papagiannaki
140 4f917833 Sofia Papagiannaki
    def group_members(self, owner, group):
141 4f917833 Sofia Papagiannaki
        """Return the list of members of a group."""
142 2715ade4 Sofia Papagiannaki
143 2715ade4 Sofia Papagiannaki
        s = select([self.groups.c.member], and_(self.groups.c.owner == owner,
144 2715ade4 Sofia Papagiannaki
                                                self.groups.c.name == group))
145 4f917833 Sofia Papagiannaki
        r = self.conn.execute(s)
146 4f917833 Sofia Papagiannaki
        l = [row[0] for row in r.fetchall()]
147 4f917833 Sofia Papagiannaki
        r.close()
148 4f917833 Sofia Papagiannaki
        return l
149 2715ade4 Sofia Papagiannaki
150 4f917833 Sofia Papagiannaki
    def group_check(self, owner, group, member):
151 4f917833 Sofia Papagiannaki
        """Check if a member is in a group."""
152 2715ade4 Sofia Papagiannaki
153 29148653 Sofia Papagiannaki
        s = select([self.groups.c.member],
154 29148653 Sofia Papagiannaki
                   and_(self.groups.c.owner == owner,
155 29148653 Sofia Papagiannaki
                        self.groups.c.name == group,
156 29148653 Sofia Papagiannaki
                        self.groups.c.member == member))
157 4f917833 Sofia Papagiannaki
        r = self.conn.execute(s)
158 4f917833 Sofia Papagiannaki
        l = r.fetchone()
159 4f917833 Sofia Papagiannaki
        r.close()
160 4f917833 Sofia Papagiannaki
        return bool(l)
161 2715ade4 Sofia Papagiannaki
162 4f917833 Sofia Papagiannaki
    def group_parents(self, member):
163 4f917833 Sofia Papagiannaki
        """Return all (owner, group) tuples that contain member."""
164 2715ade4 Sofia Papagiannaki
165 4f917833 Sofia Papagiannaki
        s = select([self.groups.c.owner, self.groups.c.name],
166 2715ade4 Sofia Papagiannaki
                   self.groups.c.member == member)
167 4f917833 Sofia Papagiannaki
        r = self.conn.execute(s)
168 4f917833 Sofia Papagiannaki
        l = r.fetchall()
169 4f917833 Sofia Papagiannaki
        r.close()
170 4f917833 Sofia Papagiannaki
        return l