From 432ff823340ed05cd9060b871aeb230de507b538 Mon Sep 17 00:00:00 2001 From: Suhrit Rimal Date: Thu, 15 Dec 2022 10:50:49 -0500 Subject: [PATCH 1/5] Support for Managed Networks --- devices_managed_networks.go | 127 +++++++++++++++++++++ devices_managed_networks_test.go | 188 +++++++++++++++++++++++++++++++ 2 files changed, 315 insertions(+) create mode 100644 devices_managed_networks.go create mode 100644 devices_managed_networks_test.go diff --git a/devices_managed_networks.go b/devices_managed_networks.go new file mode 100644 index 00000000000..c8f065ccd0c --- /dev/null +++ b/devices_managed_networks.go @@ -0,0 +1,127 @@ +package cloudflare + +import ( + "context" + "encoding/json" + "fmt" + "net/http" +) + +type Config struct { + TlsSockAddr string `json:"tls_sockaddr,omitempty"` + Sha256 string `json:"sha256,omitempty"` +} + +type DeviceManagedNetworks struct { + NetworkID string `json:"network_id,omitempty"` + Type string `json:"type"` + Name string `json:"name"` + Config *Config `json:"config"` +} + +type DeviceManagedNetworksResponse struct { + Response + Result DeviceManagedNetworks `json:"result"` +} + +type DeviceManagedNetworksListResponse struct { + Response + Result []DeviceManagedNetworks `json:"result"` +} + +// ListTeamsDevice returns all devices for a given account. +// +// API reference : https://api.cloudflare.com/#device-managed-networks-list-device-managed-networks +func (api *API) ListManagedNetworks(ctx context.Context, accountID string) ([]DeviceManagedNetworks, error) { + uri := fmt.Sprintf("/%s/%s/devices/networks", AccountRouteRoot, accountID) + + res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + if err != nil { + return []DeviceManagedNetworks{}, err + } + + var response DeviceManagedNetworksListResponse + err = json.Unmarshal(res, &response) + if err != nil { + return []DeviceManagedNetworks{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + + return response.Result, nil +} + +// CreateDeviceManagedNetwork creates a new Device Managed Network. +// +// API reference: https://api.cloudflare.com/#device-managed-networks-create-device-managed-network +func (api *API) CreateDeviceManagedNetwork(ctx context.Context, accountID string, req DeviceManagedNetworks) (DeviceManagedNetworks, error) { + uri := fmt.Sprintf("/%s/%s/devices/networks", AccountRouteRoot, accountID) + + res, err := api.makeRequestContext(ctx, http.MethodPost, uri, req) + if err != nil { + return DeviceManagedNetworks{}, err + } + + var deviceManagedNetworksResponse DeviceManagedNetworksResponse + if err := json.Unmarshal(res, &deviceManagedNetworksResponse); err != nil { + return DeviceManagedNetworks{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + + return deviceManagedNetworksResponse.Result, err +} + +// UpdateDeviceManagedNetwork Update a Device Managed Network. +// +// API reference: https://api.cloudflare.com/#device-managed-networks-update-device-managed-network +func (api *API) UpdateDeviceManagedNetwork(ctx context.Context, accountID string, networkID string, req DeviceManagedNetworks) (DeviceManagedNetworks, error) { + uri := fmt.Sprintf("/%s/%s/devices/networks/%s", AccountRouteRoot, accountID, networkID) + + res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, req) + if err != nil { + return DeviceManagedNetworks{}, err + } + + var deviceManagedNetworksResponse DeviceManagedNetworksResponse + + if err := json.Unmarshal(res, &deviceManagedNetworksResponse); err != nil { + return DeviceManagedNetworks{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + + return deviceManagedNetworksResponse.Result, err +} + +// GetDeviceManagedNetwork gets a single Managed Network. +// +// API reference: https://api.cloudflare.com/#device-managed-networks-device-managed-network-details +func (api *API) GetDeviceManagedNetwork(ctx context.Context, accountID string, networkID string) (DeviceManagedNetworks, error) { + uri := fmt.Sprintf("/%s/%s/devices/networks/%s", AccountRouteRoot, accountID, networkID) + + deviceManagedNetworksResponse := DeviceManagedNetworksResponse{} + res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + if err != nil { + return DeviceManagedNetworks{}, err + } + + if err := json.Unmarshal(res, &deviceManagedNetworksResponse); err != nil { + return DeviceManagedNetworks{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + + return deviceManagedNetworksResponse.Result, err +} + +// DeleteManagedNetworks deletes a Device Managed Network. Returns the remaining device managed networks for the account. +// +// API reference: https://api.cloudflare.com/#device-managed-networks-delete-device-managed-network +func (api *API) DeleteManagedNetworks(ctx context.Context, accountID, networkID string) ([]DeviceManagedNetworks, error) { + uri := fmt.Sprintf("/%s/%s/devices/networks/%s", AccountRouteRoot, accountID, networkID) + + res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil) + if err != nil { + return []DeviceManagedNetworks{}, err + } + + var response DeviceManagedNetworksListResponse + if err := json.Unmarshal(res, &response); err != nil { + return []DeviceManagedNetworks{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + + return response.Result, err +} diff --git a/devices_managed_networks_test.go b/devices_managed_networks_test.go new file mode 100644 index 00000000000..0fc99cc4d8d --- /dev/null +++ b/devices_managed_networks_test.go @@ -0,0 +1,188 @@ +package cloudflare + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetDeviceManagedNetworks(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": [ + { + "network_id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415", + "type": "tls", + "name": "managed-network-1", + "config": { + "tls_sockaddr": "foobar:1234", + "sha256": "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c" + } + } + ] + }`) + } + + want := []DeviceManagedNetworks{{ + NetworkID: "f174e90a-fafe-4643-bbbc-4a0ed4fc8415", + Type: "tls", + Name: "managed-network-1", + Config: &Config{ + TlsSockAddr: "foobar:1234", + Sha256: "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c", + }, + }} + + mux.HandleFunc("/accounts/"+testAccountID+"/devices/networks", handler) + + actual, err := client.ListManagedNetworks(context.Background(), testAccountID) + + if assert.NoError(t, err) { + assert.Equal(t, want, actual) + } +} + +func TestDeviceManagedNetwork(t *testing.T) { + setup() + defer teardown() + + id := "f174e90a-fafe-4643-bbbc-4a0ed4fc8415" + 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": + { + "network_id": "%s", + "type": "tls", + "name": "managed-network-1", + "config": { + "tls_sockaddr": "foobar:1234", + "sha256": "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c" + } + } + }`, id) + } + + want := DeviceManagedNetworks{ + NetworkID: id, + Type: "tls", + Name: "managed-network-1", + Config: &Config{ + TlsSockAddr: "foobar:1234", + Sha256: "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c", + }, + } + + mux.HandleFunc("/accounts/"+testAccountID+"/devices/networks/"+id, handler) + + actual, err := client.GetDeviceManagedNetwork(context.Background(), testAccountID, id) + + if assert.NoError(t, err) { + assert.Equal(t, want, actual) + } +} + +func TestCreateDeviceManagedNetwork(t *testing.T) { + setup() + defer teardown() + + id := "f174e90a-fafe-4643-bbbc-4a0ed4fc8415" + 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": + { + "network_id": "%s", + "type": "tls", + "name": "managed-network-1", + "config": { + "tls_sockaddr": "foobar:1234", + "sha256": "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c" + } + } + }`, id) + } + + want := DeviceManagedNetworks{ + NetworkID: id, + Type: "tls", + Name: "managed-network-1", + Config: &Config{ + TlsSockAddr: "foobar:1234", + Sha256: "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c", + }, + } + + mux.HandleFunc("/accounts/"+testAccountID+"/devices/networks", handler) + + actual, err := client.CreateDeviceManagedNetwork(context.Background(), testAccountID, want) + + if assert.NoError(t, err) { + assert.Equal(t, want, actual) + } +} + +func TestDeleteDeviceManagedNetwork(t *testing.T) { + setup() + defer teardown() + + id := "480f4f69-1a28-4fdd-9240-1ed29f0ac1db" + 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": [ + { + "network_id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415", + "type": "tls", + "name": "managed-network-1", + "config": { + "tls_sockaddr": "foobar:1234", + "sha256": "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c" + } + } + ] + }`) + } + + want := []DeviceManagedNetworks{{ + NetworkID: "f174e90a-fafe-4643-bbbc-4a0ed4fc8415", + Type: "tls", + Name: "managed-network-1", + Config: &Config{ + TlsSockAddr: "foobar:1234", + Sha256: "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c", + }, + }} + + mux.HandleFunc("/accounts/"+testAccountID+"/devices/networks/480f4f69-1a28-4fdd-9240-1ed29f0ac1db", handler) + + actual, err := client.DeleteManagedNetworks(context.Background(), testAccountID, id) + + if assert.NoError(t, err) { + assert.Equal(t, want, actual) + } + +} From 24cd9dcedbb8f4c74b7d76b62473dcae28cfa267 Mon Sep 17 00:00:00 2001 From: Suhrit Rimal Date: Thu, 15 Dec 2022 10:56:49 -0500 Subject: [PATCH 2/5] adding changelog for 1148 --- .changelog/1148.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/1148.txt diff --git a/.changelog/1148.txt b/.changelog/1148.txt new file mode 100644 index 00000000000..55149599e01 --- /dev/null +++ b/.changelog/1148.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +managed_networks: add CRUD functionality for managednetworks +``` \ No newline at end of file From 42eb64632fa5c60ffe33048526045bb48a40259f Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Mon, 19 Dec 2022 12:10:21 +1100 Subject: [PATCH 3/5] convert to newer method conventions --- devices_managed_networks.go | 107 ++++++++++++++++++++++++------------ 1 file changed, 72 insertions(+), 35 deletions(-) diff --git a/devices_managed_networks.go b/devices_managed_networks.go index c8f065ccd0c..1f6129a6a90 100644 --- a/devices_managed_networks.go +++ b/devices_managed_networks.go @@ -12,38 +12,59 @@ type Config struct { Sha256 string `json:"sha256,omitempty"` } -type DeviceManagedNetworks struct { +type DeviceManagedNetwork struct { NetworkID string `json:"network_id,omitempty"` Type string `json:"type"` Name string `json:"name"` Config *Config `json:"config"` } -type DeviceManagedNetworksResponse struct { +type DeviceManagedNetworkResponse struct { Response - Result DeviceManagedNetworks `json:"result"` + Result DeviceManagedNetwork `json:"result"` } -type DeviceManagedNetworksListResponse struct { +type DeviceManagedNetworkListResponse struct { Response - Result []DeviceManagedNetworks `json:"result"` + Result []DeviceManagedNetwork `json:"result"` } -// ListTeamsDevice returns all devices for a given account. +type ListDeviceManagedNetworksParams struct{} + +type CreateDeviceManagedNetworkParams struct { + NetworkID string `json:"network_id,omitempty"` + Type string `json:"type"` + Name string `json:"name"` + Config *Config `json:"config"` +} + +type UpdateDeviceManagedNetworkParams struct { + NetworkID string `json:"network_id,omitempty"` + Type string `json:"type"` + Name string `json:"name"` + Config *Config `json:"config"` +} + +// ListDeviceManagedNetwork returns all Device Managed Networks for a given +// account. // // API reference : https://api.cloudflare.com/#device-managed-networks-list-device-managed-networks -func (api *API) ListManagedNetworks(ctx context.Context, accountID string) ([]DeviceManagedNetworks, error) { - uri := fmt.Sprintf("/%s/%s/devices/networks", AccountRouteRoot, accountID) +func (api *API) ListDeviceManagedNetworks(ctx context.Context, rc *ResourceContainer, params ListDeviceManagedNetworksParams) ([]DeviceManagedNetwork, error) { + if rc.Level != AccountRouteLevel { + return []DeviceManagedNetwork{}, ErrRequiredAccountLevelResourceContainer + } + + uri := fmt.Sprintf("/%s/%s/devices/networks", rc.Level, rc.Identifier) res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) if err != nil { - return []DeviceManagedNetworks{}, err + return []DeviceManagedNetwork{}, err } - var response DeviceManagedNetworksListResponse + var response DeviceManagedNetworkListResponse err = json.Unmarshal(res, &response) if err != nil { - return []DeviceManagedNetworks{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + return []DeviceManagedNetwork{}, fmt.Errorf("%s: %w", errUnmarshalError, err) } return response.Result, nil @@ -52,17 +73,21 @@ func (api *API) ListManagedNetworks(ctx context.Context, accountID string) ([]De // CreateDeviceManagedNetwork creates a new Device Managed Network. // // API reference: https://api.cloudflare.com/#device-managed-networks-create-device-managed-network -func (api *API) CreateDeviceManagedNetwork(ctx context.Context, accountID string, req DeviceManagedNetworks) (DeviceManagedNetworks, error) { - uri := fmt.Sprintf("/%s/%s/devices/networks", AccountRouteRoot, accountID) +func (api *API) CreateDeviceManagedNetwork(ctx context.Context, rc *ResourceContainer, params CreateDeviceManagedNetworkParams) (DeviceManagedNetwork, error) { + if rc.Level != AccountRouteLevel { + return DeviceManagedNetwork{}, ErrRequiredAccountLevelResourceContainer + } + + uri := fmt.Sprintf("/%s/%s/devices/networks", rc.Level, rc.Identifier) - res, err := api.makeRequestContext(ctx, http.MethodPost, uri, req) + res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params) if err != nil { - return DeviceManagedNetworks{}, err + return DeviceManagedNetwork{}, err } - var deviceManagedNetworksResponse DeviceManagedNetworksResponse + var deviceManagedNetworksResponse DeviceManagedNetworkResponse if err := json.Unmarshal(res, &deviceManagedNetworksResponse); err != nil { - return DeviceManagedNetworks{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + return DeviceManagedNetwork{}, fmt.Errorf("%s: %w", errUnmarshalError, err) } return deviceManagedNetworksResponse.Result, err @@ -71,56 +96,68 @@ func (api *API) CreateDeviceManagedNetwork(ctx context.Context, accountID string // UpdateDeviceManagedNetwork Update a Device Managed Network. // // API reference: https://api.cloudflare.com/#device-managed-networks-update-device-managed-network -func (api *API) UpdateDeviceManagedNetwork(ctx context.Context, accountID string, networkID string, req DeviceManagedNetworks) (DeviceManagedNetworks, error) { - uri := fmt.Sprintf("/%s/%s/devices/networks/%s", AccountRouteRoot, accountID, networkID) +func (api *API) UpdateDeviceManagedNetwork(ctx context.Context, rc *ResourceContainer, params UpdateDeviceManagedNetworkParams) (DeviceManagedNetwork, error) { + if rc.Level != AccountRouteLevel { + return DeviceManagedNetwork{}, ErrRequiredAccountLevelResourceContainer + } - res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, req) + uri := fmt.Sprintf("/%s/%s/devices/networks/%s", rc.Level, rc.Identifier, params.NetworkID) + + res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, params) if err != nil { - return DeviceManagedNetworks{}, err + return DeviceManagedNetwork{}, err } - var deviceManagedNetworksResponse DeviceManagedNetworksResponse + var deviceManagedNetworksResponse DeviceManagedNetworkResponse if err := json.Unmarshal(res, &deviceManagedNetworksResponse); err != nil { - return DeviceManagedNetworks{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + return DeviceManagedNetwork{}, fmt.Errorf("%s: %w", errUnmarshalError, err) } return deviceManagedNetworksResponse.Result, err } -// GetDeviceManagedNetwork gets a single Managed Network. +// GetDeviceManagedNetwork gets a single Device Managed Network. // // API reference: https://api.cloudflare.com/#device-managed-networks-device-managed-network-details -func (api *API) GetDeviceManagedNetwork(ctx context.Context, accountID string, networkID string) (DeviceManagedNetworks, error) { - uri := fmt.Sprintf("/%s/%s/devices/networks/%s", AccountRouteRoot, accountID, networkID) +func (api *API) GetDeviceManagedNetwork(ctx context.Context, rc *ResourceContainer, networkID string) (DeviceManagedNetwork, error) { + if rc.Level != AccountRouteLevel { + return DeviceManagedNetwork{}, ErrRequiredAccountLevelResourceContainer + } + + uri := fmt.Sprintf("/%s/%s/devices/networks/%s", rc.Level, rc.Identifier, networkID) - deviceManagedNetworksResponse := DeviceManagedNetworksResponse{} + deviceManagedNetworksResponse := DeviceManagedNetworkResponse{} res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) if err != nil { - return DeviceManagedNetworks{}, err + return DeviceManagedNetwork{}, err } if err := json.Unmarshal(res, &deviceManagedNetworksResponse); err != nil { - return DeviceManagedNetworks{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + return DeviceManagedNetwork{}, fmt.Errorf("%s: %w", errUnmarshalError, err) } return deviceManagedNetworksResponse.Result, err } -// DeleteManagedNetworks deletes a Device Managed Network. Returns the remaining device managed networks for the account. +// DeleteManagedNetworks deletes a Device Managed Network. // // API reference: https://api.cloudflare.com/#device-managed-networks-delete-device-managed-network -func (api *API) DeleteManagedNetworks(ctx context.Context, accountID, networkID string) ([]DeviceManagedNetworks, error) { - uri := fmt.Sprintf("/%s/%s/devices/networks/%s", AccountRouteRoot, accountID, networkID) +func (api *API) DeleteManagedNetworks(ctx context.Context, rc *ResourceContainer, networkID string) ([]DeviceManagedNetwork, error) { + if rc.Level != AccountRouteLevel { + return []DeviceManagedNetwork{}, ErrRequiredAccountLevelResourceContainer + } + + uri := fmt.Sprintf("/%s/%s/devices/networks/%s", rc.Level, rc.Identifier, networkID) res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil) if err != nil { - return []DeviceManagedNetworks{}, err + return []DeviceManagedNetwork{}, err } - var response DeviceManagedNetworksListResponse + var response DeviceManagedNetworkListResponse if err := json.Unmarshal(res, &response); err != nil { - return []DeviceManagedNetworks{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + return []DeviceManagedNetwork{}, fmt.Errorf("%s: %w", errUnmarshalError, err) } return response.Result, err From 8f4c0301b1b4e823c0b6c7c89835aed7e8d2173b Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Mon, 19 Dec 2022 12:10:54 +1100 Subject: [PATCH 4/5] update tests to match method convention updates --- devices_managed_networks_test.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/devices_managed_networks_test.go b/devices_managed_networks_test.go index 0fc99cc4d8d..fed57837385 100644 --- a/devices_managed_networks_test.go +++ b/devices_managed_networks_test.go @@ -34,7 +34,7 @@ func TestGetDeviceManagedNetworks(t *testing.T) { }`) } - want := []DeviceManagedNetworks{{ + want := []DeviceManagedNetwork{{ NetworkID: "f174e90a-fafe-4643-bbbc-4a0ed4fc8415", Type: "tls", Name: "managed-network-1", @@ -46,7 +46,7 @@ func TestGetDeviceManagedNetworks(t *testing.T) { mux.HandleFunc("/accounts/"+testAccountID+"/devices/networks", handler) - actual, err := client.ListManagedNetworks(context.Background(), testAccountID) + actual, err := client.ListDeviceManagedNetworks(context.Background(), AccountIdentifier(testAccountID), ListDeviceManagedNetworksParams{}) if assert.NoError(t, err) { assert.Equal(t, want, actual) @@ -65,7 +65,7 @@ func TestDeviceManagedNetwork(t *testing.T) { "success": true, "errors": [], "messages": [], - "result": + "result": { "network_id": "%s", "type": "tls", @@ -78,7 +78,7 @@ func TestDeviceManagedNetwork(t *testing.T) { }`, id) } - want := DeviceManagedNetworks{ + want := DeviceManagedNetwork{ NetworkID: id, Type: "tls", Name: "managed-network-1", @@ -90,7 +90,7 @@ func TestDeviceManagedNetwork(t *testing.T) { mux.HandleFunc("/accounts/"+testAccountID+"/devices/networks/"+id, handler) - actual, err := client.GetDeviceManagedNetwork(context.Background(), testAccountID, id) + actual, err := client.GetDeviceManagedNetwork(context.Background(), AccountIdentifier(testAccountID), id) if assert.NoError(t, err) { assert.Equal(t, want, actual) @@ -109,7 +109,7 @@ func TestCreateDeviceManagedNetwork(t *testing.T) { "success": true, "errors": [], "messages": [], - "result": + "result": { "network_id": "%s", "type": "tls", @@ -122,7 +122,7 @@ func TestCreateDeviceManagedNetwork(t *testing.T) { }`, id) } - want := DeviceManagedNetworks{ + want := DeviceManagedNetwork{ NetworkID: id, Type: "tls", Name: "managed-network-1", @@ -134,7 +134,15 @@ func TestCreateDeviceManagedNetwork(t *testing.T) { mux.HandleFunc("/accounts/"+testAccountID+"/devices/networks", handler) - actual, err := client.CreateDeviceManagedNetwork(context.Background(), testAccountID, want) + actual, err := client.CreateDeviceManagedNetwork(context.Background(), AccountIdentifier(testAccountID), CreateDeviceManagedNetworkParams{ + NetworkID: id, + Type: "tls", + Name: "managed-network-1", + Config: &Config{ + TlsSockAddr: "foobar:1234", + Sha256: "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c", + }, + }) if assert.NoError(t, err) { assert.Equal(t, want, actual) @@ -167,7 +175,7 @@ func TestDeleteDeviceManagedNetwork(t *testing.T) { }`) } - want := []DeviceManagedNetworks{{ + want := []DeviceManagedNetwork{{ NetworkID: "f174e90a-fafe-4643-bbbc-4a0ed4fc8415", Type: "tls", Name: "managed-network-1", @@ -179,7 +187,7 @@ func TestDeleteDeviceManagedNetwork(t *testing.T) { mux.HandleFunc("/accounts/"+testAccountID+"/devices/networks/480f4f69-1a28-4fdd-9240-1ed29f0ac1db", handler) - actual, err := client.DeleteManagedNetworks(context.Background(), testAccountID, id) + actual, err := client.DeleteManagedNetworks(context.Background(), AccountIdentifier(testAccountID), id) if assert.NoError(t, err) { assert.Equal(t, want, actual) From db74b8da2924263eaf567428b98ee056bc03d985 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Mon, 19 Dec 2022 12:18:01 +1100 Subject: [PATCH 5/5] lint fixes --- devices_managed_networks_test.go | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/devices_managed_networks_test.go b/devices_managed_networks_test.go index fed57837385..7f2f4bb6b77 100644 --- a/devices_managed_networks_test.go +++ b/devices_managed_networks_test.go @@ -9,6 +9,8 @@ import ( "github.com/stretchr/testify/assert" ) +const testNetworkID = "f174e90a-fafe-4643-bbbc-4a0ed4fc8415" + func TestGetDeviceManagedNetworks(t *testing.T) { setup() defer teardown() @@ -35,7 +37,7 @@ func TestGetDeviceManagedNetworks(t *testing.T) { } want := []DeviceManagedNetwork{{ - NetworkID: "f174e90a-fafe-4643-bbbc-4a0ed4fc8415", + NetworkID: testNetworkID, Type: "tls", Name: "managed-network-1", Config: &Config{ @@ -57,7 +59,6 @@ func TestDeviceManagedNetwork(t *testing.T) { setup() defer teardown() - id := "f174e90a-fafe-4643-bbbc-4a0ed4fc8415" 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") @@ -75,11 +76,11 @@ func TestDeviceManagedNetwork(t *testing.T) { "sha256": "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c" } } - }`, id) + }`, testNetworkID) } want := DeviceManagedNetwork{ - NetworkID: id, + NetworkID: testNetworkID, Type: "tls", Name: "managed-network-1", Config: &Config{ @@ -88,9 +89,9 @@ func TestDeviceManagedNetwork(t *testing.T) { }, } - mux.HandleFunc("/accounts/"+testAccountID+"/devices/networks/"+id, handler) + mux.HandleFunc("/accounts/"+testAccountID+"/devices/networks/"+testNetworkID, handler) - actual, err := client.GetDeviceManagedNetwork(context.Background(), AccountIdentifier(testAccountID), id) + actual, err := client.GetDeviceManagedNetwork(context.Background(), AccountIdentifier(testAccountID), testNetworkID) if assert.NoError(t, err) { assert.Equal(t, want, actual) @@ -101,7 +102,6 @@ func TestCreateDeviceManagedNetwork(t *testing.T) { setup() defer teardown() - id := "f174e90a-fafe-4643-bbbc-4a0ed4fc8415" 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") @@ -119,11 +119,11 @@ func TestCreateDeviceManagedNetwork(t *testing.T) { "sha256": "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c" } } - }`, id) + }`, testNetworkID) } want := DeviceManagedNetwork{ - NetworkID: id, + NetworkID: testNetworkID, Type: "tls", Name: "managed-network-1", Config: &Config{ @@ -135,7 +135,7 @@ func TestCreateDeviceManagedNetwork(t *testing.T) { mux.HandleFunc("/accounts/"+testAccountID+"/devices/networks", handler) actual, err := client.CreateDeviceManagedNetwork(context.Background(), AccountIdentifier(testAccountID), CreateDeviceManagedNetworkParams{ - NetworkID: id, + NetworkID: testNetworkID, Type: "tls", Name: "managed-network-1", Config: &Config{ @@ -153,7 +153,6 @@ func TestDeleteDeviceManagedNetwork(t *testing.T) { setup() defer teardown() - id := "480f4f69-1a28-4fdd-9240-1ed29f0ac1db" 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") @@ -176,7 +175,7 @@ func TestDeleteDeviceManagedNetwork(t *testing.T) { } want := []DeviceManagedNetwork{{ - NetworkID: "f174e90a-fafe-4643-bbbc-4a0ed4fc8415", + NetworkID: testNetworkID, Type: "tls", Name: "managed-network-1", Config: &Config{ @@ -185,12 +184,11 @@ func TestDeleteDeviceManagedNetwork(t *testing.T) { }, }} - mux.HandleFunc("/accounts/"+testAccountID+"/devices/networks/480f4f69-1a28-4fdd-9240-1ed29f0ac1db", handler) + mux.HandleFunc("/accounts/"+testAccountID+"/devices/networks/"+testNetworkID, handler) - actual, err := client.DeleteManagedNetworks(context.Background(), AccountIdentifier(testAccountID), id) + actual, err := client.DeleteManagedNetworks(context.Background(), AccountIdentifier(testAccountID), testNetworkID) if assert.NoError(t, err) { assert.Equal(t, want, actual) } - }