Repository: cloudflare/cloudflare-go
Pull request #2165 (2024-05-24 11:11:04)

Add ZT connectivity settings
## Description

This PR enables configuring Zero Trust connectivity settings and addresses the missing support of enabling Warp to Warp as per 3.3 of https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/private-net/warp-connector/#3-enable-cgnat-routing, plan is to extend the provider. If discussion is needed on this change I'm happy to open a new issue.

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)
- [ ] 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.
- [ ] All new and existing tests passed.
- [x] This change is using publicly documented in [cloudflare/api-schemas](https://github.com/cloudflare/api-schemas) 
      and relies on stable APIs.

[1]: https://help.github.com/articles/closing-issues-using-keywords/



Patch:
diff --git a/.changelog/2165.txt b/.changelog/2165.txt
new file mode 100644
index 000000000..d053fdc62
--- /dev/null
+++ b/.changelog/2165.txt
@@ -0,0 +1,3 @@
+```release-note:enhancement
+teams_account: Add Zero Trust connectivity settings
+```
\ No newline at end of file
diff --git a/teams_accounts.go b/teams_accounts.go
index b13ad1bf0..3472fa487 100644
--- a/teams_accounts.go
+++ b/teams_accounts.go
@@ -147,6 +147,16 @@ type TeamsLoggingSettingsResponse struct {
 	Result TeamsLoggingSettings `json:"result"`
 }
 
+type TeamsConnectivitySettings struct {
+	ICMPProxyEnabled   *bool `json:"icmp_proxy_enabled"`
+	OfframpWARPEnabled *bool `json:"offramp_warp_enabled"`
+}
+
+type TeamsAccountConnectivitySettingsResponse struct {
+	Response
+	Result TeamsConnectivitySettings `json:"result"`
+}
+
 // TeamsAccount returns teams account information with internal and external ID.
 //
 // API reference: TBA.
@@ -227,6 +237,26 @@ func (api *API) TeamsAccountLoggingConfiguration(ctx context.Context, accountID
 	return teamsConfigResponse.Result, nil
 }
 
+// TeamsAccountConnectivityConfiguration returns zero trust account connectivity settings.
+//
+// API reference: https://developers.cloudflare.com/api/operations/zero-trust-accounts-get-connectivity-settings
+func (api *API) TeamsAccountConnectivityConfiguration(ctx context.Context, accountID string) (TeamsConnectivitySettings, error) {
+	uri := fmt.Sprintf("/accounts/%s/zerotrust/connectivity_settings", accountID)
+
+	res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
+	if err != nil {
+		return TeamsConnectivitySettings{}, err
+	}
+
+	var teamsConnectivityResponse TeamsAccountConnectivitySettingsResponse
+	err = json.Unmarshal(res, &teamsConnectivityResponse)
+	if err != nil {
+		return TeamsConnectivitySettings{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
+	}
+
+	return teamsConnectivityResponse.Result, nil
+}
+
 // TeamsAccountUpdateConfiguration updates a teams account configuration.
 //
 // API reference: TBA.
@@ -286,3 +316,23 @@ func (api *API) TeamsAccountDeviceUpdateConfiguration(ctx context.Context, accou
 
 	return teamsDeviceResponse.Result, nil
 }
+
+// TeamsAccountConnectivityUpdateConfiguration updates zero trust account connectivity settings.
+//
+// API reference: https://developers.cloudflare.com/api/operations/zero-trust-accounts-patch-connectivity-settings
+func (api *API) TeamsAccountConnectivityUpdateConfiguration(ctx context.Context, accountID string, settings TeamsConnectivitySettings) (TeamsConnectivitySettings, error) {
+	uri := fmt.Sprintf("/accounts/%s/zerotrust/connectivity_settings", accountID)
+
+	res, err := api.makeRequestContext(ctx, http.MethodPut, uri, settings)
+	if err != nil {
+		return TeamsConnectivitySettings{}, err
+	}
+
+	var teamsConnectivityResponse TeamsAccountConnectivitySettingsResponse
+	err = json.Unmarshal(res, &teamsConnectivityResponse)
+	if err != nil {
+		return TeamsConnectivitySettings{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
+	}
+
+	return teamsConnectivityResponse.Result, nil
+}


Test patch:
diff --git a/teams_accounts_test.go b/teams_accounts_test.go
index cf044f6ab..5fd3686ac 100644
--- a/teams_accounts_test.go
+++ b/teams_accounts_test.go
@@ -340,3 +340,60 @@ func TestTeamsAccountUpdateDeviceConfiguration(t *testing.T) {
 		})
 	}
 }
+
+func TestTeamsAccountGetConnectivityConfiguration(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": {"icmp_proxy_enabled": false,"offramp_warp_enabled":false}
+		}`)
+	}
+
+	mux.HandleFunc("/accounts/"+testAccountID+"/zerotrust/connectivity_settings", handler)
+
+	actual, err := client.TeamsAccountConnectivityConfiguration(context.Background(), testAccountID)
+
+	if assert.NoError(t, err) {
+		assert.Equal(t, actual, TeamsConnectivitySettings{
+			ICMPProxyEnabled:   BoolPtr(false),
+			OfframpWARPEnabled: BoolPtr(false),
+		})
+	}
+}
+
+func TestTeamsAccountUpdateConnectivityConfiguration(t *testing.T) {
+	setup()
+	defer teardown()
+
+	handler := func(w http.ResponseWriter, r *http.Request) {
+		assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method)
+		w.Header().Set("content-type", "application/json")
+		fmt.Fprintf(w, `{
+			"success": true,
+			"errors": [],
+			"messages": [],
+			"result": {"icmp_proxy_enabled": true,"offramp_warp_enabled":true}
+		}`)
+	}
+
+	mux.HandleFunc("/accounts/"+testAccountID+"/zerotrust/connectivity_settings", handler)
+
+	actual, err := client.TeamsAccountConnectivityUpdateConfiguration(context.Background(), testAccountID, TeamsConnectivitySettings{
+		ICMPProxyEnabled:   BoolPtr(true),
+		OfframpWARPEnabled: BoolPtr(true),
+	})
+
+	if assert.NoError(t, err) {
+		assert.Equal(t, actual, TeamsConnectivitySettings{
+			ICMPProxyEnabled:   BoolPtr(true),
+			OfframpWARPEnabled: BoolPtr(true),
+		})
+	}
+}