0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

dynamic VCL Snippetsを更新する

0
Posted at

目的
作ったvclを更新したい

前回まで
├ create.sh
├ delete.sh
├ vcl(この下にアップロードするvclを保存する。)

今回
├ create.sh
├ delete.sh
├ update.sh (new!)
├ vcl(この下にアップロードするvclを保存する。)

update.sh
SERVICE_ID=$1
FILE=$2

response=$(curl -X GET -s "https://api.fastly.com/service/${SERVICE_ID}/version" -H "Fastly-Key:${FASTLY_API_TOKEN}"  -H 'Content-Type: application/json')
len=$(echo $response | jq length)

for i in $( seq 0 $(($len - 1)) ); do
    row=$(echo $response | jq .[$i])
    active=$(echo $row | jq ".active" | tr -d '"')
    if [ $active = "true" ]; then
        active_version=$(echo $row | jq ".number" | tr -d '"')
    fi
done

echo "active version: $active_version"

response=$(curl -X GET -s "https://api.fastly.com/service/${SERVICE_ID}/version/${active_version}/snippet" -H "Fastly-Key:${FASTLY_API_TOKEN}"  -H 'Content-Type: application/json')
len=$(echo $response | jq length)

for i in $( seq 0 $(($len - 1)) ); do
    row=$(echo $response | jq .[$i])
    name=$(echo $row | jq ".name" | tr -d '"')
    if [ $name = $FILE ]; then
        snippet_id=$(echo $row | jq ".id" | tr -d '"')
    fi
done

if [ -z $snippet_id ]; then
    echo "対象のsnippetが存在しません"
    exit 1
fi

echo "snippet_id: $snippet_id"

content=`cat vcl/${FILE}.vcl`

curl -X PUT -s "https://api.fastly.com/service/${SERVICE_ID}/snippet/${snippet_id}" -H "Fastly-Key:${FASTLY_API_TOKEN}" -H 'Content-Type: application/x-www-form-urlencoded' --data $"content=${content}";

使い方
./update.sh vclの下に置いたファイル名(拡張子覗く)


./update.sh xxxxxx sample

まず現在activateされているversionを取得。
apiみたかんじ消すのはnameでできるのに更新はidが必要だったのでnameをidに変換。
最後に更新APIを実行しています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?