Skip to content
Snippets Groups Projects

Issue #3227078: Add a REST API to stop AWS Cloud instance

3 files
+ 85
0
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -743,4 +743,66 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
], 500);
}
/**
* {@inheritdoc}
*/
public function stopInstance(string $cloud_context, string $aws_cloud_instance): JsonResponse {
$entity = $this->entityTypeManager
->getStorage('aws_cloud_instance')
->load($aws_cloud_instance);
try {
if (!$this->updateEntitySubmitForm(new FormState(), $entity)) {
throw new \Exception();
}
}
catch (\Exception $e) {
$this->handleException($e);
return new JsonResponse([
'result' => 'NG',
'reason' => 'The instance has already been deleted.',
], 404);
}
$params = [
'InstanceIds' => [
$entity->getInstanceId(),
],
];
$result = $this->ec2Service->stopInstances($params);
if (!empty($result)) {
try {
$current_state = $result['StoppingInstances'][0]['CurrentState']['Name'];
$instance = $this->entityTypeManager
->getStorage($entity->getEntityTypeId())
->load($entity->id());
$instance->setInstanceState($current_state);
$instance->save();
$this->processOperationStatus($instance, 'stopped');
$this->clearCacheValues($entity->getCacheTags());
$this->dispatchSubmitEvent($entity);
return new JsonResponse(['result' => 'OK']);
}
catch (\Exception $e) {
$this->handleException($e);
return new JsonResponse([
'result' => 'NG',
'reason' => 'Internal Server Error',
], 500);
}
}
$singularLabel = $entity->getEntityType()->getSingularLabel();
$name = $entity->getName();
return new JsonResponse([
'result' => 'NG',
'reason' => 'The ' . $singularLabel . ' ' . $name . ' can not stop.',
], 500);
}
}
Loading