Skip to content

Task #247: #247: [MAGENTO 2] Backend modify #308

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

Merged
merged 7 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 88 additions & 16 deletions Controller/Adminhtml/FastlyCdn/Backend/ConfigureBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
*/
namespace Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Backend;

use Fastly\Cdn\Helper\Vcl;
use Fastly\Cdn\Model\Api;
use Fastly\Cdn\Model\Config;
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\Api;
use Fastly\Cdn\Helper\Vcl;
use Fastly\Cdn\Model\Config;
use Magento\Framework\Exception\LocalizedException;

/**
* Class ConfigureBackend
Expand All @@ -35,6 +36,8 @@
*/
class ConfigureBackend extends Action
{
use ValidationTrait;

/**
* @var Http
*/
Expand Down Expand Up @@ -91,45 +94,114 @@ public function execute()
{
$result = $this->resultJson->create();
try {
$activate_flag = $this->getRequest()->getParam('activate_flag');
$oldName = $this->getRequest()->getParam('old_name');
$address = $this->getRequest()->getParam('backend_address');
$this->validateAddress($address);

$override = $this->validateOverride($this->getRequest()->getParam('override_host'));

$name = $this->getRequest()->getParam('name');
$this->validateName($name);

$maxTls = $this->processRequest('max_tls_version');
$minTls = $this->processRequest('min_tls_version');
$this->validateVersion((float)$maxTls, (float)$minTls);

$activate_flag = $this->getRequest()->getParam('activate_flag') === 'true' ? true : false;
$activeVersion = $this->getRequest()->getParam('active_version');
$oldName = $this->getRequest()->getParam('name');
$params = [
'name' => $this->getRequest()->getParam('name'),
'shield' => $this->getRequest()->getParam('shield'),
'connect_timeout' => $this->getRequest()->getParam('connect_timeout'),
'between_bytes_timeout' => $this->getRequest()->getParam('between_bytes_timeout'),
'first_byte_timeout' => $this->getRequest()->getParam('first_byte_timeout'),
];

$service = $this->api->checkServiceDetails();
$this->vcl->checkCurrentVersionActive($service->versions, $activeVersion);
$currActiveVersion = $this->vcl->getCurrentVersion($service->versions);
$clone = $this->api->cloneVersion($currActiveVersion);

$tlsYesPort = $this->getRequest()->getParam('tls_yes_port');
$tlsNoPort = $this->getRequest()->getParam('tls_no_port');

$port = $tlsYesPort;
$useSsl = $this->getRequest()->getParam('use_ssl') === '1' ? true : false;

$autoLoadBalance = $this->getRequest()->getParam('auto_loadbalance') === '1' ? true : false;

if (!$useSsl) {
$port = $tlsNoPort;
}

$sslCertHostname = $this->processRequest('ssl_cert_hostname');
$sslSniHostname = $this->processRequest('ssl_sni_hostname');
$sslCaCert = $this->processRequest('ssl_ca_cert');

$conditionName = $this->getRequest()->getParam('condition_name');
$applyIf = $this->getRequest()->getParam('apply_if');
$conditionPriority = $this->getRequest()->getParam('condition_priority');
$selCondition = $this->getRequest()->getParam('request_condition');

$condition = $this->createCondition($clone, $conditionName, $applyIf, $conditionPriority, $selCondition);

$params = [
'address' => $address,
'auto_loadbalance' => $autoLoadBalance,
'between_bytes_timeout' => $this->getRequest()->getParam('between_bytes_timeout'),
'connect_timeout' => $this->getRequest()->getParam('connect_timeout'),
'first_byte_timeout' => $this->getRequest()->getParam('first_byte_timeout'),
'max_conn' => $this->getRequest()->getParam('max_conn'),
'name' => $name,
'port' => $port,
'request_condition' => $condition,
'service_id' => $service->id,
'shield' => $this->getRequest()->getParam('shield'),
'use_ssl' => $useSsl,
'version' => $clone->number,
'override_host' => $override
];

if (!$useSsl) {
$params += [
'ssl_ca_cert' => $sslCaCert,
'ssl_cert_hostname' => $sslCertHostname,
'ssl_ciphers' => $this->processRequest('ssl_ciphers'),
'ssl_client_cert' => $this->processRequest('ssl_client_cert'),
'ssl_client_key' => $this->processRequest('ssl_client_key'),
'ssl_sni_hostname' => $sslSniHostname,
'max_tls_version' => $maxTls,
'min_tls_version' => $minTls,
];
}

if ($autoLoadBalance !== false) {
$params += [
'weight' => $this->getRequest()->getParam('weight')
];
}

$configureBackend = $this->api->configureBackend($params, $clone->number, $oldName);

if (!$configureBackend) {
return $result->setData([
'status' => false,
'msg' => 'Failed to update Backend configuration.'
'msg' => 'Failed to update Backend.'
]);
}

$this->api->validateServiceVersion($clone->number);

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

if ($this->config->areWebHooksEnabled() && $this->config->canPublishConfigChanges()) {
$this->api->sendWebHook(
'*Backend '
. $this->getRequest()->getParam('name')
. ' has been changed in Fastly version '
. ' has been updated at Fastly version '
. $clone->number
. '*'
);
}

$comment = ['comment' => 'Magento Module updated the "'.$oldName.'" Backend Configuration'];
$comment = [
'comment' => 'Magento Module configured the "' . $this->getRequest()->getParam('name') . '" Backend '
];
$this->api->addComment($clone->number, $comment);

return $result->setData([
Expand Down
104 changes: 6 additions & 98 deletions Controller/Adminhtml/FastlyCdn/Backend/CreateBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@
*/
namespace Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Backend;

use Fastly\Cdn\Helper\Vcl;
use Fastly\Cdn\Model\Api;
use Fastly\Cdn\Model\Config;
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\Api;
use Fastly\Cdn\Helper\Vcl;
use Fastly\Cdn\Model\Config;
use Magento\Framework\Exception\LocalizedException;

/**
* Class CreateBackend
* @package Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Backend
*/
class CreateBackend extends Action
{
use ValidationTrait;

/**
* @var Http
*/
Expand Down Expand Up @@ -198,7 +199,7 @@ public function execute()
}

$comment = [
'comment' => 'Magento Module created the "'.$this->getRequest()->getParam('name').'" Backend '
'comment' => 'Magento Module created the "' . $this->getRequest()->getParam('name') . '" Backend '
];
$this->api->addComment($clone->number, $comment);

Expand All @@ -213,97 +214,4 @@ public function execute()
]);
}
}

/**
* @param $address
* @throws LocalizedException
*/
private function validateAddress($address)
{
if (!filter_var($address, FILTER_VALIDATE_IP) &&
!filter_var($address, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) {
throw new LocalizedException(__('Address '.$address.' is not a valid IPv4, IPv6 or hostname.'));
}
}

/**
* @param $override
* @return string|null
* @throws LocalizedException
*/
private function validateOverride($override)
{
if ($override === '') {
return null;
}

if (!filter_var($override, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) {
throw new LocalizedException(__('Override host ' . $override . ' is not a valid hostname.'));
}

return $override;
}

/**
* @param $name
* @throws LocalizedException
*/
private function validateName($name)
{
if (trim($name) == "") {
throw new LocalizedException(__("Name can't be blank"));
}
}

/**
* @param $maxTls
* @param $minTls
* @throws LocalizedException
*/
private function validateVersion($maxTls, $minTls)
{
if ($maxTls == 0) {
return;
} elseif ($maxTls < $minTls) {
throw new LocalizedException(__("Maximum TLS version must be higher than the minimum TLS version."));
}
}

/**
* @param $param
* @return mixed|null
*/
private function processRequest($param)
{
$request = $this->getRequest()->getParam($param);
if ($request == '') {
return null;
}
return $request;
}

/**
* @param $clone
* @param $conditionName
* @param $applyIf
* @param $conditionPriority
* @param $selCondition
* @return mixed
* @throws LocalizedException
*/
private function createCondition($clone, $conditionName, $applyIf, $conditionPriority, $selCondition)
{
if ($conditionName == $selCondition && !empty($selCondition) &&
!$this->api->getCondition($clone->number, $conditionName)) {
$condition = [
'name' => $conditionName,
'statement' => $applyIf,
'type' => 'REQUEST',
'priority' => $conditionPriority
];
$createCondition = $this->api->createCondition($clone->number, $condition);
return $createCondition->name;
}
return $selCondition;
}
}
101 changes: 101 additions & 0 deletions Controller/Adminhtml/FastlyCdn/Backend/ValidationTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Backend;

use Magento\Framework\Exception\LocalizedException;

trait ValidationTrait
{
/**
* @param $param
* @return mixed|null
*/
private function processRequest($param)
{
$request = $this->getRequest()->getParam($param);
if ($request == '') {
return null;
}
return $request;
}

/**
* @param float $maxTls
* @param float $minTls
* @throws LocalizedException
*/
private function validateVersion($maxTls, $minTls)
{
if ($maxTls == 0) {
return;
} elseif ($maxTls < $minTls) {
throw new LocalizedException(__("Maximum TLS version must be higher than the minimum TLS version."));
}
}

/**
* @param $name
* @throws LocalizedException
*/
private function validateName($name)
{
if (trim($name) == "") {
throw new LocalizedException(__("Name can't be blank"));
}
}

/**
* @param $address
* @throws LocalizedException
*/
private function validateAddress($address)
{
if (!filter_var($address, FILTER_VALIDATE_IP) &&
!filter_var($address, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) {
throw new LocalizedException(__('Address ' . $address . ' is not a valid IPv4, IPv6 or hostname.'));
}
}

/**
* @param $override
* @return string|null
* @throws LocalizedException
*/
private function validateOverride($override)
{
if ($override === '') {
return null;
}

if (!filter_var($override, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) {
throw new LocalizedException(__('Override host ' . $override . ' is not a valid hostname.'));
}

return $override;
}

/**
* @param $clone
* @param $conditionName
* @param $applyIf
* @param $conditionPriority
* @param $selCondition
* @return mixed
* @throws LocalizedException
*/
private function createCondition($clone, $conditionName, $applyIf, $conditionPriority, $selCondition)
{
if ($conditionName == $selCondition && !empty($selCondition) &&
!$this->api->getCondition($clone->number, $conditionName)) {
$condition = [
'name' => $conditionName,
'statement' => $applyIf,
'type' => 'REQUEST',
'priority' => $conditionPriority
];
$createCondition = $this->api->createCondition($clone->number, $condition);
return $createCondition->name;
}
return $selCondition;
}
}
2 changes: 1 addition & 1 deletion view/adminhtml/templates/system/config/dialogs.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@
<span><?php /* @noEscape */ echo __("Auto load balance") ?></span>
</label>
<div class="admin__field-control">
<select name="auto_load_balance" id="auto_load_balance" class="admin__control-select">
<select name="auto_loadbalance" id="auto_loadbalance" class="admin__control-select">
<option value="0">No</option>
<option value="1">Yes</option>
</select>
Expand Down
Loading