summaryrefslogtreecommitdiff
path: root/tools/network/cloudflare/zones.go
blob: c9a54a6dfd8c404ac3433eae43103d4ff59dfaf3 (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
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package cloudflare

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"
)

func getZonesRequest(authToken string) (*http.Request, error) {
	// construct the query
	requestURI, err := url.Parse(cloudFlareURI)
	if err != nil {
		return nil, err
	}
	requestURI.Path = requestURI.Path + "zones"
	request, err := http.NewRequest("GET", requestURI.String(), nil)
	if err != nil {
		return nil, err
	}
	addHeaders(request, authToken)
	return request, nil
}

// GetZonesResult is the JSON response for a DNS create request
type GetZonesResult struct {
	Success    bool                 `json:"success"`
	Errors     []interface{}        `json:"errors"`
	Messages   []interface{}        `json:"messages"`
	Result     []GetZonesResultItem `json:"result"`
	ResultInfo GetZonesResultPage   `json:"result_info"`
}

// GetZonesResultPage is the result of the response for the DNS create request
type GetZonesResultPage struct {
	Page       int `json:"page"`
	PerPage    int `json:"per_page"`
	TotalPages int `json:"total_pages"`
	Count      int `json:"count"`
	TotalCount int `json:"total_count"`
}

// GetZonesResultItem is the result of the response for the DNS create request
type GetZonesResultItem struct {
	ID                  string   `json:"id"`
	Name                string   `json:"name"`
	Status              string   `json:"status"`
	Paused              bool     `json:"paused"`
	Type                string   `json:"type"`
	DevelopmentMode     int      `json:"development_mode"`
	NameServers         []string `json:"name_servers"`
	OriginalNameServers []string `json:"original_name_servers"`
}

func parseGetZonesResponse(response *http.Response) (*GetZonesResult, error) {
	defer response.Body.Close()
	body, err := io.ReadAll(response.Body)
	if err != nil {
		return nil, err
	}
	if response.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("response status code %d", response.StatusCode)
	}
	var parsedReponse GetZonesResult
	if err := json.Unmarshal(body, &parsedReponse); err != nil {
		return nil, fmt.Errorf("failed to unmarshal response body '%s' : %v", string(body), err)
	}
	return &parsedReponse, nil
}

func exportZoneRequest(zoneID, authToken string) (*http.Request, error) {
	// construct the query
	requestURI, err := url.Parse(cloudFlareURI)
	if err != nil {
		return nil, err
	}
	requestURI.Path = requestURI.Path + "zones/" + zoneID + "/dns_records/export"
	request, err := http.NewRequest("GET", requestURI.String(), nil)
	if err != nil {
		return nil, err
	}
	addHeaders(request, authToken)
	return request, nil
}