/
build2_cloudflare.ps1
120 lines (104 loc) · 3.1 KB
/
build2_cloudflare.ps1
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
118
119
120
$email = "<Provided by CloudFlare Account>"
$api_key = "<Provided by CloudFlare Account>"
$overwriteip = $true
#Configure work environment and populate environment variables:
cd $env:WORKSPACE
$EIP = $env:ElasticIP
$domain_partial = $ENV:Domain
$domain_Instance_Name = $ENV:Instance_Name
$domain_FQDN = $domain_Instance_Name + "." + $domain_partial
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("X-Auth-Key", "$api_key")
$headers.Add("X-Auth-Email", "$email")
function get-cfzoneid() {
Param(
[string] $DNSname
)
if($result = invoke-restmethod -Uri "https://api.cloudflare.com/client/v4/zones/" -Method GET -headers $headers | select result) {
if($DNSname.count -ge 1) {
$dns_tmp = $DNSname.split(".")
$zone = $dns_Tmp[$dns_tmp.count-2] + "." + $dns_Tmp[$dns_tmp.count-1]
}
if ($id = $result.result | where { $_.name -match $zone} | select -expandproperty id) {
return $id
} else {
return $false
}
}
}
function create-CFdns() {
Param(
[Parameter(Mandatory=$true)]
[string] $DNSname,
[Parameter(Mandatory=$true)]
[string] $type,
[Parameter(Mandatory=$true)]
[string] $ip_update,
[string] $id="NS"
)
$uri_base = "https://api.cloudflare.com/client/v4/zones/" + $id
if(-not($result = invoke-restmethod -Uri "$uri_base/dns_records" -Method GET -headers $headers)) {
return $false
}
if(-not ($result.result | where { $_.name -eq "$DNSname"})) {
try {
$json = "{""type"":""" + $type +""",
""name"":""" + $dnsname + """,
""content"":""" + $ip_update + """ }"
$result = invoke-restmethod -Uri "https://api.cloudflare.com/client/v4/zones/$id/dns_records/" -Method POST -Body $json -headers $headers
return $result.result
} catch {
return $false
}
} else {
return $false
}
}
function update-CFdns() {
Param(
[Parameter(Mandatory=$true)]
[string] $DNSname,
[Parameter(Mandatory=$true)]
[string] $ip_update,
[string] $id="NS"
)
$uri_base = "https://api.cloudflare.com/client/v4/zones/" + $id
if($id -eq "NS") {
$id = get-cfzoneid $DNSname
}
if(-not ($result = invoke-restmethod -Uri "$uri_base/dns_records" -Method GET -headers $headers)) {
return $false
}
if($data = $result.result | where { $_.name -eq "$DNSname"}) {
try {
$data | add-member "content" "$ip_update" -force
$json = $data | ConvertTo-Json
$query_url = $uri_base + "/dns_records/" + $data.id
$result = invoke-restmethod -Uri $query_url -Method PUT -Body $json -headers $headers
return $result.result
} catch {
return $_
}
} else {
return $false
}
}
try {
$id = get-cfzoneid $domain_FQDN
if($overwriteIP) {
if(-not(update-CFdns $domain_FQDN $EIP $id)){
create-CFdns $domain_FQDN A $EIP $id
echo "Assigned $EIP to $domain_FQDN"
}
} else {
if(-not(create-CFdns $domain_FQDN A $EIP $id)) {
echo "$domain_FQDN already has an IP assigned to cloudflare. This script is not allowed to update existing cloudflare domains."
echo "Please contact your Cloudflare administrator to clear the domain."
}
}
} catch {
echo "ERROR Assigning $EIP to $domain_FQDN"
$_
exit 1
}