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

Issue #3068603 by Xiaohua Guan, yas, baldwinlouie: Fix the feature of "Copy ServerTemplate"

parent f8e21a34
No related branches found
No related tags found
No related merge requests found
......@@ -1140,6 +1140,18 @@ function aws_cloud_form_cloud_server_template_revision_revert_confirm_alter(&$fo
$form['#submit'] = ['aws_cloud_form_cloud_server_template_revision_revert_confirm_submit'];
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Alter form cloud_server_template_aws_cloud_copy_form.
*/
function aws_cloud_form_cloud_server_template_aws_cloud_copy_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Overwrite function ::save.
$form['actions']['submit']['#submit'][0] = 'aws_cloud_form_cloud_server_template_aws_cloud_copy_form_submit';
$form['#validate'][] = 'aws_cloud_form_cloud_server_template_aws_cloud_copy_form_validate';
}
/**
* Submit function for form cloud_server_template_aws_cloud_edit_form.
*
......@@ -1258,6 +1270,72 @@ function aws_cloud_form_cloud_server_template_aws_cloud_add_form_submit(array $f
}
}
/**
* Submit function for form cloud_server_template_aws_cloud_copy_form.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
function aws_cloud_form_cloud_server_template_aws_cloud_copy_form_submit(array $form, FormStateInterface $form_state) {
$server_template = $form_state
->getFormObject()
->getEntity()
->createDuplicate();
$server_template->setName($form_state->getValue('copy_server_template_name'));
$aws_ec2_service = \Drupal::service('aws_cloud.ec2');
$cloud_context = $server_template->getCloudContext();
$aws_ec2_service->setCloudContext($cloud_context);
$params = [];
$params['LaunchTemplateName'] = $server_template->getName();
$params['VersionDescription'] = $server_template->getRevisionLogMessage();
$params['LaunchTemplateData'] = aws_cloud_get_launch_template_data($server_template);
$result = $aws_ec2_service->createLaunchTemplate($params);
$success = isset($result['LaunchTemplate']);
if ($success) {
aws_cloud_server_template_update_field_tags($server_template, $params['LaunchTemplateData']);
$server_template->set('field_version', $result['LaunchTemplate']['LatestVersionNumber']);
}
if ($success && $server_template->save()
) {
\Drupal::messenger()->addMessage(
t('Server template copied.')
);
$form_state->setRedirectUrl($server_template->toUrl('canonical'));
}
else {
\Drupal::messenger()->addError(
t("The Cloud Server Template @name couldn't create.", [
'@name' => $server_template->getName(),
])
);
}
}
/**
* Validate function for form cloud_server_template_aws_cloud_copy_form.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
function aws_cloud_form_cloud_server_template_aws_cloud_copy_form_validate(array &$form, FormStateInterface $form_state) {
$form_object = $form_state->getFormObject();
$server_template = $form_object->getEntity();
$server_template->setName($form_state->getValue('copy_server_template_name'));
$violations = $server_template->validate();
foreach ($violations->getByField('name') as $violation) {
$form_state->setErrorByName('copy_server_template_name', $violation->getMessage());
}
}
/**
* Submit function for form cloud_server_template_revision_delete_confirm.
*
......
......@@ -318,7 +318,7 @@ class CloudServerTemplateTest extends AwsCloudTestCase {
$copy_url = $this->getUrl();
$this->drupalPostForm($copy_url, [], 'Copy');
$this->assertText('Server template copied.');
$this->assertText('Copy of ' . $add[0]['name[0][value]']);
$this->assertText('copy_of_' . $add[0]['name[0][value]']);
}
/**
......
......@@ -72,7 +72,7 @@ class CloudServerTemplateCopyConfirm extends ContentEntityConfirmFormBase {
'#title' => $this->t('New Server template name'),
'#type' => 'textfield',
'#description' => $this->t('The new server template name to use.'),
'#default_value' => $this->t('Copy of @name',
'#default_value' => $this->t('copy_of_@name',
[
'@name' => $this->entity->getName(),
]),
......
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