Skip to content
This repository was archived by the owner on Nov 24, 2025. It is now read-only.

Commit 5985a56

Browse files
authored
Add TO Client API for Delivery Services Required Capabilities (#6233)
* added test for pagination support * added newline at end of file
1 parent 726401a commit 5985a56

3 files changed

Lines changed: 116 additions & 18 deletions

File tree

traffic_ops/testing/api/v4/deliveryservices_required_capabilities_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020
"net/http"
2121
"net/url"
22+
"reflect"
2223
"strconv"
2324
"strings"
2425
"testing"
@@ -41,6 +42,7 @@ func TestDeliveryServicesRequiredCapabilities(t *testing.T) {
4142
header = make(map[string][]string)
4243
header.Set(rfc.IfModifiedSince, time)
4344
GetTestDeliveryServicesRequiredCapabilitiesIMSAfterChange(t, header)
45+
GetTestPaginationSupportDsrc(t)
4446
})
4547
}
4648

@@ -645,3 +647,71 @@ func helperGetDeliveryServiceID(t *testing.T, xmlID *string) *int {
645647
}
646648
return ds.Response[0].ID
647649
}
650+
651+
func GetTestPaginationSupportDsrc(t *testing.T) {
652+
opts := client.NewRequestOptions()
653+
opts.QueryParameters.Set("orderby", "requiredCapability")
654+
resp, _, err := TOSession.GetDeliveryServicesRequiredCapabilities(opts)
655+
if err != nil {
656+
t.Fatalf("cannot Get DeliveryServicesRequiredCapabilities: %v - alerts: %+v", err, resp.Alerts)
657+
}
658+
dsrc := resp.Response
659+
if len(dsrc) < 3 {
660+
t.Fatalf("Need at least 3 DeliveryServicesRequiredCapabilities in Traffic Ops to test pagination support, found: %d", len(dsrc))
661+
}
662+
opts.QueryParameters.Set("limit", "1")
663+
dsrcWithLimit, _, err := TOSession.GetDeliveryServicesRequiredCapabilities(opts)
664+
if err != nil {
665+
t.Fatalf("cannot Get DeliveryServicesRequiredCapabilities with Limit: %v - alerts: %+v", err, dsrcWithLimit.Alerts)
666+
}
667+
if !reflect.DeepEqual(dsrc[:1], dsrcWithLimit.Response) {
668+
t.Error("expected GET DeliveryServicesRequiredCapabilities with limit = 1 to return first result")
669+
}
670+
671+
opts.QueryParameters.Set("offset", "1")
672+
dsrcWithOffset, _, err := TOSession.GetDeliveryServicesRequiredCapabilities(opts)
673+
if err != nil {
674+
t.Fatalf("cannot Get DeliveryServicesRequiredCapabilities with Limit and Offset: %v - alerts: %+v", err, dsrcWithOffset.Alerts)
675+
}
676+
if !reflect.DeepEqual(dsrc[1:2], dsrcWithOffset.Response) {
677+
t.Error("expected GET DeliveryServicesRequiredCapabilities with limit = 1, offset = 1 to return second result")
678+
}
679+
680+
opts.QueryParameters.Del("offset")
681+
opts.QueryParameters.Set("page", "2")
682+
dsrcWithPage, _, err := TOSession.GetDeliveryServicesRequiredCapabilities(opts)
683+
if err != nil {
684+
t.Fatalf("cannot Get DeliveryServicesRequiredCapabilities with Limit and Page: %v - alerts: %+v", err, dsrcWithPage.Alerts)
685+
}
686+
if !reflect.DeepEqual(dsrc[1:2], dsrcWithPage.Response) {
687+
t.Error("expected GET DeliveryServicesRequiredCapabilities with limit = 1, page = 2 to return second result")
688+
}
689+
690+
opts.QueryParameters = url.Values{}
691+
opts.QueryParameters.Set("limit", "-2")
692+
resp, _, err = TOSession.GetDeliveryServicesRequiredCapabilities(opts)
693+
if err == nil {
694+
t.Error("expected GET DeliveryServicesRequiredCapabilities to return an error when limit is not bigger than -1")
695+
} else if !alertsHaveError(resp.Alerts.Alerts, "must be bigger than -1") {
696+
t.Errorf("expected GET DeliveryServicesRequiredCapabilities to return an error for limit is not bigger than -1, actual error: %v - alerts: %+v", err, resp.Alerts)
697+
}
698+
699+
opts.QueryParameters.Set("limit", "1")
700+
opts.QueryParameters.Set("offset", "0")
701+
resp, _, err = TOSession.GetDeliveryServicesRequiredCapabilities(opts)
702+
if err == nil {
703+
t.Error("expected GET DeliveryServicesRequiredCapabilities to return an error when offset is not a positive integer")
704+
} else if !alertsHaveError(resp.Alerts.Alerts, "must be a positive integer") {
705+
t.Errorf("expected GET DeliveryServicesRequiredCapabilities to return an error for offset is not a positive integer, actual error: %v - alerts: %+v", err, resp.Alerts)
706+
}
707+
708+
opts.QueryParameters = url.Values{}
709+
opts.QueryParameters.Set("limit", "1")
710+
opts.QueryParameters.Set("page", "0")
711+
resp, _, err = TOSession.GetDeliveryServicesRequiredCapabilities(opts)
712+
if err == nil {
713+
t.Error("expected GET DeliveryServicesRequiredCapabilities to return an error when page is not a positive integer")
714+
} else if !alertsHaveError(resp.Alerts.Alerts, "must be a positive integer") {
715+
t.Errorf("expected GET DeliveryServicesRequiredCapabilities to return an error for page is not a positive integer, actual error: %v - alerts: %+v", err, resp.Alerts)
716+
}
717+
}

traffic_ops/testing/api/v4/tc-fixtures.json

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,11 @@
484484
"cdnName": "cdn1",
485485
"ccrDnsTtl": 3600,
486486
"checkPath": "",
487-
"consistentHashQueryParams": ["fmt", "limit", "somethingelse"],
487+
"consistentHashQueryParams": [
488+
"fmt",
489+
"limit",
490+
"somethingelse"
491+
],
488492
"deepCachingType": "NEVER",
489493
"displayName": "d s 1",
490494
"dnsBypassCname": null,
@@ -675,7 +679,11 @@
675679
"cdnName": "cdn1",
676680
"ccrDnsTtl": 3600,
677681
"checkPath": "",
678-
"consistentHashQueryParams": ["a", "b", "c"],
682+
"consistentHashQueryParams": [
683+
"a",
684+
"b",
685+
"c"
686+
],
679687
"consistentHashRegex": "foo",
680688
"deepCachingType": "ALWAYS",
681689
"displayName": "ds-test-minor-versions",
@@ -1720,25 +1728,25 @@
17201728
"dsName": "ds1",
17211729
"typeName": "HOST_REGEXP",
17221730
"setNumber": 1,
1723-
"pattern" : ".*"
1731+
"pattern": ".*"
17241732
},
17251733
{
17261734
"dsName": "ds1",
17271735
"typeName": "HOST_REGEXP",
17281736
"setNumber": 2,
1729-
"pattern" : "\\d+"
1737+
"pattern": "\\d+"
17301738
},
17311739
{
17321740
"dsName": "ds2",
17331741
"typeName": "HOST_REGEXP",
17341742
"setNumber": 1,
1735-
"pattern" : ".*"
1743+
"pattern": ".*"
17361744
},
17371745
{
17381746
"dsName": "ds1",
17391747
"typeName": "HOST_REGEXP",
17401748
"setNumber": 3,
1741-
"pattern" : ""
1749+
"pattern": ""
17421750
}
17431751
],
17441752
"deliveryservicesRequiredCapabilities": [
@@ -1838,10 +1846,10 @@
18381846
"ipAddress": "1.2.3.4",
18391847
"ip6Address": "dead:beef:cafe::42",
18401848
"port": 1234,
1841-
"Profile" : "ATS_EDGE_TIER_CACHE",
1849+
"Profile": "ATS_EDGE_TIER_CACHE",
18421850
"protocol": "http",
18431851
"tenant": "tenant1",
1844-
"isPrimary" : true
1852+
"isPrimary": true
18451853
},
18461854
{
18471855
"name": "origin2",
@@ -5039,15 +5047,22 @@
50395047
},
50405048
{
50415049
"cachegroup": "parentCachegroup",
5042-
"parents": [0]
5050+
"parents": [
5051+
0
5052+
]
50435053
},
50445054
{
50455055
"cachegroup": "secondaryCachegroup",
5046-
"parents": [0]
5056+
"parents": [
5057+
0
5058+
]
50475059
},
50485060
{
50495061
"cachegroup": "cachegroup3",
5050-
"parents": [1, 2]
5062+
"parents": [
5063+
1,
5064+
2
5065+
]
50515066
}
50525067
]
50535068
},
@@ -5229,11 +5244,15 @@
52295244
},
52305245
{
52315246
"cachegroup": "dtrc2",
5232-
"parents": [0]
5247+
"parents": [
5248+
0
5249+
]
52335250
},
52345251
{
52355252
"cachegroup": "dtrc3",
5236-
"parents": [0]
5253+
"parents": [
5254+
0
5255+
]
52375256
}
52385257
]
52395258
},
@@ -5247,11 +5266,15 @@
52475266
},
52485267
{
52495268
"cachegroup": "dtrc1",
5250-
"parents": [0]
5269+
"parents": [
5270+
0
5271+
]
52515272
},
52525273
{
52535274
"cachegroup": "dtrc2",
5254-
"parents": [1]
5275+
"parents": [
5276+
1
5277+
]
52555278
}
52565279
]
52575280
},
@@ -5265,11 +5288,15 @@
52655288
},
52665289
{
52675290
"cachegroup": "dtrc2",
5268-
"parents": [0]
5291+
"parents": [
5292+
0
5293+
]
52695294
},
52705295
{
52715296
"cachegroup": "dtrc3",
5272-
"parents": [0]
5297+
"parents": [
5298+
0
5299+
]
52735300
}
52745301
]
52755302
},
@@ -5653,7 +5680,7 @@
56535680
"ttl": 2160
56545681
},
56555682
{
5656-
"deliveryService": "ds2",
5683+
"deliveryService": "ds2",
56575684
"regex": "\\/some-path?.+\\.jpg",
56585685
"startTime": "2100-06-19T13:57:51-06:00",
56595686
"ttl": 2.1

traffic_ops/traffic_ops_golang/deliveryservice/deliveryservices_required_capabilities.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ func (rc *RequiredCapability) getCapabilities(h http.Header, tenantIDs []int, us
202202
var runSecond bool
203203
var results []tc.DeliveryServicesRequiredCapability
204204
where, orderBy, pagination, queryValues, errs := dbhelpers.BuildWhereAndOrderByAndPagination(rc.APIInfo().Params, rc.ParamColumns())
205+
205206
if len(errs) > 0 {
206207
return nil, util.JoinErrs(errs), nil, http.StatusBadRequest, nil
207208
}

0 commit comments

Comments
 (0)