Repository: cloudflare/cloudflare-go
Pull request #1027 (2022-08-08 23:53:30)

Add worker account settings
## Description

Adds support for workers account settings: https://api.cloudflare.com/#worker-account-settings-properties

## Has your change been tested?

Created units tests and they 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)
- [ ] 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/1027.txt b/.changelog/1027.txt
new file mode 100644
index 000000000..4661228cd
--- /dev/null
+++ b/.changelog/1027.txt
@@ -0,0 +1,3 @@
+```release-note:enhancement
+workers-account-settings: Add in support for Workers account settings API
+```
\ No newline at end of file
diff --git a/workers_account_settings.go b/workers_account_settings.go
new file mode 100644
index 000000000..942db6dd0
--- /dev/null
+++ b/workers_account_settings.go
@@ -0,0 +1,74 @@
+package cloudflare
+
+import (
+	"context"
+	"encoding/json"
+	"fmt"
+	"net/http"
+)
+
+type WorkersAccountSettings struct {
+	DefaultUsageModel string `json:"default_usage_model,omitempty"`
+	GreenCompute      bool   `json:"green_compute,omitempty"`
+}
+
+type CreateWorkersAccountSettingsParameters struct {
+	DefaultUsageModel string `json:"default_usage_model,omitempty"`
+	GreenCompute      bool   `json:"green_compute,omitempty"`
+}
+
+type CreateWorkersAccountSettingsResponse struct {
+	Response
+	Result WorkersAccountSettings
+}
+
+type WorkersAccountSettingsParameters struct{}
+
+type WorkersAccountSettingsResponse struct {
+	Response
+	Result WorkersAccountSettings
+}
+
+// CreateWorkersAccountSettings sets the account settings for Workers.
+//
+// API reference: https://api.cloudflare.com/#worker-account-settings-create-worker-account-settings
+func (api *API) CreateWorkersAccountSettings(ctx context.Context, rc *ResourceContainer, params CreateWorkersAccountSettingsParameters) (WorkersAccountSettings, error) {
+	if rc.Identifier == "" {
+		return WorkersAccountSettings{}, ErrMissingAccountID
+	}
+
+	uri := fmt.Sprintf("/accounts/%s/workers/account-settings", rc.Identifier)
+	res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
+	if err != nil {
+		return WorkersAccountSettings{}, err
+	}
+
+	var workersAccountSettingsResponse CreateWorkersAccountSettingsResponse
+	if err := json.Unmarshal(res, &workersAccountSettingsResponse); err != nil {
+		return WorkersAccountSettings{}, err
+	}
+
+	return workersAccountSettingsResponse.Result, nil
+}
+
+// WorkersAccountSettings returns the current account settings for Workers.
+//
+// API reference: https://api.cloudflare.com/#worker-account-settings-fetch-worker-account-settings
+func (api *API) WorkersAccountSettings(ctx context.Context, rc *ResourceContainer, params WorkersAccountSettingsParameters) (WorkersAccountSettings, error) {
+	if rc.Identifier == "" {
+		return WorkersAccountSettings{}, ErrMissingAccountID
+	}
+
+	uri := fmt.Sprintf("/accounts/%s/workers/account-settings", rc.Identifier)
+	res, err := api.makeRequestContext(ctx, http.MethodGet, uri, params)
+	if err != nil {
+		return WorkersAccountSettings{}, err
+	}
+
+	var workersAccountSettingsResponse CreateWorkersAccountSettingsResponse
+	if err := json.Unmarshal(res, &workersAccountSettingsResponse); err != nil {
+		return WorkersAccountSettings{}, err
+	}
+
+	return workersAccountSettingsResponse.Result, nil
+}


Test patch:
diff --git a/workers_account_settings_test.go b/workers_account_settings_test.go
new file mode 100644
index 000000000..0bdd35fd3
--- /dev/null
+++ b/workers_account_settings_test.go
@@ -0,0 +1,67 @@
+package cloudflare
+
+import (
+	"context"
+	"fmt"
+	"net/http"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestWorkersAccountSettings_CreateAccountSettings(t *testing.T) {
+	setup()
+	defer teardown()
+
+	mux.HandleFunc(fmt.Sprintf("/accounts/%s/workers/account-settings", testAccountID), 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": {
+    "default_usage_model": "unbound",
+	"green_compute": false
+  }
+}`)
+	})
+	res, err := client.CreateWorkersAccountSettings(context.Background(), AccountIdentifier(testAccountID), CreateWorkersAccountSettingsParameters{DefaultUsageModel: "unbound", GreenCompute: false})
+	want := WorkersAccountSettings{
+		DefaultUsageModel: "unbound",
+		GreenCompute:      false,
+	}
+
+	if assert.NoError(t, err) {
+		assert.Equal(t, want, res)
+	}
+}
+
+func TestWorkersAccountSettings_GetAccountSettings(t *testing.T) {
+	setup()
+	defer teardown()
+
+	mux.HandleFunc(fmt.Sprintf("/accounts/%s/workers/account-settings", testAccountID), 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": {
+    "default_usage_model": "unbound",
+	"green_compute": true
+  }
+}`)
+	})
+
+	res, err := client.WorkersAccountSettings(context.Background(), AccountIdentifier(testAccountID), WorkersAccountSettingsParameters{})
+	want := WorkersAccountSettings{
+		DefaultUsageModel: "unbound",
+		GreenCompute:      true,
+	}
+
+	if assert.NoError(t, err) {
+		assert.Equal(t, want, res)
+	}
+}