Skip to content

Commit 2d312bc

Browse files
Change to use Cloudflare API (#46)
* Change to use Cloudflare API * Get zone id programtically * Bump Version --------- Co-authored-by: James Seden Smith <sedders123@gmail.com>
1 parent cbbfdaa commit 2d312bc

2 files changed

Lines changed: 46 additions & 11 deletions

File tree

Dockerfile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
FROM node:20
1+
FROM ubuntu:22.04
22

3-
RUN npm install -g cloudflare-cli
4-
RUN apt-get update
5-
RUN apt-get install -y jq
3+
RUN apt-get update && apt-get install -y build-essential software-properties-common openssl \
4+
zip unzip --no-install-recommends \
5+
&& apt-get install -y curl jq \
6+
&& apt-get auto-remove && rm -rf /var/lib/apt/lists/*
67
RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/$(dpkg --print-architecture)/kubectl"
78
RUN chmod +x ./kubectl
89
RUN mv ./kubectl /usr/local/bin/kubectl

k8s-tools.sh

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
echo cloudflare-cli: k8s-tools v0.0.17
3+
echo cloudflare-cli: k8s-tools v0.0.20
44

55
bad=0
66
if [ -z "$action" ]; then echo "variable 'action' is not set"; bad=1; fi
@@ -26,6 +26,12 @@ fi
2626

2727
record_type=${CF_DNS_TYPE:="A"}
2828
bad=1
29+
30+
zone_id=$(curl https://api.cloudflare.com/client/v4/zones?name=$CF_API_DOMAIN \
31+
-H "X-Auth-Email: $CF_API_EMAIL" \
32+
-H "X-Auth-Key: $CF_API_KEY" | jq -r ".result[] | select(.name | contains(\"$CF_API_DOMAIN\")) | .id")
33+
if [ -z "$zone_id" ]; then echo "zone not found"; exit 1; fi
34+
2935
if [ $action = "create" ]; then
3036
bad=0
3137

@@ -54,7 +60,6 @@ if [ $action = "create" ]; then
5460
if [ -z "$resource" ]; then echo "no service data returned"; exit 1; fi
5561
echo got service info
5662
fi
57-
5863
if [ $record_type = "A" ]
5964
then
6065
echo getting external IP...
@@ -79,16 +84,45 @@ if [ $action = "create" ]; then
7984
fi
8085

8186
echo deleting any existing record...
82-
cfcli -e $CF_API_EMAIL -k $CF_API_KEY -d $CF_API_DOMAIN -a -t $record_type rm $subdomain
8387

84-
echo adding...
85-
cfcli -e $CF_API_EMAIL -k $CF_API_KEY -d $CF_API_DOMAIN -t $record_type add $subdomain $dns_record_value --activate $use_proxy
88+
echo looking up existing record to delete...
89+
cloudflare_record_id=$(curl https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?search=$subdomain \
90+
-H "X-Auth-Email: $CF_API_EMAIL" \
91+
-H "X-Auth-Key: $CF_API_KEY" | jq -r ".result[] | select(.name | contains(\"$subdomain\")) | .id")
92+
93+
94+
echo deleting...
95+
curl https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$cloudflare_record_id \
96+
-X DELETE \
97+
-H "X-Auth-Email: $CF_API_EMAIL" \
98+
-H "X-Auth-Key: $CF_API_KEY"
99+
100+
echo adding...
101+
curl https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records \
102+
-H 'Content-Type: application/json' \
103+
-H "X-Auth-Email: $CF_API_EMAIL" \
104+
-H "X-Auth-Key: $CF_API_KEY" \
105+
-d '{
106+
"content": "'$dns_record_value'",
107+
"name": "'$subdomain'",
108+
"proxied": '$use_proxy',
109+
"type": "'$record_type'"
110+
}'
86111
retVal=$?
87112
fi
88113
if [ $action = "delete" ]; then
89114
bad=0
90-
echo deleting...
91-
cfcli -e $CF_API_EMAIL -k $CF_API_KEY -d $CF_API_DOMAIN -a -t $record_type rm $subdomain
115+
echo deleting...
116+
echo looking up existing record to delete...
117+
cloudflare_record_id=$(curl https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?search=$subdomain \
118+
-H "X-Auth-Email: $CF_API_EMAIL" \
119+
-H "X-Auth-Key: $CF_API_KEY" | jq -r ".result[] | select(.name | contains(\"$subdomain\")) | .id")
120+
121+
echo deleting...
122+
curl https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$cloudflare_record_id \
123+
-X DELETE \
124+
-H "X-Auth-Email: $CF_API_EMAIL" \
125+
-H "X-Auth-Key: $CF_API_KEY"
92126
retVal=$?
93127
fi
94128
if [ $bad -eq 1 ]; then echo "unknown action - use create or delete"; exit 1; fi

0 commit comments

Comments
 (0)