11#! /bin/bash
22
3- echo cloudflare-cli: k8s-tools v0.0.17
3+ echo cloudflare-cli: k8s-tools v0.0.20
44
55bad=0
66if [ -z " $action " ]; then echo " variable 'action' is not set" ; bad=1; fi
2626
2727record_type=${CF_DNS_TYPE:= " A" }
2828bad=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+
2935if [ $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=$?
87112fi
88113if [ $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=$?
93127fi
94128if [ $bad -eq 1 ]; then echo " unknown action - use create or delete" ; exit 1; fi
0 commit comments