Skip to content

Commit

Permalink
update method naming conventions and nested structs
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbednarz committed Oct 3, 2023
1 parent 3aa0da5 commit 83d0588
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 70 deletions.
48 changes: 19 additions & 29 deletions api_shield_api_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@ type ListAPIShieldDiscoveryOperationsParams struct {
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 `url:"order,omitempty"`
// Filters to only return operations that match filtering criteria, see APIShieldGetOperationsFilters
APIShieldListDiscoveryOperationsFilters
// Pagination options to apply to the request.
PaginationOptions
}

// APIShieldListDiscoveryOperationsFilters represents the filtering query parameters to set when retrieving discovery operations.
//
// API documentation: https://developers.cloudflare.com/api/operations/api-shield-api-discovery-retrieve-discovered-operations-on-a-zone
type APIShieldListDiscoveryOperationsFilters struct {
// Hosts filters results to only include the specified hosts.
Hosts []string `url:"host,omitempty"`
// Methods filters results to only include the specified methods.
Expand All @@ -57,31 +47,33 @@ type APIShieldListDiscoveryOperationsFilters struct {
Origin string `url:"origin,omitempty"`
// State filter results to only include discovery results in a particular state.
State string `url:"state,omitempty"`

// Pagination options to apply to the request.
PaginationOptions
}

// PatchAPIShieldDiscoveryOperationParams represents the parameters to pass to patch a discovery operation
// UpdateAPIShieldDiscoveryOperationParams represents the parameters to pass to patch a discovery operation
//
// API documentation: https://developers.cloudflare.com/api/operations/api-shield-api-patch-discovered-operation
type PatchAPIShieldDiscoveryOperationParams struct {
type UpdateAPIShieldDiscoveryOperationParams struct {
// OperationID is the operation to be patched
OperationID string `json:"-" url:"-"`

PatchAPIShieldDiscoveryOperation
State string `json:"state" url:"-"`
}

// PatchAPIShieldDiscoveryOperationsParams maps discovery operation IDs to PatchAPIShieldDiscoveryOperation structs
// UpdateAPIShieldDiscoveryOperationsParams maps discovery operation IDs to PatchAPIShieldDiscoveryOperation structs
//
// Example:
//
// PatchAPIShieldDiscoveryOperations{
// UpdateAPIShieldDiscoveryOperations{
// "99522293-a505-45e5-bbad-bbc339f5dc40": PatchAPIShieldDiscoveryOperation{ State: "review" },
// }
//
// API documentation: https://developers.cloudflare.com/api/operations/api-shield-api-patch-discovered-operations
type PatchAPIShieldDiscoveryOperationsParams map[string]PatchAPIShieldDiscoveryOperation
type UpdateAPIShieldDiscoveryOperationsParams map[string]UpdateAPIShieldDiscoveryOperation

// PatchAPIShieldDiscoveryOperation represents the state to set on a discovery operation.
type PatchAPIShieldDiscoveryOperation struct {
// UpdateAPIShieldDiscoveryOperation represents the state to set on a discovery operation.
type UpdateAPIShieldDiscoveryOperation struct {
// State is the state to set on the operation
State string `json:"state" url:"-"`
}
Expand All @@ -95,23 +87,21 @@ type APIShieldListDiscoveryOperationsResponse struct {

// APIShieldPatchDiscoveryOperationResponse represents the response from the PATCH api_gateway/discovery/operations/{id} endpoint.
type APIShieldPatchDiscoveryOperationResponse struct {
Result PatchAPIShieldDiscoveryOperation `json:"result"`
Result UpdateAPIShieldDiscoveryOperation `json:"result"`
Response
}

// APIShieldPatchDiscoveryOperationsResponse represents the response from the PATCH api_gateway/discovery/operations endpoint.
type APIShieldPatchDiscoveryOperationsResponse struct {
Result PatchAPIShieldDiscoveryOperationsParams `json:"result"`
Result UpdateAPIShieldDiscoveryOperationsParams `json:"result"`
Response
}

// ListAPIShieldDiscoveryOperations retrieve the most up to date view of discovered operations.
//
// API documentation: https://developers.cloudflare.com/api/operations/api-shield-api-discovery-retrieve-discovered-operations-on-a-zone
func (api *API) ListAPIShieldDiscoveryOperations(ctx context.Context, rc *ResourceContainer, params ListAPIShieldDiscoveryOperationsParams) ([]APIShieldDiscoveryOperation, ResultInfo, error) {
path := fmt.Sprintf("/zones/%s/api_gateway/discovery/operations", rc.Identifier)

uri := buildURI(path, params)
uri := buildURI(fmt.Sprintf("/zones/%s/api_gateway/discovery/operations", rc.Identifier), params)

res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
Expand All @@ -127,12 +117,12 @@ func (api *API) ListAPIShieldDiscoveryOperations(ctx context.Context, rc *Resour
return asResponse.Result, asResponse.ResultInfo, nil
}

// PatchAPIShieldDiscoveryOperation updates certain fields on a discovered operation
// UpdateAPIShieldDiscoveryOperation updates certain fields on a discovered operation.
//
// API Documentation: https://developers.cloudflare.com/api/operations/api-shield-api-patch-discovered-operation
func (api *API) PatchAPIShieldDiscoveryOperation(ctx context.Context, rc *ResourceContainer, params PatchAPIShieldDiscoveryOperationParams) (*PatchAPIShieldDiscoveryOperation, error) {
func (api *API) UpdateAPIShieldDiscoveryOperation(ctx context.Context, rc *ResourceContainer, params UpdateAPIShieldDiscoveryOperationParams) (*UpdateAPIShieldDiscoveryOperation, error) {
if params.OperationID == "" {
return nil, fmt.Errorf("params.OperationID must be provided")
return nil, fmt.Errorf("operation ID must be provided")
}

uri := fmt.Sprintf("/zones/%s/api_gateway/discovery/operations/%s", rc.Identifier, params.OperationID)
Expand All @@ -152,10 +142,10 @@ func (api *API) PatchAPIShieldDiscoveryOperation(ctx context.Context, rc *Resour
return &asResponse.Result, nil
}

// PatchAPIShieldDiscoveryOperations bulk updates certain fields on multiple discovered operations
// UpdateAPIShieldDiscoveryOperations bulk updates certain fields on multiple discovered operations
//
// API documentation: https://developers.cloudflare.com/api/operations/api-shield-api-patch-discovered-operations
func (api *API) PatchAPIShieldDiscoveryOperations(ctx context.Context, rc *ResourceContainer, params PatchAPIShieldDiscoveryOperationsParams) (*PatchAPIShieldDiscoveryOperationsParams, error) {
func (api *API) UpdateAPIShieldDiscoveryOperations(ctx context.Context, rc *ResourceContainer, params UpdateAPIShieldDiscoveryOperationsParams) (*UpdateAPIShieldDiscoveryOperationsParams, error) {
uri := fmt.Sprintf("/zones/%s/api_gateway/discovery/operations", rc.Identifier)

res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, params)
Expand Down
66 changes: 25 additions & 41 deletions api_shield_api_discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,12 @@ func TestListAPIShieldDiscoveryOperationsWithParams(t *testing.T) {
params: ListAPIShieldDiscoveryOperationsParams{
Direction: "desc",
OrderBy: "host",
APIShieldListDiscoveryOperationsFilters: APIShieldListDiscoveryOperationsFilters{
Hosts: []string{"api.cloudflare.com", "developers.cloudflare.com"},
Methods: []string{"GET", "PUT"},
Endpoint: "/client",
Origin: "ML",
State: "review",
Diff: true,
},
Hosts: []string{"api.cloudflare.com", "developers.cloudflare.com"},
Methods: []string{"GET", "PUT"},
Endpoint: "/client",
Origin: "ML",
State: "review",
Diff: true,
PaginationOptions: PaginationOptions{
Page: 1,
PerPage: 25,
Expand Down Expand Up @@ -168,9 +166,7 @@ func TestListAPIShieldDiscoveryOperationsWithParams(t *testing.T) {
{
name: "hosts only",
params: ListAPIShieldDiscoveryOperationsParams{
APIShieldListDiscoveryOperationsFilters: APIShieldListDiscoveryOperationsFilters{
Hosts: []string{"api.cloudflare.com", "developers.cloudflare.com"},
},
Hosts: []string{"api.cloudflare.com", "developers.cloudflare.com"},
},
expectedParams: url.Values{
"host": []string{"api.cloudflare.com", "developers.cloudflare.com"},
Expand All @@ -179,9 +175,7 @@ func TestListAPIShieldDiscoveryOperationsWithParams(t *testing.T) {
{
name: "methods only",
params: ListAPIShieldDiscoveryOperationsParams{
APIShieldListDiscoveryOperationsFilters: APIShieldListDiscoveryOperationsFilters{
Methods: []string{"GET", "PUT"},
},
Methods: []string{"GET", "PUT"},
},
expectedParams: url.Values{
"method": []string{"GET", "PUT"},
Expand All @@ -190,9 +184,7 @@ func TestListAPIShieldDiscoveryOperationsWithParams(t *testing.T) {
{
name: "endpoint only",
params: ListAPIShieldDiscoveryOperationsParams{
APIShieldListDiscoveryOperationsFilters: APIShieldListDiscoveryOperationsFilters{
Endpoint: "/client",
},
Endpoint: "/client",
},
expectedParams: url.Values{
"endpoint": []string{"/client"},
Expand All @@ -201,9 +193,7 @@ func TestListAPIShieldDiscoveryOperationsWithParams(t *testing.T) {
{
name: "origin only",
params: ListAPIShieldDiscoveryOperationsParams{
APIShieldListDiscoveryOperationsFilters: APIShieldListDiscoveryOperationsFilters{
Origin: "ML",
},
Origin: "ML",
},
expectedParams: url.Values{
"origin": []string{"ML"},
Expand All @@ -212,9 +202,7 @@ func TestListAPIShieldDiscoveryOperationsWithParams(t *testing.T) {
{
name: "state only",
params: ListAPIShieldDiscoveryOperationsParams{
APIShieldListDiscoveryOperationsFilters: APIShieldListDiscoveryOperationsFilters{
State: "review",
},
State: "review",
},
expectedParams: url.Values{
"state": []string{"review"},
Expand All @@ -223,9 +211,7 @@ func TestListAPIShieldDiscoveryOperationsWithParams(t *testing.T) {
{
name: "diff only",
params: ListAPIShieldDiscoveryOperationsParams{
APIShieldListDiscoveryOperationsFilters: APIShieldListDiscoveryOperationsFilters{
Diff: true,
},
Diff: true,
},
expectedParams: url.Values{
"diff": []string{"true"},
Expand Down Expand Up @@ -316,20 +302,18 @@ func TestPatchAPIShieldDiscoveryOperation(t *testing.T) {

mux.HandleFunc(endpoint, handler)

actual, err := client.PatchAPIShieldDiscoveryOperation(
actual, err := client.UpdateAPIShieldDiscoveryOperation(
context.Background(),
ZoneIdentifier(testZoneID),
PatchAPIShieldDiscoveryOperationParams{
UpdateAPIShieldDiscoveryOperationParams{
OperationID: testAPIShieldDiscoveryOperationID,
PatchAPIShieldDiscoveryOperation: PatchAPIShieldDiscoveryOperation{
State: "ignored",
},
State: "ignored",
},
)

// patch result is a cut down representation of the schema
// so metadata like created date is not populated
expected := &PatchAPIShieldDiscoveryOperation{
expected := &UpdateAPIShieldDiscoveryOperation{
State: "ignored",
}

Expand Down Expand Up @@ -369,18 +353,18 @@ func TestPatchAPIShieldDiscoveryOperations(t *testing.T) {

mux.HandleFunc(endpoint, handler)

actual, err := client.PatchAPIShieldDiscoveryOperations(
actual, err := client.UpdateAPIShieldDiscoveryOperations(
context.Background(),
ZoneIdentifier(testZoneID),
PatchAPIShieldDiscoveryOperationsParams{
"9b16ce22-d1bf-425d-869f-a11f8240fafb": PatchAPIShieldDiscoveryOperation{State: "ignored"},
"c51c2ea1-a690-48fd-8e3f-7fc79b269947": PatchAPIShieldDiscoveryOperation{State: "review"},
UpdateAPIShieldDiscoveryOperationsParams{
"9b16ce22-d1bf-425d-869f-a11f8240fafb": UpdateAPIShieldDiscoveryOperation{State: "ignored"},
"c51c2ea1-a690-48fd-8e3f-7fc79b269947": UpdateAPIShieldDiscoveryOperation{State: "review"},
},
)

expected := &PatchAPIShieldDiscoveryOperationsParams{
"9b16ce22-d1bf-425d-869f-a11f8240fafb": PatchAPIShieldDiscoveryOperation{State: "ignored"},
"c51c2ea1-a690-48fd-8e3f-7fc79b269947": PatchAPIShieldDiscoveryOperation{State: "review"},
expected := &UpdateAPIShieldDiscoveryOperationsParams{
"9b16ce22-d1bf-425d-869f-a11f8240fafb": UpdateAPIShieldDiscoveryOperation{State: "ignored"},
"c51c2ea1-a690-48fd-8e3f-7fc79b269947": UpdateAPIShieldDiscoveryOperation{State: "review"},
}

if assert.NoError(t, err) {
Expand All @@ -394,6 +378,6 @@ func TestMustProvideDiscoveryOperationID(t *testing.T) {
setup()
t.Cleanup(teardown)

_, err := client.PatchAPIShieldDiscoveryOperation(context.Background(), ZoneIdentifier(testZoneID), PatchAPIShieldDiscoveryOperationParams{})
require.ErrorContains(t, err, "params.OperationID must be provided")
_, err := client.UpdateAPIShieldDiscoveryOperation(context.Background(), ZoneIdentifier(testZoneID), UpdateAPIShieldDiscoveryOperationParams{})
require.ErrorContains(t, err, "operation ID must be provided")
}

0 comments on commit 83d0588

Please sign in to comment.