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