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

Improvements to custom VCL snippets upload logic #530

Merged
merged 3 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
44 changes: 40 additions & 4 deletions Controller/Adminhtml/FastlyCdn/Vcl/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@
use Magento\Framework\App\Cache\TypeListInterface;

/**
* Class Upload
* Class for VCL Upload
*
* @package Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Vcl
*/
class Upload extends Action
{
Expand Down Expand Up @@ -132,7 +131,6 @@ public function __construct(
parent::__construct($context);
$this->coreConfig = $coreConfig;
$this->typeList = $typeList;

}

/**
Expand All @@ -154,7 +152,10 @@ public function execute()
$read = $this->filesystem->getDirectoryRead(DirectoryList::VAR_DIR);
$customSnippetPath = $read->getAbsolutePath(Config::CUSTOM_SNIPPET_PATH);
$customSnippets = $this->config->getCustomSnippets($customSnippetPath);
//$customSnippets['recv_200_Proba'] = 'set req.http.proba = "1";';
vvuksan marked this conversation as resolved.
Show resolved Hide resolved
//$customSnippets['recv_200_Proba2'] = 'set req.http.proba2 = "1";';

$allowedSnippets = [];
foreach ($snippets as $key => $value) {
$priority = 50;
if ($key == 'hash') {
Expand All @@ -167,6 +168,7 @@ public function execute()
'priority' => $priority,
'content' => $value
];
$allowedSnippets[] = $snippetData['name'];
$this->api->uploadSnippet($clone->number, $snippetData);
}

Expand All @@ -183,9 +185,12 @@ public function execute()
'content' => $value,
'dynamic' => '0'
];
$allowedSnippets[] = $customSnippetData['name'];
$this->api->uploadSnippet($clone->number, $customSnippetData);
}

$this->syncSnippets($allowedSnippets, $clone->number);

$this->createGzipHeader($clone);

$condition = [
Expand Down Expand Up @@ -273,6 +278,8 @@ private function validateCustomSnippet($customSnippet)
}

/**
* Setup Dictionary
*
* @param $cloneNumber
* @param $currActiveVersion
* @return bool|mixed
Expand All @@ -291,6 +298,8 @@ private function setupDictionary($cloneNumber, $currActiveVersion)
}

/**
* Setup Acl
*
* @param $cloneNumber
* @param $currActiveVersion
* @return bool|mixed
Expand All @@ -309,10 +318,12 @@ private function setupAcl($cloneNumber, $currActiveVersion)
}

/**
* Create Gzip Header
*
* @param $clone
* @throws LocalizedException
*/
private function createGzipHeader($clone)
private function createGzipHeader($clone): void
{
$condition = [
'name' => Config::FASTLY_MAGENTO_MODULE . '_gzip_safety',
Expand All @@ -334,4 +345,29 @@ private function createGzipHeader($clone)

$this->api->createHeader($clone->number, $headerData);
}

/**
* Remove disabled snippets from current vcl file
*
* @param array $allowedSnippets
* @param int $version
* @throws LocalizedException
*/
private function syncSnippets(array $allowedSnippets, int $version): void
{
$snippets = $this->api->getSnippets($version);

$currentActiveSnippets = [];
foreach ($snippets as $item) {
$currentActiveSnippets[] = $item->name;
}
$snippetsForDelete = array_diff($currentActiveSnippets, $allowedSnippets);

foreach ($snippetsForDelete as $snippetName) {
//remove only snippet name which starts with magento prefix
if (strpos($snippetName, Config::FASTLY_MAGENTO_MODULE . '_') === 0) {
$this->api->removeSnippet($version, $snippetName);
}
}
}
}
15 changes: 15 additions & 0 deletions Model/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,21 @@ public function activateVersion($version)
return $result;
}

/**
* Get all VCL snippets
*
* @param $version
* @return bool|mixed
* @throws LocalizedException
*/
public function getSnippets($version)
{
$url = $this->_getApiServiceUri() . 'version/' .rawurlencode($version). '/snippet';
$result = $this->_fetch($url, 'GET');

return $result;
}

/**
* Creating and updating regular VCL snippets
*
Expand Down