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 authored and djhworld committed Oct 4, 2023
1 parent 3aa0da5 commit 80d9650
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 95 deletions.
98 changes: 57 additions & 41 deletions api_shield_api_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,40 @@ import (
"github.com/goccy/go-json"
)

// APIShieldDiscoveryOrigin is an enumeration on what discovery engine an operation was discovered by.
type APIShieldDiscoveryOrigin string

const (

// APIShieldDiscoveryOriginML discovered operations that were sourced using ML API Discovery.
APIShieldDiscoveryOriginML APIShieldDiscoveryOrigin = "ML"
// APIShieldDiscoveryOriginSessionIdentifier discovered operations that were sourced using Session Identifier
// API Discovery.
APIShieldDiscoveryOriginSessionIdentifier APIShieldDiscoveryOrigin = "SessionIdentifier"
)

// APIShieldDiscoveryState is an enumeration on states a discovery operation can be in.
type APIShieldDiscoveryState string

const (
// APIShieldDiscoveryStateReview discovered operations that are not saved into API Shield Endpoint Management.
APIShieldDiscoveryStateReview APIShieldDiscoveryState = "review"
// APIShieldDiscoveryStateSaved discovered operations that are already saved into API Shield Endpoint Management.
APIShieldDiscoveryStateSaved APIShieldDiscoveryState = "saved"
// APIShieldDiscoveryStateIgnored discovered operations that have been marked as ignored.
APIShieldDiscoveryStateIgnored APIShieldDiscoveryState = "ignored"
)

// APIShieldDiscoveryOperation is an operation that was discovered by API Discovery.
type APIShieldDiscoveryOperation struct {
// ID represents the ID of the operation
// ID represents the ID of the operation, formatted as UUID
ID string `json:"id"`
// Origin represents the API discovery engine(s) that discovered this operation
Origin []string `json:"origin"`
Origin []APIShieldDiscoveryOrigin `json:"origin"`
// State represents the state of operation in API Discovery
State string `json:"state"`
State APIShieldDiscoveryState `json:"state"`
// LastUpdated timestamp of when this operation was last updated
LastUpdated *time.Time `json:"last_updated,omitempty"`
LastUpdated *time.Time `json:"last_updated"`
// Features are additional data about the operation
Features map[string]any `json:"features,omitempty"`

Expand All @@ -35,55 +59,49 @@ 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.
Methods []string `url:"method,omitempty"`
// Endpoint filter results to only include endpoints containing this pattern.
// Endpoint filters results to only include endpoints containing this pattern.
Endpoint string `url:"endpoint,omitempty"`
// Diff when true, only return API Discovery results that are not saved into API Shield Endpoint Management
Diff bool `url:"diff,omitempty"`
// Origin filter results to only include discovery results sourced from a particular discovery engine
Origin string `url:"origin,omitempty"`
// State filter results to only include discovery results in a particular state.
State string `url:"state,omitempty"`
// Origin filters results to only include discovery results sourced from a particular discovery engine
// See APIShieldDiscoveryOrigin for valid values.
Origin APIShieldDiscoveryOrigin `url:"origin,omitempty"`
// State filters results to only include discovery results in a particular state
// See APIShieldDiscoveryState for valid values.
State APIShieldDiscoveryState `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 {
// OperationID is the operation to be patched
OperationID string `json:"-" url:"-"`

PatchAPIShieldDiscoveryOperation
type UpdateAPIShieldDiscoveryOperationParams struct {
// OperationID is the ID, formatted as UUID, of the operation to be updated
OperationID string `json:"-" url:"-"`
State APIShieldDiscoveryState `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:"-"`
State APIShieldDiscoveryState `json:"state" url:"-"`
}

// APIShieldListDiscoveryOperationsResponse represents the response from the api_gateway/discovery/operations endpoint.
Expand All @@ -95,23 +113,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 +143,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 +168,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
92 changes: 38 additions & 54 deletions api_shield_api_discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func TestListAPIShieldDiscoveryOperations(t *testing.T) {
Endpoint: "/client/v4/zones",
ID: "9def2cb0-3ed0-4737-92ca-f09efa4718fd",
LastUpdated: &lastUpdated,
Origin: []string{"ML"},
State: "review",
Origin: []APIShieldDiscoveryOrigin{APIShieldDiscoveryOriginML},
State: APIShieldDiscoveryStateReview,
},
}

Expand Down Expand Up @@ -96,8 +96,8 @@ func TestListAPIShieldDiscoveryOperationsWithParams(t *testing.T) {
"host": "api.cloudflare.com",
"endpoint": "/client/v4/zones",
"last_updated": "2023-03-02T15:46:06.000000Z",
"origin": ["ML"],
"state": "review",
"origin": ["SessionIdentifier"],
"state": "saved",
"features": {
"traffic_stats": {}
}
Expand All @@ -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: APIShieldDiscoveryOriginSessionIdentifier,
State: APIShieldDiscoveryStateSaved,
Diff: true,
PaginationOptions: PaginationOptions{
Page: 1,
PerPage: 25,
Expand All @@ -140,8 +138,8 @@ func TestListAPIShieldDiscoveryOperationsWithParams(t *testing.T) {
"host": []string{"api.cloudflare.com", "developers.cloudflare.com"},
"method": []string{"GET", "PUT"},
"endpoint": []string{"/client"},
"origin": []string{"ML"},
"state": []string{"review"},
"origin": []string{"SessionIdentifier"},
"state": []string{"saved"},
"diff": []string{"true"},
"page": []string{"1"},
"per_page": []string{"25"},
Expand All @@ -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,31 +193,25 @@ func TestListAPIShieldDiscoveryOperationsWithParams(t *testing.T) {
{
name: "origin only",
params: ListAPIShieldDiscoveryOperationsParams{
APIShieldListDiscoveryOperationsFilters: APIShieldListDiscoveryOperationsFilters{
Origin: "ML",
},
Origin: APIShieldDiscoveryOriginSessionIdentifier,
},
expectedParams: url.Values{
"origin": []string{"ML"},
"origin": []string{"SessionIdentifier"},
},
},
{
name: "state only",
params: ListAPIShieldDiscoveryOperationsParams{
APIShieldListDiscoveryOperationsFilters: APIShieldListDiscoveryOperationsFilters{
State: "review",
},
State: APIShieldDiscoveryStateSaved,
},
expectedParams: url.Values{
"state": []string{"review"},
"state": []string{"saved"},
},
},
{
name: "diff only",
params: ListAPIShieldDiscoveryOperationsParams{
APIShieldListDiscoveryOperationsFilters: APIShieldListDiscoveryOperationsFilters{
Diff: true,
},
Diff: true,
},
expectedParams: url.Values{
"diff": []string{"true"},
Expand Down Expand Up @@ -273,8 +259,8 @@ func TestListAPIShieldDiscoveryOperationsWithParams(t *testing.T) {
Endpoint: "/client/v4/zones",
ID: "9def2cb0-3ed0-4737-92ca-f09efa4718fd",
LastUpdated: &lastUpdated,
Origin: []string{"ML"},
State: "review",
Origin: []APIShieldDiscoveryOrigin{APIShieldDiscoveryOriginSessionIdentifier},
State: APIShieldDiscoveryStateSaved,
Features: map[string]any{
"traffic_stats": map[string]any{},
},
Expand All @@ -288,7 +274,7 @@ func TestListAPIShieldDiscoveryOperationsWithParams(t *testing.T) {
}
}

func TestPatchAPIShieldDiscoveryOperation(t *testing.T) {
func TestUpdateAPIShieldDiscoveryOperation(t *testing.T) {
setup()
t.Cleanup(teardown)

Expand Down Expand Up @@ -316,21 +302,19 @@ 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: APIShieldDiscoveryStateIgnored,
},
)

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

if assert.NoError(t, err) {
Expand All @@ -340,7 +324,7 @@ func TestPatchAPIShieldDiscoveryOperation(t *testing.T) {
assert.NoError(t, err)
}

func TestPatchAPIShieldDiscoveryOperations(t *testing.T) {
func TestUpdateAPIShieldDiscoveryOperations(t *testing.T) {
setup()
t.Cleanup(teardown)

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: APIShieldDiscoveryStateIgnored},
"c51c2ea1-a690-48fd-8e3f-7fc79b269947": UpdateAPIShieldDiscoveryOperation{State: APIShieldDiscoveryStateReview},
},
)

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: APIShieldDiscoveryStateIgnored},
"c51c2ea1-a690-48fd-8e3f-7fc79b269947": UpdateAPIShieldDiscoveryOperation{State: APIShieldDiscoveryStateReview},
}

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 80d9650

Please sign in to comment.