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

Issue #3359518 by Xiaohua Guan, yas: Add "approve" feature to OpenStack quota...

Issue #3359518 by Xiaohua Guan, yas: Add "approve" feature to OpenStack quota (6) (Add original value to form)
parent 5528c42e
No related branches found
No related tags found
No related merge requests found
...@@ -24,4 +24,4 @@ services: ...@@ -24,4 +24,4 @@ services:
openstack.operations: openstack.operations:
class: Drupal\openstack\Service\OpenStackOperationsService class: Drupal\openstack\Service\OpenStackOperationsService
arguments: ['@aws_cloud.openstack.operations', '@plugin.manager.cloud_config_plugin', '@cloud', '@openstack.ec2', '@entity_type.manager', '@messenger', '@openstack.factory', '@request_stack', '@current_route_match', '@entity.link_renderer', '@datetime.time', '@module_handler', '@queue'] arguments: ['@aws_cloud.openstack.operations', '@plugin.manager.cloud_config_plugin', '@cloud', '@openstack.ec2', '@entity_type.manager', '@messenger', '@openstack.factory', '@request_stack', '@current_route_match', '@entity.link_renderer', '@datetime.time', '@module_handler', '@queue', '@current_user']
...@@ -1353,7 +1353,11 @@ class OpenStackQuota extends CloudContentEntityBase implements OpenStackQuotaInt ...@@ -1353,7 +1353,11 @@ class OpenStackQuota extends CloudContentEntityBase implements OpenStackQuotaInt
$definition->setName("{$field_name}_new"); $definition->setName("{$field_name}_new");
// Create a new field. // Create a new field.
$new_definition = BaseFieldDefinition::createFromFieldStorageDefinition($definition); $new_definition = BaseFieldDefinition::createFromFieldStorageDefinition($definition)
->setDisplayOptions('form', [
'type' => 'number',
'weight' => -5,
]);
$fields["{$field_name}_new"] = $new_definition; $fields["{$field_name}_new"] = $new_definition;
// Restore the name of definition. // Restore the name of definition.
......
...@@ -204,11 +204,27 @@ class OpenStackQuotaEditForm extends AwsCloudContentForm { ...@@ -204,11 +204,27 @@ class OpenStackQuotaEditForm extends AwsCloudContentForm {
]; ];
foreach ($field_set['fields'] as $field) { foreach ($field_set['fields'] as $field) {
$form[$field_set['name']][$field] = $form[$field]; $new_field = "{$field}_new";
unset($form[$field]); $form[$field_set['name']][$new_field] = $form[$new_field];
// Hide *_new fields. $new_field_control =& $form[$field_set['name']][$new_field]['widget'][0]['value'];
unset($form["{$field}_new"]); $old_description = $new_field_control['#description'];
$new_field_control['#description'] =
$this->t(
'Original value: %value. %description',
[
'%value' => $entity->get($field)->value,
'%description' => $old_description,
]
);
if ($entity->get($field)->value !== $entity->get($new_field)->value) {
$new_field_control['#attributes']['class'][] = 'border-danger';
$new_field_control['#attributes']['class'][] = 'border-4';
}
unset($form[$new_field]);
// Hide original fields.
unset($form["{$field}"]);
} }
} }
......
...@@ -28,6 +28,7 @@ use Drupal\Core\Messenger\Messenger; ...@@ -28,6 +28,7 @@ use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Queue\QueueFactory; use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Render\Markup; use Drupal\Core\Render\Markup;
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\openstack\Entity\OpenStackFloatingIpInterface; use Drupal\openstack\Entity\OpenStackFloatingIpInterface;
...@@ -152,6 +153,13 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface ...@@ -152,6 +153,13 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
*/ */
protected $queueFactory; protected $queueFactory;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/** /**
* OpenStackOperationsService constructor. * OpenStackOperationsService constructor.
* *
...@@ -181,6 +189,8 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface ...@@ -181,6 +189,8 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
* The module handler. * The module handler.
* @param \Drupal\Core\Queue\QueueFactory $queue_factory * @param \Drupal\Core\Queue\QueueFactory $queue_factory
* The queue factory. * The queue factory.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/ */
public function __construct( public function __construct(
AwsCloudOperationsServiceInterface $aws_cloud_operations_service, AwsCloudOperationsServiceInterface $aws_cloud_operations_service,
...@@ -195,7 +205,8 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface ...@@ -195,7 +205,8 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
EntityLinkRendererInterface $entity_link_renderer, EntityLinkRendererInterface $entity_link_renderer,
TimeInterface $time, TimeInterface $time,
ModuleHandlerInterface $module_handler, ModuleHandlerInterface $module_handler,
QueueFactory $queue_factory QueueFactory $queue_factory,
AccountInterface $current_user
) { ) {
$this->awsCloudOperationsService = $aws_cloud_operations_service; $this->awsCloudOperationsService = $aws_cloud_operations_service;
$this->cloudConfigPluginManager = $cloud_config_plugin_manager; $this->cloudConfigPluginManager = $cloud_config_plugin_manager;
...@@ -210,6 +221,7 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface ...@@ -210,6 +221,7 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
$this->time = $time; $this->time = $time;
$this->moduleHandler = $module_handler; $this->moduleHandler = $module_handler;
$this->queueFactory = $queue_factory; $this->queueFactory = $queue_factory;
$this->currentUser = $current_user;
} }
/** /**
...@@ -517,7 +529,7 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface ...@@ -517,7 +529,7 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
* *
* @param string $image_api_endpoint * @param string $image_api_endpoint
* The image api endpoint. * The image api endpoint.
* @param array $cloud_context * @param string $cloud_context
* The cloud context. * The cloud context.
* *
* @return bool * @return bool
...@@ -2334,6 +2346,11 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface ...@@ -2334,6 +2346,11 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
$this->awsCloudOperationsService->copyFormItemValues($entity, $form); $this->awsCloudOperationsService->copyFormItemValues($entity, $form);
$this->awsCloudOperationsService->trimTextfields($entity, $form, $form_state); $this->awsCloudOperationsService->trimTextfields($entity, $form, $form_state);
if ($entity->getStatus() === OpenStackQuotaInterface::APPROVED
&& !$this->currentUser->hasPermission(OpenStackQuotaInterface::PERMISSION_TO_APPROVE)) {
$entity->setStatus(OpenStackQuotaInterface::DRAFT);
}
$form_state->setRedirect("view.{$entity->getEntityTypeId()}.list", ['cloud_context' => $entity->getCloudContext()]); $form_state->setRedirect("view.{$entity->getEntityTypeId()}.list", ['cloud_context' => $entity->getCloudContext()]);
if ($entity->getStatus() !== OpenStackQuotaInterface::APPROVED) { if ($entity->getStatus() !== OpenStackQuotaInterface::APPROVED) {
$entity->save(); $entity->save();
...@@ -2346,26 +2363,26 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface ...@@ -2346,26 +2363,26 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
$params = [ $params = [
'Name' => $entity->getName(), 'Name' => $entity->getName(),
'ProjectId' => $entity->getProjectId(), 'ProjectId' => $entity->getProjectId(),
'Instances' => $entity->getInstances(), 'Instances' => (int) $entity->get('instances_new')->value,
'Cores' => $entity->getCores(), 'Cores' => (int) $entity->get('cores_new')->value,
'Ram' => $entity->getRam(), 'Ram' => (int) $entity->get('ram_new')->value,
'MetadataItems' => $entity->getMetadataItems(), 'MetadataItems' => (int) $entity->get('metadata_items_new')->value,
'KeyPairs' => $entity->getKeyPairs(), 'KeyPairs' => (int) $entity->get('key_pairs_new')->value,
'ServerGroups' => $entity->getServerGroups(), 'ServerGroups' => (int) $entity->get('server_groups_new')->value,
'ServerGroupMembers' => $entity->getServerGroupMembers(), 'ServerGroupMembers' => (int) $entity->get('server_group_members_new')->value,
'InjectedFiles' => $entity->getInjectedFiles(), 'InjectedFiles' => (int) $entity->get('injected_files_new')->value,
'InjectedFileContentBytes' => $entity->getInjectedFileContentBytes(), 'InjectedFileContentBytes' => (int) $entity->get('injected_file_content_bytes_new')->value,
'InjectedFilePathBytes' => $entity->getInjectedFilePathBytes(), 'InjectedFilePathBytes' => (int) $entity->get('injected_file_path_bytes_new')->value,
'Volumes' => $entity->getVolumes(), 'Volumes' => (int) $entity->get('volumes_new')->value,
'Snapshots' => $entity->getSnapshots(), 'Snapshots' => (int) $entity->get('snapshots_new')->value,
'GigaBytes' => $entity->getGigaBytes(), 'GigaBytes' => (int) $entity->get('gigabytes_new')->value,
'Network' => $entity->getNetwork(), 'Network' => (int) $entity->get('network_new')->value,
'Subnet' => $entity->getSubnet(), 'Subnet' => (int) $entity->get('subnet_new')->value,
'Port' => $entity->getPort(), 'Port' => (int) $entity->get('port_new')->value,
'Router' => $entity->getRouter(), 'Router' => (int) $entity->get('router_new')->value,
'FloatingIp' => $entity->getFloatingIp(), 'FloatingIp' => (int) $entity->get('floatingip_new')->value,
'SecurityGroup' => $entity->getSecurityGroup(), 'SecurityGroup' => (int) $entity->get('security_group_new')->value,
'SecurityGroupRule' => $entity->getSecurityGroupRule(), 'SecurityGroupRule' => (int) $entity->get('security_group_rule_new')->value,
]; ];
$result = $this->ec2Service->updateQuota($params); $result = $this->ec2Service->updateQuota($params);
......
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