summaryrefslogtreecommitdiff
path: root/adapters
diff options
context:
space:
mode:
authorsimponic <simponic@hatecomputers.club>2024-03-28 12:57:35 -0400
committersimponic <simponic@hatecomputers.club>2024-03-28 12:57:35 -0400
commitb2fc689bdcff28bf75c0128db19ba4730d726b4f (patch)
tree37c16d95183242516ba667aa5f441539d152c279 /adapters
parent75ba836d6072235fc7a71659f8630ab3c1b210ad (diff)
downloadhatecomputers.club-b2fc689bdcff28bf75c0128db19ba4730d726b4f.tar.gz
hatecomputers.club-b2fc689bdcff28bf75c0128db19ba4730d726b4f.zip
dns api (#1)
Co-authored-by: Elizabeth Hunt <elizabeth.hunt@simponic.xyz> Reviewed-on: https://git.hatecomputers.club/hatecomputers/hatecomputers.club/pulls/1
Diffstat (limited to 'adapters')
-rw-r--r--adapters/cloudflare/cloudflare.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/adapters/cloudflare/cloudflare.go b/adapters/cloudflare/cloudflare.go
new file mode 100644
index 0000000..40b04a5
--- /dev/null
+++ b/adapters/cloudflare/cloudflare.go
@@ -0,0 +1,71 @@
+package cloudflare
+
+import (
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "strings"
+
+ "git.hatecomputers.club/hatecomputers/hatecomputers.club/database"
+)
+
+type CloudflareDNSResponse struct {
+ Result database.DNSRecord `json:"result"`
+}
+
+func CreateDNSRecord(zoneId string, apiToken string, record *database.DNSRecord) (string, error) {
+ url := fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/dns_records", zoneId)
+
+ reqBody := fmt.Sprintf(`{"type":"%s","name":"%s","content":"%s","ttl":%d,"proxied":false}`, record.Type, record.Name, record.Content, record.TTL)
+ payload := strings.NewReader(reqBody)
+
+ req, _ := http.NewRequest("POST", url, payload)
+
+ req.Header.Add("Authorization", "Bearer "+apiToken)
+ req.Header.Add("Content-Type", "application/json")
+
+ res, err := http.DefaultClient.Do(req)
+ if err != nil {
+ return "", err
+ }
+
+ defer res.Body.Close()
+ body, _ := io.ReadAll(res.Body)
+
+ if res.StatusCode != 200 {
+ return "", fmt.Errorf("error creating dns record: %s", body)
+ }
+
+ var response CloudflareDNSResponse
+ err = json.Unmarshal(body, &response)
+ if err != nil {
+ return "", err
+ }
+
+ result := &response.Result
+
+ return result.ID, nil
+}
+
+func DeleteDNSRecord(zoneId string, apiToken string, id string) error {
+ url := fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/dns_records/%s", zoneId, id)
+
+ req, _ := http.NewRequest("DELETE", url, nil)
+
+ req.Header.Add("Authorization", "Bearer "+apiToken)
+
+ res, err := http.DefaultClient.Do(req)
+ if err != nil {
+ return err
+ }
+
+ defer res.Body.Close()
+ body, _ := io.ReadAll(res.Body)
+
+ if res.StatusCode != 200 {
+ return fmt.Errorf("error deleting dns record: %s", body)
+ }
+
+ return nil
+}