forked from linode/linode_api4-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmappings.py
More file actions
59 lines (48 loc) · 1.58 KB
/
Copy pathmappings.py
File metadata and controls
59 lines (48 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import linode.objects
from linode import util
def get_mapping(id):
id_map = {
'lnde': linode.objects.Linode,
'disk': linode.objects.Disk,
'dist': linode.objects.Distribution,
'serv': linode.objects.Service,
'dctr': linode.objects.Datacenter,
'stck': linode.objects.StackScript,
'conf': linode.objects.Config,
'krnl': linode.objects.Kernel,
'ljob': linode.objects.Job,
'zone': linode.objects.Zone,
'zrcd': linode.objects.ZoneRecord,
}
parts = id.split('_')
if not len(parts) == 2:
return False
if parts[0] in id_map:
return id_map[parts[0]]
return None
def make(id, client, parent_id=None):
"""
Makes an api object based on an id. The type depends on the mapping.
"""
c = get_mapping(id)
if c:
if issubclass(c, linode.objects.DerivedBase):
return c(client, id, parent_id)
else:
return c(client, id)
return None
def make_list(json_arr, client, parent_id=None):
result = []
for obj in json_arr:
if not 'id' in obj:
continue
o = make(obj['id'], client, parent_id=parent_id)
o._populate(obj)
result.append(o)
return result
def make_paginated_list(json, key, client, parent_id=None, page_url=None):
l = make_list(json[key], client, parent_id=parent_id)
p = util.PaginatedList(client, page_url if page_url else key, page=l, \
max_pages=json['total_pages'], total_items=json['total_results'], parent_id=parent_id, \
key=key)
return p