Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tunnel: return tunnel token, fix uri #881

Merged
merged 2 commits into from May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 15 additions & 9 deletions tunnel.go
Expand Up @@ -48,6 +48,12 @@ type TunnelDetailResponse struct {
Response
}

// TunnelTokenResponse is the API response for a tunnel token.
type TunnelTokenResponse struct {
Result string `json:"result"`
Response
}

type TunnelParams struct {
AccountID string
ID string
Expand Down Expand Up @@ -263,27 +269,27 @@ func (api *API) CleanupTunnelConnections(ctx context.Context, params TunnelClean
// TunnelToken that allows to run a tunnel.
//
// API reference: https://api.cloudflare.com/#cloudflare-tunnel-get-cloudflare-tunnel-token
func (api *API) TunnelToken(ctx context.Context, params TunnelTokenParams) error {
func (api *API) TunnelToken(ctx context.Context, params TunnelTokenParams) (string, error) {
if params.AccountID == "" {
return ErrMissingAccountID
return "", ErrMissingAccountID
}

if params.ID == "" {
return errors.New("missing tunnel ID")
return "", errors.New("missing tunnel ID")
}

uri := fmt.Sprintf("accounts/%s/cfd_tunnel/%s/token", params.AccountID, params.ID)
uri := fmt.Sprintf("/accounts/%s/cfd_tunnel/%s/token", params.AccountID, params.ID)

res, err := api.makeRequestContextWithHeaders(ctx, http.MethodGet, uri, nil, nil)
if err != nil {
return err
return "", err
}

var argoDetailsResponse TunnelDetailResponse
err = json.Unmarshal(res, &argoDetailsResponse)
var tunnelTokenResponse TunnelTokenResponse
err = json.Unmarshal(res, &tunnelTokenResponse)
if err != nil {
return errors.Wrap(err, errUnmarshalError)
return "", errors.Wrap(err, errUnmarshalError)
}

return nil
return tunnelTokenResponse.Result, nil
}
23 changes: 23 additions & 0 deletions tunnel_test.go
Expand Up @@ -247,3 +247,26 @@ func TestCleanupTunnelConnections(t *testing.T) {
err := client.CleanupTunnelConnections(context.Background(), TunnelCleanupParams{AccountID: testAccountID, ID: "f174e90a-fafe-4643-bbbc-4a0ed4fc8415"})
assert.NoError(t, err)
}

func TestTunnelToken(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": "ZHNraGdhc2RraGFza2hqZGFza2poZGFza2poYXNrZGpoYWtzamRoa2FzZGpoa2FzamRoa2Rhc2po\na2FzamRoa2FqCg=="
}
`)
}

mux.HandleFunc("/accounts/"+testAccountID+"/cfd_tunnel/f174e90a-fafe-4643-bbbc-4a0ed4fc8415/token", handler)

token, err := client.TunnelToken(context.Background(), TunnelTokenParams{AccountID: testAccountID, ID: "f174e90a-fafe-4643-bbbc-4a0ed4fc8415"})
assert.NoError(t, err)
assert.Equal(t, "ZHNraGdhc2RraGFza2hqZGFza2poZGFza2poYXNrZGpoYWtzamRoa2FzZGpoa2FzamRoa2Rhc2po\na2FzamRoa2FqCg==", token)
}