populate.py

Peter Amstutz, 05/01/2020 02:55 PM

 
1
import arvados
2
import arvados.util
3
import pprint
4

    
5
# Create a database with
6
# 500 users
7
# 100 groups
8
# 5000 projects
9

    
10
# each user belongs to 10 groups
11
# each group can read 10 projects
12
# there are 100 top level projects
13
# each project has 10 subprojects
14
# each subproject has 5 sub-sub-projects
15

    
16
def create_users(api, n=500):
17
    users = {}
18
    items = arvados.util.list_all(api.users().list)
19
    for u in items:
20
        users[u["username"]] = u
21
    for i in range(0, n):
22
        username = "user%i" % i
23
        if username not in users:
24
            u = api.users().create(body={"username": username}).execute()
25
            users[u["username"]] = u
26
        if i % 25 == 0:
27
            print(i)
28
    return users
29

    
30
def create_groups(api, n=100):
31
    groups = {}
32
    items = arvados.util.list_all(api.groups().list)
33
    for u in items:
34
        groups[u["name"]] = u
35
    for i in range(0, n):
36
        name = "group%i" % i
37
        if name not in groups:
38
            u = api.groups().create(body={"name": name, "group_class":"role"}).execute()
39
            groups[u["name"]] = u
40
        if i % 25 == 0:
41
            print(i)
42
    return groups
43

    
44
def add_users_to_groups(api, users, groups):
45
    links = {}
46
    items = arvados.util.list_all(api.links().list)
47
    for u in items:
48
        if "id" in u["properties"]:
49
            links[u["properties"]["id"]] = u
50
    for i in range(0, 500):
51
        username = "user%i" % i
52
        for j in range(0, 10):
53
            linkid = "permission_link_%i_%i" % (i, j)
54
            if linkid not in links:
55
                joingroup = "group%i" % ((i+j) % 100)
56
                u = api.links().create(body={"link_class": "permission",
57
                                             "name": "can_read",
58
                                             "tail_uuid": groups[joingroup]["uuid"],
59
                                             "head_uuid": users[username]["uuid"],
60
                                             "properties": {"id": linkid}}).execute()
61
                u = api.links().create(body={"link_class": "permission",
62
                                             "name": "can_manage",
63
                                             "tail_uuid": users[username]["uuid"],
64
                                             "head_uuid": groups[joingroup]["uuid"],
65
                                             "properties": {"id": linkid}}).execute()
66
                links[u["properties"]["id"]] = u
67
        if i % 25 == 0:
68
            print(i)
69
    return links
70

    
71

    
72
users = create_users(arvados.api())
73

    
74
groups = create_groups(arvados.api())
75

    
76
links = add_users_to_groups(arvados.api(), users, groups)
77

    
78
pprint.pprint(links)