-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools2.py
46 lines (40 loc) · 1.89 KB
/
tools2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from requests import *
from requests.auth import HTTPBasicAuth
from xml.dom.minidom import parseString
import datetime
def getraw(url,token):
r=get(url,headers={'x-vcloud-authorization':token,'Accept':'application/*+xml;version=1.5'})
return r.text
def getorgurl(orgname,token):
orgurl=[];roleurl=""
dom=parseString(getraw('https://www.cloud.kth.se/api/admin',token))
for e in dom.getElementsByTagName("OrganizationReferences")[0].childNodes:
if e.nodeType==e.ELEMENT_NODE:
orgurl.append(e.getAttribute("href"))
for e in dom.getElementsByTagName("RoleReferences")[0].childNodes:
if e.nodeType==e.ELEMENT_NODE:
if e.getAttribute("name")=="Organization Administrator":
roleurl=e.getAttribute("href")
return (orgurl,roleurl)
def getvdcurl(orgurl,token):
dom=parseString(getraw(orgurl,token))
return {e.getAttribute("name"):e.getAttribute("href") for e in dom.getElementsByTagName("Vdcs")[0].childNodes if e.nodeType==e.ELEMENT_NODE}
def getdesc(vdcurl,token):
dom=parseString(getraw(vdcurl,token))
return "".join([e.data for e in dom.getElementsByTagName("Description")[0].childNodes if e])
def getcapacity(vdcurl,token):
dom=parseString(getraw(vdcurl,token))
#print {e.parentNode.tagName:e.childNodes[0].data for e in dom.getElementsByTagName("Units")}
return {e.parentNode.tagName:int(e.childNodes[0].data) for e in dom.getElementsByTagName("Allocated")}
if __name__ == "__main__":
url='https://www.cloud.kth.se/api/sessions'
with open('.c') as f:
(u,pw)=f.read().strip().split(':')
org=u.split('@')[-1]
r=post(url,auth=HTTPBasicAuth(u, pw),headers={'Accept':'application/*+xml;version=1.5'})
token=r.headers['x-vcloud-authorization']
(orgurl,roleurl)=getorgurl(org,token)
for o in orgurl:
vdcs=getvdcurl(o,token)
for (vdc,u) in vdcs.items():
print datetime.datetime.now().strftime("%Y%m%d"),vdc,";".join(["%s:%s" % x for x in sorted(getcapacity(u,token).items())]), getdesc(u,token)