diff --git a/api_shield_operations.go b/api_shield_operations.go index 86e9ee2c370..f4b0cd5bd21 100644 --- a/api_shield_operations.go +++ b/api_shield_operations.go @@ -2,66 +2,82 @@ package cloudflare import ( "context" - "encoding/json" "fmt" "net/http" - "net/url" - "strconv" - "strings" "time" -) -// APIShieldCreateOperation should be used when creating an operation in API Shield Endpoint Management. -type APIShieldCreateOperation struct { - Method string `json:"method"` - Host string `json:"host"` - Endpoint string `json:"endpoint"` -} + "github.com/goccy/go-json" +) // APIShieldOperation represents an operation stored in API Shield Endpoint Management. type APIShieldOperation struct { - APIShieldCreateOperation + APIShieldBasicOperation ID string `json:"operation_id"` LastUpdated time.Time `json:"last_updated"` Features map[string]any `json:"features,omitempty"` } -// APIShieldGetOperationParams represents the query parameters to set when retrieving an operation. +// GetAPIShieldOperationParams represents the parameters to pass when retrieving an operation. // // See API documentation https://developers.cloudflare.com/api/operations/api-shield-endpoint-management-retrieve-information-about-an-operation -type APIShieldGetOperationParams struct { +type GetAPIShieldOperationParams struct { + // The Operation ID to retrieve + OperationID string `url:"-"` // Features represents a set of features to return in `features` object when // performing making read requests against an Operation or listing operations. - Features []string + Features []string `url:"feature,omitempty"` +} + +// CreateAPIShieldOperationsParams represents the parameters to pass when adding one or more operations. +// +// See API documentation https://developers.cloudflare.com/api/operations/api-shield-endpoint-management-add-operations-to-a-zone +type CreateAPIShieldOperationsParams struct { + // Operations are a slice of operations to be created in API Shield Endpoint Management + Operations []APIShieldBasicOperation `url:"-"` +} + +// APIShieldBasicOperation should be used when creating an operation in API Shield Endpoint Management. +type APIShieldBasicOperation struct { + Method string `json:"method"` + Host string `json:"host"` + Endpoint string `json:"endpoint"` +} + +// DeleteAPIShieldOperationParams represents the parameters to pass to delete an operation. +// +// See API documentation https://developers.cloudflare.com/api/operations/api-shield-endpoint-management-delete-an-operation +type DeleteAPIShieldOperationParams struct { + // OperationID is the operation to be deleted + OperationID string `url:"-"` } -// APIShieldGetOperationsParams represents the query parameters to set when retrieving operations +// ListAPIShieldOperationsParams represents the parameters to pass when retrieving operations // // See API documentation https://developers.cloudflare.com/api/operations/api-shield-endpoint-management-retrieve-information-about-all-operations-on-a-zone -type APIShieldGetOperationsParams struct { +type ListAPIShieldOperationsParams struct { // Features represents a set of features to return in `features` object when // performing making read requests against an Operation or listing operations. - Features []string + Features []string `url:"feature,omitempty"` // Direction to order results. - Direction string + Direction string `url:"direction,omitempty"` // OrderBy when requesting a feature, the feature keys are available for ordering as well, e.g., thresholds.suggested_threshold. - OrderBy string + OrderBy string `url:"order,omitempty"` // Filters to only return operations that match filtering criteria, see APIShieldGetOperationsFilters - Filters *APIShieldGetOperationsFilters + APIShieldListOperationsFilters // Pagination options to apply to the request. - Pagination *PaginationOptions + PaginationOptions } -// APIShieldGetOperationsFilters represents the filtering query parameters to set when retrieving operations +// APIShieldListOperationsFilters represents the filtering query parameters to set when retrieving operations // // See API documentation https://developers.cloudflare.com/api/operations/api-shield-endpoint-management-retrieve-information-about-all-operations-on-a-zone -type APIShieldGetOperationsFilters struct { - // Host filters results to only include the specified hosts. - Hosts []string - // Host filters results to only include the specified methods. - Methods []string +type APIShieldListOperationsFilters struct { + // Hosts filters results to only include the specified hosts. + Hosts []string `url:"host,omitempty"` + // Methods filters results to only include the specified methods. + Methods []string `url:"method,omitempty"` // Endpoint filter results to only include endpoints containing this pattern. - Endpoint string + Endpoint string `url:"endpoint,omitempty"` } // APIShieldGetOperationResponse represents the response from the api_gateway/operations/{id} endpoint. @@ -86,12 +102,10 @@ type APIShieldDeleteOperationResponse struct { // GetAPIShieldOperation returns information about an operation // // API documentation https://developers.cloudflare.com/api/operations/api-shield-endpoint-management-retrieve-information-about-an-operation -func (api *API) GetAPIShieldOperation(ctx context.Context, rc *ResourceContainer, operationID string, params *APIShieldGetOperationParams) (*APIShieldOperation, error) { - uri := fmt.Sprintf("/zones/%s/api_gateway/operations/%s", rc.Identifier, operationID) +func (api *API) GetAPIShieldOperation(ctx context.Context, rc *ResourceContainer, params GetAPIShieldOperationParams) (*APIShieldOperation, error) { + path := fmt.Sprintf("/zones/%s/api_gateway/operations/%s", rc.Identifier, params.OperationID) - if params != nil { - uri = strings.Join([]string{uri, params.Encode()}, "?") - } + uri := buildURI(path, params) res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) if err != nil { @@ -107,14 +121,13 @@ func (api *API) GetAPIShieldOperation(ctx context.Context, rc *ResourceContainer return &asResponse.Result, nil } -// GetAPIShieldOperations retrieve information about all operations on a zone +// ListAPIShieldOperations retrieve information about all operations on a zone // // API documentation https://developers.cloudflare.com/api/operations/api-shield-endpoint-management-retrieve-information-about-all-operations-on-a-zone -func (api *API) GetAPIShieldOperations(ctx context.Context, rc *ResourceContainer, params *APIShieldGetOperationsParams) ([]APIShieldOperation, ResultInfo, error) { - uri := fmt.Sprintf("/zones/%s/api_gateway/operations", rc.Identifier) - if params != nil { - uri = strings.Join([]string{uri, params.Encode()}, "?") - } +func (api *API) ListAPIShieldOperations(ctx context.Context, rc *ResourceContainer, params ListAPIShieldOperationsParams) ([]APIShieldOperation, ResultInfo, error) { + path := fmt.Sprintf("/zones/%s/api_gateway/operations", rc.Identifier) + + uri := buildURI(path, params) res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) if err != nil { @@ -130,13 +143,13 @@ func (api *API) GetAPIShieldOperations(ctx context.Context, rc *ResourceContaine return asResponse.Result, asResponse.ResultInfo, nil } -// PostAPIShieldOperations add one or more operations to a zone. +// CreateAPIShieldOperations add one or more operations to a zone. // // API documentation https://developers.cloudflare.com/api/operations/api-shield-endpoint-management-add-operations-to-a-zone -func (api *API) PostAPIShieldOperations(ctx context.Context, rc *ResourceContainer, operations []APIShieldCreateOperation) ([]APIShieldOperation, error) { +func (api *API) CreateAPIShieldOperations(ctx context.Context, rc *ResourceContainer, params CreateAPIShieldOperationsParams) ([]APIShieldOperation, error) { uri := fmt.Sprintf("/zones/%s/api_gateway/operations", rc.Identifier) - res, err := api.makeRequestContext(ctx, http.MethodPost, uri, operations) + res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params.Operations) if err != nil { return nil, err } @@ -154,8 +167,8 @@ func (api *API) PostAPIShieldOperations(ctx context.Context, rc *ResourceContain // DeleteAPIShieldOperation deletes a single operation // // API documentation https://developers.cloudflare.com/api/operations/api-shield-endpoint-management-delete-an-operation -func (api *API) DeleteAPIShieldOperation(ctx context.Context, rc *ResourceContainer, operationID string) error { - uri := fmt.Sprintf("/zones/%s/api_gateway/operations/%s", rc.Identifier, operationID) +func (api *API) DeleteAPIShieldOperation(ctx context.Context, rc *ResourceContainer, params DeleteAPIShieldOperationParams) error { + uri := fmt.Sprintf("/zones/%s/api_gateway/operations/%s", rc.Identifier, params.OperationID) res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil) if err != nil { @@ -170,52 +183,3 @@ func (api *API) DeleteAPIShieldOperation(ctx context.Context, rc *ResourceContai return nil } - -// Encode is a custom method for encoding APIShieldGetOperationParams into a usable HTTP -// query parameter string. -func (a APIShieldGetOperationParams) Encode() string { - v := url.Values{} - for _, f := range a.Features { - v.Add("feature", f) - } - - return v.Encode() -} - -// Encode is a custom method for encoding APIShieldGetOperationsParams into a usable HTTP -// query parameter string. -func (a APIShieldGetOperationsParams) Encode() string { - v := url.Values{} - for _, f := range a.Features { - v.Add("feature", f) - } - - if a.Direction != "" { - v.Set("direction", a.Direction) - } - - if a.OrderBy != "" { - v.Set("order", a.OrderBy) - } - - if a.Pagination != nil { - v.Set("page", strconv.Itoa(a.Pagination.Page)) - v.Set("per_page", strconv.Itoa(a.Pagination.PerPage)) - } - - if a.Filters != nil { - if a.Filters.Endpoint != "" { - v.Set("endpoint", a.Filters.Endpoint) - } - - for _, h := range a.Filters.Hosts { - v.Add("host", h) - } - - for _, m := range a.Filters.Methods { - v.Add("method", m) - } - } - - return v.Encode() -} diff --git a/api_shield_operations_test.go b/api_shield_operations_test.go index 9e8a40ab03c..44f994ef401 100644 --- a/api_shield_operations_test.go +++ b/api_shield_operations_test.go @@ -45,12 +45,13 @@ func TestGetAPIShieldOperation(t *testing.T) { actual, err := client.GetAPIShieldOperation( context.Background(), ZoneIdentifier(testZoneID), - testAPIShieldOperationId, - nil, + GetAPIShieldOperationParams{ + OperationID: testAPIShieldOperationId, + }, ) expected := &APIShieldOperation{ - APIShieldCreateOperation: APIShieldCreateOperation{ + APIShieldBasicOperation: APIShieldBasicOperation{ Method: "POST", Host: "api.cloudflare.com", Endpoint: "/client/v4/zones", @@ -65,7 +66,7 @@ func TestGetAPIShieldOperation(t *testing.T) { } } -func TestGetAPIShieldOperationWithOptions(t *testing.T) { +func TestGetAPIShieldOperationWithParams(t *testing.T) { endpoint := fmt.Sprintf("/zones/%s/api_gateway/operations/%s", testZoneID, testAPIShieldOperationId) response := `{ "success" : true, @@ -86,13 +87,14 @@ func TestGetAPIShieldOperationWithOptions(t *testing.T) { tests := []struct { name string - getOptions *APIShieldGetOperationParams + getParams GetAPIShieldOperationParams expectedParams url.Values }{ { name: "one feature", - getOptions: &APIShieldGetOperationParams{ - Features: []string{"thresholds"}, + getParams: GetAPIShieldOperationParams{ + OperationID: testAPIShieldOperationId, + Features: []string{"thresholds"}, }, expectedParams: url.Values{ "feature": []string{"thresholds"}, @@ -100,8 +102,9 @@ func TestGetAPIShieldOperationWithOptions(t *testing.T) { }, { name: "more than one feature", - getOptions: &APIShieldGetOperationParams{ - Features: []string{"thresholds", "parameter_schemas"}, + getParams: GetAPIShieldOperationParams{ + OperationID: testAPIShieldOperationId, + Features: []string{"thresholds", "parameter_schemas"}, }, expectedParams: url.Values{ "feature": []string{"thresholds", "parameter_schemas"}, @@ -125,12 +128,11 @@ func TestGetAPIShieldOperationWithOptions(t *testing.T) { actual, err := client.GetAPIShieldOperation( context.Background(), ZoneIdentifier(testZoneID), - testAPIShieldOperationId, - test.getOptions, + test.getParams, ) expected := &APIShieldOperation{ - APIShieldCreateOperation: APIShieldCreateOperation{ + APIShieldBasicOperation: APIShieldBasicOperation{ Method: "POST", Host: "api.cloudflare.com", Endpoint: "/client/v4/zones", @@ -150,7 +152,7 @@ func TestGetAPIShieldOperationWithOptions(t *testing.T) { } } -func TestGetAPIShieldOperations(t *testing.T) { +func TestListAPIShieldOperations(t *testing.T) { endpoint := fmt.Sprintf("/zones/%s/api_gateway/operations", testZoneID) response := `{ "success" : true, @@ -185,15 +187,15 @@ func TestGetAPIShieldOperations(t *testing.T) { mux.HandleFunc(endpoint, handler) - actual, actualResultInfo, err := client.GetAPIShieldOperations( + actual, actualResultInfo, err := client.ListAPIShieldOperations( context.Background(), ZoneIdentifier(testZoneID), - nil, + ListAPIShieldOperationsParams{}, ) expectedOps := []APIShieldOperation{ { - APIShieldCreateOperation: APIShieldCreateOperation{ + APIShieldBasicOperation: APIShieldBasicOperation{ Method: "POST", Host: "api.cloudflare.com", Endpoint: "/client/v4/zones", @@ -217,7 +219,7 @@ func TestGetAPIShieldOperations(t *testing.T) { } } -func TestGetAPIShieldOperationsWithOptions(t *testing.T) { +func TestListAPIShieldOperationsWithParams(t *testing.T) { endpoint := fmt.Sprintf("/zones/%s/api_gateway/operations", testZoneID) response := `{ "success" : true, @@ -245,21 +247,21 @@ func TestGetAPIShieldOperationsWithOptions(t *testing.T) { tests := []struct { name string - params *APIShieldGetOperationsParams + params ListAPIShieldOperationsParams expectedParams url.Values }{ { name: "all params", - params: &APIShieldGetOperationsParams{ + params: ListAPIShieldOperationsParams{ Features: []string{"thresholds", "parameter_schemas"}, Direction: "desc", OrderBy: "host", - Filters: &APIShieldGetOperationsFilters{ + APIShieldListOperationsFilters: APIShieldListOperationsFilters{ Hosts: []string{"api.cloudflare.com", "developers.cloudflare.com"}, Methods: []string{"GET", "PUT"}, Endpoint: "/client", }, - Pagination: &PaginationOptions{ + PaginationOptions: PaginationOptions{ Page: 1, PerPage: 25, }, @@ -277,7 +279,7 @@ func TestGetAPIShieldOperationsWithOptions(t *testing.T) { }, { name: "features only", - params: &APIShieldGetOperationsParams{ + params: ListAPIShieldOperationsParams{ Features: []string{"thresholds", "parameter_schemas"}, }, expectedParams: url.Values{ @@ -286,7 +288,7 @@ func TestGetAPIShieldOperationsWithOptions(t *testing.T) { }, { name: "direction only", - params: &APIShieldGetOperationsParams{ + params: ListAPIShieldOperationsParams{ Direction: "desc", }, expectedParams: url.Values{ @@ -295,7 +297,7 @@ func TestGetAPIShieldOperationsWithOptions(t *testing.T) { }, { name: "order only", - params: &APIShieldGetOperationsParams{ + params: ListAPIShieldOperationsParams{ OrderBy: "host", }, expectedParams: url.Values{ @@ -304,8 +306,8 @@ func TestGetAPIShieldOperationsWithOptions(t *testing.T) { }, { name: "hosts only", - params: &APIShieldGetOperationsParams{ - Filters: &APIShieldGetOperationsFilters{ + params: ListAPIShieldOperationsParams{ + APIShieldListOperationsFilters: APIShieldListOperationsFilters{ Hosts: []string{"api.cloudflare.com", "developers.cloudflare.com"}, }, }, @@ -315,8 +317,8 @@ func TestGetAPIShieldOperationsWithOptions(t *testing.T) { }, { name: "methods only", - params: &APIShieldGetOperationsParams{ - Filters: &APIShieldGetOperationsFilters{ + params: ListAPIShieldOperationsParams{ + APIShieldListOperationsFilters: APIShieldListOperationsFilters{ Methods: []string{"GET", "PUT"}, }, }, @@ -326,8 +328,8 @@ func TestGetAPIShieldOperationsWithOptions(t *testing.T) { }, { name: "endpoint only", - params: &APIShieldGetOperationsParams{ - Filters: &APIShieldGetOperationsFilters{ + params: ListAPIShieldOperationsParams{ + APIShieldListOperationsFilters: APIShieldListOperationsFilters{ Endpoint: "/client", }, }, @@ -337,8 +339,8 @@ func TestGetAPIShieldOperationsWithOptions(t *testing.T) { }, { name: "pagination only", - params: &APIShieldGetOperationsParams{ - Pagination: &PaginationOptions{ + params: ListAPIShieldOperationsParams{ + PaginationOptions: PaginationOptions{ Page: 1, PerPage: 25, }, @@ -363,7 +365,7 @@ func TestGetAPIShieldOperationsWithOptions(t *testing.T) { mux.HandleFunc(endpoint, handler) - actual, _, err := client.GetAPIShieldOperations( + actual, _, err := client.ListAPIShieldOperations( context.Background(), ZoneIdentifier(testZoneID), test.params, @@ -371,7 +373,7 @@ func TestGetAPIShieldOperationsWithOptions(t *testing.T) { expected := []APIShieldOperation{ { - APIShieldCreateOperation: APIShieldCreateOperation{ + APIShieldBasicOperation: APIShieldBasicOperation{ Method: "POST", Host: "api.cloudflare.com", Endpoint: "/client/v4/zones", @@ -391,7 +393,7 @@ func TestGetAPIShieldOperationsWithOptions(t *testing.T) { } } -func TestPostAPIShieldOperations(t *testing.T) { +func TestCreateAPIShieldOperations(t *testing.T) { setup() t.Cleanup(teardown) @@ -424,21 +426,23 @@ func TestPostAPIShieldOperations(t *testing.T) { mux.HandleFunc(endpoint, handler) - actual, err := client.PostAPIShieldOperations( + actual, err := client.CreateAPIShieldOperations( context.Background(), ZoneIdentifier(testZoneID), - []APIShieldCreateOperation{ - { - Method: "POST", - Host: "api.cloudflare.com", - Endpoint: "/client/v4/zones", + CreateAPIShieldOperationsParams{ + Operations: []APIShieldBasicOperation{ + { + Method: "POST", + Host: "api.cloudflare.com", + Endpoint: "/client/v4/zones", + }, }, }, ) expected := []APIShieldOperation{ { - APIShieldCreateOperation: APIShieldCreateOperation{ + APIShieldBasicOperation: APIShieldBasicOperation{ Method: "POST", Host: "api.cloudflare.com", Endpoint: "/client/v4/zones", @@ -473,7 +477,9 @@ func TestDeleteAPIShieldOperation(t *testing.T) { err := client.DeleteAPIShieldOperation( context.Background(), ZoneIdentifier(testZoneID), - testAPIShieldOperationId, + DeleteAPIShieldOperationParams{ + OperationID: testAPIShieldOperationId, + }, ) assert.NoError(t, err)