summaryrefslogtreecommitdiff
path: root/http.go
blob: e1865af1a3195ab464596a980ff57b1d794de6f6 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// This is a simple example
package cloudflare

import 	(
	"io/ioutil"
	"net/http"
	"bytes"

	"go.wit.com/log"
)

/*
curl --request POST \
  --url https://api.cloudflare.com/client/v4/zones/zone_identifier/dns_records \
  --header 'Content-Type: application/json' \
  --header 'X-Auth-Email: ' \
  --data '{
  "content": "198.51.100.4",
  "name": "example.com",
  "proxied": false,
  "type": "A",
  "comment": "Domain verification record",
  "tags": [
    "owner:dns-team"
  ],
  "ttl": 3600
}'
*/

func doCurlDelete(auth string, email string, zoneId string, rrId string) string {
	var err error
	var req *http.Request

	if zoneId == "" {
		log.Warn("doCurlDelete() zoneId == nil")
		return ""
	}

	if rrId == "" {
		log.Warn("doCurlDelete() rrId == nil")
		return ""
	}

	data := []byte("")

	url := "https://api.cloudflare.com/client/v4/zones/" + zoneId + "/dns_records/" + rrId

	req, err = http.NewRequest(http.MethodDelete, url, bytes.NewBuffer(data))

	// Set headers
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authorization", "Bearer " + auth)
	// changed from this 2024-01-05
	// req.Header.Set("X-Auth-Key", auth)
	// req.Header.Set("X-Auth-Email", email)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Error(err)
		return ""
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Error(err)
		return ""
	}

	return string(body)
}

func doCurlCreate(auth string, email string, zoneId string, data string) string {
	var err error
	var req *http.Request

	if zoneId == "" {
		log.Warn("doCurlDelete() zoneId == nil")
		return ""
	}

	url := "https://api.cloudflare.com/client/v4/zones/" + zoneId + "/dns_records/"

	log.Log(CURL, "doCurlCreate() POST url =", url)
	log.Log(CURL, "doCurlCreate() POST Auth =", auth)
	log.Log(CURL, "doCurlCreate() POST Email =", email)
	log.Log(CURL, "doCurlCreate() POST data =", data)

	req, err = http.NewRequest(http.MethodPost, url, bytes.NewBuffer( []byte(data) ))

	// Set headers
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authorization", "Bearer " + auth)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Error(err, "client.Do() failed")
		return ""
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Error(err, "ioutil.ReadAll(body) failed")
		return ""
	}

	pretty, _ := FormatJSON(string(body))
	log.Log(CURL, "Create() result =", pretty)
	return string(body)
}

func doCurl(method string, rr *RRT) string {
	var err error
	var req *http.Request

	data := []byte(rr.data)

	if (method == "PUT") {
		req, err = http.NewRequest(http.MethodPut, rr.url, bytes.NewBuffer(data))
	} else {
		req, err = http.NewRequest(http.MethodPost, rr.url, bytes.NewBuffer(data))
	}

	// Set headers
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authorization", "Bearer " + rr.Auth)

	log.Log(CURL, "http PUT url =", rr.url)
	log.Log(CURL, "http PUT Auth =", rr.Auth)
	log.Log(CURL, "http PUT Email =", rr.Email)
	log.Log(CURL, "http PUT data =", rr.data)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Error(err)
		return ""
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Error(err)
		return ""
	}

	return string(body)
}

func curlPost(dnsRow *RRT) string {
	var authKey string = dnsRow.Auth
	var email string = dnsRow.Email

	url := dnsRow.url
	tmp := dnsRow.data

	log.Log(CURL, "curlPost() START")
	log.Log(CURL, "curlPost() authkey = ", authKey)
	log.Log(CURL, "curlPost() email   = ", email)
	log.Log(CURL, "curlPost() url     = ", url)
	data := []byte(tmp)
	req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data))

	// Set headers
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authorization", "Bearer " + authKey)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Error(err, "client.Do() failed")
		return ""
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Error(err)
		return ""
	}
	log.Spew("http PUT body =", body)

	pretty, _ := FormatJSON(string(body))
	log.Log(CURL, "result =", pretty)
	log.Log(CURL, "curl() END")
	return pretty
}