@@ -21,11 +21,63 @@ async def upload_profiles(session):
21
21
22
22
23
23
async def upload_extension_attributes (session , url , user , passwd ):
24
- pass
24
+ mypath = dirname (realpath (__file__ ))
25
+ ext_attrs = [f .name for f in os .scandir (join (mypath , 'extension_attributes' ))
26
+ if f .is_file () and f .name .split ('.' )[- 1 ] in supported_ea_extensions ]
27
+ tasks = []
28
+ for ea in ext_attrs :
29
+ task = asyncio .ensure_future (upload_extension_attribute (session , url , user , passwd , ea ))
30
+ tasks .append (task )
31
+ responses = await asyncio .gather (* tasks )
25
32
26
33
27
34
async def upload_extension_attribute (session , url , user , passwd , ext_attr ):
28
- pass
35
+ mypath = dirname (realpath (__file__ ))
36
+ auth = aiohttp .BasicAuth (user , passwd )
37
+ headers = {'content-type' : 'application/xml' }
38
+ with open (join (mypath , 'extension_attributes/' + ext_attr ), 'r' ) as f :
39
+ data = f .read ()
40
+ with async_timeout .timeout (10 ):
41
+ template = await get_ea_template (session , url , user , passwd , ext_attr )
42
+ async with session .get (url + '/JSSResource/computerextensionattributes/name/' + template .find ('name' ).text ,
43
+ auth = auth ) as resp :
44
+ template .find ('input_type/script' ).text = data
45
+ if args .verbose :
46
+ print (ET .tostring (template ))
47
+ if resp .status == 200 :
48
+ put_url = url + '/JSSResource/computerextensionattributes/name/' + template .find ('name' ).text
49
+ resp = await session .put (put_url , auth = auth , data = ET .tostring (template ), headers = headers )
50
+ else :
51
+ post_url = url + '/JSSResource/computerextensionattributes/id/0'
52
+ resp = await session .post (post_url , auth = auth , data = ET .tostring (template ), headers = headers )
53
+ if resp .status in (201 , 200 ):
54
+ print (f'Uploaded Extension Attribute { ext_attr } ' )
55
+ return resp .status
56
+
57
+
58
+ async def get_ea_template (session , url , user , passwd , ext_attr ):
59
+ auth = aiohttp .BasicAuth (user , passwd )
60
+ mypath = dirname (realpath (__file__ ))
61
+ try :
62
+ with open (join (mypath , 'extension_attributes/templates/' + ext_attr .split ('.' )[0 ] + '.xml' ), 'r' ) as file :
63
+ template = ET .fromstring (file .read ())
64
+ except FileNotFoundError :
65
+ with async_timeout .timeout (10 ):
66
+ async with session .get (url + '/JSSResource/computerextensionattributes/name/' + ext_attr ,
67
+ auth = auth ) as resp :
68
+ if resp .status == 200 :
69
+ async with session .get (url + '/JSSResource/computerextensionattributes/name/' + ext_attr , auth = auth ) as response :
70
+ template = ET .fromstring (await response .text ())
71
+ else :
72
+ template = ET .parse (join (mypath , 'templates/ea.xml' )).getroot ()
73
+ # name is mandatory, so we use the filename if nothing is set in a template
74
+ if args .verbose :
75
+ print (ET .tostring (template ))
76
+ if template .find ('name' ) is None :
77
+ ET .SubElement (template , 'name' ).text = ext_attr
78
+ elif template .find ('name' ).text is '' or template .find ('name' ).text is None :
79
+ template .find ('name' ).text = ext_attr
80
+ return template
29
81
30
82
31
83
async def upload_scripts (session , url , user , passwd ):
@@ -90,6 +142,7 @@ async def get_script_template(session, url, user, passwd, script):
90
142
async def main (args ):
91
143
async with aiohttp .ClientSession () as session :
92
144
await upload_scripts (session , args .url , args .username , args .password )
145
+ await upload_extension_attributes (session , args .url , args .username , args .password )
93
146
94
147
95
148
if __name__ == '__main__' :
0 commit comments