[TT-11966/TT-12064] refactor oas events, update contract#6263
Conversation
|
API Changes --- prev.txt 2024-05-09 15:15:50.111713571 +0000
+++ current.txt 2024-05-09 15:15:47.239713082 +0000
@@ -2420,6 +2420,9 @@
)
Enumerated constants for inputs and conditions.
+const WebhookKind = event.WebhookKind
+ WebhookKind is an alias maintained to be used in imports.
+
VARIABLES
@@ -2996,13 +2999,13 @@
func (et *EnforceTimeout) Fill(meta apidef.HardTimeoutMeta)
Fill fills *EnforceTimeout from apidef.HardTimeoutMeta.
-type Event struct {
+type EventHandler struct {
// Enabled enables the event handler.
Enabled bool `json:"enabled" bson:"enabled"`
- // Type specifies the TykEvent that should trigger the event handler.
- Type event.Event `json:"type" bson:"type"`
- // Action specifies the action to be taken on the event trigger.
- Action event.Action `json:"action" bson:"action"`
+ // Trigger specifies the TykEvent that should trigger the event handler.
+ Trigger event.Event `json:"trigger" bson:"trigger"`
+ // Kind specifies the action to be taken on the event trigger.
+ Kind Kind `json:"type" bson:"type"` // json tag is changed as per contract
// ID is the ID of event handler in storage.
ID string `json:"id,omitempty" bson:"id,omitempty"`
// Name is the name of event handler
@@ -3010,26 +3013,27 @@
Webhook WebhookEvent `bson:"-" json:"-"`
}
- Event holds information about individual event to be configured on the API.
+ EventHandler holds information about individual event to be configured on
+ the API.
-func (e *Event) GetWebhookConf() apidef.WebHookHandlerConf
- GetWebhookConf converts Event.WebhookEvent apidef.WebHookHandlerConf.
+func (e *EventHandler) GetWebhookConf() apidef.WebHookHandlerConf
+ GetWebhookConf converts EventHandler.WebhookEvent apidef.WebHookHandlerConf.
-func (e *Event) MarshalJSON() ([]byte, error)
- MarshalJSON marshals Event as per Tyk OAS API definition contract.
+func (e EventHandler) MarshalJSON() ([]byte, error)
+ MarshalJSON marshals EventHandler as per Tyk OAS API definition contract.
-func (e *Event) UnmarshalJSON(in []byte) error
- UnmarshalJSON unmarshals Event as per Tyk OAS API definition contract.
+func (e *EventHandler) UnmarshalJSON(in []byte) error
+ UnmarshalJSON unmarshal EventHandler as per Tyk OAS API definition contract.
-type Events []Event
- Events holds the list of events to be processed for the API.
+type EventHandlers []EventHandler
+ EventHandlers holds the list of events to be processed for the API.
-func (e *Events) ExtractTo(api *apidef.APIDefinition)
- ExtractTo extracts events to apidef.APIDefinition.
+func (e *EventHandlers) ExtractTo(api *apidef.APIDefinition)
+ ExtractTo EventHandlers events to apidef.APIDefinition.
-func (e *Events) Fill(api apidef.APIDefinition)
- Fill fills Events from classic API definition. Currently only webhook events
- are supported.
+func (e *EventHandlers) Fill(api apidef.APIDefinition)
+ Fill fills EventHandlers from classic API definition. Currently only webhook
+ events are supported.
type ExternalOAuth struct {
Enabled bool `bson:"enabled" json:"enabled"` // required
@@ -3196,6 +3200,15 @@
}
Header holds a header name and value pair.
+type Headers []Header
+ Headers is an array of Header.
+
+func NewHeaders(in map[string]string) Headers
+ NewHeaders creates Headers from in map.
+
+func (hs Headers) Map() map[string]string
+ Map transforms Headers into a map.
+
type IDExtractor struct {
// Enabled activates ID extractor with coprocess authentication.
Enabled bool `bson:"enabled" json:"enabled"` // required
@@ -3363,6 +3376,9 @@
func (j *JWTValidation) Fill(jwt apidef.JWTValidation)
+type Kind = event.Kind
+ Kind is an alias maintained to be used in imports.
+
type ListenPath struct {
// Value is the value of the listen path e.g. `/api/` or `/` or `/httpbin/`.
// Tyk classic API definition: `proxy.listen_path`
@@ -3411,7 +3427,7 @@
// Body is the HTTP response body that will be returned.
Body string `bson:"body,omitempty" json:"body,omitempty"`
// Headers are the HTTP response headers that will be returned.
- Headers []Header `bson:"headers,omitempty" json:"headers,omitempty"`
+ Headers Headers `bson:"headers,omitempty" json:"headers,omitempty"`
// FromOASExamples is the configuration to extract a mock response from OAS documentation.
FromOASExamples *FromOASExamples `bson:"fromOASExamples,omitempty" json:"fromOASExamples,omitempty"`
}
@@ -3838,7 +3854,7 @@
// be considered as 0s/empty.
//
// Tyk classic API definition: `global_rate_limit.per`.
- Per string `json:"per" bson:"per"`
+ Per time.ReadableDuration `json:"per" bson:"per"`
}
RateLimit holds the configurations related to rate limit. The API-level rate
limit applies a base-line limit on the frequency of requests to the upstream
@@ -3852,6 +3868,9 @@
func (r *RateLimit) Fill(api apidef.APIDefinition)
Fill fills *RateLimit from apidef.APIDefinition.
+type ReadableDuration = time.ReadableDuration
+ ReadableDuration is an alias maintained to be used in imports.
+
type RequestSizeLimit struct {
// Enabled activates the Request Size Limit functionality.
Enabled bool `bson:"enabled" json:"enabled"`
@@ -3954,7 +3973,7 @@
// Events contains the configuration related to Tyk Events.
//
// Tyk classic API definition: `event_handlers`
- Events Events `bson:"events,omitempty" json:"events,omitempty"`
+ EventHandlers EventHandlers `bson:"eventHandlers,omitempty" json:"eventHandlers,omitempty"`
}
Server contains the configuration that sets Tyk up to receive requests from
the client applications.
@@ -4206,7 +4225,7 @@
// Remove specifies header names to be removed from the request/response.
Remove []string `bson:"remove,omitempty" json:"remove,omitempty"`
// Add specifies headers to be added to the request/response.
- Add []Header `bson:"add,omitempty" json:"add,omitempty"`
+ Add Headers `bson:"add,omitempty" json:"add,omitempty"`
}
TransformHeaders holds configuration about request/response header
transformations.
@@ -4474,11 +4493,31 @@
that were previously used to represent the same data.
type WebhookEvent struct {
- URL string `json:"url" bson:"url"`
- Method string `json:"method" bson:"method"`
- Timeout int64 `json:"timeout" bson:"timeout"`
- BodyTemplate string `json:"bodyTemplate,omitempty" bson:"bodyTemplate,omitempty"`
- Headers map[string]string `json:"headers,omitempty" bson:"headers,omitempty"`
+ // URL is the target URL for the webhook.
+ URL string `json:"url" bson:"url"`
+ // Method is the HTTP method for the webhook.
+ Method string `json:"method" bson:"method"`
+ // CoolDownPeriod defines cool-down for the event, so it does not trigger again.
+ // It uses shorthand notation.
+ // The value of CoolDownPeriod is a string that specifies the interval in a compact form,
+ // where hours, minutes and seconds are denoted by 'h', 'm' and 's' respectively.
+ // Multiple units can be combined to represent the duration.
+ //
+ // Examples of valid shorthand notations:
+ // - "1h" : one hour
+ // - "20m" : twenty minutes
+ // - "30s" : thirty seconds
+ // - "1m29s": one minute and twenty-nine seconds
+ // - "1h30m" : one hour and thirty minutes
+ //
+ // An empty value is interpreted as "0s", implying no cool-down.
+ // It's important to format the string correctly, as invalid formats will
+ // be considered as 0s/empty.
+ CoolDownPeriod time.ReadableDuration `json:"coolDownPeriod" bson:"coolDownPeriod"`
+ // BodyTemplate is the template to be used for request payload.
+ BodyTemplate string `json:"bodyTemplate,omitempty" bson:"bodyTemplate,omitempty"`
+ // Headers are the list of request headers to be used.
+ Headers Headers `json:"headers,omitempty" bson:"headers,omitempty"`
}
WebhookEvent stores the core information about a webhook event.
|
|
PR Description updated to latest commit (e465b9e) |
PR Review 🔍
Code feedback:
|
PR Code Suggestions ✨
|
💥 CI tests failed 🙈git-stateall okPlease look at the run or in the Checks tab. |
- update schema.
refactor OAS contracts - rename Action to Kind(type) - rename type to trigger.
f01b8ec to
dfe9792
Compare
dfe9792 to
e0a59b5
Compare
💥 CI tests failed 🙈git-stateall okPlease look at the run or in the Checks tab. |
083db91 to
e731c72
Compare
### **User description** <!-- Provide a general summary of your changes in the Title above --> ## Description <!-- Describe your changes in detail --> ## Related Issue Parent: https://tyktech.atlassian.net/browse/TT-11966 Subtask: [TT-12064](https://tyktech.atlassian.net/browse/TT-12064) ## Motivation and Context <!-- Why is this change required? What problem does it solve? --> ## How This Has Been Tested <!-- Please describe in detail how you tested your changes --> <!-- Include details of your testing environment, and the tests --> <!-- you ran to see how your change affects other areas of the code, etc. --> <!-- This information is helpful for reviewers and QA. --> ## Screenshots (if appropriate) ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Refactoring or add test (improvements in base code or adds test coverage to functionality) ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply --> <!-- If there are no documentation updates required, mark the item as checked. --> <!-- Raise up any additional concerns not covered by the checklist. --> - [ ] I ensured that the documentation is up to date - [ ] I explained why this PR updates go.mod in detail with reasoning why it's required - [ ] I would like a code coverage CI quality gate exception and have explained why ___ ### **PR Type** enhancement ___ ### **Description** - Introduced a new type `ReadableDuration` to handle duration fields in a user-friendly format. - Updated `WebhookEvent`, `RateLimit`, and related tests to use `ReadableDuration`. - Simplified duration parsing and formatting throughout the codebase using the new type. - Added comprehensive tests for the new `ReadableDuration` type. ___ ### **Changes walkthrough** 📝 <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Enhancement </strong></td><td><table> <tr> <td> <details> <summary><strong>event.go</strong><dd><code>Use ReadableDuration for WebhookEvent CoolDownPeriod</code> </dd></summary> <hr> apidef/oas/event.go <li>Introduced <code>ReadableDuration</code> type for <code>CoolDownPeriod</code> in <code>WebhookEvent</code>.<br> <li> Simplified <code>GetWebhookConf</code> by directly using <code>Seconds()</code> method from <br><code>ReadableDuration</code>.<br> <li> Adjusted <code>Fill</code> method to use <code>ReadableDuration</code> for initializing <br><code>CoolDownPeriod</code>.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6265/files#diff-528a9f5b311ff21c0b3a9b273e61398209ca8b51550327e4d437bba81e49d577">+10/-14</a> </td> </tr> <tr> <td> <details> <summary><strong>time.go</strong><dd><code>Define ReadableDuration Type Alias</code> </dd></summary> <hr> apidef/oas/time.go <li>Created a new file to define <code>ReadableDuration</code> as an alias for <br><code>time.Duration</code>.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6265/files#diff-56f5ed8764cba1fab5cc4615f02a769b2c9124e69b5972cb1b7921d1bded577b">+6/-0</a> </td> </tr> <tr> <td> <details> <summary><strong>upstream.go</strong><dd><code>Implement ReadableDuration in Upstream RateLimit</code> </dd></summary> <hr> apidef/oas/upstream.go <li>Replaced string type with <code>ReadableDuration</code> for <code>Per</code> in <code>RateLimit</code>.<br> <li> Simplified <code>Fill</code> and <code>ExtractTo</code> methods by using <code>ReadableDuration</code>.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6265/files#diff-7b0941c7f37fe5a2a23047e0822a65519ca11c371660f36555b59a60f000e3f4">+4/-12</a> </td> </tr> <tr> <td> <details> <summary><strong>duration.go</strong><dd><code>Implement Custom ReadableDuration Type</code> </dd></summary> <hr> internal/time/duration.go <li>Implemented <code>ReadableDuration</code> type with custom JSON marshal/unmarshal <br>methods.<br> <li> Added methods for parsing and formatting durations in a user-friendly <br>format.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6265/files#diff-6e8ef3118f84cbcc935f27d5a3ad5f4eb86eb22728400e9322c9b796b9d8d855">+57/-0</a> </td> </tr> </table></td></tr><tr><td><strong>Tests </strong></td><td><table> <tr> <td> <details> <summary><strong>event_test.go</strong><dd><code>Update Event Tests to Use ReadableDuration</code> </dd></summary> <hr> apidef/oas/event_test.go - Updated test cases to use `ReadableDuration` for `CoolDownPeriod`. </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6265/files#diff-a1f19f96579a470e73c131a0e37895da0c21d378f6aa48067608274564e62da7">+8/-7</a> </td> </tr> <tr> <td> <details> <summary><strong>linter_test.go</strong><dd><code>Update Linter Tests for ReadableDuration Usage</code> </dd></summary> <hr> apidef/oas/linter_test.go <li>Updated linter tests to use <code>ReadableDuration</code> for <code>CoolDownPeriod</code> and <br><code>Per</code>.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6265/files#diff-b92239afd81e77a829fe7fe8410044dfd4dfda525d17dbf5f8811714a9c986d3">+3/-2</a> </td> </tr> <tr> <td> <details> <summary><strong>upstream_test.go</strong><dd><code>Update Upstream Tests for ReadableDuration</code> </dd></summary> <hr> apidef/oas/upstream_test.go - Updated tests to use `ReadableDuration` for `Per` in `RateLimit`. </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6265/files#diff-222cc254c0c6c09fa0cf50087860b837a0873e2aef3c84ec7d80b1014c149057">+2/-24</a> </td> </tr> <tr> <td> <details> <summary><strong>duration_test.go</strong><dd><code>Test Custom ReadableDuration Type</code> </dd></summary> <hr> internal/time/duration_test.go <li>Added tests for marshaling and unmarshaling <code>ReadableDuration</code>.<br> <li> Tested parsing and formatting of various duration strings.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6265/files#diff-71942cdc77128266498b62e712f82d0c63bbb39d236fe9e6677f49080c28cea1">+59/-0</a> </td> </tr> </table></td></tr></tr></tbody></table> ___ > 💡 **PR-Agent usage**: >Comment `/help` on the PR to get a list of all available PR-Agent tools and their descriptions
|
### **User description** - refactor OAS events - update OAS schema - update Events contract - introduce ReadableTimeout to make use of shorthand notation for time.Duration in json contracts. ## Related Issue Parent task: https://tyktech.atlassian.net/browse/TT-11966 Sub task: https://tyktech.atlassian.net/browse/TT-12064 ## Motivation and Context <!-- Why is this change required? What problem does it solve? --> ## How This Has Been Tested <!-- Please describe in detail how you tested your changes --> <!-- Include details of your testing environment, and the tests --> <!-- you ran to see how your change affects other areas of the code, etc. --> <!-- This information is helpful for reviewers and QA. --> ## Screenshots (if appropriate) ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Refactoring or add test (improvements in base code or adds test coverage to functionality) ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply --> <!-- If there are no documentation updates required, mark the item as checked. --> <!-- Raise up any additional concerns not covered by the checklist. --> - [ ] I ensured that the documentation is up to date - [ ] I explained why this PR updates go.mod in detail with reasoning why it's required - [ ] I would like a code coverage CI quality gate exception and have explained why ___ ### **PR Type** enhancement ___ ### **Description** - Introduced new structures (`Event` and `WebhookEvent`) in `event.go` to handle API events according to OAS specifications. - Implemented JSON marshaling/unmarshaling for these structures to ensure compatibility with OAS. - Developed methods to convert and manage event configurations, facilitating integration with existing API definitions. - Added comprehensive unit tests in `event_test.go` to validate the functionality of the new event handling features. - Updated the OAS schema in `x-tyk-api-gateway.json` to reflect the new event handling capabilities, ensuring accurate API documentation and client generation. ___ ### **Changes walkthrough** 📝 <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Enhancement </strong></td><td><table> <tr> <td> <details> <summary><strong>event.go</strong><dd><code>Implement Event Handling Structures and Methods</code> </dd></summary> <hr> apidef/oas/event.go <li>Introduced new <code>Event</code> and <code>WebhookEvent</code> structures for handling API <br>events.<br> <li> Added JSON marshaling and unmarshaling for these structures to align <br>with the OAS API definition.<br> <li> Implemented methods to convert between internal event configurations <br>and the new OAS structure.<br> <li> Added comprehensive methods to manage event lists, including filling <br>from and extracting to API definitions.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6263/files#diff-528a9f5b311ff21c0b3a9b273e61398209ca8b51550327e4d437bba81e49d577">+193/-0</a> </td> </tr> <tr> <td> <details> <summary><strong>x-tyk-api-gateway.json</strong><dd><code>Update OAS Schema for Enhanced Event Definitions</code> </dd></summary> <hr> apidef/oas/schema/x-tyk-api-gateway.json <li>Updated the OAS schema to include definitions for events with and <br>without IDs.<br> <li> Added new properties and required fields to the webhook event <br>definitions.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6263/files#diff-78828969c0c04cc1a776dfc93a8bad3c499a8c83e6169f83e96d090bed3e7dd0">+70/-51</a> </td> </tr> </table></td></tr><tr><td><strong>Tests </strong></td><td><table> <tr> <td> <details> <summary><strong>event_test.go</strong><dd><code>Unit Tests for Event Handling in OAS</code> </dd></summary> <hr> apidef/oas/event_test.go <li>Added extensive unit tests for the new event handling features.<br> <li> Tests cover the conversion between internal and OAS event <br>configurations.<br> <li> Ensures correct behavior for both enabling and disabling events, and <br>for handling webhook-specific settings.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6263/files#diff-a1f19f96579a470e73c131a0e37895da0c21d378f6aa48067608274564e62da7">+222/-0</a> </td> </tr> </table></td></tr></tr></tbody></table> ___ > 💡 **PR-Agent usage**: >Comment `/help` on the PR to get a list of all available PR-Agent tools and their descriptions



User description
refactor OAS events
update OAS schema
update Events contract
introduce ReadableTimeout to make use of shorthand notation for time.Duration in json contracts.
Related Issue
Parent task: https://tyktech.atlassian.net/browse/TT-11966
Sub task: https://tyktech.atlassian.net/browse/TT-12064
Motivation and Context
How This Has Been Tested
Screenshots (if appropriate)
Types of changes
Checklist
PR Type
enhancement
Description
EventandWebhookEvent) inevent.goto handle API events according to OAS specifications.event_test.goto validate the functionality of the new event handling features.x-tyk-api-gateway.jsonto reflect the new event handling capabilities, ensuring accurate API documentation and client generation.Changes walkthrough 📝
event.go
Implement Event Handling Structures and Methodsapidef/oas/event.go
EventandWebhookEventstructures for handling APIevents.
with the OAS API definition.
and the new OAS structure.
from and extracting to API definitions.
x-tyk-api-gateway.json
Update OAS Schema for Enhanced Event Definitionsapidef/oas/schema/x-tyk-api-gateway.json
without IDs.
definitions.
event_test.go
Unit Tests for Event Handling in OASapidef/oas/event_test.go
configurations.
for handling webhook-specific settings.