cli icon indicating copy to clipboard operation
cli copied to clipboard

[FEATURE REQUEST] Sync functionality for VCL snippets

Open shyim opened this issue 2 years ago • 2 comments

Is your feature request related to a problem? Please describe. I have to automate to sync VCL files to the service in headless way automated

Describe the solution you'd like I expected to have a functionality that I can have a fastly.toml in the project, where all vcl snippets are defined, and then I can run fastly vcl update and they are defined in the remote

Describe alternatives you've considered

  • https://registry.terraform.io/providers/fastly/fastly/latest/docs/resources/service_vcl
    • As I am building a template I don't want to force people to use terraform. The fastly cli seemed a better way
  • I need to implement this my own, I am the only one? I am lazy 😂

Additional context

shyim avatar Apr 22 '22 11:04 shyim

My solution so far

#!/usr/bin/env bash

# fail on error
set -e

if [[ -z "$FASTLY_API_TOKEN" ]]; then
  echo "Environment variable FASTLY_API_TOKEN is not set. Skipping"
  exit 0
fi

if [[ -z "$FASTLY_SERVICE_ID" ]]; then
  echo "Environment variable FASTLY_SERVICE_ID is not set. Skipping"
  exit 0
fi

created_version=0

create_version_if_not_done() {
    if [[ "$created_version" == "1" ]]; then
        return
    fi

    echo "Creating version from active version"
    fastly service-version clone --version=active
    created_version=1
}

get_md5()
{
  if builtin command -v md5 > /dev/null; then
    echo "$1" | md5
  elif builtin command -v md5sum > /dev/null ; then
    echo "$1" | md5sum | awk '{print $1}'
  else
    echo "Neither md5 nor md5sum were found in the PATH"
    return 1
  fi

  return 0
}

for vcl in ./config/fastly/*.vcl; do
    trigger=$(basename $vcl .vcl)
    name="shopware_${trigger}"

    if fastly vcl snippet describe --version=latest "--name=$name" > /dev/null; then
        # The snippet exists on remote
        localContent=$(cat "$vcl")
        localContentMd5=$(get_md5 "$localContent")

        remoteContent=$(fastly vcl snippet describe --version=latest "--name=$name" --json | jq -r '.Content')
        remoteContentMd5=$(get_md5 "$remoteContent")

        if [[ "$localContentMd5" != "$remoteContentMd5" ]]; then
            echo "Snippet ${trigger} has changed. Updating"

            create_version_if_not_done

            fastly vcl snippet update "--name=shopware_${trigger}" "--content=${vcl}" "--type=${trigger}" --version=latest
        else
            echo "Snippet ${trigger} is up to date"
        fi
    else
        create_version_if_not_done

        fastly vcl snippet create "--name=shopware_${trigger}" "--content=${vcl}" "--type=${trigger}" --version=latest
    fi
done

if [[ "$created_version" == "1" ]]; then
    echo "Activating latest version"

    fastly service-version activate --version latest
fi

shyim avatar Apr 22 '22 13:04 shyim

Hi @shyim

Thanks for opening this issue.

Although we appreciate that there are users who would prefer to avoid the complexity of using Terraform, our primary concern is that this feature is beginning to tread on the toes of a tool such as Terraform, which is designed specifically to handle this sort of use case.

We'll get back to you once we've had a chance to discuss internally whether this is an appropriate feature for the CLI.

Thanks.

Integralist avatar Sep 28 '22 11:09 Integralist