Skip to content

Commit

Permalink
Update CDN in a Box enroller to use TO API v5 (apache#7795)
Browse files Browse the repository at this point in the history
* Session.CreateServerCapabilityV5 -> Session.CreateServerCapability

* Session.GetServerCapabilitiesV5 -> Session.GetServerCapabilities

* Session.UpdateServerCapabilityV5 -> Session.UpdateServerCapability

* Update CiaB enroller to use TO API v5

* Update field names for API v5

* Remove server response ID validation
  • Loading branch information
zrhoffman authored Sep 13, 2023
1 parent 6f527a1 commit 3192836
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 58 deletions.
1 change: 1 addition & 0 deletions infrastructure/cdn-in-a-box/enroller/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ COPY ./go.mod ./go.sum /go/src/github.com/apache/trafficcontrol/
COPY ./vendor/ /go/src/github.com/apache/trafficcontrol/vendor/
COPY ./traffic_ops/toclientlib/ /go/src/github.com/apache/trafficcontrol/traffic_ops/toclientlib/
COPY ./traffic_ops/v4-client/ /go/src/github.com/apache/trafficcontrol/traffic_ops/v4-client/
COPY ./traffic_ops/v5-client/ /go/src/github.com/apache/trafficcontrol/traffic_ops/v5-client/
COPY ./infrastructure/cdn-in-a-box/ /go/src/github.com/apache/trafficcontrol/infrastructure/cdn-in-a-box/

# varnishcfg requires t3c for ToData struct and not needed for enroller
Expand Down
65 changes: 27 additions & 38 deletions infrastructure/cdn-in-a-box/enroller/enroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (

log "github.com/apache/trafficcontrol/lib/go-log"
tc "github.com/apache/trafficcontrol/lib/go-tc"
client "github.com/apache/trafficcontrol/traffic_ops/v4-client"
client "github.com/apache/trafficcontrol/traffic_ops/v5-client"

"github.com/fsnotify/fsnotify"
"github.com/kelseyhightower/envconfig"
Expand All @@ -51,7 +51,7 @@ func newSession(reqTimeout time.Duration, toURL string, toUser string, toPass st
return session{s}, err
}

func (s session) getParameter(m tc.Parameter, header http.Header) (tc.Parameter, error) {
func (s session) getParameter(m tc.ParameterV5, header http.Header) (tc.ParameterV5, error) {
// TODO: s.GetParameterByxxx() does not seem to work with values with spaces --
// doing this the hard way for now
opts := client.RequestOptions{Header: header}
Expand All @@ -70,7 +70,7 @@ func (s session) getParameter(m tc.Parameter, header http.Header) (tc.Parameter,
// enrollType takes a json file and creates a Type object using the TO API
func enrollType(toSession *session, r io.Reader) error {
dec := json.NewDecoder(r)
var s tc.Type
var s tc.TypeV5
err := dec.Decode(&s)
if err != nil {
log.Infof("error decoding Type: %s", err)
Expand Down Expand Up @@ -100,7 +100,7 @@ func enrollType(toSession *session, r io.Reader) error {
// enrollCDN takes a json file and creates a CDN object using the TO API
func enrollCDN(toSession *session, r io.Reader) error {
dec := json.NewDecoder(r)
var s tc.CDN
var s tc.CDNV5
err := dec.Decode(&s)
if err != nil {
log.Infof("error decoding CDN: %v", err)
Expand Down Expand Up @@ -128,7 +128,7 @@ func enrollCDN(toSession *session, r io.Reader) error {

func enrollASN(toSession *session, r io.Reader) error {
dec := json.NewDecoder(r)
var s tc.ASN
var s tc.ASNV5
err := dec.Decode(&s)
if err != nil {
log.Infof("error decoding ASN: %s\n", err)
Expand Down Expand Up @@ -158,7 +158,7 @@ func enrollASN(toSession *session, r io.Reader) error {
// enrollCachegroup takes a json file and creates a Cachegroup object using the TO API
func enrollCachegroup(toSession *session, r io.Reader) error {
dec := json.NewDecoder(r)
var s tc.CacheGroupNullable
var s tc.CacheGroupNullableV5
err := dec.Decode(&s)
if err != nil {
log.Infof("error decoding Cache Group: '%s'", err)
Expand Down Expand Up @@ -187,7 +187,7 @@ func enrollCachegroup(toSession *session, r io.Reader) error {

func enrollTopology(toSession *session, r io.Reader) error {
dec := json.NewDecoder(r)
var s tc.Topology
var s tc.TopologyV5
err := dec.Decode(&s)
if err != nil && err != io.EOF {
log.Infof("error decoding Topology: %s", err)
Expand Down Expand Up @@ -216,7 +216,7 @@ func enrollTopology(toSession *session, r io.Reader) error {

func enrollDeliveryService(toSession *session, r io.Reader) error {
dec := json.NewDecoder(r)
var s tc.DeliveryServiceV4
var s tc.DeliveryServiceV5
err := dec.Decode(&s)
if err != nil {
log.Infof("error decoding DeliveryService: %v", err)
Expand All @@ -227,7 +227,7 @@ func enrollDeliveryService(toSession *session, r io.Reader) error {
if err != nil {
for _, alert := range alerts.Alerts.Alerts {
if strings.Contains(alert.Text, "already exists") {
log.Infof("Delivery Service '%s' already exists", *s.XMLID)
log.Infof("Delivery Service '%s' already exists", s.XMLID)
return nil
}
}
Expand Down Expand Up @@ -318,10 +318,7 @@ func enrollDeliveryServiceServer(toSession *session, r io.Reader) error {
if len(servers.Response) == 0 {
return errors.New("no server with hostName " + sn)
}
if servers.Response[0].ID == nil {
return fmt.Errorf("Traffic Ops gave back a representation for server '%s' with null or undefined ID", sn)
}
serverIDs = append(serverIDs, *servers.Response[0].ID)
serverIDs = append(serverIDs, servers.Response[0].ID)
}
resp, _, err := toSession.CreateDeliveryServiceServers(dsID, serverIDs, true, client.RequestOptions{})
if err != nil {
Expand All @@ -333,7 +330,7 @@ func enrollDeliveryServiceServer(toSession *session, r io.Reader) error {

func enrollDivision(toSession *session, r io.Reader) error {
dec := json.NewDecoder(r)
var s tc.Division
var s tc.DivisionV5
err := dec.Decode(&s)
if err != nil {
log.Infof("error decoding Division: %s", err)
Expand Down Expand Up @@ -361,21 +358,18 @@ func enrollDivision(toSession *session, r io.Reader) error {

func enrollOrigin(toSession *session, r io.Reader) error {
dec := json.NewDecoder(r)
var s tc.Origin
var s tc.OriginV5
err := dec.Decode(&s)
if err != nil {
log.Infof("error decoding Origin: %v", err)
return err
}
if s.Name == nil {
return errors.New("cannot create an Origin with no name")
}

alerts, _, err := toSession.CreateOrigin(s, client.RequestOptions{})
if err != nil {
for _, alert := range alerts.Alerts.Alerts {
if alert.Level == tc.ErrorLevel.String() && strings.Contains(alert.Text, "already exists") {
log.Infof("Origin '%s' already exists", *s.Name)
log.Infof("Origin '%s' already exists", s.Name)
return nil
}
}
Expand All @@ -392,7 +386,7 @@ func enrollOrigin(toSession *session, r io.Reader) error {

func enrollParameter(toSession *session, r io.Reader) error {
dec := json.NewDecoder(r)
var params []tc.Parameter
var params []tc.ParameterV5
err := dec.Decode(&params)
if err != nil {
log.Infof("error decoding Parameter: %s\n", err)
Expand Down Expand Up @@ -469,7 +463,7 @@ func enrollParameter(toSession *session, r io.Reader) error {

func enrollPhysLocation(toSession *session, r io.Reader) error {
dec := json.NewDecoder(r)
var s tc.PhysLocation
var s tc.PhysLocationV5
err := dec.Decode(&s)
if err != nil {
err = fmt.Errorf("error decoding Physical Location: %v", err)
Expand Down Expand Up @@ -500,7 +494,7 @@ func enrollPhysLocation(toSession *session, r io.Reader) error {

func enrollRegion(toSession *session, r io.Reader) error {
dec := json.NewDecoder(r)
var s tc.Region
var s tc.RegionV5
err := dec.Decode(&s)
if err != nil {
log.Infof("error decoding Region: %s\n", err)
Expand Down Expand Up @@ -529,7 +523,7 @@ func enrollRegion(toSession *session, r io.Reader) error {

func enrollStatus(toSession *session, r io.Reader) error {
dec := json.NewDecoder(r)
var s tc.StatusNullable
var s tc.StatusV5
err := dec.Decode(&s)
if err != nil {
log.Infof("error decoding Status: %s", err)
Expand Down Expand Up @@ -557,7 +551,7 @@ func enrollStatus(toSession *session, r io.Reader) error {

func enrollTenant(toSession *session, r io.Reader) error {
dec := json.NewDecoder(r)
var s tc.Tenant
var s tc.TenantV5
err := dec.Decode(&s)
if err != nil {
log.Infof("error decoding Tenant: %s", err)
Expand All @@ -568,7 +562,7 @@ func enrollTenant(toSession *session, r io.Reader) error {
if err != nil {
for _, alert := range alerts.Alerts.Alerts {
if alert.Level == tc.ErrorLevel.String() && strings.Contains(alert.Text, "already exists") {
log.Infof("tenant %s already exists", s.Name)
log.Infof("tenant %s already exists", *s.Name)
return nil
}
}
Expand Down Expand Up @@ -617,7 +611,7 @@ func enrollUser(toSession *session, r io.Reader) error {
// enrollProfile takes a json file and creates a Profile object using the TO API
func enrollProfile(toSession *session, r io.Reader) error {
dec := json.NewDecoder(r)
var profile tc.Profile
var profile tc.ProfileV5

err := dec.Decode(&profile)
if err != nil {
Expand Down Expand Up @@ -700,7 +694,7 @@ func enrollProfile(toSession *session, r io.Reader) error {
if p.Value != nil {
value = *p.Value
}
param := tc.Parameter{ConfigFile: configFile, Name: name, Value: value, Secure: secure}
param := tc.ParameterV5{ConfigFile: configFile, Name: name, Value: value, Secure: secure}
eparam, err := toSession.getParameter(param, nil)
if err != nil {
// create it
Expand Down Expand Up @@ -750,7 +744,7 @@ func enrollProfile(toSession *session, r io.Reader) error {
// enrollServer takes a json file and creates a Server object using the TO API
func enrollServer(toSession *session, r io.Reader) error {
dec := json.NewDecoder(r)
var s tc.ServerV40
var s tc.ServerV5
err := dec.Decode(&s)
if err != nil {
log.Infof("error decoding Server: %v", err)
Expand All @@ -774,15 +768,15 @@ func enrollServer(toSession *session, r io.Reader) error {
// enrollServerCapability takes a json file and creates a ServerCapabilityV41 object using the TO API
func enrollServerCapability(toSession *session, r io.Reader) error {
dec := json.NewDecoder(r)
var s tc.ServerCapabilityV41
var s tc.ServerCapabilityV5
err := dec.Decode(&s)
if err != nil {
err = fmt.Errorf("error decoding Server Capability: %v", err)
log.Infoln(err)
return err
}

alerts, _, err := toSession.CreateServerCapabilityV41(s, client.RequestOptions{})
alerts, _, err := toSession.CreateServerCapability(s, client.RequestOptions{})
if err != nil {
err = fmt.Errorf("error creating Server Capability: %v - alerts: %+v", err, alerts.Alerts)
log.Infoln(err)
Expand Down Expand Up @@ -827,11 +821,6 @@ func enrollFederation(toSession *session, r io.Reader) error {
return err
}
deliveryService := deliveryServices.Response[0]
if deliveryService.CDNName == nil || deliveryService.ID == nil || deliveryService.XMLID == nil {
err = fmt.Errorf("Delivery Service '%s' as returned from Traffic Ops had null or undefined CDN name and/or ID", xmlID)
log.Infoln(err)
return err
}
cdnName = *deliveryService.CDNName
cdnFederation = tc.CDNFederation{
CName: mapping.CName,
Expand Down Expand Up @@ -938,7 +927,7 @@ func createFederationResolversOfType(toSession *session, resolverTypeName tc.Fed

var resolverIDs []int
for _, ipAddress := range ipAddresses {
resolver := tc.FederationResolver{
resolver := tc.FederationResolverV5{
IPAddress: &ipAddress,
TypeID: &typeID,
}
Expand All @@ -958,7 +947,7 @@ func createFederationResolversOfType(toSession *session, resolverTypeName tc.Fed
// enrollServerServerCapability takes a json file and creates a ServerServerCapability object using the TO API
func enrollServerServerCapability(toSession *session, r io.Reader) error {
dec := json.NewDecoder(r)
var s tc.ServerServerCapability
var s tc.ServerServerCapabilityV5
err := dec.Decode(&s)
if err != nil {
err = fmt.Errorf("error decoding Server/Capability relationship: %s", err)
Expand Down Expand Up @@ -986,7 +975,7 @@ func enrollServerServerCapability(toSession *session, r io.Reader) error {
log.Infoln(err.Error())
return err
}
s.ServerID = resp.Response[0].ID
s.ServerID = &resp.Response[0].ID

alerts, _, err := toSession.CreateServerServerCapability(s, client.RequestOptions{})
if err != nil {
Expand Down
9 changes: 4 additions & 5 deletions infrastructure/cdn-in-a-box/enroller/server_template.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"hostName": "$MY_HOSTNAME",
"domainName": "$MY_DOMAINNAME",
"cachegroup": "$MY_CACHE_GROUP",
"cacheGroup": "$MY_CACHE_GROUP",
"interfaces": [
{
"ipAddresses": [],
Expand All @@ -11,10 +11,9 @@
}
],
"type": "$MY_TYPE",
"physLocation": "Apachecon North America 2018",
"profileNames": ["$MY_PROFILE"],
"cdnName": "$MY_CDN",
"updPending": false,
"physicalLocation": "Apachecon North America 2018",
"profiles": ["$MY_PROFILE"],
"cdn": "$MY_CDN",
"status": "$MY_STATUS",
"tcpPort": $MY_TCP_PORT,
"httpsPort": $MY_HTTPS_PORT
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/cdn-in-a-box/traffic_ops/to-access.sh
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ to-enroll() {

# replace env references in the file
<"/server_template.json" envsubst | #first envsubst expands $MY_TCP_PORT and $MY_HTTPS_PORT so they are valid JSON
jq '.cdnName = "$MY_CDN"' |
jq '.cdn = "$MY_CDN"' |
if [[ -n "$MY_IP" && -n "$MY_GATEWAY" ]]; then
jq '.interfaces[0].ipAddresses += [({} | .address = "$MY_IP" | .gateway = "$MY_GATEWAY" | .serviceAddress = true)]'
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"orgServerFqdn": "http://origin.infra.ciab.test",
"cdnName": "$CDN_NAME",
"type": "HTTP",
"active": true,
"active": "ACTIVE",
"dscp": 0,
"geoLimit": 0,
"geoProvider": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"orgServerFqdn": "http://origin.infra.ciab.test",
"cdnName": "$CDN_NAME",
"type": "DNS",
"active": true,
"active": "ACTIVE",
"dscp": 0,
"geoLimit": 0,
"geoProvider": 0,
Expand Down
12 changes: 6 additions & 6 deletions traffic_ops/testing/api/v5/server_capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,21 @@ func TestServerCapabilities(t *testing.T) {
switch method {
case "GET":
t.Run(name, func(t *testing.T) {
resp, reqInf, err := testCase.ClientSession.GetServerCapabilitiesV5(testCase.RequestOpts)
resp, reqInf, err := testCase.ClientSession.GetServerCapabilities(testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, resp.Response, resp.Alerts, err)
}
})
case "POST":
t.Run(name, func(t *testing.T) {
resp, reqInf, err := testCase.ClientSession.CreateServerCapabilityV5(testCase.RequestBody, testCase.RequestOpts)
resp, reqInf, err := testCase.ClientSession.CreateServerCapability(testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, resp.Response, resp.Alerts, err)
}
})
case "PUT":
t.Run(name, func(t *testing.T) {
resp, reqInf, err := testCase.ClientSession.UpdateServerCapabilityV5(testCase.RequestOpts.QueryParameters["name"][0], testCase.RequestBody, testCase.RequestOpts)
resp, reqInf, err := testCase.ClientSession.UpdateServerCapability(testCase.RequestOpts.QueryParameters["name"][0], testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, resp.Response, resp.Alerts, err)
}
Expand Down Expand Up @@ -200,13 +200,13 @@ func validateServerCapabilitiesSort() utils.CkReqFunc {

func CreateTestServerCapabilities(t *testing.T) {
for _, sc := range testData.ServerCapabilities {
resp, _, err := TOSession.CreateServerCapabilityV5(sc, client.RequestOptions{})
resp, _, err := TOSession.CreateServerCapability(sc, client.RequestOptions{})
assert.RequireNoError(t, err, "Unexpected error creating Server Capability '%s': %v - alerts: %+v", sc.Name, err, resp.Alerts)
}
}

func DeleteTestServerCapabilities(t *testing.T) {
serverCapabilities, _, err := TOSession.GetServerCapabilitiesV5(client.RequestOptions{})
serverCapabilities, _, err := TOSession.GetServerCapabilities(client.RequestOptions{})
assert.NoError(t, err, "Cannot get Server Capabilities: %v - alerts: %+v", err, serverCapabilities.Alerts)

for _, serverCapability := range serverCapabilities.Response {
Expand All @@ -215,7 +215,7 @@ func DeleteTestServerCapabilities(t *testing.T) {
// Retrieve the Server Capability to see if it got deleted
opts := client.NewRequestOptions()
opts.QueryParameters.Set("name", serverCapability.Name)
getServerCapability, _, err := TOSession.GetServerCapabilitiesV5(opts)
getServerCapability, _, err := TOSession.GetServerCapabilities(opts)
assert.NoError(t, err, "Error getting Server Capability '%s' after deletion: %v - alerts: %+v", serverCapability.Name, err, getServerCapability.Alerts)
assert.Equal(t, 0, len(getServerCapability.Response), "Expected Server Capability '%s' to be deleted, but it was found in Traffic Ops", serverCapability.Name)
}
Expand Down
Loading

0 comments on commit 3192836

Please sign in to comment.