define_accessors accepts a list of classes if needed
[plcapi.git] / db-config.d / 000-functions
1 # -*-python-*-
2 #################### 
3 import sys, os
4 import resource
5
6 g_url = ""
7 def GetMyPLCURL(): return g_url
8 def SetMyPLCURL(url):
9     global g_url
10     g_url = url
11
12 # Get all currently registered roles
13 g_role_names = [ role['name'] for role in GetRoles()]
14 g_role_names.sort()
15
16 def SetRole(level, role):
17     global g_role_names
18     if role not in g_role_names:
19         AddRole(level, role)
20         g_role_names.append(role)
21         g_role_names.sort()
22
23 # Get list of existing tag types
24 g_known_tag_types = [tag_type['tagname'] for tag_type in GetTagTypes()]
25 g_known_tag_types.sort()
26
27 def SetTagType(tag_type):
28     global g_known_tag_types
29     # Create/update default slice tag types
30     if tag_type['tagname'] not in g_known_tag_types:
31         AddTagType(tag_type)
32         g_known_tag_types.append(tag_type['tagname'])
33         g_known_tag_types.sort()
34     else:
35         UpdateTagType(tag_type['tagname'], tag_type)
36     if 'roles' in tag_type:
37         for role in tag_type['roles']:
38             AddRoleToTagType(role,tag_type['tagname'])
39
40 # Get list of existing (enabled, global) files
41 g_conf_files = GetConfFiles()
42 g_conf_files = filter(lambda conf_file: conf_file['enabled'] and \
43                     not conf_file['node_ids'] and \
44                     not conf_file['nodegroup_ids'],
45                     g_conf_files)
46 g_dests = [conf_file['dest'] for conf_file in g_conf_files]
47 g_conf_files = dict(zip(g_dests, g_conf_files))
48
49 # Get list of existing initscripts
50 g_oldinitscripts = GetInitScripts()
51 g_oldinitscript_names = [script['name'] for script in g_oldinitscripts]
52 g_oldinitscripts = dict(zip(g_oldinitscript_names, g_oldinitscripts))
53
54 def SetInitScript(initscript):
55     global g_oldinitscripts, g_oldinitscript_names
56     if initscript['name'] not in g_oldinitscript_names:
57         initscript_id = AddInitScript(initscript)
58         g_oldinitscript_names.append(initscript['name'])
59         initscript['initscript_id']=initscript_id
60         g_oldinitscripts[initscript['name']]=initscript
61     else:
62         orig_initscript = g_oldinitscripts[initscript['name']]
63         initscript_id = orig_initscript['initscript_id']
64         UpdateInitScript(initscript_id, initscript)
65         
66 def SetConfFile(conf_file):
67     global g_conf_files, g_dests
68     if conf_file['dest'] not in g_dests:
69         AddConfFile(conf_file)
70     else:
71         orig_conf_file = g_conf_files[conf_file['dest']]
72         conf_file_id = orig_conf_file['conf_file_id']
73         UpdateConfFile(conf_file_id, conf_file)
74
75 def SetSlice(slice, tags):
76     # Create or Update slice
77     slice_name = slice['name']
78     slices = GetSlices([slice_name])
79     if len(slices)==1:
80         slice_id = slices[0]['slice_id']
81         if slice.has_key('name'):
82             del slice['name']
83         UpdateSlice(slice_id, slice)
84         slice['name']=slice_name
85     else:
86         expires = None
87         if slice.has_key('expires'):
88             expires = slice['expires']
89             del slice['expires']
90         slice_id = AddSlice(slice)
91         if expires <> None:
92             UpdateSlice(slice_id, {'expires':expires})
93
94     # Get slice structure with all fields
95     slice = GetSlices([slice_name])[0]
96
97     # Create/delete all tags
98     # NOTE: update is not needed, since unspecified tags are deleted, 
99     #       and new tags are added
100     slice_tags = []
101     if slice['slice_tag_ids']:
102         # Delete unknown attributes
103         for slice_tag in GetSliceTags(slice['slice_tag_ids']):
104             # ignore sliver tags, as those are custom/run-time values
105             if slice_tag['node_id'] <> None: continue
106             if (slice_tag['tagname'], slice_tag['value']) not in tags:
107                 DeleteSliceTag(slice_tag['slice_tag_id'])
108             else:
109                 slice_tags.append((slice_tag['tagname'],slice_tag['value']))
110
111     # only add slice tags that are new
112     for (name, value) in tags:
113         if (name,value) not in slice_tags:
114             AddSliceTag(slice_name, name, value)            
115         else:
116             # NOTE: this confirms that the user-specified tag is 
117             #       returned by GetSliceTags
118             pass
119
120 def SetMessage(message):
121     messages = GetMessages([message['message_id']])
122     if len(messages)==0:
123         AddMessage(message)
124     else:
125         UpdateMessage(message['message_id'],message)
126
127 # Get all model names
128 g_pcu_models = [type['model'] for type in GetPCUTypes()]
129
130 def SetPCUType(pcu_type):
131     global g_pcu_models
132     if 'pcu_protocol_types' in pcu_type:
133         protocol_types = pcu_type['pcu_protocol_types']
134         # Take this value out of the struct.
135         del pcu_type['pcu_protocol_types']
136     else:
137         protocol_types = []
138
139     if pcu_type['model'] not in g_pcu_models:
140         # Add the name/model info into DB
141         id = AddPCUType(pcu_type)
142         # for each protocol, also add this.
143         for ptype in protocol_types:
144             AddPCUProtocolType(id, ptype)
145