|
| 1 | +package cloudflare |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/pkg/errors" |
| 12 | +) |
| 13 | + |
| 14 | +// TunnelRoute is the full record for a route. |
| 15 | +type TunnelRoute struct { |
| 16 | + Network string `json:"network"` |
| 17 | + TunnelID string `json:"tunnel_id"` |
| 18 | + TunnelName string `json:"tunnel_name"` |
| 19 | + Comment string `json:"comment"` |
| 20 | + CreatedAt *time.Time `json:"created_at"` |
| 21 | + DeletedAt *time.Time `json:"deleted_at"` |
| 22 | +} |
| 23 | + |
| 24 | +type TunnelRoutesListParams struct { |
| 25 | + AccountID string `json:"-"` |
| 26 | + TunnelID string `json:"tunnel_id,omitempty"` |
| 27 | + Comment string `json:"comment,omitempty"` |
| 28 | + IsDeleted *bool `json:"is_deleted,omitempty"` |
| 29 | + NetworkSubset string `json:"network_subset,omitempty"` |
| 30 | + NetworkSuperset string `json:"network_superset,omitempty"` |
| 31 | + ExistedAt *time.Time `json:"existed_at,omitempty"` |
| 32 | + PaginationOptions |
| 33 | +} |
| 34 | + |
| 35 | +type TunnelRoutesCreateParams struct { |
| 36 | + AccountID string `json:"-"` |
| 37 | + Network string `json:"-"` |
| 38 | + TunnelID string `json:"tunnel_id"` |
| 39 | + Comment string `json:"comment,omitempty"` |
| 40 | +} |
| 41 | + |
| 42 | +type TunnelRoutesUpdateParams struct { |
| 43 | + AccountID string `json:"-"` |
| 44 | + Network string `json:"network"` |
| 45 | + TunnelID string `json:"tunnel_id"` |
| 46 | + Comment string `json:"comment,omitempty"` |
| 47 | +} |
| 48 | + |
| 49 | +type TunnelRoutesForIPParams struct { |
| 50 | + AccountID string `json:"-"` |
| 51 | + Network string `json:"-"` |
| 52 | +} |
| 53 | + |
| 54 | +type TunnelRoutesDeleteParams struct { |
| 55 | + AccountID string `json:"-"` |
| 56 | + Network string `json:"-"` |
| 57 | +} |
| 58 | + |
| 59 | +// tunnelRouteListResponse is the API response for listing tunnel routes. |
| 60 | +type tunnelRouteListResponse struct { |
| 61 | + Response |
| 62 | + Result []TunnelRoute `json:"result"` |
| 63 | +} |
| 64 | + |
| 65 | +type tunnelRouteResponse struct { |
| 66 | + Response |
| 67 | + Result TunnelRoute `json:"result"` |
| 68 | +} |
| 69 | + |
| 70 | +// TunnelRoutes lists all defined routes for tunnels in the account. |
| 71 | +// |
| 72 | +// See: https://api.cloudflare.com/#tunnel-route-list-tunnel-routes |
| 73 | +func (api *API) TunnelRoutes(ctx context.Context, params TunnelRoutesListParams) ([]TunnelRoute, error) { |
| 74 | + if params.AccountID == "" { |
| 75 | + return []TunnelRoute{}, ErrMissingAccountID |
| 76 | + } |
| 77 | + |
| 78 | + uri := fmt.Sprintf("/%s/%s/teamnet/routes", AccountRouteRoot, params.AccountID) |
| 79 | + res, err := api.makeRequestContext(ctx, http.MethodGet, uri, params) |
| 80 | + |
| 81 | + if err != nil { |
| 82 | + return []TunnelRoute{}, err |
| 83 | + } |
| 84 | + |
| 85 | + var resp tunnelRouteListResponse |
| 86 | + err = json.Unmarshal(res, &resp) |
| 87 | + if err != nil { |
| 88 | + return []TunnelRoute{}, errors.Wrap(err, errUnmarshalError) |
| 89 | + } |
| 90 | + |
| 91 | + return resp.Result, nil |
| 92 | +} |
| 93 | + |
| 94 | +// TunnelRouteForIP finds the Tunnel Route that encompasses the given IP. |
| 95 | +// |
| 96 | +// See: https://api.cloudflare.com/#tunnel-route-get-tunnel-route-by-ip |
| 97 | +func (api *API) TunnelRouteForIP(ctx context.Context, params TunnelRoutesForIPParams) (TunnelRoute, error) { |
| 98 | + uri := fmt.Sprintf("/%s/%s/teamnet/routes/ip/%s", AccountRouteRoot, params.AccountID, params.Network) |
| 99 | + |
| 100 | + responseBody, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) |
| 101 | + if err != nil { |
| 102 | + return TunnelRoute{}, err |
| 103 | + } |
| 104 | + |
| 105 | + var routeResponse tunnelRouteResponse |
| 106 | + err = json.Unmarshal(responseBody, &routeResponse) |
| 107 | + if err != nil { |
| 108 | + return TunnelRoute{}, errors.Wrap(err, errUnmarshalError) |
| 109 | + } |
| 110 | + |
| 111 | + return routeResponse.Result, nil |
| 112 | +} |
| 113 | + |
| 114 | +// CreateTunnelRoute add a new route to the account routing table for the given |
| 115 | +// tunnel. |
| 116 | +// |
| 117 | +// See: https://api.cloudflare.com/#tunnel-route-create-route |
| 118 | +func (api *API) CreateTunnelRoute(ctx context.Context, params TunnelRoutesCreateParams) (TunnelRoute, error) { |
| 119 | + if params.AccountID == "" { |
| 120 | + return TunnelRoute{}, ErrMissingAccountID |
| 121 | + } |
| 122 | + |
| 123 | + uri := fmt.Sprintf("/%s/%s/teamnet/routes/network/%s", AccountRouteRoot, params.AccountID, url.PathEscape(params.Network)) |
| 124 | + |
| 125 | + responseBody, err := api.makeRequestContext(ctx, http.MethodPost, uri, params) |
| 126 | + if err != nil { |
| 127 | + return TunnelRoute{}, err |
| 128 | + } |
| 129 | + |
| 130 | + var routeResponse tunnelRouteResponse |
| 131 | + err = json.Unmarshal(responseBody, &routeResponse) |
| 132 | + if err != nil { |
| 133 | + return TunnelRoute{}, errors.Wrap(err, errUnmarshalError) |
| 134 | + } |
| 135 | + |
| 136 | + return routeResponse.Result, nil |
| 137 | +} |
| 138 | + |
| 139 | +// DeleteTunnelRoute delete an existing route from the account routing table. |
| 140 | +// |
| 141 | +// See: https://api.cloudflare.com/#tunnel-route-delete-route |
| 142 | +func (api *API) DeleteTunnelRoute(ctx context.Context, params TunnelRoutesDeleteParams) error { |
| 143 | + if params.AccountID == "" { |
| 144 | + return ErrMissingAccountID |
| 145 | + } |
| 146 | + |
| 147 | + uri := fmt.Sprintf("/%s/%s/teamnet/routes/network/%s", AccountRouteRoot, params.AccountID, url.PathEscape(params.Network)) |
| 148 | + |
| 149 | + responseBody, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil) |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + |
| 154 | + var routeResponse tunnelRouteResponse |
| 155 | + err = json.Unmarshal(responseBody, &routeResponse) |
| 156 | + if err != nil { |
| 157 | + return errors.Wrap(err, errUnmarshalError) |
| 158 | + } |
| 159 | + |
| 160 | + return nil |
| 161 | +} |
| 162 | + |
| 163 | +// UpdateTunnelRoute updates an existing route in the account routing table for |
| 164 | +// the given tunnel. |
| 165 | +// |
| 166 | +// See: https://api.cloudflare.com/#tunnel-route-update-route |
| 167 | +func (api *API) UpdateTunnelRoute(ctx context.Context, params TunnelRoutesUpdateParams) (TunnelRoute, error) { |
| 168 | + if params.AccountID == "" { |
| 169 | + return TunnelRoute{}, ErrMissingAccountID |
| 170 | + } |
| 171 | + |
| 172 | + uri := fmt.Sprintf("/%s/%s/teamnet/routes/network/%s", AccountRouteRoot, params.AccountID, url.PathEscape(params.Network)) |
| 173 | + |
| 174 | + responseBody, err := api.makeRequestContext(ctx, http.MethodPatch, uri, params) |
| 175 | + if err != nil { |
| 176 | + return TunnelRoute{}, err |
| 177 | + } |
| 178 | + |
| 179 | + var routeResponse tunnelRouteResponse |
| 180 | + err = json.Unmarshal(responseBody, &routeResponse) |
| 181 | + if err != nil { |
| 182 | + return TunnelRoute{}, errors.Wrap(err, errUnmarshalError) |
| 183 | + } |
| 184 | + |
| 185 | + return routeResponse.Result, nil |
| 186 | +} |
0 commit comments