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