-
Notifications
You must be signed in to change notification settings - Fork 597
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1027 from Cyb3r-Jak3/workers_account_settings
Add worker account settings
- Loading branch information
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |