forked from Craigdigital/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargo_tunnel.go
153 lines (126 loc) · 4.8 KB
/
argo_tunnel.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
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/pkg/errors"
)
// ArgoTunnel is the struct definition of a tunnel.
type ArgoTunnel struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Secret string `json:"tunnel_secret,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
DeletedAt *time.Time `json:"deleted_at,omitempty"`
Connections []ArgoTunnelConnection `json:"connections,omitempty"`
}
// ArgoTunnelConnection represents the connections associated with a tunnel.
type ArgoTunnelConnection struct {
ColoName string `json:"colo_name"`
UUID string `json:"uuid"`
IsPendingReconnect bool `json:"is_pending_reconnect"`
}
// ArgoTunnelsDetailResponse is used for representing the API response payload for
// multiple tunnels.
type ArgoTunnelsDetailResponse struct {
Result []ArgoTunnel `json:"result"`
Response
}
// ArgoTunnelDetailResponse is used for representing the API response payload for
// a single tunnel.
type ArgoTunnelDetailResponse struct {
Result ArgoTunnel `json:"result"`
Response
}
// ArgoTunnels lists all tunnels.
//
// API reference: https://api.cloudflare.com/#argo-tunnel-list-argo-tunnels
func (api *API) ArgoTunnels(ctx context.Context, accountID string) ([]ArgoTunnel, error) {
uri := fmt.Sprintf("/accounts/%s/tunnels", accountID)
res, err := api.makeRequestContextWithHeaders(ctx, http.MethodGet, uri, nil, argoV1Header())
if err != nil {
return []ArgoTunnel{}, err
}
var argoDetailsResponse ArgoTunnelsDetailResponse
err = json.Unmarshal(res, &argoDetailsResponse)
if err != nil {
return []ArgoTunnel{}, errors.Wrap(err, errUnmarshalError)
}
return argoDetailsResponse.Result, nil
}
// ArgoTunnel returns a single Argo tunnel.
//
// API reference: https://api.cloudflare.com/#argo-tunnel-get-argo-tunnel
func (api *API) ArgoTunnel(ctx context.Context, accountID, tunnelUUID string) (ArgoTunnel, error) {
uri := fmt.Sprintf("/accounts/%s/tunnels/%s", accountID, tunnelUUID)
res, err := api.makeRequestContextWithHeaders(ctx, http.MethodGet, uri, nil, argoV1Header())
if err != nil {
return ArgoTunnel{}, err
}
var argoDetailsResponse ArgoTunnelDetailResponse
err = json.Unmarshal(res, &argoDetailsResponse)
if err != nil {
return ArgoTunnel{}, errors.Wrap(err, errUnmarshalError)
}
return argoDetailsResponse.Result, nil
}
// CreateArgoTunnel creates a new tunnel for the account.
//
// API reference: https://api.cloudflare.com/#argo-tunnel-create-argo-tunnel
func (api *API) CreateArgoTunnel(ctx context.Context, accountID, name, secret string) (ArgoTunnel, error) {
uri := fmt.Sprintf("/accounts/%s/tunnels", accountID)
tunnel := ArgoTunnel{Name: name, Secret: secret}
res, err := api.makeRequestContextWithHeaders(ctx, http.MethodPost, uri, tunnel, argoV1Header())
if err != nil {
return ArgoTunnel{}, err
}
var argoDetailsResponse ArgoTunnelDetailResponse
err = json.Unmarshal(res, &argoDetailsResponse)
if err != nil {
return ArgoTunnel{}, errors.Wrap(err, errUnmarshalError)
}
return argoDetailsResponse.Result, nil
}
// DeleteArgoTunnel removes a single Argo tunnel.
//
// API reference: https://api.cloudflare.com/#argo-tunnel-delete-argo-tunnel
func (api *API) DeleteArgoTunnel(ctx context.Context, accountID, tunnelUUID string) error {
uri := fmt.Sprintf("/accounts/%s/tunnels/%s", accountID, tunnelUUID)
res, err := api.makeRequestContextWithHeaders(ctx, http.MethodDelete, uri, nil, argoV1Header())
if err != nil {
return err
}
var argoDetailsResponse ArgoTunnelDetailResponse
err = json.Unmarshal(res, &argoDetailsResponse)
if err != nil {
return errors.Wrap(err, errUnmarshalError)
}
return nil
}
// CleanupArgoTunnelConnections deletes any inactive connections on a tunnel.
//
// API reference: https://api.cloudflare.com/#argo-tunnel-clean-up-argo-tunnel-connections
func (api *API) CleanupArgoTunnelConnections(ctx context.Context, accountID, tunnelUUID string) error {
uri := fmt.Sprintf("/accounts/%s/tunnels/%s/connections", accountID, tunnelUUID)
res, err := api.makeRequestContextWithHeaders(ctx, http.MethodDelete, uri, nil, argoV1Header())
if err != nil {
return err
}
var argoDetailsResponse ArgoTunnelDetailResponse
err = json.Unmarshal(res, &argoDetailsResponse)
if err != nil {
return errors.Wrap(err, errUnmarshalError)
}
return nil
}
// The early implementation of Argo Tunnel endpoints didn't conform to the V4
// API standard response structure. This has been remedied going forward however
// to support older clients this isn't yet the default. An explicit `Accept`
// header is used to get the V4 compatible version.
func argoV1Header() http.Header {
header := make(http.Header)
header.Set("Accept", "application/json;version=1")
return header
}