Skip to content

Commit 8dfa137

Browse files
committed
Merge pull request #2 from InfraSIM/mark-dev-branch
Support to modify EXSi vm memory size
2 parents 5a6d4f0 + 6ca27be commit 8dfa137

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

AutoDeployUI/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
url(r'^api/v1/esxi/(?P<id>[0-9]+)/datastores$', 'app.view.views.esxi_datastore', name='esxi_get_datastore'),
4343
url(r'^api/v1/esxi/(?P<id>[0-9]+)/networks$', 'app.view.views.esxi_network', name='esxi_get_network'),
4444
url(r'^api/v1/esxi/(?P<id>[0-9]+)/deploy$', 'app.view.views.esxi_deploy', name='esxi_deploy'),
45+
url(r'^api/v1/esxi/(?P<id>[0-9]+)/changemem$', 'app.view.views.esxi_change_memory', name='esxi_change_memory'),
4546
url(r'^api/v1/esxi/(?P<id>[0-9]+)/adddrive$', 'app.view.views.esxi_add_drive', name='esxi_add_drive'),
4647
url(r'^api/v1/esxi/(?P<id>[0-9]+)/addnic$', 'app.view.views.esxi_add_nic', name='esxi_add_nic'),
4748
url(r'^api/v1/esxi/(?P<id>[0-9]+)/vpduhostlist$', 'app.view.views.esxi_vpdu_host_config_list', name='esxi_vpdu_host_config_list'),

app/view/views.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,39 @@ def esxi_network(request, id, format=None):
7676
else:
7777
return Response(network, status=status.HTTP_200_OK)
7878

79+
80+
@api_view(('POST'),)
81+
def esxi_change_memory(request, id, format=None):
82+
"""
83+
Change VM Memory
84+
---
85+
parameters:
86+
- name: name
87+
description: Please input VM name
88+
required: true
89+
type: string
90+
paramType: form
91+
- name: size
92+
description: Please input memory size(MB)
93+
required: true
94+
type: string
95+
paramType: form
96+
"""
97+
try:
98+
esxi = ESXi.objects.get(id = id)
99+
except ESXi.DoesNotExist:
100+
content = "Cannot find the ESXi."
101+
return HttpResponse(content, status = status.HTTP_404_NOT_FOUND)
102+
103+
usr = esxi.username
104+
pwd = esxi.password
105+
host = esxi.esxiIP
106+
107+
content = vRackBuilder.esxi_change_memory(host, usr, pwd, request.data["name"], request.data["size"])
108+
if "fail" in content.lower() or "can't" in content.lower():
109+
return Response("Fail to Change VM memory size, please check your VM or ESXi", status=status.HTTP_500_INTERNAL_SERVER_ERROR)
110+
111+
79112
@api_view(('POST',))
80113
def esxi_add_drive(request, id, format=None):
81114
"""

lib/vRackBuilder/api.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,30 @@ def esxi_destory_vm(host, usr, pwd, vm_name):
222222

223223
return "Destory VM(%s) success!" % vm_name
224224

225+
def esxi_change_memory(host, usr, pwd, vm_name, mem_size):
226+
try:
227+
si = connect.Connect(host=host, user=usr, pwd=pwd)
228+
vm_view = si.content.viewManager.CreateContainerView(si.content.rootFolder, [vim.VirtualMachine], True)
229+
uuid = ""
230+
for vm in vm_view.view:
231+
if vm.summary.config.name == vm_name:
232+
uuid = vm.summary.config.instanceUuid
233+
break
234+
if uuid == "":
235+
connect.Disconnect(si)
236+
return "Can't find VM(%s)!" % vm_name
237+
238+
vm = si.content.searchIndex.FindByUuid(None, uuid, True, True)
239+
spec = vim.vm.ConfigSpec()
240+
spec.memoryMB = int(mem_size) + 512
241+
task = vm.ReconfigVM_Task(spec)
242+
tasks.wait_for_tasks(si, [task])
243+
244+
connect.Disconnect(si)
245+
except Exception, e:
246+
return "Change VM Memory on VM(%s) failed!" % vm_name
247+
return "Change VM(%s) Memory successfully!" % vm_name
248+
225249
def esxi_add_drive(host, usr, pwd, vm_name, disk_size):
226250
try:
227251
si = connect.Connect(host=host, user=usr, pwd=pwd)

0 commit comments

Comments
 (0)