forked from dougsland/ovirt-restapi-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateVM.py
86 lines (73 loc) · 2.21 KB
/
createVM.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
#!/usr/bin/python
#
# Copyright (C) 2011
#
# Douglas Schilling Landgraf <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import urllib2
import base64
import sys
if len(sys.argv) <= 3:
print "Usage: %s cluster_name vm_NAME memory (in bytes) template (if you have one, otherwise it will be Blank)" %(sys.argv[0])
print "Example: %s cluster_ISCSI myNEW_VM 536870912" %(sys.argv[0])
print "Note:"
print "536870912 (bytes) = (512MB) "
sys.exit(1)
print "Creating vm %s" %(sys.argv[2])
if len(sys.argv) < 5:
xml_request ="""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<vm>
<name>""" + sys.argv[2] + """</name>
<cluster>
<name>""" + sys.argv[1] + """</name>
</cluster>
<template>
<name>Blank</name>
</template>
<memory>""" + sys.argv[3] + """</memory>
</vm>
"""
else:
xml_request ="""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<vm>
<name>""" + sys.argv[2] + """</name>
<cluster>
<name>""" + sys.argv[1] + """</name>
</cluster>
<template>
<name>""" + sys.argv[4] + """</name>
</template>
<memory>""" + sys.argv[3] + """</memory>
</vm>
"""
#print xml_request
# Example
ADDR = "192.168.123.176"
API_PORT = "8443"
USER = "[email protected]"
PASSWD = "T0pSecreT!"
# Setting URL
URL = "https://" + ADDR + ":" + API_PORT + "/api/vms"
request = urllib2.Request(URL)
print "Connecting to: " + URL
base64string = base64.encodestring('%s:%s' % (USER, PASSWD)).strip()
request.add_header("Authorization", "Basic %s" % base64string)
request.add_header('Content-Type', 'application/xml')
try:
ret = urllib2.urlopen(request, xml_request)
except urllib2.URLError, e:
print "%s" %(e)
print "Are you trying to add an existing item?"
sys.exit(-1)
print "Done!"
# To see the response from the server
#response = ret.read()
#print response