Setting tag plcapi-5.4-2
[plcapi.git] / db-config.d / 000-functions
1 # -*-python-*-
2 #################### 
3 import sys, os
4
5 g_url = ""
6 def GetMyPLCURL(): return g_url
7 def SetMyPLCURL(url):
8     global g_url
9     g_url = url
10
11 # Get all currently registered roles
12 g_role_names = [ role['name'] for role in GetRoles()]
13 g_role_names.sort()
14
15 def SetRole(level, role):
16     global g_role_names
17     if role not in g_role_names:
18         AddRole(level, role)
19         g_role_names.append(role)
20         g_role_names.sort()
21
22 # Get list of existing tag types
23 g_known_tag_types = [tag_type['tagname'] for tag_type in GetTagTypes()]
24 g_known_tag_types.sort()
25
26 def AllPersonRoles (): return [ 'pi','user','tech' ]
27
28 def SetTagType(tag_type):
29     try:
30         tagname=tag_type['tagname']
31         global g_known_tag_types
32         # handle 'roles' field differently
33         if 'roles' in tag_type:
34             roles=tag_type['roles']
35             del tag_type['roles']
36         else:
37             roles=['admin']
38         # just in case
39         if 'min_role_id' in tag_type:
40             print "WARNING: ignoring deprecated field min_role_id for tagtype %s"%tagname
41             del tag_type['min_role_id']
42         # Create/update default slice tag types
43         if tagname not in g_known_tag_types:
44             AddTagType(tag_type)
45             g_known_tag_types.append(tagname)
46             g_known_tag_types.sort()
47         else:
48             UpdateTagType(tagname, tag_type)
49         # enforce provided roles if present
50         old_roles=GetTagTypes(tagname)[0]['roles']
51         for minus_role in set(old_roles).difference(set(roles)):
52             DeleteRoleFromTagType(minus_role,tagname)
53         for plus_role in set(roles).difference(set(old_roles)):
54             AddRoleToTagType(plus_role,tagname)
55     except:
56         # something went wrong for that tagname, 
57         # but don't want to break the whole startup sequence
58         print "Could not enforce tagtype %s --- beg"%tagname
59         import traceback
60         traceback.print_exc()
61         print "Could not enforce tagtype %s --- end"%tagname
62
63 # Get list of existing (enabled, global) files
64 g_conf_files = GetConfFiles()
65 g_conf_files = filter(lambda conf_file: conf_file['enabled'] and \
66                     not conf_file['node_ids'] and \
67                     not conf_file['nodegroup_ids'],
68                     g_conf_files)
69 g_dests = [conf_file['dest'] for conf_file in g_conf_files]
70 g_conf_files = dict(zip(g_dests, g_conf_files))
71
72 # Get list of existing initscripts
73 g_oldinitscripts = GetInitScripts()
74 g_oldinitscript_names = [script['name'] for script in g_oldinitscripts]
75 g_oldinitscripts = dict(zip(g_oldinitscript_names, g_oldinitscripts))
76
77 def SetInitScript(initscript):
78     global g_oldinitscripts, g_oldinitscript_names
79     if initscript['name'] not in g_oldinitscript_names:
80         initscript_id = AddInitScript(initscript)
81         g_oldinitscript_names.append(initscript['name'])
82         initscript['initscript_id']=initscript_id
83         g_oldinitscripts[initscript['name']]=initscript
84     else:
85         orig_initscript = g_oldinitscripts[initscript['name']]
86         initscript_id = orig_initscript['initscript_id']
87         UpdateInitScript(initscript_id, initscript)
88         
89 def SetConfFile(conf_file):
90     global g_conf_files, g_dests
91     if conf_file['dest'] not in g_dests:
92         AddConfFile(conf_file)
93     else:
94         orig_conf_file = g_conf_files[conf_file['dest']]
95         conf_file_id = orig_conf_file['conf_file_id']
96         UpdateConfFile(conf_file_id, conf_file)
97
98 def SetSlice(slice, tags):
99     try:
100         # Create or Update slice
101         slice_name = slice['name']
102         slices = GetSlices([slice_name])
103         if len(slices)==1:
104             slice_id = slices[0]['slice_id']
105             if slice.has_key('name'):
106                 del slice['name']
107             UpdateSlice(slice_id, slice)
108             slice['name']=slice_name
109         else:
110             expires = None
111             if slice.has_key('expires'):
112                 expires = slice['expires']
113                 del slice['expires']
114             slice_id = AddSlice(slice)
115             if expires <> None:
116                 UpdateSlice(slice_id, {'expires':expires})
117     
118         # Get slice structure with all fields
119         slice = GetSlices([slice_name])[0]
120     
121         # Create/delete all tags
122         # NOTE: update is not needed, since unspecified tags are deleted, 
123         #       and new tags are added
124         slice_tags = []
125         if slice['slice_tag_ids']:
126             # Delete unknown attributes
127             for slice_tag in GetSliceTags(slice['slice_tag_ids']):
128                 # ignore sliver tags, as those are custom/run-time values
129                 if slice_tag['node_id'] <> None: continue
130                 if (slice_tag['tagname'], slice_tag['value']) not in tags:
131                     DeleteSliceTag(slice_tag['slice_tag_id'])
132                 else:
133                     slice_tags.append((slice_tag['tagname'],slice_tag['value']))
134     
135         # only add slice tags that are new
136         for (name, value) in tags:
137             if (name,value) not in slice_tags:
138                 AddSliceTag(slice_name, name, value)            
139             else:
140                 # NOTE: this confirms that the user-specified tag is 
141                 #       returned by GetSliceTags
142                 pass
143     except:
144         # something went wrong for that tagname, 
145         print "Could not create init slice %s --- beg"%slice['name']
146         import traceback
147         traceback.print_exc()
148         print "Could not create init slice %s --- end"%slice['name']
149
150 def SetMessage(message):
151     messages = GetMessages([message['message_id']])
152     if len(messages)==0:
153         AddMessage(message)
154     ### Thierry 2012-03
155     # let people customize their messages if they want to
156     #else:
157     #    UpdateMessage(message['message_id'],message)
158
159 # Get all model names
160 g_pcu_models = [type['model'] for type in GetPCUTypes()]
161
162 def SetPCUType(pcu_type):
163     global g_pcu_models
164     if 'pcu_protocol_types' in pcu_type:
165         protocol_types = pcu_type['pcu_protocol_types']
166         # Take this value out of the struct.
167         del pcu_type['pcu_protocol_types']
168     else:
169         protocol_types = []
170
171     if pcu_type['model'] not in g_pcu_models:
172         # Add the name/model info into DB
173         id = AddPCUType(pcu_type)
174         # for each protocol, also add this.
175         for ptype in protocol_types:
176             AddPCUProtocolType(id, ptype)
177