forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectrum.go
158 lines (132 loc) · 5.14 KB
/
spectrum.go
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
package cloudflare
import (
"encoding/json"
"fmt"
"time"
"github.com/pkg/errors"
)
// SpectrumApplication defines a single Spectrum Application.
type SpectrumApplication struct {
ID string `json:"id,omitempty"`
Protocol string `json:"protocol,omitempty"`
IPv4 bool `json:"ipv4,omitempty"`
DNS SpectrumApplicationDNS `json:"dns,omitempty"`
OriginDirect []string `json:"origin_direct,omitempty"`
OriginPort int `json:"origin_port,omitempty"`
OriginDNS *SpectrumApplicationOriginDNS `json:"origin_dns,omitempty"`
IPFirewall bool `json:"ip_firewall,omitempty"`
ProxyProtocol bool `json:"proxy_protocol,omitempty"`
TLS string `json:"tls,omitempty"`
CreatedOn *time.Time `json:"created_on,omitempty"`
ModifiedOn *time.Time `json:"modified_on,omitempty"`
}
// SpectrumApplicationDNS holds the external DNS configuration for a Spectrum
// Application.
type SpectrumApplicationDNS struct {
Type string `json:"type"`
Name string `json:"name"`
}
// SpectrumApplicationOriginDNS holds the origin DNS configuration for a Spectrum
// Application.
type SpectrumApplicationOriginDNS struct {
Name string `json:"name"`
}
// SpectrumApplicationDetailResponse is the structure of the detailed response
// from the API.
type SpectrumApplicationDetailResponse struct {
Response
Result SpectrumApplication `json:"result"`
}
// SpectrumApplicationsDetailResponse is the structure of the detailed response
// from the API.
type SpectrumApplicationsDetailResponse struct {
Response
Result []SpectrumApplication `json:"result"`
}
// SpectrumApplications fetches all of the Spectrum applications for a zone.
//
// API reference: https://developers.cloudflare.com/spectrum/api-reference/#list-spectrum-applications
func (api *API) SpectrumApplications(zoneID string) ([]SpectrumApplication, error) {
uri := "/zones/" + zoneID + "/spectrum/apps"
res, err := api.makeRequest("GET", uri, nil)
if err != nil {
return []SpectrumApplication{}, errors.Wrap(err, errMakeRequestError)
}
var spectrumApplications SpectrumApplicationsDetailResponse
err = json.Unmarshal(res, &spectrumApplications)
if err != nil {
return []SpectrumApplication{}, errors.Wrap(err, errUnmarshalError)
}
return spectrumApplications.Result, nil
}
// SpectrumApplication fetches a single Spectrum application based on the ID.
//
// API reference: https://developers.cloudflare.com/spectrum/api-reference/#list-spectrum-applications
func (api *API) SpectrumApplication(zoneID string, applicationID string) (SpectrumApplication, error) {
uri := fmt.Sprintf(
"/zones/%s/spectrum/apps/%s",
zoneID,
applicationID,
)
res, err := api.makeRequest("GET", uri, nil)
if err != nil {
return SpectrumApplication{}, errors.Wrap(err, errMakeRequestError)
}
var spectrumApplication SpectrumApplicationDetailResponse
err = json.Unmarshal(res, &spectrumApplication)
if err != nil {
return SpectrumApplication{}, errors.Wrap(err, errUnmarshalError)
}
return spectrumApplication.Result, nil
}
// CreateSpectrumApplication creates a new Spectrum application.
//
// API reference: https://developers.cloudflare.com/spectrum/api-reference/#create-a-spectrum-application
func (api *API) CreateSpectrumApplication(zoneID string, appDetails SpectrumApplication) (SpectrumApplication, error) {
uri := "/zones/" + zoneID + "/spectrum/apps"
res, err := api.makeRequest("POST", uri, appDetails)
if err != nil {
return SpectrumApplication{}, errors.Wrap(err, errMakeRequestError)
}
var spectrumApplication SpectrumApplicationDetailResponse
err = json.Unmarshal(res, &spectrumApplication)
if err != nil {
return SpectrumApplication{}, errors.Wrap(err, errUnmarshalError)
}
return spectrumApplication.Result, nil
}
// UpdateSpectrumApplication updates an existing Spectrum application.
//
// API reference: https://developers.cloudflare.com/spectrum/api-reference/#update-a-spectrum-application
func (api *API) UpdateSpectrumApplication(zoneID, appID string, appDetails SpectrumApplication) (SpectrumApplication, error) {
uri := fmt.Sprintf(
"/zones/%s/spectrum/apps/%s",
zoneID,
appID,
)
res, err := api.makeRequest("PUT", uri, appDetails)
if err != nil {
return SpectrumApplication{}, errors.Wrap(err, errMakeRequestError)
}
var spectrumApplication SpectrumApplicationDetailResponse
err = json.Unmarshal(res, &spectrumApplication)
if err != nil {
return SpectrumApplication{}, errors.Wrap(err, errUnmarshalError)
}
return spectrumApplication.Result, nil
}
// DeleteSpectrumApplication removes a Spectrum application based on the ID.
//
// API reference: https://developers.cloudflare.com/spectrum/api-reference/#delete-a-spectrum-application
func (api *API) DeleteSpectrumApplication(zoneID string, applicationID string) error {
uri := fmt.Sprintf(
"/zones/%s/spectrum/apps/%s",
zoneID,
applicationID,
)
_, err := api.makeRequest("DELETE", uri, nil)
if err != nil {
return errors.Wrap(err, errMakeRequestError)
}
return nil
}