-
Notifications
You must be signed in to change notification settings - Fork 2
/
CSUtils.py
92 lines (78 loc) · 3.12 KB
/
CSUtils.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#
#
import urllib
import os
import sys
import xml.dom.minidom
import re
import base64
import hmac
import hashlib
import httplib
import time
import marvin
import json
import urllib
import urllib2
import logging
from marvin.cloudstackConnection import CSConnection
from marvin.cloudstackException import CloudstackAPIException
from marvin.cloudstackAPI import *
from marvin import cloudstackAPI
class CSUtils(object):
def getApiKeys(self, host, username, password, domain):
if domain == None :
loginparams = urllib.urlencode({'username': username, 'password': password, 'command': 'login'})
else:
loginparams = urllib.urlencode({'username': username, 'password': password, 'domain': domain, 'command': 'login'})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
connection = httplib.HTTPConnection(host, 8080)
request = connection.request("POST", "/client/api?login", loginparams, headers);
resp = connection.getresponse()
cookies = resp.getheader('Set-cookie')
matchObj = re.match( r'JSESSIONID=(.*);.*', cookies, re.M|re.I)
sessionId = matchObj.group(1);
dom = xml.dom.minidom.parseString(resp.read())
if len(dom.getElementsByTagName('sessionkey')) == 0:
print "Login failed"
sys.exit(-1)
sessionKey = dom.getElementsByTagName('sessionkey')[0].firstChild.data
# userId = dom.getElementsByTagName('userid')[0].firstChild.data
userId = 2
print "# Connected with user %s (%s) with sessionKey %s" % (username, userId, sessionKey)
params = urllib.urlencode({'command':'listUsers', 'id':userId, 'sessionkey':sessionKey})
headers = {"Cookie" : "JSESSIONID=%s" % sessionId}
request = connection.request("GET", "/client/api?%s" % params, None, headers);
resp = connection.getresponse()
dom = xml.dom.minidom.parseString(resp.read())
if dom.getElementsByTagName('apikey') :
apiKey = dom.getElementsByTagName('apikey')[0].firstChild.data
secretKey = dom.getElementsByTagName('secretkey')[0].firstChild.data
else:
print "# Account has no apikey, executing registerUserKeys"
params = urllib.urlencode({'command':'registerUserKeys', 'id':userId, 'sessionkey':sessionKey})
headers = {"Cookie" : "JSESSIONID=%s" % sessionId}
request = connection.request("GET", "/client/api?%s" % params, None, headers);
resp = connection.getresponse()
dom = xml.dom.minidom.parseString(resp.read())
apiKey = dom.getElementsByTagName('apikey')[0].firstChild.data
secretKey = dom.getElementsByTagName('secretkey')[0].firstChild.data
connection.close()
return (apiKey, secretKey)
def getConnection(self):
(apikey, secretkey) = self.getApiKeys("localhost", "admin", "password", None)
blub = mgmtDetails()
blub.apiKey=apikey
blub.securityKey=secretkey
conn = CSConnection(blub, logger=logging)
return conn
class mgmtDetails(object):
apiKey = ""
securityKey = ""
mgtSvrIp = "localhost"
port = 8080
user = "admin"
passwd = "password"
certCAPath = None
certPath = None
useHttps = "False"