Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Get/Patch API Shield API Discovery Operations #1413

Merged
merged 3 commits into from Oct 5, 2023

Conversation

djhworld
Copy link
Contributor

@djhworld djhworld commented Sep 29, 2023

This change adds support for the following API Shield related endpoints related to API Discovery:

Description

This change adds support for API Shield endpoints related to API Shield API Discovery. This is so they can be used from the library and will enable this feature to be added to terraform in the future.

Has your change been tested?

  • Unit tests
  • Local testing

Types of changes

What sort of change does your code introduce/modify?

  • New feature (non-breaking change which adds functionality)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • This change is using publicly documented in cloudflare/api-schemas
    and relies on stable APIs. (see API Shield API Discovery)

@djhworld djhworld changed the title APISHI-2365 Add support for Get/Patch API Shield API Discovery Operations Add support for Get/Patch API Shield API Discovery Operations Sep 29, 2023
@github-actions
Copy link
Contributor

changelog detected ✅

…ions

This change adds support for the following API Shield related endpoints related to API Discovery:

- Retrieve discovered operations on a zone
- Patch discovered operations
- Patch discovered operation
@djhworld djhworld marked this pull request as ready for review October 3, 2023 16:41
@djhworld
Copy link
Contributor Author

djhworld commented Oct 3, 2023

.changelog/1413.txt Outdated Show resolved Hide resolved
// PatchAPIShieldDiscoveryOperation 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of Patch, we prefer to use Update unless the HTTP method is important context to have for the caller. more details in https://github.com/cloudflare/cloudflare-go/blob/master/docs/experimental.md#consistent-crud-method-signatures

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense to me - thanks!

@jacobbednarz
Copy link
Member

i went ahead and updated this to match the library conventions from the code review. feel free to take a look and let me know if anything doesn't make sense. otherwise, i'll merge this one in.

Copy link

@janrueth janrueth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I'd provide consts for enum like filter conditions that are modeled here as strings

// Origin represents the API discovery engine(s) that discovered this operation
Origin []string `json:"origin"`
// State represents the state of operation in API Discovery
State string `json:"state"`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we provide the set of valid state strings as consts that one can pass here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

entirely up to you folks. we do half and half in other places but once we do SDK generation, we will have allowed enums.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have opted to add typed field + consts for these fields 80d9650

api_shield_api_discovery.go Outdated Show resolved Hide resolved
api_shield_api_discovery.go Outdated Show resolved Hide resolved
api_shield_api_discovery.go Outdated Show resolved Hide resolved
api_shield_api_discovery.go Outdated Show resolved Hide resolved
// API documentation: https://developers.cloudflare.com/api/operations/api-shield-api-patch-discovered-operation
type UpdateAPIShieldDiscoveryOperationParams struct {
// OperationID is the operation to be patched
OperationID string `json:"-" url:"-"`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this not a uuid?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we pass string for the ID field in normal operations too https://github.com/cloudflare/cloudflare-go/blob/master/api_shield_operations.go#L13-L18

I'm not sure if it's a good idea to enforce the type here or not (we'd need cloudflare-go to depend on a uuid library like github.com/google/uuid)

@jacobbednarz what do you think? I'm inclined to just stick with string. If a non uuid is provided then the API will respond with an invalid input error

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string seems fine to me here.

api_shield_api_discovery.go Show resolved Hide resolved
api_shield_api_discovery.go Outdated Show resolved Hide resolved

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

res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, params)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to check: Does the .makeRequestContext function add the respective request body and corresponding headers?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it know that it should only add the state field and not the operation ID to the request body?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or should we rather pass the UpdateAPIShieldDiscoveryOperation type here?

Copy link
Contributor Author

@djhworld djhworld Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the params (of type UpdateAPIShieldDiscoveryOperationParams) is what gets put in the body.

The params struct is serialized to JSON here

cloudflare-go/cloudflare.go

Lines 222 to 227 in 217beeb

var jsonBody []byte
jsonBody, err = json.Marshal(params)
if err != nil {
return nil, fmt.Errorf("error marshalling params to JSON: %w", err)
}
reqBody = bytes.NewReader(jsonBody)

and the content-type header is set to application/json if none is set here

cloudflare-go/cloudflare.go

Lines 382 to 384 in 217beeb

if req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", "application/json")
}

The operation ID does not get serialized as we set the json tag to - https://github.com/cloudflare/cloudflare-go/pull/1413/files#diff-a8b0a1c475237e4f8ba6786369d22fa9008203858112922a8a8c1426ce423e6dR60

@djhworld
Copy link
Contributor Author

djhworld commented Oct 5, 2023

@jacobbednarz We're happy for this to be merged

@jacobbednarz jacobbednarz merged commit 69ddaca into cloudflare:master Oct 5, 2023
11 checks passed
@github-actions github-actions bot added this to the v0.79.0 milestone Oct 5, 2023
github-actions bot pushed a commit that referenced this pull request Oct 5, 2023
@github-actions
Copy link
Contributor

This functionality has been released in v0.79.0.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants