Skip to content

Commit 2c76263

Browse files
WIP commit for COmanage mass CO Person creation / modification script.
1 parent 5236b3e commit 2c76263

File tree

4 files changed

+498
-0
lines changed

4 files changed

+498
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.vscode/launch.json
2+
__pycache__/*

comanage_person_schema_utils.py

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
#!/usr/bin/env python3
2+
3+
CO_PERSON = {"co_id": None, "timezone": None, "dateofbirth": None, "status": None}
4+
5+
IDENTIFIER = {
6+
"identifier": None,
7+
"type": None,
8+
"login": False,
9+
"status": None,
10+
}
11+
12+
NAME = {
13+
"honorific": None,
14+
"given": "None",
15+
"middle": None,
16+
"family": "None",
17+
"suffix": None,
18+
"type": "official",
19+
"language": None,
20+
"primary_name": True,
21+
}
22+
23+
24+
GROUP = {
25+
"co_group_id": None,
26+
"member": True,
27+
"owner": False,
28+
}
29+
30+
31+
COU = {
32+
"co_id": None,
33+
"name": None,
34+
"description": None,
35+
"parent_id": None,
36+
}
37+
38+
39+
ORG_IDENTITY = {
40+
"co_id": None,
41+
"title": None,
42+
# Organization for OrgID
43+
"o": None,
44+
# Department for OrgID
45+
"ou": None,
46+
"valid_from": None,
47+
"valid_through": None,
48+
"status": "",
49+
"affiliation": None,
50+
"date_of_birth": None,
51+
"Address": [],
52+
"AdHocAttribute": [],
53+
"EmailAddress": [],
54+
"Identifier": [],
55+
"Name": [],
56+
"TelephoneNumber": [],
57+
"Url": [],
58+
}
59+
60+
61+
EMAIL_ADDRESS = {
62+
"mail": None,
63+
"type": None,
64+
"verified": None,
65+
}
66+
67+
68+
UNIX_CLUSTER_ACCOUNT = {
69+
"sync_mode": "F",
70+
"status": None,
71+
"username": None,
72+
"uid": None,
73+
"gecos": None,
74+
"login_shell": "/bin/bash",
75+
"home_directory": None,
76+
"primary_co_group_id": None,
77+
"valid_from": None,
78+
"valid_through": None,
79+
"unix_cluster_id": None,
80+
}
81+
82+
83+
SSHKEY = {
84+
"type": None,
85+
"skey": None,
86+
"comment": "",
87+
"ssh_key_authenticator_id": None,
88+
}
89+
90+
91+
def co_person_schema(co_id, timezone=None, dob=None, status="Active"):
92+
person_data = CO_PERSON.copy()
93+
person_data["co_id"] = co_id
94+
person_data["timezone"] = timezone
95+
person_data["dateofbirth"] = dob
96+
person_data["status"] = status
97+
return person_data
98+
99+
100+
def co_person_identifier(identifier, type, login=False, status="Active"):
101+
identifier_data = IDENTIFIER.copy()
102+
identifier_data["identifier"] = identifier
103+
identifier_data["type"] = type
104+
identifier_data["login"] = login
105+
identifier_data["status"] = status
106+
return identifier_data
107+
108+
109+
def co_person_name(given, family=None, middle=None, type="official", primary=False):
110+
name_data = NAME.copy()
111+
name_data["given"] = given
112+
name_data["family"] = family
113+
name_data["middle"] = middle
114+
name_data["type"] = type
115+
name_data["primary_name"] = primary
116+
return name_data
117+
118+
119+
def name_split(whole_name):
120+
name_sections = str(whole_name).split()
121+
parts_count = len(name_sections)
122+
if parts_count == 1 or parts_count > 3:
123+
return co_person_name(whole_name)
124+
elif parts_count == 2:
125+
return co_person_name(name_sections[0], name_sections[1])
126+
else:
127+
return co_person_name(co_person_name(name_sections[0], name_sections[2], name_sections[1]))
128+
129+
130+
def co_person_group_member(group_id, member=True, owner=False):
131+
group_member = GROUP.copy()
132+
group_member["co_group_id"] = group_id
133+
group_member["member"] = member
134+
group_member["owner"] = owner
135+
return group_member
136+
137+
138+
def co_person_org_id(
139+
osg_co_id, name, organization="", department="", title="", affiliation="member", id_list=[]
140+
):
141+
#org_id = {"co_id" : osg_co_id}
142+
org_id = ORG_IDENTITY.copy()
143+
org_id["co_id"] = osg_co_id
144+
org_id["title"] = title
145+
org_id["o"] = organization
146+
org_id["ou"] = department
147+
org_id["affiliation"] = affiliation
148+
org_id["Identifier"] = id_list
149+
org_id["Name"] = name
150+
return org_id
151+
152+
153+
def co_person_email_address(mail, type="delivery", verified=False):
154+
email = EMAIL_ADDRESS.copy()
155+
email["mail"] = mail
156+
email["type"] = type
157+
email["verified"] = verified
158+
return email
159+
160+
161+
def co_person_unix_cluster_acc(unix_cluster_id, username, uid, name, group_id, status="A"):
162+
uca = UNIX_CLUSTER_ACCOUNT.copy()
163+
uca["unix_cluster_id"] = unix_cluster_id
164+
uca["username"] = username
165+
uca["uid"] = uid
166+
uca["status"] = status
167+
uca["gecos"] = name
168+
uca["home_directory"] = f"/home/{username}"
169+
uca["primary_co_group_id"] = group_id
170+
return uca
171+
172+
173+
def co_person_sshkey(type, skey, comment, auth_id):
174+
sshkey_data = SSHKEY.copy()
175+
sshkey_data["type"] = type
176+
sshkey_data["skey"] = skey
177+
sshkey_data["comment"] = comment
178+
sshkey_data["ssh_key_authenticator_id"] = auth_id
179+
return sshkey_data
180+
181+
182+
# def merge_schema(base_data, new_data, type):
183+
# temp = base_data
184+
# for field in type.keys():
185+
# for entry in new_data[field]:
186+
# if "meta" in entry:
187+
#
188+
#
189+
# return temp

comanage_utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def call_api3(method, target, data, endpoint, authstr, **kw):
9999
resp = urllib.request.urlopen(req, timeout=current_timeout)
100100
# exception catching, mainly for request timeouts, "Service Temporarily Unavailable" (Rate limiting), and DNS failures.
101101
except urllib.error.URLError as exception:
102+
print(exception)
102103
req_attempts += 1
103104
if req_attempts >= MAX_ATTEMPTS:
104105
raise URLRequestError(
@@ -140,6 +141,18 @@ def get_co_group(gid, endpoint, authstr):
140141
return grouplist[0]
141142

142143

144+
def core_api_co_person_read(identifier, coid, endpoint, authstr):
145+
return call_api(f"api/co/{coid}/core/v1/people/{identifier}", endpoint, authstr)
146+
147+
148+
def core_api_co_person_create(data, coid, endpoint, authstr):
149+
return call_api3(POST, f"api/co/{coid}/core/v1/people/", data, endpoint, authstr)
150+
151+
152+
def core_api_co_person_update(identifier, coid, data, endpoint, authstr):
153+
return call_api3(PUT, f"api/co/{coid}/core/v1/people/{identifier}", data, endpoint, authstr)
154+
155+
143156
def get_identifier(id_, endpoint, authstr):
144157
resp_data = call_api("identifiers/%s.json" % id_, endpoint, authstr)
145158
idfs = get_datalist(resp_data, "Identifiers")
@@ -190,6 +203,23 @@ def identifier_matches(id_list, id_type, regex_string):
190203
return (value is not None) and (pattern.match(value) is not None)
191204

192205

206+
def create_co_group(groupname, description, coId, endpoint, authstr, open=False,):
207+
group_info = {
208+
"Version" : "1.0",
209+
"CoId" : coId,
210+
"Name" : groupname,
211+
"Description" : description,
212+
"Open" : open,
213+
"Status" : "Active",
214+
}
215+
data = {
216+
"CoGroups" : [group_info],
217+
"RequestType" : "CoGroups",
218+
"Version" : "1.0"
219+
}
220+
return call_api3(POST, "co_groups/.json", data, endpoint, authstr)
221+
222+
193223
def rename_co_group(gid, group, newname, endpoint, authstr):
194224
# minimal edit CoGroup Request includes Name+CoId+Status+Version
195225
new_group_info = {

0 commit comments

Comments
 (0)