Skip to content

Commit

Permalink
Add pagination parameters to downtimes listing (#2167)
Browse files Browse the repository at this point in the history
Co-authored-by: ci.datadog-api-spec <packages@datadoghq.com>
  • Loading branch information
api-clients-generation-pipeline[bot] and ci.datadog-api-spec authored Sep 6, 2023
1 parent 6d5f22d commit 6f0d94c
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 4 deletions.
8 changes: 4 additions & 4 deletions .apigentools-info
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
14 changes: 14 additions & 0 deletions .generator/schemas/v2/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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/).'
Expand Down
70 changes: 70 additions & 0 deletions api/datadogV2/api_downtimes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down
30 changes: 30 additions & 0 deletions examples/v2/downtimes/ListDowntimes_805770330.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2023-09-05T12:32:39.085Z
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions tests/scenarios/features/v2/downtimes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6f0d94c

Please sign in to comment.