Skip to content

Commit 5b567e3

Browse files
authored
SDK-88: Add edge_ips support for Spectrum (#431)
1 parent 2da4acc commit 5b567e3

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

spectrum.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package cloudflare
33
import (
44
"encoding/json"
55
"fmt"
6+
"net"
7+
"strings"
68
"time"
79

810
"github.com/pkg/errors"
@@ -48,6 +50,7 @@ type SpectrumApplication struct {
4850
ProxyProtocol ProxyProtocol `json:"proxy_protocol,omitempty"`
4951
TLS string `json:"tls,omitempty"`
5052
TrafficType string `json:"traffic_type,omitempty"`
53+
EdgeIPs *EdgeIPs `json:"edge_ips,omitempty"`
5154
CreatedOn *time.Time `json:"created_on,omitempty"`
5255
ModifiedOn *time.Time `json:"modified_on,omitempty"`
5356
}
@@ -102,6 +105,43 @@ type SpectrumApplicationsDetailResponse struct {
102105
Result []SpectrumApplication `json:"result"`
103106
}
104107

108+
// EdgeIPs represents configuration for Bring-Your-Own-IP
109+
// https://developers.cloudflare.com/spectrum/getting-started/byoip/
110+
type EdgeIPs struct {
111+
Type EdgeType `json:"type"`
112+
IPs []net.IP `json:"ips,omitempty"`
113+
}
114+
115+
// EdgeType for possible Edge configurations
116+
type EdgeType string
117+
118+
const (
119+
// STATIC IP config
120+
STATIC EdgeType = "static"
121+
)
122+
123+
// UnmarshalJSON function for EdgeType enum
124+
func (t *EdgeType) UnmarshalJSON(b []byte) error {
125+
var s string
126+
err := json.Unmarshal(b, &s)
127+
if err != nil {
128+
return err
129+
}
130+
131+
newEdgeType := EdgeType(strings.ToLower(s))
132+
switch newEdgeType {
133+
case STATIC:
134+
*t = newEdgeType
135+
return nil
136+
}
137+
138+
return errors.New(errUnmarshalError)
139+
}
140+
141+
func (t EdgeType) String() string {
142+
return string(t)
143+
}
144+
105145
// SpectrumApplications fetches all of the Spectrum applications for a zone.
106146
//
107147
// API reference: https://developers.cloudflare.com/spectrum/api-reference/#list-spectrum-applications

spectrum_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cloudflare
22

33
import (
44
"fmt"
5+
"net"
56
"net/http"
67
"testing"
78
"time"
@@ -400,3 +401,70 @@ func TestSpectrumApplicationProxyProtocolDeprecations(t *testing.T) {
400401
teardown()
401402
}
402403
}
404+
405+
func TestSpectrumApplicationEdgeIPs(t *testing.T) {
406+
setup()
407+
defer teardown()
408+
409+
handler := func(w http.ResponseWriter, r *http.Request) {
410+
assert.Equal(t, r.Method, "GET", "Expected method 'GET', got %s", r.Method)
411+
w.Header().Set("content-type", "application/json")
412+
fmt.Fprint(w, `{
413+
"result": {
414+
"id": "f68579455bd947efb65ffa1bcf33b52c",
415+
"protocol": "tcp/22",
416+
"ipv4": true,
417+
"dns": {
418+
"type": "CNAME",
419+
"name": "spectrum.example.com"
420+
},
421+
"origin_direct": [
422+
"tcp://192.0.2.1:22"
423+
],
424+
"ip_firewall": true,
425+
"proxy_protocol": "off",
426+
"tls": "off",
427+
"edge_ips": {
428+
"type": "static",
429+
"ips": [
430+
"192.0.2.1",
431+
"2001:db8::1"
432+
]
433+
},
434+
"created_on": "2018-03-28T21:25:55.643771Z",
435+
"modified_on": "2018-03-28T21:25:55.643771Z"
436+
},
437+
"success": true,
438+
"errors": [],
439+
"messages": []
440+
}`)
441+
}
442+
443+
mux.HandleFunc("/zones/01a7362d577a6c3019a474fd6f485823/spectrum/apps/f68579455bd947efb65ffa1bcf33b52c", handler)
444+
createdOn, _ := time.Parse(time.RFC3339, "2018-03-28T21:25:55.643771Z")
445+
modifiedOn, _ := time.Parse(time.RFC3339, "2018-03-28T21:25:55.643771Z")
446+
want := SpectrumApplication{
447+
ID: "f68579455bd947efb65ffa1bcf33b52c",
448+
CreatedOn: &createdOn,
449+
ModifiedOn: &modifiedOn,
450+
Protocol: "tcp/22",
451+
IPv4: true,
452+
DNS: SpectrumApplicationDNS{
453+
Name: "spectrum.example.com",
454+
Type: "CNAME",
455+
},
456+
OriginDirect: []string{"tcp://192.0.2.1:22"},
457+
IPFirewall: true,
458+
ProxyProtocol: "off",
459+
TLS: "off",
460+
EdgeIPs: &EdgeIPs{
461+
Type: STATIC,
462+
IPs: []net.IP{net.ParseIP("192.0.2.1"), net.ParseIP("2001:db8::1")},
463+
},
464+
}
465+
466+
actual, err := client.SpectrumApplication("01a7362d577a6c3019a474fd6f485823", "f68579455bd947efb65ffa1bcf33b52c")
467+
if assert.NoError(t, err) {
468+
assert.Equal(t, want, actual)
469+
}
470+
}

0 commit comments

Comments
 (0)