Controller/Adminhtml/FastlyCdn/BasicAuthentication/EnableAuth.php
public function execute()
    {
        $result = $this->resultJson->create();
        try {
            $activeVersion = $this->getRequest()->getParam('active_version');
            $activateVcl = $this->getRequest()->getParam('activate_flag');
            $service = $this->api->checkServiceDetails();
            $enabled = false;
            $this->vcl->checkCurrentVersionActive($service->versions, $activeVersion);
            $currActiveVersion = $this->vcl->getCurrentVersion($service->versions);
            $vclPath = Config::VCL_AUTH_SNIPPET_PATH;
            $snippets = $this->config->getVclSnippets($vclPath);

            // Check if snippets exist
            $status = true;
            foreach ($snippets as $key => $value) {
                $name = Config::FASTLY_MAGENTO_MODULE.'_basic_auth_'.$key;
                $status = $this->api->getSnippet($activeVersion, $name);

                if (!$status) {
                    break;
                }
            }

            if (!$status) {
                // Check if Auth has entries
                $this->api->checkAuthDictionaryPopulation($activeVersion);
                $clone = $this->api->cloneVersion($currActiveVersion);

                // Insert snippet
                foreach ($snippets as $key => $value) {
                    $snippetData = [
                        'name'      => Config::FASTLY_MAGENTO_MODULE.'_basic_auth_'.$key,
                        'type'      => $key,
                        'dynamic'   => "0",
                        'content'   => $value,
                        'priority'  => 10
                    ];
                    $this->api->uploadSnippet($clone->number, $snippetData);
                }

                $enabled = true;
            } else {
                $clone = $this->api->cloneVersion($currActiveVersion);

                // Remove snippets
                foreach ($snippets as $key => $value) {
                    $name = Config::FASTLY_MAGENTO_MODULE.'_basic_auth_'.$key;
                    $this->api->removeSnippet($clone->number, $name);
                }
            }

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

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

            $this->sendWebhook($enabled, $clone);

            $comment = ['comment' => 'Magento Module turned ON Basic Authentication'];
            if (!$enabled) {
                $comment = ['comment' => 'Magento Module turned OFF Basic Authentication'];
            }
            $this->api->addComment($clone->number, $comment);

            return $result->setData(['status' => true]);
        } catch (\Exception $e) {
            return $result->setData([
                'status'    => false,
                'msg'       => $e->getMessage()
            ]);
        }
    }
public function execute()
    {
        $result = $this->resultJson->create();
        try {
            $activeVersion = $this->getRequest()->getParam('active_version');
            $activateVcl = $this->getRequest()->getParam('activate_flag');
            $service = $this->api->checkServiceDetails();
            $enabled = false;
            $this->vcl->checkCurrentVersionActive($service->versions, $activeVersion);
            $currActiveVersion = $this->vcl->getCurrentVersion($service->versions);
            $vclPath = Config::VCL_AUTH_SNIPPET_PATH;
            $snippets = $this->config->getVclSnippets($vclPath);

            // Check if snippets exist
            $status = true;
            foreach ($snippets as $key => $value) {
                $name = Config::FASTLY_MAGENTO_MODULE.'_basic_auth_'.$key;
                $status = $this->api->getSnippet($activeVersion, $name);

                if (!$status) {
                    break;
                }
            }

            if (!$status) {
                // Check if Auth has entries
                $this->api->checkAuthDictionaryPopulation($activeVersion);
                $clone = $this->api->cloneVersion($currActiveVersion);

                // Insert snippet
                foreach ($snippets as $key => $value) {
                    $snippetData = [
                        'name'      => Config::FASTLY_MAGENTO_MODULE.'_basic_auth_'.$key,
                        'type'      => $key,
                        'dynamic'   => "0",
                        'content'   => $value,
                        'priority'  => 10
                    ];
                    $this->api->uploadSnippet($clone->number, $snippetData);
                }

                $enabled = true;
            } else {
                $clone = $this->api->cloneVersion($currActiveVersion);

                // Remove snippets
                foreach ($snippets as $key => $value) {
                    $name = Config::FASTLY_MAGENTO_MODULE.'_basic_auth_'.$key;
                    $this->api->removeSnippet($clone->number, $name);
                }
            }

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

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

            $this->sendWebhook($enabled, $clone);

            $comment = ['comment' => 'Magento Module turned ON Basic Authentication'];
            if (!$enabled) {
                $comment = ['comment' => 'Magento Module turned OFF Basic Authentication'];
            }
            $this->api->addComment($clone->number, $comment);

            return $result->setData(['status' => true]);
        } catch (\Exception $e) {
            return $result->setData([
                'status'    => false,
                'msg'       => $e->getMessage()
            ]);
        }
    }
Upload Auth VCL snippets

@return $this|ResponseInterface|ResultInterface