Skip to content
Snippets Groups Projects
Commit b6c555c2 authored by baldwinlouie's avatar baldwinlouie
Browse files

git commit -m 'Issue #2991008 by baldwinlouie: Add a start/stop operation to AWS Instances'

parent 1a5e195c
No related branches found
No related tags found
No related merge requests found
......@@ -12,6 +12,8 @@ use Drupal\Core\Entity;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_libraries_info()
......@@ -172,3 +174,30 @@ function aws_cloud_cloud_config_delete(\Drupal\cloud\Entity\CloudConfig $cloud_c
$aws_ec2_service->setCloudContext($cloud_config->cloud_context());
$aws_ec2_service->clearAllEntities();
}
/**
* Implements hook_entity_operation().
*
* @params \Drupal\Core\Entity\EntityInterface $entity
*/
function aws_cloud_entity_operation(EntityInterface $entity) {
$operations = [];
if ($entity->getEntityTypeId() == 'aws_cloud_instance') {
if ($entity->instance_state() == 'running') {
$operations['stop'] = [
'title' => t('Stop'),
'url' => $entity->toUrl('stop-form'),
'weight' => 20,
];
}
else if ($entity->instance_state() == 'stopped') {
$operations['start'] = [
'title' => t('Start'),
'url' => $entity->toUrl('start-form'),
'weight' => 20,
];
}
}
return $operations;
}
\ No newline at end of file
......@@ -147,6 +147,21 @@ entity.aws_cloud_instance.delete_form:
requirements:
_permission: 'delete aws cloud instance'
entity.aws_cloud_instance.stop_form:
path: '/clouds/aws_cloud/{cloud_context}/instance/{aws_cloud_instance}/stop'
defaults:
_entity_form: 'aws_cloud_instance.stop'
_title: 'Stop Instance'
requirements:
_permission: 'edit aws cloud instance'
entity.aws_cloud_instance.start_form:
path: '/clouds/aws_cloud/{cloud_context}/instance/{aws_cloud_instance}/start'
defaults:
_entity_form: 'aws_cloud_instance.start'
_title: 'Start Instance'
requirements:
_permission: 'edit aws cloud instance'
##################
......
......@@ -41,6 +41,8 @@ use Drupal\Core\Field\BaseFieldDefinition;
* "add" = "Drupal\aws_cloud\Form\Ec2\InstanceLaunchForm",
* "edit" = "Drupal\aws_cloud\Form\Ec2\InstanceEditForm" ,
* "delete" = "Drupal\aws_cloud\Form\Ec2\InstanceDeleteForm",
* "stop" = "Drupal\aws_cloud\Form\Ec2\InstanceStopForm",
* "start" = "Drupal\aws_cloud\Form\Ec2\InstanceStartForm"
* },
* "access" = "Drupal\aws_cloud\Controller\Ec2\InstanceAccessControlHandler",
* },
......@@ -57,6 +59,8 @@ use Drupal\Core\Field\BaseFieldDefinition;
* "edit-form" = "/clouds/aws_cloud/{cloud_context}/instance/{aws_cloud_instance}/edit",
* "delete-form" = "/clouds/aws_cloud/{cloud_context}/instance/{aws_cloud_instance}/terminate",
* "collection" = "/clouds/aws_cloud/{cloud_context}/instance",
* "stop-form" = "/clouds/aws_cloud/{cloud_context}/instance/{aws_cloud_instance}/stop",
* "start-form" = "/clouds/aws_cloud/{cloud_context}/instance/{aws_cloud_instance}/start"
* },
* field_ui_base_route = "aws_cloud_instance.settings"
* )
......
<?php
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\Core\Form\FormStateInterface;
use Drupal\aws_cloud\Entity\Ec2\Instance;
/**
* Starts a stopped AWS Instance
* @package Drupal\aws_cloud\Form\Ec2
*/
class InstanceStartForm extends AwsDeleteForm {
/**
* {@inheritdoc}
*/
public function getQuestion() {
$entity = $this->entity;
return t('Are you sure you want to start instance: %name?', [
'%name' => $entity->label(),
]);
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return t('Start');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$params = [
'InstanceIds' => [
$this->entity->instance_id(),
],
];
$this->awsEc2Service->setCloudContext($this->entity->cloud_context());
$result = $this->awsEc2Service->startInstances($params);
if ($result != NULL) {
$current_state = $result['StartingInstances'][0]['CurrentState']['Name'];
$instance = Instance::load($this->entity->id());
$instance->setInstanceState($current_state);
$instance->save();
$message = $this->t('@name starting.', [
'@name' => $this->entity->label(),
]);
$this->messenger->addMessage($message);
}
$form_state->setRedirectUrl($this->getCancelUrl());
}
}
\ No newline at end of file
<?php
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\Core\Form\FormStateInterface;
use Drupal\aws_cloud\Entity\Ec2\Instance;
/**
* Stops an AWS Instance. This form confirms with the user if they really
* want to stop the instance.
* @package Drupal\aws_cloud\Form\Ec2
*/
class InstanceStopForm extends AwsDeleteForm {
/**
* {@inheritdoc}
*/
public function getQuestion() {
$entity = $this->entity;
return t('Are you sure you want to stop instance: %name?', [
'%name' => $entity->label(),
]);
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return t('Stop');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$params = [
'InstanceIds' => [
$this->entity->instance_id(),
],
];
$this->awsEc2Service->setCloudContext($this->entity->cloud_context());
$result = $this->awsEc2Service->stopInstances($params);
if ($result != NULL) {
$current_state = $result['StoppingInstances'][0]['CurrentState']['Name'];
$instance = Instance::load($this->entity->id());
$instance->setInstanceState($current_state);
$instance->save();
$message = $this->t('@name stopping.', [
'@name' => $this->entity->label(),
]);
$this->messenger->addMessage($message);
}
$form_state->setRedirectUrl($this->getCancelUrl());
}
}
\ No newline at end of file
......@@ -484,6 +484,25 @@ class AwsEc2Service implements AwsEc2ServiceInterface {
return $results;
}
/**
* {@inheritdoc}
*/
public function stopInstances($params = []) {
$params += $this->getDefaultParameters();
$results = $this->execute('StopInstances', $params);
return $results;
}
/**
* {@inheritdoc}
*/
public function startInstances($params = []) {
$params += $this->getDefaultParameters();
$results = $this->execute('StartInstances', $params);
return $results;
}
/**
* {@inheritdoc}
*/
......
......@@ -401,4 +401,20 @@ interface AwsEc2ServiceInterface {
*/
public function clearAllEntities();
/**
* Stops ec2 instance given an instance array
* @param array $params
* @return mixed
*/
public function stopInstances($params = []);
/**
* Start ec2 instance given an instance array
* @param array $params
* @return mixed
*/
public function startInstances($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