Skip to content

Commit

Permalink
Merge pull request #1027 from Cyb3r-Jak3/workers_account_settings
Browse files Browse the repository at this point in the history
Add worker account settings
  • Loading branch information
jacobbednarz authored Aug 9, 2022
2 parents 461fe9e + 607456d commit c4b9df0
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/1027.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
workers-account-settings: Add in support for Workers account settings API
```
74 changes: 74 additions & 0 deletions workers_account_settings.go
Original file line number Diff line number Diff line change
@@ -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
}
67 changes: 67 additions & 0 deletions workers_account_settings_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit c4b9df0

Please sign in to comment.