Skip to content

Fixing problems with VCL upload using CLI #652

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 2 commits into from
Jul 20, 2023
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
26 changes: 24 additions & 2 deletions Console/Command/EnableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use Fastly\Cdn\Model\Config;
use Fastly\Cdn\Model\Api;
use Fastly\Cdn\Helper\Vcl;
use Fastly\Cdn\Model\Upload\Acl;
use Fastly\Cdn\Model\Upload\Dictionary;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -76,6 +78,16 @@ class EnableCommand extends Command
*/
private $filesystem;

/**
* @var Acl
*/
private $acl;

/**
* @var Dictionary
*/
private $dictionary;

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -257,14 +269,18 @@ protected function configure() // @codingStandardsIgnoreLine - required by paren
* @param WriterInterface $configWriter
* @param Manager $cacheManager
* @param Filesystem $filesystem
* @param Acl $acl
* @param Dictionary $dictionary
*/
public function __construct(
Config $config,
Api $api,
Vcl $vcl,
WriterInterface $configWriter,
Manager $cacheManager,
Filesystem $filesystem
Filesystem $filesystem,
Acl $acl,
Dictionary $dictionary
) {
parent::__construct();
$this->config = $config;
Expand All @@ -273,6 +289,8 @@ public function __construct(
$this->configWriter = $configWriter;
$this->cacheManager = $cacheManager;
$this->filesystem = $filesystem;
$this->acl = $acl;
$this->dictionary = $dictionary;
}

/**
Expand Down Expand Up @@ -733,6 +751,10 @@ private function uploadVcl($activate)
];

$this->api->createRequest($clone->number, $request);

$this->dictionary->setupDictionary($clone->number, $currActiveVersion);
$this->acl->setupAcl($clone->number, $currActiveVersion);

$this->api->validateServiceVersion($clone->number);
$msg = 'Successfully uploaded VCL. ';

Expand Down Expand Up @@ -893,7 +915,7 @@ private function validateCustomSnippet($customSnippet)
$snippetNameData = explode('_', $snippetName, 3);
$containsEmpty = in_array("", $snippetNameData, true);
$types = ['init', 'recv', 'hit', 'miss', 'pass', 'fetch', 'error', 'log', 'deliver', 'hash', 'none'];
$exception = 'Failed to upload VCL snippets. Please make sure the custom VCL snippets
$exception = 'Failed to upload VCL snippets. Please make sure the custom VCL snippets
follow this naming convention: [vcl_snippet_type]_[priority]_[short_name_description].vcl';

if (count($snippetNameData) < 3) {
Expand Down
61 changes: 21 additions & 40 deletions Controller/Adminhtml/FastlyCdn/Vcl/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Fastly\Cdn\Model\Api;
use Fastly\Cdn\Model\Config;
use Fastly\Cdn\Model\Config\Backend\CustomSnippetUpload;
use Fastly\Cdn\Model\Upload\Acl;
use Fastly\Cdn\Model\Upload\Dictionary;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Config\ScopeConfigInterface;
Expand Down Expand Up @@ -90,6 +92,16 @@ class Upload extends Action
*/
private $typeList;

/**
* @var Dictionary
*/
private $dictionary;

/**
* @var Acl
*/
private $acl;

/**
* Upload constructor.
*
Expand All @@ -105,6 +117,8 @@ class Upload extends Action
* @param Filesystem $filesystem
* @param CoreConfig $coreConfig
* @param TypeListInterface $typeList
* @param Dictionary $dictionary
* @param Acl $acl
*/
public function __construct(
Context $context,
Expand All @@ -118,7 +132,9 @@ public function __construct(
TimezoneInterface $timezone,
Filesystem $filesystem,
CoreConfig $coreConfig,
TypeListInterface $typeList
TypeListInterface $typeList,
Dictionary $dictionary,
Acl $acl
) {
$this->request = $request;
$this->resultJson = $resultJsonFactory;
Expand All @@ -132,7 +148,8 @@ public function __construct(
parent::__construct($context);
$this->coreConfig = $coreConfig;
$this->typeList = $typeList;

$this->dictionary = $dictionary;
$this->acl = $acl;
}

/**
Expand Down Expand Up @@ -205,8 +222,8 @@ public function execute()
];

$this->api->createRequest($clone->number, $request);
$dictionary = $this->setupDictionary($clone->number, $currActiveVersion);
$acl = $this->setupAcl($clone->number, $currActiveVersion);
$dictionary = $this->dictionary->setupDictionary($clone->number, $currActiveVersion);
$acl = $this->acl->setupAcl($clone->number, $currActiveVersion);

if (!$dictionary || !$acl) {
throw new LocalizedException(__('Failed to create Containers'));
Expand Down Expand Up @@ -272,42 +289,6 @@ private function validateCustomSnippet($customSnippet)
return $snippetNameData;
}

/**
* @param $cloneNumber
* @param $currActiveVersion
* @return bool|mixed
* @throws LocalizedException
*/
private function setupDictionary($cloneNumber, $currActiveVersion)
{
$dictionaryName = Config::CONFIG_DICTIONARY_NAME;
$dictionary = $this->api->getSingleDictionary($currActiveVersion, $dictionaryName);

if (!$dictionary) {
$params = ['name' => $dictionaryName];
$dictionary = $this->api->createDictionary($cloneNumber, $params);
}
return $dictionary;
}

/**
* @param $cloneNumber
* @param $currActiveVersion
* @return bool|mixed
* @throws LocalizedException
*/
private function setupAcl($cloneNumber, $currActiveVersion)
{
$aclName = Config::MAINT_ACL_NAME;
$acl = $this->api->getSingleAcl($currActiveVersion, $aclName);

if (!$acl) {
$params = ['name' => $aclName];
$acl = $this->api->createAcl($cloneNumber, $params);
}
return $acl;
}

/**
* @param $clone
* @throws LocalizedException
Expand Down
47 changes: 47 additions & 0 deletions Model/Upload/Acl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Fastly\Cdn\Model\Upload;

use Fastly\Cdn\Model\Api;
use Fastly\Cdn\Model\Config;

class Acl
{

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

/**
* @param Api $api
*/
public function __construct(
Api $api
) {
$this->api = $api;
}

/**
* @param $cloneNumber
* @param $currActiveVersion
* @return bool|mixed
*/
public function setupAcl($cloneNumber, $currActiveVersion)
{
try {
$aclName = Config::MAINT_ACL_NAME;
$acl = $this->api->getSingleAcl($currActiveVersion, $aclName);

if (!$acl) {
$params = ['name' => $aclName];
$acl = $this->api->createAcl($cloneNumber, $params);
}
return $acl;
} catch (\Exception $e) {

return false;
}
}

}
46 changes: 46 additions & 0 deletions Model/Upload/Dictionary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Fastly\Cdn\Model\Upload;

use Fastly\Cdn\Model\Api;
use Fastly\Cdn\Model\Config;

class Dictionary
{

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

/**
* @param Api $api
*/
public function __construct(
Api $api
) {
$this->api = $api;
}

/**
* @param $cloneNumber
* @param $currActiveVersion
* @return bool|mixed
*/
public function setupDictionary($cloneNumber, $currActiveVersion)
{
try {
$dictionaryName = Config::CONFIG_DICTIONARY_NAME;
$dictionary = $this->api->getSingleDictionary($currActiveVersion, $dictionaryName);

if (!$dictionary) {
$params = ['name' => $dictionaryName];
$dictionary = $this->api->createDictionary($cloneNumber, $params);
}
return $dictionary;
} catch (\Exception $e) {

return false;
}
}
}