Skip to content

Commit 3ff47da

Browse files
Update existing record instead of delete & add, use API token auth (#48)
* Update existing record if it exists rather than deleting then recreating * Use API bearer token authentication --------- Co-authored-by: James Seden Smith <sedders123@gmail.com>
1 parent 2d312bc commit 3ff47da

1 file changed

Lines changed: 32 additions & 30 deletions

File tree

k8s-tools.sh

Lines changed: 32 additions & 30 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.20
3+
echo cloudflare-cli: k8s-tools v0.0.21
44

55
bad=0
66
if [ -z "$action" ]; then echo "variable 'action' is not set"; bad=1; fi
@@ -28,8 +28,7 @@ record_type=${CF_DNS_TYPE:="A"}
2828
bad=1
2929

3030
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")
31+
-H "Authorization: Bearer $CF_API_KEY" | jq -r ".result[] | select(.name | contains(\"$CF_API_DOMAIN\")) | .id")
3332
if [ -z "$zone_id" ]; then echo "zone not found"; exit 1; fi
3433

3534
if [ $action = "create" ]; then
@@ -83,46 +82,49 @@ if [ $action = "create" ]; then
8382
echo public Host Name: $dns_record_value
8483
fi
8584

86-
echo deleting any existing record...
87-
8885
echo looking up existing record to delete...
8986
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"
87+
-H "Authorization: Bearer $CF_API_KEY" | jq -r ".result[] | select(.name | contains(\"$subdomain\")) | .id")
9988

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-
}'
111-
retVal=$?
89+
if [ -z "$cloudflare_record_id" ]
90+
then
91+
echo creating for first time...
92+
curl https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records \
93+
-H 'Content-Type: application/json' \
94+
-H "Authorization: Bearer $CF_API_KEY" \
95+
-d '{
96+
"content": "'$dns_record_value'",
97+
"name": "'$subdomain'",
98+
"proxied": '$use_proxy',
99+
"type": "'$record_type'"
100+
}'
101+
retVal=$?
102+
else
103+
echo updating...
104+
curl https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records \
105+
-X PATCH \
106+
-H 'Content-Type: application/json' \
107+
-H "Authorization: Bearer $CF_API_KEY" \
108+
-d '{
109+
"content": "'$dns_record_value'",
110+
"name": "'$subdomain'",
111+
"proxied": '$use_proxy',
112+
"type": "'$record_type'"
113+
}'
114+
retVal=$?
115+
fi
112116
fi
113117
if [ $action = "delete" ]; then
114118
bad=0
115119
echo deleting...
116120
echo looking up existing record to delete...
117121
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")
122+
-H "Authorization: Bearer $CF_API_KEY" | jq -r ".result[] | select(.name | contains(\"$subdomain\")) | .id")
120123

121124
echo deleting...
122125
curl https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$cloudflare_record_id \
123126
-X DELETE \
124-
-H "X-Auth-Email: $CF_API_EMAIL" \
125-
-H "X-Auth-Key: $CF_API_KEY"
127+
-H "Authorization: Bearer $CF_API_KEY"
126128
retVal=$?
127129
fi
128130
if [ $bad -eq 1 ]; then echo "unknown action - use create or delete"; exit 1; fi

0 commit comments

Comments
 (0)