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/#223 ui to add backend #274

Merged
merged 9 commits into from
May 11, 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
88 changes: 88 additions & 0 deletions Block/System/Config/Form/Field/CreateBackendBtn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Fastly CDN for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Fastly CDN for Magento End User License Agreement
* that is bundled with this package in the file LICENSE_FASTLY_CDN.txt.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Fastly CDN to newer
* versions in the future. If you wish to customize this module for your
* needs please refer to http://www.magento.com for more information.
*
* @category Fastly
* @package Fastly_Cdn
* @copyright Copyright (c) 2016 Fastly, Inc. (http://www.fastly.com)
* @license BSD, see LICENSE_FASTLY_CDN.txt
*/
namespace Fastly\Cdn\Block\System\Config\Form\Field;

use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;

/**
* Class CreateBackendBtn
* @package Fastly\Cdn\Block\System\Config\Form\Field
*/
class CreateBackendBtn extends Field
{
protected function _construct() // @codingStandardsIgnoreLine - required by parent class
{
$this->_template = 'Fastly_Cdn::system/config/form/field/createBackendBtn.phtml';

parent::_construct();
}

/**
* Remove scope label
*
* @param AbstractElement $element
* @return string
*/
public function render(AbstractElement $element)
{
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
return parent::render($element);
}

/**
* Return element html
*
* @param AbstractElement $element
* @return string
*/
protected function _getElementHtml(AbstractElement $element) // @codingStandardsIgnoreLine - required by parent class
{
return $this->_toHtml();
}

/**
* Return ajax url for collect button
*
* @return string
*/
public function getAjaxUrl()
{
return $this->getUrl('adminhtml/fastlyCdn/vcl/serviceinfo');
}

/**
* Render Create Backend button HTML
* @return mixed
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getButtonHtml()
{
$button = $this->getLayout()->createBlock(
'Magento\Backend\Block\Widget\Button'
)->setData([
'id' => 'fastly_create_backend_button',
'label' => __('Create')
]);

return $button->toHtml();
}
}
256 changes: 256 additions & 0 deletions Controller/Adminhtml/FastlyCdn/Backend/CreateBackend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
<?php
/**
* Fastly CDN for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Fastly CDN for Magento End User License Agreement
* that is bundled with this package in the file LICENSE_FASTLY_CDN.txt.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Fastly CDN to newer
* versions in the future. If you wish to customize this module for your
* needs please refer to http://www.magento.com for more information.
*
* @category Fastly
* @package Fastly_Cdn
* @copyright Copyright (c) 2016 Fastly, Inc. (http://www.fastly.com)
* @license BSD, see LICENSE_FASTLY_CDN.txt
*/
namespace Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Backend;

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
{
/**
* @var Http
*/
private $request;
/**
* @var JsonFactory
*/
private $resultJson;
/**
* @var Api
*/
private $api;
/**
* @var Vcl
*/
private $vcl;
/**
* @var Config
*/
private $config;

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

/**
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\Result\Json|\Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
$result = $this->resultJson->create();
try {
$address = $this->getRequest()->getParam('address');
$form = $this->getRequest()->getParam('form');
$this->validateAddress($address);

if ($form == 'false') {
return $result->setData(['status' => true]);
}

$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');
$activeVersion = $this->getRequest()->getParam('active_version');

$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');

$autoLoadBalance = $this->getRequest()->getParam('auto_loadbalance');

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

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

$params = [
'address' => $this->getRequest()->getParam('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' => $this->getRequest()->getParam('request_condition'),
'service_id' => $service->id,
'shield' => $this->getRequest()->getParam('shield'),
'use_ssl' => $useSsl,
'version' => $clone->number
];

if ($useSsl == '1') {
$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 == '1') {
$params += [
'weight' => $this->getRequest()->getParam('weight')
];
}

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

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

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

if ($activate_flag === 'true') {
$this->api->activateVersion($clone->number);
}
if ($this->config->areWebHooksEnabled() && $this->config->canPublishConfigChanges()) {
$this->api->sendWebHook(
'*Backend '
. $this->getRequest()->getParam('name')
. ' has been created in Fastly version '
. $clone->number
. '*'
);
}

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

return $result->setData([
'status' => true,
'active_version' => $clone->number
]);
} catch (\Exception $e) {
return $result->setData([
'status' => false,
'msg' => $e->getMessage()
]);
}
}

/**
* @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 $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;
}
}
14 changes: 14 additions & 0 deletions Model/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,20 @@ public function configureBackend($params, $version, $old_name)
return $result;
}

/**
* @param $params
* @param $version
* @return bool|mixed
* @throws LocalizedException
*/
public function createBackend($params, $version)
{
$url = $this->_getApiServiceUri(). 'version/'. $version. '/backend';
$result = $this->_fetch($url, \Zend_Http_Client::POST, $params);

return $result;
}

/**
* Send message to Slack channel
*
Expand Down
10 changes: 9 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,18 @@
=========================== -->
<group id="fastly_backend_settings" sortOrder="100" translate="label comment" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Backend Settings</label>
<!-- =========================
Create Backend button
========================== -->
<field id="create_backend" translate="label comment" sortOrder="5" showInDefault="1" showInStore="0" showInWebsite="0">
<label><![CDATA[Create Backend]]></label>
<comment><![CDATA[]]></comment>
<frontend_model>Fastly\Cdn\Block\System\Config\Form\Field\CreateBackendBtn</frontend_model>
</field>
<!-- ==================
Backends field
=================== -->
<field id="backends" translate="label comment tooltip" sortOrder="5" showInDefault="1" showInWebsite="0" showInStore="0">
<field id="backends" translate="label comment tooltip" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Backends</label>
<frontend_model>Fastly\Cdn\Block\System\Config\Form\Field\Backend</frontend_model>
<comment>
Expand Down
Loading