diff --git a/.apigentools-info b/.apigentools-info index 60c0c54e5ae..cd4a1f0246a 100644 --- a/.apigentools-info +++ b/.apigentools-info @@ -4,13 +4,13 @@ "spec_versions": { "v1": { "apigentools_version": "1.6.5", - "regenerated": "2023-09-06 11:51:26.107168", - "spec_repo_commit": "c59cafad" + "regenerated": "2023-09-06 12:26:32.656691", + "spec_repo_commit": "07ee6775" }, "v2": { "apigentools_version": "1.6.5", - "regenerated": "2023-09-06 11:51:26.121714", - "spec_repo_commit": "c59cafad" + "regenerated": "2023-09-06 12:26:32.675428", + "spec_repo_commit": "07ee6775" } } } \ No newline at end of file diff --git a/.generator/schemas/v2/openapi.yaml b/.generator/schemas/v2/openapi.yaml index 19fdbd6b3ea..05d2d226b42 100644 --- a/.generator/schemas/v2/openapi.yaml +++ b/.generator/schemas/v2/openapi.yaml @@ -18815,6 +18815,16 @@ paths: schema: example: created_by,monitor type: string + - $ref: '#/components/parameters/PageOffset' + - description: Maximum number of downtimes in the response. + example: 100 + in: query + name: page[limit] + required: false + schema: + default: 30 + format: int64 + type: integer responses: '200': content: @@ -18838,6 +18848,10 @@ paths: summary: Get all downtimes tags: - Downtimes + x-pagination: + limitParam: page[limit] + pageOffsetParam: page[offset] + resultsPath: data x-unstable: '**Note**: This endpoint is in private beta. If you have any feedback, contact [Datadog support](https://docs.datadoghq.com/help/).' diff --git a/api/datadogV2/api_downtimes.go b/api/datadogV2/api_downtimes.go index ca84ffa217c..997a980b18d 100644 --- a/api/datadogV2/api_downtimes.go +++ b/api/datadogV2/api_downtimes.go @@ -276,6 +276,8 @@ func (a *DowntimesApi) GetDowntime(ctx _context.Context, downtimeId string, o .. type ListDowntimesOptionalParameters struct { CurrentOnly *bool Include *string + PageOffset *int64 + PageLimit *int64 } // NewListDowntimesOptionalParameters creates an empty struct for parameters. @@ -296,6 +298,18 @@ func (r *ListDowntimesOptionalParameters) WithInclude(include string) *ListDownt return r } +// WithPageOffset sets the corresponding parameter name and returns the struct. +func (r *ListDowntimesOptionalParameters) WithPageOffset(pageOffset int64) *ListDowntimesOptionalParameters { + r.PageOffset = &pageOffset + return r +} + +// WithPageLimit sets the corresponding parameter name and returns the struct. +func (r *ListDowntimesOptionalParameters) WithPageLimit(pageLimit int64) *ListDowntimesOptionalParameters { + r.PageLimit = &pageLimit + return r +} + // ListDowntimes Get all downtimes. // Get all scheduled downtimes. func (a *DowntimesApi) ListDowntimes(ctx _context.Context, o ...ListDowntimesOptionalParameters) (ListDowntimesResponse, *_nethttp.Response, error) { @@ -336,6 +350,12 @@ func (a *DowntimesApi) ListDowntimes(ctx _context.Context, o ...ListDowntimesOpt if optionalParams.Include != nil { localVarQueryParams.Add("include", datadog.ParameterToString(*optionalParams.Include, "")) } + if optionalParams.PageOffset != nil { + localVarQueryParams.Add("page[offset]", datadog.ParameterToString(*optionalParams.PageOffset, "")) + } + if optionalParams.PageLimit != nil { + localVarQueryParams.Add("page[limit]", datadog.ParameterToString(*optionalParams.PageLimit, "")) + } localVarHeaderParams["Accept"] = "application/json" datadog.SetAuthKeys( @@ -387,6 +407,56 @@ func (a *DowntimesApi) ListDowntimes(ctx _context.Context, o ...ListDowntimesOpt return localVarReturnValue, localVarHTTPResponse, nil } +// ListDowntimesWithPagination provides a paginated version of ListDowntimes returning a channel with all items. +func (a *DowntimesApi) ListDowntimesWithPagination(ctx _context.Context, o ...ListDowntimesOptionalParameters) (<-chan datadog.PaginationResult[DowntimeResponseData], func()) { + ctx, cancel := _context.WithCancel(ctx) + pageSize_ := int64(30) + if len(o) == 0 { + o = append(o, ListDowntimesOptionalParameters{}) + } + if o[0].PageLimit != nil { + pageSize_ = *o[0].PageLimit + } + o[0].PageLimit = &pageSize_ + + items := make(chan datadog.PaginationResult[DowntimeResponseData], pageSize_) + go func() { + for { + resp, _, err := a.ListDowntimes(ctx, o...) + if err != nil { + var returnItem DowntimeResponseData + items <- datadog.PaginationResult[DowntimeResponseData]{returnItem, err} + break + } + respData, ok := resp.GetDataOk() + if !ok { + break + } + results := *respData + + for _, item := range results { + select { + case items <- datadog.PaginationResult[DowntimeResponseData]{item, nil}: + case <-ctx.Done(): + close(items) + return + } + } + if len(results) < int(pageSize_) { + break + } + if o[0].PageOffset == nil { + o[0].PageOffset = &pageSize_ + } else { + pageOffset_ := *o[0].PageOffset + pageSize_ + o[0].PageOffset = &pageOffset_ + } + } + close(items) + }() + return items, cancel +} + // ListMonitorDowntimes Get active downtimes for a monitor. // Get all active downtimes for the specified monitor. func (a *DowntimesApi) ListMonitorDowntimes(ctx _context.Context, monitorId int64) (MonitorDowntimeMatchResponse, *_nethttp.Response, error) { diff --git a/examples/v2/downtimes/ListDowntimes_805770330.go b/examples/v2/downtimes/ListDowntimes_805770330.go new file mode 100644 index 00000000000..3e6fae7827f --- /dev/null +++ b/examples/v2/downtimes/ListDowntimes_805770330.go @@ -0,0 +1,30 @@ +// Get all downtimes returns "OK" response with pagination + +package main + +import ( + "context" + "encoding/json" + "fmt" + "os" + + "github.com/DataDog/datadog-api-client-go/v2/api/datadog" + "github.com/DataDog/datadog-api-client-go/v2/api/datadogV2" +) + +func main() { + ctx := datadog.NewDefaultContext(context.Background()) + configuration := datadog.NewConfiguration() + configuration.SetUnstableOperationEnabled("v2.ListDowntimes", true) + apiClient := datadog.NewAPIClient(configuration) + api := datadogV2.NewDowntimesApi(apiClient) + resp, _ := api.ListDowntimesWithPagination(ctx, *datadogV2.NewListDowntimesOptionalParameters().WithPageLimit(2)) + + for paginationResult := range resp { + if paginationResult.Error != nil { + fmt.Fprintf(os.Stderr, "Error when calling `DowntimesApi.ListDowntimes`: %v\n", paginationResult.Error) + } + responseContent, _ := json.MarshalIndent(paginationResult.Item, "", " ") + fmt.Fprintf(os.Stdout, "%s\n", responseContent) + } +} diff --git a/tests/scenarios/cassettes/TestScenarios/v2/Feature_Downtimes/Scenario_Get_all_downtimes_returns_OK_response_with_pagination.freeze b/tests/scenarios/cassettes/TestScenarios/v2/Feature_Downtimes/Scenario_Get_all_downtimes_returns_OK_response_with_pagination.freeze new file mode 100644 index 00000000000..d76ab527dee --- /dev/null +++ b/tests/scenarios/cassettes/TestScenarios/v2/Feature_Downtimes/Scenario_Get_all_downtimes_returns_OK_response_with_pagination.freeze @@ -0,0 +1 @@ +2023-09-05T12:32:39.085Z \ No newline at end of file diff --git a/tests/scenarios/cassettes/TestScenarios/v2/Feature_Downtimes/Scenario_Get_all_downtimes_returns_OK_response_with_pagination.yaml b/tests/scenarios/cassettes/TestScenarios/v2/Feature_Downtimes/Scenario_Get_all_downtimes_returns_OK_response_with_pagination.yaml new file mode 100644 index 00000000000..7193d068615 --- /dev/null +++ b/tests/scenarios/cassettes/TestScenarios/v2/Feature_Downtimes/Scenario_Get_all_downtimes_returns_OK_response_with_pagination.yaml @@ -0,0 +1,41 @@ +interactions: +- request: + body: '' + form: {} + headers: + Accept: + - application/json + method: GET + url: https://api.datadoghq.com/api/v2/downtime?page%5Blimit%5D=2 + response: + body: '{"data":[{"type":"downtime","attributes":{"mute_first_recovery_notification":false,"canceled":null,"monitor_identifier":{"monitor_tags":["*"]},"schedule":{"start":"2023-05-22T03:06:54.072998+00:00","end":null},"notify_end_types":["expired"],"notify_end_states":["no + data","warn","alert"],"status":"active","scope":"host:\"java-hostsMuteErrorsTest-local-1684724813\"","created":"2023-05-22T03:06:54.079122+00:00","display_timezone":"UTC","message":null,"modified":"2023-05-22T03:06:54.079122+00:00"},"id":"b4613732-f84d-11ed-a766-da7ad0900002"},{"type":"downtime","attributes":{"mute_first_recovery_notification":false,"canceled":null,"monitor_identifier":{"monitor_tags":["*"]},"schedule":{"start":"2023-05-23T03:21:54.687109+00:00","end":null},"notify_end_types":["expired"],"notify_end_states":["no + data","warn","alert"],"status":"active","scope":"host:\"java-hostsMuteErrorsTest-local-1684812114\"","created":"2023-05-23T03:21:54.690618+00:00","display_timezone":"UTC","message":null,"modified":"2023-05-23T03:21:54.690618+00:00"},"id":"f799770a-f918-11ed-8b48-da7ad0900002"}],"meta":{"page":{"total_filtered_count":3}}} + + ' + code: 200 + duration: '' + headers: + Content-Type: + - application/json + status: 200 OK +- request: + body: '' + form: {} + headers: + Accept: + - application/json + method: GET + url: https://api.datadoghq.com/api/v2/downtime?page%5Blimit%5D=2&page%5Boffset%5D=2 + response: + body: '{"data":[{"type":"downtime","attributes":{"modified":"2023-05-24T03:29:35.343207+00:00","created":"2023-05-24T03:29:35.343207+00:00","canceled":null,"status":"active","scope":"host:\"java-hostsMuteErrorsTest-local-1684898975\"","display_timezone":"UTC","schedule":{"end":null,"start":"2023-05-24T03:29:35.340446+00:00"},"message":null,"mute_first_recovery_notification":false,"notify_end_types":["expired"],"notify_end_states":["warn","no + data","alert"],"monitor_identifier":{"monitor_tags":["*"]}},"id":"34953930-f9e3-11ed-85d4-da7ad0900002"}],"meta":{"page":{"total_filtered_count":3}}} + + ' + code: 200 + duration: '' + headers: + Content-Type: + - application/json + status: 200 OK +version: 1 diff --git a/tests/scenarios/features/v2/downtimes.feature b/tests/scenarios/features/v2/downtimes.feature index 7f8a5bcdbd6..35c4a65f020 100644 --- a/tests/scenarios/features/v2/downtimes.feature +++ b/tests/scenarios/features/v2/downtimes.feature @@ -98,6 +98,15 @@ Feature: Downtimes Then the response status is 200 OK And the response "data" has item with field "id" with value "1dcb33f8-b23a-11ed-ae77-da7ad0900002" + @replay-only @skip-validation @team:DataDog/monitor-app @with-pagination + Scenario: Get all downtimes returns "OK" response with pagination + Given operation "ListDowntimes" enabled + And new "ListDowntimes" request + And request contains "page[limit]" parameter with value 2 + When the request with pagination is sent + Then the response status is 200 OK + And the response has 3 items + @skip-validation @team:DataDog/monitor-app Scenario: Schedule a downtime returns "Bad Request" response Given new "CreateDowntime" request