Skip to content

Commit 7345d5f

Browse files
committed
add a bunch of functions to support co group fixups (SOFTWARE-5057)
1 parent 4999af2 commit 7345d5f

File tree

1 file changed

+127
-2
lines changed

1 file changed

+127
-2
lines changed

group_fixup.py

Lines changed: 127 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
import os
4+
import re
45
import sys
56
import json
67
import getopt
@@ -110,12 +111,13 @@ def call_api3(method, target, data, **kw):
110111
return json.loads(payload) if payload else None
111112

112113

114+
# primary api calls
115+
116+
113117
def get_osg_co_groups():
114118
return call_api("co_groups.json", coid=options.osg_co_id)
115119

116120

117-
# primary api calls
118-
119121
def get_co_group_identifiers(gid):
120122
return call_api("identifiers.json", cogroupid=gid)
121123

@@ -128,6 +130,13 @@ def get_co_person_identifiers(pid):
128130
return call_api("identifiers.json", copersonid=pid)
129131

130132

133+
def get_co_group(gid):
134+
grouplist = call_api("co_groups/%d.json" % gid) | get_datalist("CoGroups")
135+
if not grouplist:
136+
raise RuntimeError("No such CO Group Id: %s" % gid)
137+
return grouplist[0]
138+
139+
131140
# @rorable
132141
# def foo(x): ...
133142
# x | foo -> foo(x)
@@ -146,11 +155,127 @@ def get(data):
146155
# api call results massagers
147156

148157

158+
def get_unixcluser_autogroups():
159+
groups = get_osg_co_groups()
160+
return [ g for g in groups["CoGroups"]
161+
if "automatically by UnixCluster" in g["Description"] ]
162+
163+
164+
def get_misnamed_unixcluster_groups():
165+
groups = get_osg_co_groups()
166+
return [ g for g in groups["CoGroups"]
167+
if "UnixCluster Group" in g["Name"] ]
168+
169+
170+
def _osgid_sortkey(i):
171+
return int(i["Identifier"])
172+
173+
def get_identifiers_to_delete(identifiers):
174+
by_type = collections.defaultdict(list)
175+
ids_to_delete = []
176+
177+
for i in identifiers:
178+
by_type[i["Type"]].append(i)
179+
180+
if len(by_type["osggid"]) == 2:
181+
min_identifier = min(by_type["osggid"], key=_osgid_sortkey)
182+
ids_to_delete.append(min_identifier["Id"])
183+
184+
for i in by_type["osggroup"]:
185+
if i["Identifier"].endswith("unixclustergroup"):
186+
ids_to_delete.append(i["Id"])
187+
188+
return ids_to_delete
189+
190+
191+
def get_fixed_unixcluster_group_name(name):
192+
m = re.search(r'^(.*) UnixCluster Group', name)
193+
return m.group(1) if m else name
194+
195+
196+
# display functions
197+
198+
199+
def show_misnamed_unixcluster_group(group):
200+
print('CO {CoId} Group {Id}: "{Name}"'.format(**group))
201+
oldname = group["Name"]
202+
newname = get_fixed_unixcluster_group_name(oldname)
203+
if oldname != newname:
204+
print(' ** Rename group to: "%s"' % newname)
205+
show_group_identifiers(group["Id"])
206+
print("")
207+
208+
209+
def show_all_unixcluster_groups():
210+
groups = get_unixcluser_autogroups()
211+
for group in groups:
212+
show_misnamed_unixcluster_group(group)
213+
214+
215+
def show_one_unixcluster_group(gid):
216+
group = get_co_group(gid)
217+
show_misnamed_unixcluster_group(group)
218+
219+
220+
def show_misnamed_unixcluster_groups():
221+
groups = get_misnamed_unixcluster_groups()
222+
for group in groups:
223+
show_misnamed_unixcluster_group(group)
224+
225+
226+
def show_group_identifiers(gid):
227+
identifiers = get_co_group_identifiers(gid) | get_datalist("Identifiers")
228+
for i in identifiers:
229+
print(' - Identifier {Id}: ({Type}) "{Identifier}"'.format(**i))
230+
231+
ids_to_delete = get_identifiers_to_delete(identifiers)
232+
if ids_to_delete:
233+
print(' ** Identifier Ids to delete: %s' % ', '.join(ids_to_delete))
234+
235+
236+
237+
# fixup functions
238+
239+
240+
def delete_identifier(id_):
241+
return call_api2(DELETE, "identifiers/%d.json" % id_)
242+
243+
244+
def rename_co_group(gid, group, newname):
245+
# minimal edit CoGroup Request includes Name+CoId+Status+Version
246+
new_group_info = {
247+
"Name" : newname,
248+
"CoId" : group["CoId"],
249+
"Status" : group["Status"],
250+
"Version" : group["Version"]
251+
}
252+
data = {
253+
"CoGroups" : [new_group_info],
254+
"RequestType" : "CoGroups",
255+
"Version" : "1.0"
256+
}
257+
return call_api3(PUT, "co_groups/%d.json" % gid, data)
258+
149259

260+
def fixup_unixcluster_group(gid):
261+
group = get_co_group(gid)
262+
oldname = group["Name"]
263+
newname = get_fixed_unixcluster_group_name(oldname)
264+
identifiers = get_co_group_identifiers(gid) | get_datalist("Identifiers")
265+
ids_to_delete = get_identifiers_to_delete(identifiers)
150266

267+
show_misnamed_unixcluster_group(group)
268+
if oldname != newname:
269+
rename_co_group(gid, group, newname)
270+
for id_ in ids_to_delete:
271+
delete_identifier(id_)
151272

273+
# http errors raise exceptions, so at this point we apparently succeeded
274+
print(":thumbsup:")
275+
return 0
152276

153277

278+
# CLI
154279

155280

156281
def parse_options(args):

0 commit comments

Comments
 (0)