Skip to content

Commit 2bd830c

Browse files
committed
added support for extension attributes
1 parent 66360e7 commit 2bd830c

File tree

5 files changed

+84
-3
lines changed

5 files changed

+84
-3
lines changed

extension_attributes/sample_ea.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
echo "<result>hello world</result>"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<computer_extension_attribute>
2+
<id></id>
3+
<name>Sample EA Uploaded by git2jss</name>
4+
<description/>
5+
<data_type>String</data_type>
6+
<input_type>
7+
<type>script</type>
8+
<platform>Mac</platform>
9+
<script></script>
10+
</input_type>
11+
<inventory_display>Extension Attributes</inventory_display>
12+
<recon_display>Extension Attributes</recon_display>
13+
</computer_extension_attribute>

scripts/templates/sample_script.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<script>
2-
<notes>testing testing testing</notes>
2+
<name>Sample Script Uploaded by git2jss</name>
33
<script_contents> </script_contents>
44
</script>

sync.py

+55-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,63 @@ async def upload_profiles(session):
2121

2222

2323
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)
2532

2633

2734
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
2981

3082

3183
async def upload_scripts(session, url, user, passwd):
@@ -90,6 +142,7 @@ async def get_script_template(session, url, user, passwd, script):
90142
async def main(args):
91143
async with aiohttp.ClientSession() as session:
92144
await upload_scripts(session, args.url, args.username, args.password)
145+
await upload_extension_attributes(session, args.url, args.username, args.password)
93146

94147

95148
if __name__ == '__main__':

templates/ea.xml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<computer_extension_attribute>
2+
<id></id>
3+
<name></name>
4+
<description/>
5+
<data_type>String</data_type>
6+
<input_type>
7+
<type>script</type>
8+
<platform>Mac</platform>
9+
<script></script>
10+
</input_type>
11+
<inventory_display>Extension Attributes</inventory_display>
12+
<recon_display>Extension Attributes</recon_display>
13+
</computer_extension_attribute>

0 commit comments

Comments
 (0)