Repository: cloudflare/cloudflare-go
Pull request #1227 (2023-03-03 21:41:10)

Update tunnels
## Description

- Updates the tunnels structs to match the current API docs
- Changes to using `ListTunnels` and `GetTunnel` instead of `Tunnels` and `Tunnel`.

## Has your change been tested?

Existing and updated tests pass.

## Screenshots (if appropriate):

## Types of changes

What sort of change does your code introduce/modify?

- [ ] Bug fix (non-breaking change which fixes an issue)
- [X] New feature (non-breaking change which adds functionality)
- [X] Breaking change (fix or feature that would cause existing functionality to change)

## Checklist:

- [X] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [] I have updated the documentation accordingly.
- [X] I have added tests to cover my changes.
- [X] All new and existing tests passed.
- [X] This change is using publicly documented (api.cloudflare.com or developers.cloudflare.com) and stable APIs.




Patch:
diff --git a/.changelog/1227.txt b/.changelog/1227.txt
new file mode 100644
index 000000000..96c1ce052
--- /dev/null
+++ b/.changelog/1227.txt
@@ -0,0 +1,10 @@
+```release-note:breaking-change
+tunnel: renamed `Tunnels` to `ListTunnels`
+```
+
+```release-note:breaking-change
+tunnel: renamed `Tunnel` to `GetTunnel`
+```
+```release-note:enhancement
+tunnel: updated parameters to latest API docs
+```
\ No newline at end of file
diff --git a/tunnel.go b/tunnel.go
index 8ae91d391..a43a81e35 100644
--- a/tunnel.go
+++ b/tunnel.go
@@ -23,16 +23,20 @@ type Tunnel struct {
 	Connections    []TunnelConnection `json:"connections,omitempty"`
 	ConnsActiveAt  *time.Time         `json:"conns_active_at,omitempty"`
 	ConnInactiveAt *time.Time         `json:"conns_inactive_at,omitempty"`
+	TunnelType     string             `json:"tun_type,omitempty"`
+	Status         string             `json:"status,omitempty"`
+	RemoteConfig   bool               `json:"remote_config,omitempty"`
 }
 
 // Connection is the struct definition of a connection.
 type Connection struct {
-	ID          string             `json:"id,omitempty"`
-	Features    []string           `json:"features,omitempty"`
-	Version     string             `json:"version,omitempty"`
-	Arch        string             `json:"arch,omitempty"`
-	Connections []TunnelConnection `json:"conns,omitempty"`
-	RunAt       *time.Time         `json:"run_at,omitempty"`
+	ID            string             `json:"id,omitempty"`
+	Features      []string           `json:"features,omitempty"`
+	Version       string             `json:"version,omitempty"`
+	Arch          string             `json:"arch,omitempty"`
+	Connections   []TunnelConnection `json:"conns,omitempty"`
+	RunAt         *time.Time         `json:"run_at,omitempty"`
+	ConfigVersion int                `json:"config_version,omitempty"`
 }
 
 // TunnelConnection represents the connections associated with a tunnel.
@@ -91,8 +95,9 @@ type TunnelTokenResponse struct {
 }
 
 type TunnelCreateParams struct {
-	Name   string `json:"name,omitempty"`
-	Secret string `json:"tunnel_secret,omitempty"`
+	Name      string `json:"name,omitempty"`
+	Secret    string `json:"tunnel_secret,omitempty"`
+	ConfigSrc string `json:"config_src,omitempty"`
 }
 
 type TunnelUpdateParams struct {
@@ -179,10 +184,10 @@ type TunnelListParams struct {
 	ResultInfo
 }
 
-// Tunnels lists all tunnels.
+// ListTunnels lists all tunnels.
 //
 // API reference: https://api.cloudflare.com/#cloudflare-tunnel-list-cloudflare-tunnels
-func (api *API) Tunnels(ctx context.Context, rc *ResourceContainer, params TunnelListParams) ([]Tunnel, *ResultInfo, error) {
+func (api *API) ListTunnels(ctx context.Context, rc *ResourceContainer, params TunnelListParams) ([]Tunnel, *ResultInfo, error) {
 	if rc.Identifier == "" {
 		return []Tunnel{}, &ResultInfo{}, ErrMissingAccountID
 	}
@@ -225,10 +230,10 @@ func (api *API) Tunnels(ctx context.Context, rc *ResourceContainer, params Tunne
 	return records, &listResponse.ResultInfo, nil
 }
 
-// Tunnel returns a single Argo tunnel.
+// GetTunnel returns a single Argo tunnel.
 //
 // API reference: https://api.cloudflare.com/#cloudflare-tunnel-get-cloudflare-tunnel
-func (api *API) Tunnel(ctx context.Context, rc *ResourceContainer, tunnelID string) (Tunnel, error) {
+func (api *API) GetTunnel(ctx context.Context, rc *ResourceContainer, tunnelID string) (Tunnel, error) {
 	if rc.Identifier == "" {
 		return Tunnel{}, ErrMissingAccountID
 	}
@@ -386,10 +391,10 @@ func (api *API) GetTunnelConfiguration(ctx context.Context, rc *ResourceContaine
 	return tunnelDetails, nil
 }
 
-// TunnelConnections gets all connections on a tunnel.
+// ListTunnelConnections gets all connections on a tunnel.
 //
 // API reference: https://api.cloudflare.com/#cloudflare-tunnel-list-cloudflare-tunnel-connections
-func (api *API) TunnelConnections(ctx context.Context, rc *ResourceContainer, tunnelID string) ([]Connection, error) {
+func (api *API) ListTunnelConnections(ctx context.Context, rc *ResourceContainer, tunnelID string) ([]Connection, error) {
 	if rc.Identifier == "" {
 		return []Connection{}, ErrMissingAccountID
 	}
@@ -461,10 +466,10 @@ func (api *API) CleanupTunnelConnections(ctx context.Context, rc *ResourceContai
 	return nil
 }
 
-// TunnelToken that allows to run a tunnel.
+// GetTunnelToken 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, rc *ResourceContainer, tunnelID string) (string, error) {
+func (api *API) GetTunnelToken(ctx context.Context, rc *ResourceContainer, tunnelID string) (string, error) {
 	if rc.Identifier == "" {
 		return "", ErrMissingAccountID
 	}


Test patch:
diff --git a/testdata/fixtures/tunnel/single_full.json b/testdata/fixtures/tunnel/single_full.json
index 5408bdb56..e5178e650 100644
--- a/testdata/fixtures/tunnel/single_full.json
+++ b/testdata/fixtures/tunnel/single_full.json
@@ -1,22 +1,25 @@
-{
-  "success": true,
-  "errors": [],
-  "messages": [],
-  "result": {
-    "id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
-    "name": "blog",
-    "created_at": "2009-11-10T23:00:00Z",
-    "deleted_at": "2009-11-10T23:00:00Z",
-    "connections": [
-      {
-        "colo_name": "DFW",
-        "id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
-        "is_pending_reconnect": false,
-        "client_id": "dc6472cc-f1ae-44a0-b795-6b8a0ce29f90",
-        "client_version": "2022.2.0",
-        "opened_at": "2021-01-25T18:22:34.317854Z",
-        "origin_ip": "198.51.100.1"
-      }
-    ]
-  }
-}
+{
+  "success": true,
+  "errors": [],
+  "messages": [],
+  "result": {
+    "id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+    "name": "blog",
+    "created_at": "2009-11-10T23:00:00Z",
+    "deleted_at": "2009-11-10T23:00:00Z",
+    "connections": [
+      {
+        "colo_name": "DFW",
+        "id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+        "is_pending_reconnect": false,
+        "client_id": "dc6472cc-f1ae-44a0-b795-6b8a0ce29f90",
+        "client_version": "2022.2.0",
+        "opened_at": "2021-01-25T18:22:34.317854Z",
+        "origin_ip": "198.51.100.1"
+      }
+    ],
+    "status": "healthy",
+    "tun_type": "cfd_tunnel",
+    "remote_config": true
+  }
+}
diff --git a/tunnel_test.go b/tunnel_test.go
index 5e6291047..ced320c74 100644
--- a/tunnel_test.go
+++ b/tunnel_test.go
@@ -11,7 +11,7 @@ import (
 	"github.com/stretchr/testify/assert"
 )
 
-func TestTunnels(t *testing.T) {
+func TestListTunnels(t *testing.T) {
 	setup()
 	defer teardown()
 
@@ -26,13 +26,13 @@ func TestTunnels(t *testing.T) {
 	createdAt, _ := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z")
 	deletedAt, _ := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z")
 	want := []Tunnel{{
-		ID:        "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+		ID:        testTunnelID,
 		Name:      "blog",
 		CreatedAt: &createdAt,
 		DeletedAt: &deletedAt,
 		Connections: []TunnelConnection{{
 			ColoName:           "DFW",
-			ID:                 "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+			ID:                 testTunnelID,
 			IsPendingReconnect: false,
 			ClientID:           "dc6472cc-f1ae-44a0-b795-6b8a0ce29f90",
 			ClientVersion:      "2022.2.0",
@@ -41,14 +41,14 @@ func TestTunnels(t *testing.T) {
 		}},
 	}}
 
-	actual, _, err := client.Tunnels(context.Background(), AccountIdentifier(testAccountID), TunnelListParams{UUID: "f174e90a-fafe-4643-bbbc-4a0ed4fc8415"})
+	actual, _, err := client.ListTunnels(context.Background(), AccountIdentifier(testAccountID), TunnelListParams{UUID: testTunnelID})
 
 	if assert.NoError(t, err) {
 		assert.Equal(t, want, actual)
 	}
 }
 
-func TestTunnelsPagination(t *testing.T) {
+func TestListTunnelsPagination(t *testing.T) {
 	setup()
 	defer teardown()
 
@@ -68,13 +68,13 @@ func TestTunnelsPagination(t *testing.T) {
 	deletedAt, _ := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z")
 	want := []Tunnel{
 		{
-			ID:        "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+			ID:        testTunnelID,
 			Name:      "blog",
 			CreatedAt: &createdAt,
 			DeletedAt: &deletedAt,
 			Connections: []TunnelConnection{{
 				ColoName:           "DFW",
-				ID:                 "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+				ID:                 testTunnelID,
 				IsPendingReconnect: false,
 				ClientID:           "dc6472cc-f1ae-44a0-b795-6b8a0ce29f90",
 				ClientVersion:      "2022.2.0",
@@ -84,7 +84,7 @@ func TestTunnelsPagination(t *testing.T) {
 		},
 	}
 
-	actual, _, err := client.Tunnels(context.Background(), AccountIdentifier(testAccountID),
+	actual, _, err := client.ListTunnels(context.Background(), AccountIdentifier(testAccountID),
 		TunnelListParams{
 			Name: "blog",
 			ResultInfo: ResultInfo{
@@ -98,7 +98,7 @@ func TestTunnelsPagination(t *testing.T) {
 	}
 }
 
-func TestTunnel(t *testing.T) {
+func TestGetTunnel(t *testing.T) {
 	setup()
 	defer teardown()
 
@@ -108,27 +108,30 @@ func TestTunnel(t *testing.T) {
 		fmt.Fprint(w, loadFixture("tunnel", "single_full"))
 	}
 
-	mux.HandleFunc("/accounts/"+testAccountID+"/cfd_tunnel/f174e90a-fafe-4643-bbbc-4a0ed4fc8415", handler)
+	mux.HandleFunc("/accounts/"+testAccountID+"/cfd_tunnel/"+testTunnelID, handler)
 
 	createdAt, _ := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z")
 	deletedAt, _ := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z")
 	want := Tunnel{
-		ID:        "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+		ID:        testTunnelID,
 		Name:      "blog",
 		CreatedAt: &createdAt,
 		DeletedAt: &deletedAt,
 		Connections: []TunnelConnection{{
 			ColoName:           "DFW",
-			ID:                 "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+			ID:                 testTunnelID,
 			IsPendingReconnect: false,
 			ClientID:           "dc6472cc-f1ae-44a0-b795-6b8a0ce29f90",
 			ClientVersion:      "2022.2.0",
 			OpenedAt:           "2021-01-25T18:22:34.317854Z",
 			OriginIP:           "198.51.100.1",
 		}},
+		TunnelType:   "cfd_tunnel",
+		Status:       "healthy",
+		RemoteConfig: true,
 	}
 
-	actual, err := client.Tunnel(context.Background(), AccountIdentifier(testAccountID), "f174e90a-fafe-4643-bbbc-4a0ed4fc8415")
+	actual, err := client.GetTunnel(context.Background(), AccountIdentifier(testAccountID), testTunnelID)
 
 	if assert.NoError(t, err) {
 		assert.Equal(t, want, actual)
@@ -150,22 +153,25 @@ func TestCreateTunnel(t *testing.T) {
 	createdAt, _ := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z")
 	deletedAt, _ := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z")
 	want := Tunnel{
-		ID:        "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+		ID:        testTunnelID,
 		Name:      "blog",
 		CreatedAt: &createdAt,
 		DeletedAt: &deletedAt,
 		Connections: []TunnelConnection{{
 			ColoName:           "DFW",
-			ID:                 "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+			ID:                 testTunnelID,
 			IsPendingReconnect: false,
 			ClientID:           "dc6472cc-f1ae-44a0-b795-6b8a0ce29f90",
 			ClientVersion:      "2022.2.0",
 			OpenedAt:           "2021-01-25T18:22:34.317854Z",
 			OriginIP:           "198.51.100.1",
 		}},
+		TunnelType:   "cfd_tunnel",
+		Status:       "healthy",
+		RemoteConfig: true,
 	}
 
-	actual, err := client.CreateTunnel(context.Background(), AccountIdentifier(testAccountID), TunnelCreateParams{Name: "blog", Secret: "notarealsecret"})
+	actual, err := client.CreateTunnel(context.Background(), AccountIdentifier(testAccountID), TunnelCreateParams{Name: "blog", Secret: "notarealsecret", ConfigSrc: "cloudflare"})
 
 	if assert.NoError(t, err) {
 		assert.Equal(t, want, actual)
@@ -183,9 +189,9 @@ func TestUpdateTunnelConfiguration(t *testing.T) {
 		fmt.Fprint(w, loadFixture("tunnel", "configuration"))
 	}
 
-	mux.HandleFunc("/accounts/"+testAccountID+"/cfd_tunnel/f174e90a-fafe-4643-bbbc-4a0ed4fc8415/configurations", handler)
+	mux.HandleFunc(fmt.Sprintf("/accounts/%s/cfd_tunnel/%s/configurations", testAccountID, testTunnelID), handler)
 	want := TunnelConfigurationResult{
-		TunnelID: "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+		TunnelID: testTunnelID,
 		Version:  5,
 		Config: TunnelConfiguration{
 			Ingress: []UnvalidatedIngressRule{
@@ -206,7 +212,7 @@ func TestUpdateTunnelConfiguration(t *testing.T) {
 		}}
 
 	actual, err := client.UpdateTunnelConfiguration(context.Background(), AccountIdentifier(testAccountID), TunnelConfigurationParams{
-		TunnelID: "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+		TunnelID: testTunnelID,
 		Config: TunnelConfiguration{
 			Ingress: []UnvalidatedIngressRule{
 				{
@@ -242,9 +248,9 @@ func TestGetTunnelConfiguration(t *testing.T) {
 		fmt.Fprint(w, loadFixture("tunnel", "configuration"))
 	}
 
-	mux.HandleFunc("/accounts/"+testAccountID+"/cfd_tunnel/f174e90a-fafe-4643-bbbc-4a0ed4fc8415/configurations", handler)
+	mux.HandleFunc(fmt.Sprintf("/accounts/%s/cfd_tunnel/%s/configurations", testAccountID, testTunnelID), handler)
 	want := TunnelConfigurationResult{
-		TunnelID: "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+		TunnelID: testTunnelID,
 		Version:  5,
 		Config: TunnelConfiguration{
 			Ingress: []UnvalidatedIngressRule{
@@ -264,7 +270,7 @@ func TestGetTunnelConfiguration(t *testing.T) {
 			},
 		}}
 
-	actual, err := client.GetTunnelConfiguration(context.Background(), AccountIdentifier(testAccountID), "f174e90a-fafe-4643-bbbc-4a0ed4fc8415")
+	actual, err := client.GetTunnelConfiguration(context.Background(), AccountIdentifier(testAccountID), testTunnelID)
 
 	if assert.NoError(t, err) {
 		assert.Equal(t, want, actual)
@@ -291,6 +297,7 @@ func TestTunnelConnections(t *testing.T) {
 				],
 				"version": "2022.2.0",
             	"arch": "linux_amd64",
+				"config_version": 15,
 				"run_at":"2009-11-10T23:00:00Z",
 				"conns": [
 					{
@@ -308,7 +315,7 @@ func TestTunnelConnections(t *testing.T) {
 		`)
 	}
 
-	mux.HandleFunc("/accounts/"+testAccountID+"/cfd_tunnel/f174e90a-fafe-4643-bbbc-4a0ed4fc8415/connections", handler)
+	mux.HandleFunc(fmt.Sprintf("/accounts/%s/cfd_tunnel/%s/connections", testAccountID, testTunnelID), handler)
 
 	runAt, _ := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z")
 	want := []Connection{
@@ -324,17 +331,18 @@ func TestTunnelConnections(t *testing.T) {
 			RunAt:   &runAt,
 			Connections: []TunnelConnection{{
 				ColoName:           "DFW",
-				ID:                 "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+				ID:                 testTunnelID,
 				IsPendingReconnect: false,
 				ClientID:           "dc6472cc-f1ae-44a0-b795-6b8a0ce29f90",
 				ClientVersion:      "2022.2.0",
 				OpenedAt:           "2021-01-25T18:22:34.317854Z",
 				OriginIP:           "198.51.100.1",
 			}},
+			ConfigVersion: 15,
 		},
 	}
 
-	actual, err := client.TunnelConnections(context.Background(), AccountIdentifier(testAccountID), "f174e90a-fafe-4643-bbbc-4a0ed4fc8415")
+	actual, err := client.ListTunnelConnections(context.Background(), AccountIdentifier(testAccountID), testTunnelID)
 
 	if assert.NoError(t, err) {
 		assert.Equal(t, want, actual)
@@ -351,9 +359,9 @@ func TestDeleteTunnel(t *testing.T) {
 		fmt.Fprint(w, loadFixture("tunnel", "single_full"))
 	}
 
-	mux.HandleFunc("/accounts/"+testAccountID+"/cfd_tunnel/f174e90a-fafe-4643-bbbc-4a0ed4fc8415", handler)
+	mux.HandleFunc(fmt.Sprintf("/accounts/%s/cfd_tunnel/%s", testAccountID, testTunnelID), handler)
 
-	err := client.DeleteTunnel(context.Background(), AccountIdentifier(testAccountID), "f174e90a-fafe-4643-bbbc-4a0ed4fc8415")
+	err := client.DeleteTunnel(context.Background(), AccountIdentifier(testAccountID), testTunnelID)
 	assert.NoError(t, err)
 }
 
@@ -367,9 +375,9 @@ func TestCleanupTunnelConnections(t *testing.T) {
 		fmt.Fprint(w, loadFixture("tunnel", "empty"))
 	}
 
-	mux.HandleFunc("/accounts/"+testAccountID+"/cfd_tunnel/f174e90a-fafe-4643-bbbc-4a0ed4fc8415/connections", handler)
+	mux.HandleFunc(fmt.Sprintf("/accounts/%s/cfd_tunnel/%s/connections", testAccountID, testTunnelID), handler)
 
-	err := client.CleanupTunnelConnections(context.Background(), AccountIdentifier(testAccountID), "f174e90a-fafe-4643-bbbc-4a0ed4fc8415")
+	err := client.CleanupTunnelConnections(context.Background(), AccountIdentifier(testAccountID), testTunnelID)
 	assert.NoError(t, err)
 }
 
@@ -383,9 +391,9 @@ func TestTunnelToken(t *testing.T) {
 		fmt.Fprint(w, loadFixture("tunnel", "token"))
 	}
 
-	mux.HandleFunc("/accounts/"+testAccountID+"/cfd_tunnel/f174e90a-fafe-4643-bbbc-4a0ed4fc8415/token", handler)
+	mux.HandleFunc(fmt.Sprintf("/accounts/%s/cfd_tunnel/%s/token", testAccountID, testTunnelID), handler)
 
-	token, err := client.TunnelToken(context.Background(), AccountIdentifier(testAccountID), "f174e90a-fafe-4643-bbbc-4a0ed4fc8415")
+	token, err := client.GetTunnelToken(context.Background(), AccountIdentifier(testAccountID), testTunnelID)
 	assert.NoError(t, err)
 	assert.Equal(t, "ZHNraGdhc2RraGFza2hqZGFza2poZGFza2poYXNrZGpoYWtzamRoa2FzZGpoa2FzamRoa2Rhc2po\na2FzamRoa2FqCg==", token)
 }