-
Notifications
You must be signed in to change notification settings - Fork 541
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 #1209 from dhens/master
Support for DEX Tests
- Loading branch information
Showing
3 changed files
with
396 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 | ||
dex_test: add CRUD functionality for DEX test configurations | ||
``` |
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,173 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type DeviceDexTestData map[string]interface{} | ||
|
||
type DeviceDexTest struct { | ||
TestID string `json:"test_id"` | ||
Name string `json:"name"` | ||
Description string `json:"description,omitempty"` | ||
Interval string `json:"interval"` | ||
Enabled bool `json:"enabled"` | ||
Updated time.Time `json:"updated"` | ||
Created time.Time `json:"created"` | ||
Data *DeviceDexTestData `json:"data"` | ||
} | ||
|
||
type DeviceDexTests struct { | ||
DexTests []DeviceDexTest `json:"dex_tests"` | ||
} | ||
|
||
type DeviceDexTestResponse struct { | ||
Response | ||
Result DeviceDexTest `json:"result"` | ||
} | ||
|
||
type DeviceDexTestListResponse struct { | ||
Response | ||
Result DeviceDexTests `json:"result"` | ||
} | ||
|
||
type ListDeviceDexTestParams struct{} | ||
|
||
type CreateDeviceDexTestParams struct { | ||
TestID string `json:"network_id,omitempty"` | ||
Name string `json:"name"` | ||
Description string `json:"description,omitempty"` | ||
Interval string `json:"interval"` | ||
Enabled bool `json:"enabled"` | ||
Data *DeviceDexTestData `json:"data"` | ||
} | ||
|
||
type UpdateDeviceDexTestParams struct { | ||
TestID string `json:"network_id,omitempty"` | ||
Name string `json:"name"` | ||
Description string `json:"description,omitempty"` | ||
Interval string `json:"interval"` | ||
Enabled bool `json:"enabled"` | ||
Data *DeviceDexTestData `json:"data"` | ||
} | ||
|
||
// ListDexTests returns all Device Dex Tests for a given account. | ||
// | ||
// API reference : https://developers.cloudflare.com/api/operations/device-dex-test-details | ||
func (api *API) ListDexTests(ctx context.Context, rc *ResourceContainer, params ListDeviceDexTestParams) (DeviceDexTests, error) { | ||
if rc.Level != AccountRouteLevel { | ||
return DeviceDexTests{}, ErrRequiredAccountLevelResourceContainer | ||
} | ||
|
||
uri := fmt.Sprintf("/%s/%s/devices/dex_tests", rc.Level, rc.Identifier) | ||
|
||
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) | ||
if err != nil { | ||
return DeviceDexTests{}, err | ||
} | ||
|
||
var response DeviceDexTestListResponse | ||
err = json.Unmarshal(res, &response) | ||
if err != nil { | ||
return DeviceDexTests{}, fmt.Errorf("%s: %w", errUnmarshalError, err) | ||
} | ||
|
||
return response.Result, nil | ||
} | ||
|
||
// CreateDeviceDexTest created a new Device Dex Test | ||
// | ||
// API reference: https://developers.cloudflare.com/api/operations/device-dex-test-create-device-dex-test | ||
func (api *API) CreateDeviceDexTest(ctx context.Context, rc *ResourceContainer, params CreateDeviceDexTestParams) (DeviceDexTest, error) { | ||
if rc.Level != AccountRouteLevel { | ||
return DeviceDexTest{}, ErrRequiredAccountLevelResourceContainer | ||
} | ||
|
||
uri := fmt.Sprintf("/%s/%s/devices/dex_tests", rc.Level, rc.Identifier) | ||
|
||
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params) | ||
if err != nil { | ||
return DeviceDexTest{}, err | ||
} | ||
|
||
var deviceDexTestResponse DeviceDexTestResponse | ||
if err := json.Unmarshal(res, &deviceDexTestResponse); err != nil { | ||
return DeviceDexTest{}, fmt.Errorf("%s: %w\n\nres: %s", errUnmarshalError, err, string(res)) | ||
} | ||
|
||
return deviceDexTestResponse.Result, err | ||
} | ||
|
||
// UpdateDeviceDexTest Updates a Device Dex Test. | ||
// | ||
// API reference: https://developers.cloudflare.com/api/operations/device-dex-test-update-device-dex-test | ||
func (api *API) UpdateDeviceDexTest(ctx context.Context, rc *ResourceContainer, params UpdateDeviceDexTestParams) (DeviceManagedNetwork, error) { | ||
if rc.Level != AccountRouteLevel { | ||
return DeviceManagedNetwork{}, ErrRequiredAccountLevelResourceContainer | ||
} | ||
|
||
uri := fmt.Sprintf("/%s/%s/devices/dex_tests/%s", rc.Level, rc.Identifier, params.TestID) | ||
|
||
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params) | ||
if err != nil { | ||
return DeviceManagedNetwork{}, err | ||
} | ||
|
||
var deviceManagedNetworksResponse DeviceManagedNetworkResponse | ||
|
||
if err := json.Unmarshal(res, &deviceManagedNetworksResponse); err != nil { | ||
return DeviceManagedNetwork{}, fmt.Errorf("%s: %w", errUnmarshalError, err) | ||
} | ||
|
||
return deviceManagedNetworksResponse.Result, err | ||
} | ||
|
||
// GetDeviceDexTest gets a single Device Dex Test. | ||
// | ||
// API reference: https://developers.cloudflare.com/api/operations/device-dex-test-get-device-dex-test | ||
func (api *API) GetDeviceDexTest(ctx context.Context, rc *ResourceContainer, testID string) (DeviceDexTest, error) { | ||
if rc.Level != AccountRouteLevel { | ||
return DeviceDexTest{}, ErrRequiredAccountLevelResourceContainer | ||
} | ||
|
||
uri := fmt.Sprintf("/%s/%s/devices/dex_tests/%s", rc.Level, rc.Identifier, testID) | ||
|
||
deviceDexTestResponse := DeviceDexTestResponse{} | ||
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) | ||
if err != nil { | ||
return DeviceDexTest{}, err | ||
} | ||
|
||
if err := json.Unmarshal(res, &deviceDexTestResponse); err != nil { | ||
return DeviceDexTest{}, fmt.Errorf("%s: %w", errUnmarshalError, err) | ||
} | ||
|
||
return deviceDexTestResponse.Result, err | ||
} | ||
|
||
// DeleteDexTest deletes a Device Dex Test. | ||
// | ||
// API reference: https://developers.cloudflare.com/api/operations/device-dex-test-delete-device-dex-test | ||
func (api *API) DeleteDexTest(ctx context.Context, rc *ResourceContainer, testID string) (DeviceDexTests, error) { | ||
if rc.Level != AccountRouteLevel { | ||
return DeviceDexTests{}, ErrRequiredAccountLevelResourceContainer | ||
} | ||
|
||
uri := fmt.Sprintf("/%s/%s/devices/dex_tests/%s", rc.Level, rc.Identifier, testID) | ||
|
||
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil) | ||
if err != nil { | ||
return DeviceDexTests{}, err | ||
} | ||
|
||
var response DeviceDexTestListResponse | ||
if err := json.Unmarshal(res, &response); err != nil { | ||
return DeviceDexTests{}, fmt.Errorf("%s: %w", errUnmarshalError, err) | ||
} | ||
|
||
return response.Result, err | ||
} |
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,220 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const testID = "f174e90a-fafe-4643-bbbc-4a0ed4fc8415" | ||
|
||
var dexTimestamp, _ = time.Parse(time.RFC3339, "2023-01-30T19:59:44.401278Z") | ||
|
||
func TestGetDeviceDexTests(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": { | ||
"dex_tests": [ | ||
{ | ||
"test_id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415", | ||
"name": "http test dash", | ||
"description": "dex test description", | ||
"interval": "0h30m0s", | ||
"enabled": true, | ||
"data": { | ||
"host": "https://dash.cloudflare.com", | ||
"kind": "http", | ||
"method": "GET" | ||
}, | ||
"updated": "2023-01-30T19:59:44.401278Z", | ||
"created": "2023-01-30T19:59:44.401278Z" | ||
} | ||
] | ||
} | ||
}`) | ||
} | ||
|
||
dexTest := []DeviceDexTest{{ | ||
TestID: testID, | ||
Name: "http test dash", | ||
Description: "dex test description", | ||
Interval: "0h30m0s", | ||
Enabled: true, | ||
Data: &DeviceDexTestData{ | ||
"kind": "http", | ||
"method": "GET", | ||
"host": "https://dash.cloudflare.com", | ||
}, | ||
Updated: dexTimestamp, | ||
Created: dexTimestamp, | ||
}} | ||
|
||
want := DeviceDexTests{ | ||
DexTests: dexTest, | ||
} | ||
|
||
mux.HandleFunc("/accounts/"+testAccountID+"/devices/dex_tests", handler) | ||
|
||
actual, err := client.ListDexTests(context.Background(), AccountIdentifier(testAccountID), ListDeviceDexTestParams{}) | ||
|
||
if assert.NoError(t, err) { | ||
assert.Equal(t, want, actual) | ||
} | ||
} | ||
|
||
func TestDeviceDexTest(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": { | ||
"test_id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415", | ||
"name": "http test dash", | ||
"description": "dex test description", | ||
"interval": "0h30m0s", | ||
"enabled": true, | ||
"data": { | ||
"host": "https://dash.cloudflare.com", | ||
"kind": "http", | ||
"method": "GET" | ||
}, | ||
"updated": "2023-01-30T19:59:44.401278Z", | ||
"created": "2023-01-30T19:59:44.401278Z" | ||
} | ||
}`) | ||
} | ||
|
||
want := DeviceDexTest{ | ||
TestID: testID, | ||
Name: "http test dash", | ||
Description: "dex test description", | ||
Interval: "0h30m0s", | ||
Enabled: true, | ||
Data: &DeviceDexTestData{ | ||
"kind": "http", | ||
"method": "GET", | ||
"host": "https://dash.cloudflare.com", | ||
}, | ||
Updated: dexTimestamp, | ||
Created: dexTimestamp, | ||
} | ||
|
||
mux.HandleFunc("/accounts/"+testAccountID+"/devices/dex_tests/"+testID, handler) | ||
|
||
actual, err := client.GetDeviceDexTest(context.Background(), AccountIdentifier(testAccountID), testID) | ||
|
||
if assert.NoError(t, err) { | ||
assert.Equal(t, want, actual) | ||
} | ||
} | ||
|
||
func TestCreateDeviceDexTest(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, http.MethodPost, r.Method, "Expected method 'POST', got %s", r.Method) | ||
w.Header().Set("content-type", "application/json") | ||
fmt.Fprintf(w, `{ | ||
"success": true, | ||
"errors": [], | ||
"messages": [], | ||
"result": { | ||
"test_id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415", | ||
"name": "http test dash", | ||
"description": "dex test description", | ||
"interval": "0h30m0s", | ||
"enabled": true, | ||
"data": { | ||
"host": "https://dash.cloudflare.com", | ||
"kind": "http", | ||
"method": "GET" | ||
}, | ||
"updated": "2023-01-30T19:59:44.401278Z", | ||
"created": "2023-01-30T19:59:44.401278Z" | ||
} | ||
}`) | ||
} | ||
|
||
want := DeviceDexTest{ | ||
TestID: testID, | ||
Name: "http test dash", | ||
Description: "dex test description", | ||
Interval: "0h30m0s", | ||
Enabled: true, | ||
Data: &DeviceDexTestData{ | ||
"kind": "http", | ||
"method": "GET", | ||
"host": "https://dash.cloudflare.com", | ||
}, | ||
Updated: dexTimestamp, | ||
Created: dexTimestamp, | ||
} | ||
|
||
mux.HandleFunc("/accounts/"+testAccountID+"/devices/dex_tests", handler) | ||
|
||
actual, err := client.CreateDeviceDexTest(context.Background(), AccountIdentifier(testAccountID), CreateDeviceDexTestParams{ | ||
TestID: testID, | ||
Name: "http test dash", | ||
Description: "dex test description", | ||
Interval: "0h30m0s", | ||
Enabled: true, | ||
Data: &DeviceDexTestData{ | ||
"kind": "http", | ||
"method": "GET", | ||
"host": "https://dash.cloudflare.com", | ||
}, | ||
}) | ||
|
||
if assert.NoError(t, err) { | ||
assert.Equal(t, want, actual) | ||
} | ||
} | ||
|
||
func TestDeleteDeviceDexTest(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, http.MethodDelete, r.Method, "Expected method 'DELETE', got %s", r.Method) | ||
w.Header().Set("content-type", "application/json") | ||
fmt.Fprintf(w, `{ | ||
"success": true, | ||
"errors": [], | ||
"messages": [], | ||
"result": { | ||
"dex_tests": [] | ||
} | ||
}`) | ||
} | ||
|
||
want := DeviceDexTests{ | ||
DexTests: []DeviceDexTest{}, | ||
} | ||
|
||
mux.HandleFunc("/accounts/"+testAccountID+"/devices/dex_tests/"+testID, handler) | ||
|
||
actual, err := client.DeleteDexTest(context.Background(), AccountIdentifier(testAccountID), testID) | ||
|
||
if assert.NoError(t, err) { | ||
assert.Equal(t, want, actual) | ||
} | ||
} |