-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnsme.functions
117 lines (100 loc) · 3.47 KB
/
dnsme.functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash
function dnsme_find () {
local domain domain_id serial id name type value ttl json data
domain=$1
shift
id=$( dnsme cat $domain |
jq --arg name "$name" '.data[]|select(.name==$name)|.id' )
}
function dnsme_mk () {
local domain domain_id serial id name type value ttl json data
local api_history=$(api_history)
domain=$1
shift
if [[ -z $1 ]] ; then
data=$( jq -n "{ name: \"$domain\" }")
dnsme_api dns/managed/ -X POST -d "$data"
return
fi
dnsme domain id $domain > /dev/zero || return
domain_id=$(echo $res | jq -r .id)
domain=$(echo $res | jq -r .name)
######################################
local data put_records post_records i
for data in "$@" ; do ## Start for loop
((i++))
data=$( echo "$data" |
jq 'if .name=="@" then .name="" else . end |
if .ttl == null then .ttl=1800 else . end |
if has("gtdLocation") then . else .gtdLocation="DEFAULT" end ' )
unset vars
declare -A vars # associative arrays are local by default
json2vars "$data"
flow='If no id provided and not naked record, check if record already exists and get its ip'
if [[ -z ${vars[id]} ]] ; then
if [[ ${vars[name]} == '""' ]] ; then
vars[id]=$( dnsme cat $domain |
jq ".data[]|select(.name==${vars[name]} and .type==${vars[type]} and .value==${vars[value]}) | .id" )
else
vars[id]=$( dnsme cat $domain |
jq ".data[]|select(.name==${vars[name]})|.id" )
fi
local count=$( echo ${vars[id]} | wc -w )
if (( count == 0 )) ; then
local do_nothing='id is not set anyway'
elif (( count == 1 )) ; then
if [[ ${vars[name]} == '""' ]] ; then
echo -e " src: $api_history \n msg: Record already exists name: ${vars[name]} type: ${vars[type]} value: ${vars[value]}" | error_red
continue
fi
elif (( count > 1 )) ; then
echo -e " src: $api_history \n msg: $record_count identical records ${vars[name]} of type ${vars[type]} " | error_red
continue
fi
fi
# Adding records to relevant arrays
if [[ -n ${vars[id]} ]] ; then
# dnsme_api dns/managed/$domain_id/records/${vars[id]} -X PUT -d "$data"
put_records[$i]="$(jq -n "$data | .id=${vars[id]}")"
else
# dnsme_api dns/managed/$domain_id/records/ -X POST -d "$data"
post_records[$i]="$data"
fi
done # end for data in "$@"
###############################################
if (( ${#put_records[@]} > 0 )) ; then
dnsme_api dns/managed/$domain_id/records/updateMulti -X PUT -d "$(echo "${put_records[@]}" | jq -c -s .)"
elif (( ${#post_records[@]} > 0 )) ; then
dnsme_api dns/managed/$domain_id/records/createMulti -X POST -d "$(echo "${post_records[@]}" | jq -c -s .)" >/dev/zero
fi
} # function dnsme_mk
function dnsme_domain_rm () {
local domain=$1 serial domain_id
shift
read -r domain_id serial domain < <(dnsme_domain_id $domain)
if [[ -z $domain ]] ; then
echo " error: No such domain $domain" | error_red
return 1
fi
read -n 1 -p "Y/y to delete domain $domain: " ; read_nl
if [[ x$REPLY == x[Yy] ]] ; then
if dnsme_api dns/managed/$domain_id -X DELETE -d "[$domain_id]" ; then
rm -f $dnsme_domains_dir/$domain
echo -e " msgs: Domain $domain deleted" 1>&2
fi
fi
} # end function
function dnsme_rm () {
[[ -n $2 ]] || return 2
local domain=$1 domain_id
shift
read -r domain_id serial domain < <(dnsme_domain_id $domain)
if [[ -z $domain ]] ; then
echo " error: No such domain $domain" | error_red
return 1
fi
ids=$(echo $*) # second and every next record id to have exactly one leading space
ids=${ids// /&ids=}
dnsme_api dns/managed/$domain_id/records?ids=$ids -X DELETE &&
echo -e " msgs: Deleted records $*" 1>&2
} # end function dnsme_rm