diff --git a/Console/Command/UpdateVclCommand.php b/Console/Command/UpdateVclCommand.php index 2be5c90..9d4e616 100644 --- a/Console/Command/UpdateVclCommand.php +++ b/Console/Command/UpdateVclCommand.php @@ -23,21 +23,26 @@ class UpdateVclCommand extends Command protected $state; /** @var \Magento\PageCache\Model\Config\PageCache $pageCacheConfig */ protected $pageCacheConfig; + /** @var \Sectionio\Metrics\Helper\Data $helper */ + protected $helper; /** * @param \Sectionio\Metrics\Helper\Aperture $aperture * @param \Sectionio\Metrics\Helper\State $state + * @param \Sectionio\Metrics\Helper\Data $helper * @param \Magento\PageCache\Model\Config $pageCacheConfig */ public function __construct( \Sectionio\Metrics\Helper\Aperture $aperture, \Sectionio\Metrics\Helper\State $state, + \Sectionio\Metrics\Helper\Data $helper, \Magento\PageCache\Model\Config $pageCacheConfig ) { parent::__construct(); $this->aperture = $aperture; $this->state = $state; $this->pageCacheConfig = $pageCacheConfig; + $this->helper = $helper; } /** @@ -60,6 +65,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $application_id = $this->state->getApplicationId(); $environment_name = $this->state->getEnvironmentName(); $proxy_name = $this->state->getProxyName(); + /** @var string $proxy_image*/ + $proxy_image = $this->helper->getProxyImage($account_id, $application_id); if (!$account_id) { throw new \Exception('account_id has not been set, please run sectionio:setup'); @@ -77,8 +84,13 @@ protected function execute(InputInterface $input, OutputInterface $output) throw new \Exception('proxy_name has not been set, please run sectionio:setup'); } - /** Extract the generated Varnish 4 VCL code */ - $vcl = $this->pageCacheConfig->getVclFile(\Magento\PageCache\Model\Config::VARNISH_4_CONFIGURATION_PATH); + /** Extract the generated VCL code appropriate for their version*/ + if (strpos($proxy_image, "4" !== false)){ + $vcl = $this->pageCacheConfig->getVclFile(\Magento\PageCache\Model\Config::VARNISH_4_CONFIGURATION_PATH); + } else { + $vcl = $this->pageCacheConfig->getVclFile(\Magento\PageCache\Model\Config::VARNISH_5_CONFIGURATION_PATH); + } + $result = $this->aperture->updateProxyConfiguration($account_id, $application_id, $environment_name, $proxy_name, $vcl, 'MagentoTurpentine'); if ($result['http_code'] == 200) { diff --git a/Controller/Adminhtml/Report/Save.php b/Controller/Adminhtml/Report/Save.php index 96bf4a9..c2fcb20 100644 --- a/Controller/Adminhtml/Report/Save.php +++ b/Controller/Adminhtml/Report/Save.php @@ -202,9 +202,16 @@ public function updateVarnishConfiguration() $application_id = $this->state->getApplicationId(); /** @var string $environment_name */ $environment_name = $this->state->getEnvironmentName(); + /** @var string $proxy_image*/ + $proxy_image = $this->helper->getProxyImage($account_id, $application_id); + + /** Extract the generated VCL code appropriate for their version*/ + if (strpos($proxy_image, "4" !== false)){ + $vcl = $this->pageCacheConfig->getVclFile(\Magento\PageCache\Model\Config::VARNISH_4_CONFIGURATION_PATH); + } else { + $vcl = $this->pageCacheConfig->getVclFile(\Magento\PageCache\Model\Config::VARNISH_5_CONFIGURATION_PATH); + } - /** Extract the generated Varnish 4 VCL code */ - $vcl = $this->pageCacheConfig->getVclFile(\Magento\PageCache\Model\Config::VARNISH_4_CONFIGURATION_PATH); $result = $this->aperture->updateProxyConfiguration($account_id, $application_id, $environment_name, 'varnish', $vcl, 'MagentoTurpentine'); if ($result['http_code'] == 200) { diff --git a/Helper/Data.php b/Helper/Data.php index e7e6000..b9205ee 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -580,4 +580,27 @@ public function clearDefaultApplication() $applicationFactory->save(); } } + + public function getProxyImage($account_id, $application_id) + { + /**generateApertureUrl takes an associative array */ + $parameters = array("accountId"=>$account_id, "applicationId"=>$application_id, "environmentName"=>"Production"); + + /** build the account url */ + $partial_url = $this->generateApertureUrl($parameters); + $url = $partial_url . "/stack"; + + $curl_response = $this->performCurl($url); + /** return response as an associative array */ + $response = json_decode($curl_response['body_content'], true); + /** find object with name=varnish, grab the image */ + + $image; + foreach($response as $proxy){ + if ($proxy['name'] == 'varnish') { + $image = $proxy['image']; + } + } + return explode(".", $image)[0]; + } } diff --git a/Helper/State.php b/Helper/State.php index a87df67..f031815 100644 --- a/Helper/State.php +++ b/Helper/State.php @@ -96,4 +96,9 @@ public function getProxyName() { return 'varnish'; } + + public function getProxyImage() + { + + } }