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
1 merge request!1781Issue #3359518: Add "approve" feature to OpenStack quota (Implementation) (6) (Add original value to form)
......@@ -24,4 +24,4 @@ services:
openstack.operations:
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
$definition->setName("{$field_name}_new");
// 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;
// Restore the name of definition.
......
......@@ -204,11 +204,27 @@ class OpenStackQuotaEditForm extends AwsCloudContentForm {
];
foreach ($field_set['fields'] as $field) {
$form[$field_set['name']][$field] = $form[$field];
unset($form[$field]);
$new_field = "{$field}_new";
$form[$field_set['name']][$new_field] = $form[$new_field];
// Hide *_new fields.
unset($form["{$field}_new"]);
$new_field_control =& $form[$field_set['name']][$new_field]['widget'][0]['value'];
$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;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Render\Markup;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\openstack\Entity\OpenStackFloatingIpInterface;
......@@ -152,6 +153,13 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
*/
protected $queueFactory;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* OpenStackOperationsService constructor.
*
......@@ -181,6 +189,8 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
* The module handler.
* @param \Drupal\Core\Queue\QueueFactory $queue_factory
* The queue factory.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/
public function __construct(
AwsCloudOperationsServiceInterface $aws_cloud_operations_service,
......@@ -195,7 +205,8 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
EntityLinkRendererInterface $entity_link_renderer,
TimeInterface $time,
ModuleHandlerInterface $module_handler,
QueueFactory $queue_factory
QueueFactory $queue_factory,
AccountInterface $current_user
) {
$this->awsCloudOperationsService = $aws_cloud_operations_service;
$this->cloudConfigPluginManager = $cloud_config_plugin_manager;
......@@ -210,6 +221,7 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
$this->time = $time;
$this->moduleHandler = $module_handler;
$this->queueFactory = $queue_factory;
$this->currentUser = $current_user;
}
/**
......@@ -517,7 +529,7 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
*
* @param string $image_api_endpoint
* The image api endpoint.
* @param array $cloud_context
* @param string $cloud_context
* The cloud context.
*
* @return bool
......@@ -2334,6 +2346,11 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
$this->awsCloudOperationsService->copyFormItemValues($entity, $form);
$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()]);
if ($entity->getStatus() !== OpenStackQuotaInterface::APPROVED) {
$entity->save();
......@@ -2346,26 +2363,26 @@ class OpenStackOperationsService implements OpenStackOperationsServiceInterface
$params = [
'Name' => $entity->getName(),
'ProjectId' => $entity->getProjectId(),
'Instances' => $entity->getInstances(),
'Cores' => $entity->getCores(),
'Ram' => $entity->getRam(),
'MetadataItems' => $entity->getMetadataItems(),
'KeyPairs' => $entity->getKeyPairs(),
'ServerGroups' => $entity->getServerGroups(),
'ServerGroupMembers' => $entity->getServerGroupMembers(),
'InjectedFiles' => $entity->getInjectedFiles(),
'InjectedFileContentBytes' => $entity->getInjectedFileContentBytes(),
'InjectedFilePathBytes' => $entity->getInjectedFilePathBytes(),
'Volumes' => $entity->getVolumes(),
'Snapshots' => $entity->getSnapshots(),
'GigaBytes' => $entity->getGigaBytes(),
'Network' => $entity->getNetwork(),
'Subnet' => $entity->getSubnet(),
'Port' => $entity->getPort(),
'Router' => $entity->getRouter(),
'FloatingIp' => $entity->getFloatingIp(),
'SecurityGroup' => $entity->getSecurityGroup(),
'SecurityGroupRule' => $entity->getSecurityGroupRule(),
'Instances' => (int) $entity->get('instances_new')->value,
'Cores' => (int) $entity->get('cores_new')->value,
'Ram' => (int) $entity->get('ram_new')->value,
'MetadataItems' => (int) $entity->get('metadata_items_new')->value,
'KeyPairs' => (int) $entity->get('key_pairs_new')->value,
'ServerGroups' => (int) $entity->get('server_groups_new')->value,
'ServerGroupMembers' => (int) $entity->get('server_group_members_new')->value,
'InjectedFiles' => (int) $entity->get('injected_files_new')->value,
'InjectedFileContentBytes' => (int) $entity->get('injected_file_content_bytes_new')->value,
'InjectedFilePathBytes' => (int) $entity->get('injected_file_path_bytes_new')->value,
'Volumes' => (int) $entity->get('volumes_new')->value,
'Snapshots' => (int) $entity->get('snapshots_new')->value,
'GigaBytes' => (int) $entity->get('gigabytes_new')->value,
'Network' => (int) $entity->get('network_new')->value,
'Subnet' => (int) $entity->get('subnet_new')->value,
'Port' => (int) $entity->get('port_new')->value,
'Router' => (int) $entity->get('router_new')->value,
'FloatingIp' => (int) $entity->get('floatingip_new')->value,
'SecurityGroup' => (int) $entity->get('security_group_new')->value,
'SecurityGroupRule' => (int) $entity->get('security_group_rule_new')->value,
];
$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