Skip to content
Snippets Groups Projects
Commit 064b6dc1 authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3173261 by Xiaohua Guan, yas: Edit VMware virtual machine resources

parent ba6d061b
No related branches found
No related tags found
No related merge requests found
......@@ -25,6 +25,7 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface;
* "access" = "Drupal\vmware\Controller\VmwareVmAccessControlHandler",
* "form" = {
* "add" = "Drupal\vmware\Form\VmwareVmCreateForm",
* "edit" = "Drupal\vmware\Form\VmwareVmEditForm",
* "start" = "Drupal\vmware\Form\VmwareVmStartForm",
* "stop" = "Drupal\vmware\Form\VmwareVmStopForm",
* "reboot" = "Drupal\vmware\Form\VmwareVmRebootForm",
......@@ -48,6 +49,7 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface;
* "canonical" = "/clouds/vmware/{cloud_context}/vm/{vmware_vm}",
* "collection" = "/clouds/vmware/{cloud_context}/vm",
* "add-form" = "/clouds/vmware/{cloud_context}/vm/add",
* "edit-form" = "/clouds/vmware/{cloud_context}/vm/{vmware_vm}/edit",
* "start-form" = "/clouds/vmware/{cloud_context}/vm/{vmware_vm}/start",
* "stop-form" = "/clouds/vmware/{cloud_context}/vm/{vmware_vm}/stop",
* "reboot-form" = "/clouds/vmware/{cloud_context}/vm/{vmware_vm}/reboot",
......
......@@ -10,7 +10,7 @@ use Drupal\vmware\Service\VmwareService;
use Drupal\vmware\Service\VmwareServiceException;
/**
* Form controller for the Namespace entity create form.
* Form controller for the VMware VM entity create form.
*
* @ingroup vmware
*/
......
<?php
namespace Drupal\vmware\Form;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Form\FormStateInterface;
use Drupal\vmware\Service\VmwareService;
use Drupal\vmware\Service\VmwareServiceException;
/**
* Form controller for the VMware VM entity edit form.
*
* @ingroup vmware
*/
class VmwareVmEditForm extends VmwareContentForm {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $cloud_context = '') {
$form = parent::buildForm($form, $form_state);
$entity = $this->entity;
$weight = -50;
$form['vm'] = [
'#type' => 'details',
'#title' => $this->t('VM'),
'#open' => TRUE,
'#weight' => $weight++,
];
$form['vm']['name'] = [
'#type' => 'item',
'#title' => $this->getItemTitle($this->t('Name')),
'#markup' => $entity->getName(),
'#weight' => $weight++,
];
$form['vm']['power_state'] = [
'#type' => 'item',
'#title' => $this->getItemTitle($this->t('Power State')),
'#markup' => $entity->getPowerState(),
'#weight' => $weight++,
];
$form['vm']['cpu_count'] = [
'#type' => 'number',
'#title' => $this->t('CPU Count'),
'#description' => $this->t('The CPU count can be modified when virtual machine is powered off.'),
'#default_value' => $entity->getCpuCount(),
'#required' => TRUE,
'#weight' => $weight++,
'#disabled' => $entity->getPowerState() != 'POWERED_OFF',
];
$form['vm']['memory_size'] = [
'#type' => 'number',
'#title' => $this->t('Memory Size (MiB)'),
'#description' => $this->t('The memory size can be modified when virtual machine is powered off.'),
'#default_value' => $entity->getMemorySize(),
'#weight' => $weight++,
'#disabled' => $entity->getPowerState() != 'POWERED_OFF',
];
$form['vm']['guest_os'] = [
'#type' => 'item',
'#title' => $this->getItemTitle($this->t('Guest OS')),
'#markup' => $this->vmwareVmGuestOsDataProvider->getLabel($entity->getGuestOs()),
'#weight' => $weight++,
];
$form['vm']['disk_size'] = $form['disk_size'];
$form['vm']['disk_size']['#disabled'] = TRUE;
unset($form['disk_size']);
$this->addOthersFieldset($form, $weight++, $cloud_context);
$form['actions'] = $this->actions($form, $form_state, $cloud_context);
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$this->copyFormItemValues($form);
$this->trimTextfields($form, $form_state);
$entity = $this->entity;
try {
$this->vmwareService->setCloudContext($entity->getCloudContext());
$this->vmwareService->login();
if ($entity->getPowerState() == 'POWERED_OFF') {
$this->vmwareService->updateVmCpu(
$entity->getVmId(),
$entity->getCpuCount()
);
$this->vmwareService->updateVmMemory(
$entity->getVmId(),
$entity->getMemorySize()
);
}
// Save author to entity.
$entity->save();
$this->vmwareService->updateVms([
'VmId' => $entity->getVmId(),
], FALSE);
$this->processOperationStatus($entity, 'updated');
VmwareService::clearCacheValue();
$form_state->setRedirect('view.vmware_vm.list', ['cloud_context' => $entity->getCloudContext()]);
}
catch (VmwareServiceException
| EntityStorageException
| EntityMalformedException $e) {
try {
$this->processOperationErrorStatus($entity, 'updated');
}
catch (EntityMalformedException $e) {
$this->handleException($e);
}
}
}
}
......@@ -220,6 +220,38 @@ class VmwareService extends CloudServiceBase implements VmwareServiceInterface {
return $this->callApi('delete', "rest/vcenter/vm/{$params['VmId']}");
}
/**
* {@inheritdoc}
*/
public function updateVmMemory($vm_id, $memory_size) {
$body = [
'spec' => [
'size_MiB' => $memory_size,
],
];
return $this->callApi(
'patch',
"rest/vcenter/vm/$vm_id/hardware/memory",
['json' => $body]
);
}
/**
* {@inheritdoc}
*/
public function updateVmCpu($vm_id, $cpu_count) {
$body = [
'spec' => [
'count' => $cpu_count,
],
];
return $this->callApi(
'patch',
"rest/vcenter/vm/$vm_id/hardware/cpu",
['json' => $body]
);
}
/**
* {@inheritdoc}
*/
......@@ -324,6 +356,7 @@ class VmwareService extends CloudServiceBase implements VmwareServiceInterface {
}
}
catch (GuzzleException $error) {
$response = $error->getResponse();
$message = new FormattableMarkup(
'VMware API error. Error details are as follows:<pre>@response</pre>', [
'@response' => !empty($response)
......
......@@ -98,11 +98,24 @@ interface VmwareServiceInterface {
* @param bool $clear
* TRUE to clear stale entities.
*
* @return bool
* Indicates success so failure.
* @return array
* The response of API.
*/
public function updateVms(array $params = [], $clear = TRUE);
/**
* Update the VM memory.
*
* @param string $vm_id
* The VM's ID.
* @param int $memory_size
* The memory size.
*
* @return array
* The response of API.
*/
public function updateVmMemory($vm_id, $memory_size);
/**
* Describe the folders.
*
......
......@@ -30,9 +30,16 @@ entity.vmware_vm.collection:
title: 'List VMware VMs'
appears_on:
- entity.vmware_vm.add_form
- entity.vmware_vm.edit_form
- entity.vmware_vm.canonical
- entity.vmware_vm.delete_form
entity.vmware_vm.edit:
route_name: entity.vmware_vm.edit_form
title: 'Edit'
appears_on:
- entity.vmware_vm.canonical
entity.vmware_vm.delete:
route_name: entity.vmware_vm.delete_form
title: 'Delete'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment