Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/146 option to remove edge dictionaries and acl containers #157

Merged
merged 7 commits into from
Mar 15, 2018
108 changes: 108 additions & 0 deletions Controller/Adminhtml/FastlyCdn/Edge/Acl/Delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Edge\Acl;

use Fastly\Cdn\Model\Api;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Controller\Result\JsonFactory;
use Fastly\Cdn\Model\Config;
use Fastly\Cdn\Helper\Vcl;
use Magento\Framework\Exception\LocalizedException;

class Delete extends Action
{
/**
* @var Http
*/
private $request;

/**
* @var JsonFactory
*/
private $resultJson;

/**
* @var Config
*/
private $config;

/**
* @var Api
*/
private $api;

/**
* @var Vcl
*/
private $vcl;

/**
* ForceTls constructor.
*
* @param Context $context
* @param Http $request
* @param JsonFactory $resultJsonFactory
* @param Config $config
* @param Api $api
* @param Vcl $vcl
*/
public function __construct(
Context $context,
Http $request,
JsonFactory $resultJsonFactory,
Config $config,
Api $api,
Vcl $vcl
) {
$this->request = $request;
$this->resultJson = $resultJsonFactory;
$this->config = $config;
$this->api = $api;
$this->vcl = $vcl;

parent::__construct($context);
}

public function execute()
{
$result = $this->resultJson->create();

try {
$activeVersion = $this->getRequest()->getParam('active_version');
$activateVcl = $this->getRequest()->getParam('activate_flag');
$acl = $this->getRequest()->getParam('acl');
$service = $this->api->checkServiceDetails();
$this->vcl->checkCurrentVersionActive($service->versions, $activeVersion);
$currActiveVersion = $this->vcl->getCurrentVersion($service->versions);
$clone = $this->api->cloneVersion($currActiveVersion);
$used = '';

$this->api->deleteAcl($clone->number, $acl);
$validation = $this->api->containerValidateServiceVersion($clone->number);
if ($validation->status == 'error') {
$used = $acl;
}
if ($used != '') {
throw new LocalizedException(__(
'Failed to validate service, the container "' . $used . '" may be in use. '
));
}

if ($activateVcl === 'true') {
$this->api->activateVersion($clone->number);
}

return $result->setData([
'status' => true,
'active_version' => $clone->number
]);
} catch (\Exception $e) {
return $result->setData([
'status' => false,
'msg' => $e->getMessage()
]);
}
}
}
108 changes: 108 additions & 0 deletions Controller/Adminhtml/FastlyCdn/Edge/Dictionary/Delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Edge\Dictionary;

use Fastly\Cdn\Model\Api;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Controller\Result\JsonFactory;
use Fastly\Cdn\Model\Config;
use Fastly\Cdn\Helper\Vcl;
use Magento\Framework\Exception\LocalizedException;

class Delete extends Action
{
/**
* @var Http
*/
private $request;

/**
* @var JsonFactory
*/
private $resultJson;

/**
* @var Config
*/
private $config;

/**
* @var Api
*/
private $api;

/**
* @var Vcl
*/
private $vcl;

/**
* ForceTls constructor.
*
* @param Context $context
* @param Http $request
* @param JsonFactory $resultJsonFactory
* @param Config $config
* @param Api $api
* @param Vcl $vcl
*/
public function __construct(
Context $context,
Http $request,
JsonFactory $resultJsonFactory,
Config $config,
Api $api,
Vcl $vcl
) {
$this->request = $request;
$this->resultJson = $resultJsonFactory;
$this->config = $config;
$this->api = $api;
$this->vcl = $vcl;

parent::__construct($context);
}

public function execute()
{
$result = $this->resultJson->create();

try {
$activeVersion = $this->getRequest()->getParam('active_version');
$activateVcl = $this->getRequest()->getParam('activate_flag');
$dictionary = $this->getRequest()->getParam('dictionary');
$service = $this->api->checkServiceDetails();
$this->vcl->checkCurrentVersionActive($service->versions, $activeVersion);
$currActiveVersion = $this->vcl->getCurrentVersion($service->versions);
$clone = $this->api->cloneVersion($currActiveVersion);
$used = '';

$this->api->deleteDictionary($clone->number, $dictionary);
$validation = $this->api->containerValidateServiceVersion($clone->number);
if ($validation->status == 'error') {
$used = $dictionary;
}
if ($used != '') {
throw new LocalizedException(__(
'Failed to validate service, the container "' . $used . '" may be in use. '
));
}

if ($activateVcl === 'true') {
$this->api->activateVersion($clone->number);
}

return $result->setData([
'status' => true,
'active_version' => $clone->number
]);
} catch (\Exception $e) {
return $result->setData([
'status' => false,
'msg' => $e->getMessage()
]);
}
}
}
27 changes: 27 additions & 0 deletions Model/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,18 @@ public function validateServiceVersion($version)
}
}

/**
* @param $version
* @return bool|mixed
*/
public function containerValidateServiceVersion($version)
{
$url = $this->_getApiServiceUri() . 'version/' .$version. '/validate';
$result = $this->_fetch($url, 'GET');

return $result;
}

/**
* Activate the current version.
*
Expand Down Expand Up @@ -925,6 +937,21 @@ public function getAcls($version)
return $result;
}

/**
* Delete named acl for a particular service and version.
*
* @param $version
* @param $name
* @return bool|mixed
*/
public function deleteAcl($version, $name)
{
$url = $this->_getApiServiceUri(). 'version/'. $version . '/acl/' . $name;
$result = $this->_fetch($url, \Zend_Http_Client::DELETE);

return $result;
}

/**
* Fetch ACL entry list for particular ACL
*
Expand Down
80 changes: 80 additions & 0 deletions view/adminhtml/templates/system/config/dialogs.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,82 @@
</form>
</script>

<script type="text/x-magento-template" id="fastly-delete-dictionary-container-template">
<div class="messages">
<div class="message message-warning fastly-message-warning" style="display: none">
<?php /* @noEscape */ echo __("You are about to clone your active version #x.")?>
<?php /* @noEscape */ echo __("We'll make the changes to version #y.")?>
</div>
<div class="message message-error fastly-message-error" style="display: none">
</div>
<div class="message message-warning" id="fastly-container-warning" style="display: none">
</div>
</div>
<form action="<?php /* @noEscape */ echo $block->getUrl('adminhtml/fastlyCdn_Vcl/upload'); ?>"
method="POST" id="fastly-delete-dictionary-form"
class="form-inline"
enctype="multipart/form-data">

<fieldset class="admin__fieldset form-list question">
<div class="admin__field field maintenance-checkbox-container">
<label for="fastly_activate_vcl" class="admin__field-label">
<span><?php /* @noEscape */ echo __('Activate after the change')?></span>
</label>
<div class="admin__field-control">
<div class="admin__field-option">
<input class="admin__control-checkbox"
type="checkbox"
name="fastly_activate_vcl"
id="fastly_activate_vcl"
checked/>
<label class="admin__field-label" for="fastly_activate_tls"></label>
</div>
</div>
</div>
<div id="delete-dictionary-container">
</div>
</fieldset>
</form>
</script>

<script type="text/x-magento-template" id="fastly-delete-acl-container-template">
<div class="messages">
<div class="message message-warning fastly-message-warning" style="display: none">
<?php /* @noEscape */ echo __("You are about to clone your active version #x.")?>
<?php /* @noEscape */ echo __("We'll make the changes to version #y.")?>
</div>
<div class="message message-error fastly-message-error" style="display: none">
</div>
<div class="message message-warning" id="fastly-container-warning" style="display: none">
</div>
</div>
<form action="<?php /* @noEscape */ echo $block->getUrl('adminhtml/fastlyCdn_Vcl/upload'); ?>"
method="POST" id="fastly-delete-acl-form"
class="form-inline"
enctype="multipart/form-data">

<fieldset class="admin__fieldset form-list question">
<div class="admin__field field maintenance-checkbox-container">
<label for="fastly_activate_vcl" class="admin__field-label">
<span><?php /* @noEscape */ echo __('Activate after the change')?></span>
</label>
<div class="admin__field-control">
<div class="admin__field-option">
<input class="admin__control-checkbox"
type="checkbox"
name="fastly_activate_vcl"
id="fastly_activate_vcl"
checked/>
<label class="admin__field-label" for="fastly_activate_tls"></label>
</div>
</div>
</div>
<div id="delete-acl-container">
</div>
</fieldset>
</form>
</script>

<script type="text/x-magento-template" id="fastly-acl-items-template">
<div class="messages">
<div class="message message-error fastly-message-error" style="display: none; margin-bottom: 15px;"></div>
Expand Down Expand Up @@ -726,11 +802,13 @@ $isAlreadyConfiguredUrl = $block->getUrl('adminhtml/fastlyCdn_Vcl/isAlreadyConfi
$savePageErrorHtml = $block->getUrl('adminhtml/fastlyCdn_Vcl/SaveErrorPageHtml');
$getErrorPageRespObjUrl = $block->getUrl('adminhtml/fastlyCdn_Vcl/GetErrorPageRespObj');
$createDictionary = $block->getUrl('adminhtml/fastlyCdn_Edge_Dictionary/Create');
$deleteDictionary = $block->getUrl('adminhtml/fastlyCdn_Edge_Dictionary/Delete');
$listDictionaries = $block->getUrl('adminhtml/fastlyCdn_Edge_Dictionary/ListAll');
$dictionaryItems = $block->getUrl('adminhtml/fastlyCdn_Edge_Dictionary_Item/ListAll');
$createDictionaryItem = $block->getUrl('adminhtml/fastlyCdn_Edge_Dictionary_Item/Create');
$deleteDictionaryItem = $block->getUrl('adminhtml/fastlyCdn_Edge_Dictionary_Item/Delete');
$createAcl = $block->getUrl('adminhtml/fastlyCdn_Edge_Acl/Create');
$deleteAcl = $block->getUrl('adminhtml/fastlyCdn_Edge_Acl/Delete');
$listAcls = $block->getUrl('adminhtml/fastlyCdn_Edge_Acl/ListAll');
$aclItems = $block->getUrl('adminhtml/fastlyCdn_Edge_Acl_Item/ListAll');
$createAclItem = $block->getUrl('adminhtml/fastlyCdn_Edge_Acl_Item/Create');
Expand Down Expand Up @@ -770,11 +848,13 @@ $isFastlyEnabled = $block->getConfig()->isFastlyEnabled();
"saveErrorPageHtmlUrl": "<?php echo $block->escapeJsQuote($savePageErrorHtml);?>",
"getErrorPageRespObj": "<?php echo $block->escapeJsQuote($getErrorPageRespObjUrl);?>",
"createDictionary": "<?php echo $block->escapeJsQuote($createDictionary);?>",
"deleteDictionary": "<?php echo $block->escapeJsQuote($deleteDictionary);?>",
"getDictionaries": "<?php echo $block->escapeJsQuote($listDictionaries);?>",
"getDictionaryItems": "<?php echo $block->escapeJsQuote($dictionaryItems);?>",
"createDictionaryItem": "<?php echo $block->escapeJsQuote($createDictionaryItem);?>",
"deleteDictionaryItem": "<?php echo $block->escapeJsQuote($deleteDictionaryItem);?>",
"createAcl": "<?php echo $block->escapeJsQuote($createAcl);?>",
"deleteAcl": "<?php echo $block->escapeJsQuote($deleteAcl);?>",
"getAcls": "<?php echo $block->escapeJsQuote($listAcls);?>",
"getAclItems": "<?php echo $block->escapeJsQuote($aclItems);?>",
"createAclItem": "<?php echo $block->escapeJsQuote($createAclItem);?>",
Expand Down
Loading