Skip to content
This repository was archived by the owner on Nov 24, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions lib/go-tc/cachegroups.go

This file was deleted.

58 changes: 58 additions & 0 deletions lib/go-tc/v13/cachegroups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package v13

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import tc "github.com/apache/incubator-trafficcontrol/lib/go-tc"

// CacheGroupResponse ...
type CacheGroupsResponse struct {
Response []CacheGroup `json:"response"`
}

// CacheGroup contains information about a given Cachegroup in Traffic Ops.
type CacheGroup struct {
ID int `json:"id" db:"id"`
Name string `json:"name" db:"name"`
ShortName string `json:"shortName" db:"short_name"`
Latitude float64 `json:"latitude" db:"latitude"`
Longitude float64 `json:"longitude" db:"longitude"`
ParentName string `json:"parentCachegroupName"`
ParentCachegroupID int `json:"parentCachegroupId" db:"parent_cachegroup_id"`
SecondaryParentName string `json:"secondaryParentCachegroupName"`
SecondaryParentCachegroupID int `json:"secondaryParentCachegroupId" db:"secondary_parent_cachegroup_id"`
Type string `json:"typeName" db:"type_name"` // aliased to type_name to disambiguate struct scans due to join on 'type' table
TypeID int `json:"typeId" db:"type_id"` // aliased to type_id to disambiguate struct scans due join on 'type' table
LastUpdated tc.TimeNoMod `json:"lastUpdated" db:"last_updated"`
}

type CacheGroupNullable struct {
ID *int `json:"id" db:"id"`
Name *string `json:"name" db:"name"`
ShortName *string `json:"shortName" db:"short_name"`
Latitude *float64 `json:"latitude" db:"latitude"`
Longitude *float64 `json:"longitude"db:"longitude"`
ParentName *string `json:"parentCachegroupName"`
ParentCachegroupID *int `json:"parentCachegroupId" db:"parent_cachegroup_id"`
SecondaryParentName *string `json:"secondaryParentCachegroupName"`
SecondaryParentCachegroupID *int `json:"secondaryParentCachegroupId" db:"secondary_parent_cachegroup_id"`
Type *string `json:"typeName" db:"type_name"` // aliased to type_name to disambiguate struct scans due to join on 'type' table
TypeID *int `json:"typeId" db:"type_id"` // aliased to type_id to disambiguate struct scans due join on 'type' table
LastUpdated *tc.TimeNoMod `json:"lastUpdated" db:"last_updated"`
}
17 changes: 9 additions & 8 deletions traffic_ops/client/v13/cachegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import (
"net/http"

"github.com/apache/incubator-trafficcontrol/lib/go-tc"
"github.com/apache/incubator-trafficcontrol/lib/go-tc/v13"
)

const (
API_v13_CacheGroups = "/api/1.3/cachegroups"
)

// Create a CacheGroup
func (to *Session) CreateCacheGroup(cachegroup tc.CacheGroup) (tc.Alerts, ReqInf, error) {
func (to *Session) CreateCacheGroup(cachegroup v13.CacheGroup) (tc.Alerts, ReqInf, error) {

var remoteAddr net.Addr
reqBody, err := json.Marshal(cachegroup)
Expand All @@ -48,7 +49,7 @@ func (to *Session) CreateCacheGroup(cachegroup tc.CacheGroup) (tc.Alerts, ReqInf
}

// Update a CacheGroup by ID
func (to *Session) UpdateCacheGroupByID(id int, cachegroup tc.CacheGroup) (tc.Alerts, ReqInf, error) {
func (to *Session) UpdateCacheGroupByID(id int, cachegroup v13.CacheGroup) (tc.Alerts, ReqInf, error) {

var remoteAddr net.Addr
reqBody, err := json.Marshal(cachegroup)
Expand All @@ -68,21 +69,21 @@ func (to *Session) UpdateCacheGroupByID(id int, cachegroup tc.CacheGroup) (tc.Al
}

// Returns a list of CacheGroups
func (to *Session) GetCacheGroups() ([]tc.CacheGroup, ReqInf, error) {
func (to *Session) GetCacheGroups() ([]v13.CacheGroup, ReqInf, error) {
resp, remoteAddr, err := to.request(http.MethodGet, API_v13_CacheGroups, nil)
reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
if err != nil {
return nil, reqInf, err
}
defer resp.Body.Close()

var data tc.CacheGroupsResponse
var data v13.CacheGroupsResponse
err = json.NewDecoder(resp.Body).Decode(&data)
return data.Response, reqInf, nil
}

// GET a CacheGroup by the CacheGroup id
func (to *Session) GetCacheGroupByID(id int) ([]tc.CacheGroup, ReqInf, error) {
func (to *Session) GetCacheGroupByID(id int) ([]v13.CacheGroup, ReqInf, error) {
route := fmt.Sprintf("%s/%d", API_v13_CacheGroups, id)
resp, remoteAddr, err := to.request(http.MethodGet, route, nil)
reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
Expand All @@ -91,7 +92,7 @@ func (to *Session) GetCacheGroupByID(id int) ([]tc.CacheGroup, ReqInf, error) {
}
defer resp.Body.Close()

var data tc.CacheGroupsResponse
var data v13.CacheGroupsResponse
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, reqInf, err
}
Expand All @@ -100,7 +101,7 @@ func (to *Session) GetCacheGroupByID(id int) ([]tc.CacheGroup, ReqInf, error) {
}

// GET a CacheGroup by the CacheGroup name
func (to *Session) GetCacheGroupByName(name string) ([]tc.CacheGroup, ReqInf, error) {
func (to *Session) GetCacheGroupByName(name string) ([]v13.CacheGroup, ReqInf, error) {
url := fmt.Sprintf("%s?name=%s", API_v13_CacheGroups, name)
resp, remoteAddr, err := to.request(http.MethodGet, url, nil)
reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
Expand All @@ -109,7 +110,7 @@ func (to *Session) GetCacheGroupByName(name string) ([]tc.CacheGroup, ReqInf, er
}
defer resp.Body.Close()

var data tc.CacheGroupsResponse
var data v13.CacheGroupsResponse
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, reqInf, err
}
Expand Down
183 changes: 183 additions & 0 deletions traffic_ops/testing/api/v13/cachegroups_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package v13

/*

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import (
"testing"

"github.com/apache/incubator-trafficcontrol/lib/go-log"
tc "github.com/apache/incubator-trafficcontrol/lib/go-tc"
"github.com/apache/incubator-trafficcontrol/lib/go-tc/v13"
)

func TestCacheGroups(t *testing.T) {
CreateTestTypes(t)
CreateTestCacheGroups(t)
GetTestCacheGroups(t)
UpdateTestCacheGroups(t)
DeleteTestCacheGroups(t)
DeleteTestTypes(t)
}

func CreateTestCacheGroups(t *testing.T) {
failed := false

for _, cg := range testData.CacheGroups {
// get the typeID
typeResp, _, err := TOSession.GetTypeByName(cg.Type)
if err != nil {
t.Error("could not lookup a typeID for this cachegroup")
failed = true
}
cg.TypeID = typeResp[0].ID

_, _, err = TOSession.CreateCacheGroup(cg)
if err != nil {
t.Errorf("could not CREATE cachegroups: %v\n", err)
failed = true
}
}
if !failed {
log.Debugln("CreateTestCacheGroups() PASSED: ")
}
}

func GetTestCacheGroups(t *testing.T) {
failed := false
for _, cg := range testData.CacheGroups {
resp, _, err := TOSession.GetCacheGroupByName(cg.Name)
if err != nil {
t.Errorf("cannot GET CacheGroup by name: %v - %v\n", err, resp)
failed = true
}
}
if !failed {
log.Debugln("GetTestCacheGroups() PASSED: ")
}
}

func UpdateTestCacheGroups(t *testing.T) {
failed := false
firstCG := testData.CacheGroups[0]
resp, _, err := TOSession.GetCacheGroupByName(firstCG.Name)
if err != nil {
t.Errorf("cannot GET CACHEGROUP by name: %v - %v\n", firstCG.Name, err)
failed = true
}
cg := resp[0]
expectedShortName := "blah"
cg.ShortName = expectedShortName

// fix the type id for test
typeResp, _, err := TOSession.GetTypeByID(cg.TypeID)
if err != nil {
t.Error("could not lookup a typeID for this cachegroup")
failed = true
}
cg.TypeID = typeResp[0].ID

var alert tc.Alerts
alert, _, err = TOSession.UpdateCacheGroupByID(cg.ID, cg)
if err != nil {
t.Errorf("cannot UPDATE CacheGroup by id: %v - %v\n", err, alert)
failed = true
}

// Retrieve the CacheGroup to check CacheGroup name got updated
resp, _, err = TOSession.GetCacheGroupByID(cg.ID)
if err != nil {
t.Errorf("cannot GET CacheGroup by name: '$%s', %v\n", firstCG.Name, err)
failed = true
}
cg = resp[0]
if cg.ShortName != expectedShortName {
t.Errorf("results do not match actual: %s, expected: %s\n", cg.ShortName, expectedShortName)
}
if !failed {
log.Debugln("UpdateTestCacheGroups() PASSED: ")
}
}

func DeleteTestCacheGroups(t *testing.T) {
failed := false
var mids []v13.CacheGroup

// delete the edge caches.
for _, cg := range testData.CacheGroups {
// Retrieve the CacheGroup by name so we can get the id for the Update
resp, _, err := TOSession.GetCacheGroupByName(cg.Name)
if err != nil {
t.Errorf("cannot GET CacheGroup by name: %v - %v\n", cg.Name, err)
failed = true
}
// Mids are parents and need to be deleted only after the children
// cachegroups are deleted.
if cg.Type == "MID_LOC" {
mids = append(mids, cg)
continue
}
if len(resp) > 0 {
respCG := resp[0]
_, _, err := TOSession.DeleteCacheGroupByID(respCG.ID)
if err != nil {
t.Errorf("cannot DELETE CacheGroup by name: '%s' %v\n", respCG.Name, err)
failed = true
}
// Retrieve the CacheGroup to see if it got deleted
cgs, _, err := TOSession.GetCacheGroupByName(cg.Name)
if err != nil {
t.Errorf("error deleting CacheGroup name: %s\n", err.Error())
failed = true
}
if len(cgs) > 0 {
t.Errorf("expected CacheGroup name: %s to be deleted\n", cg.Name)
failed = true
}
}
}
// now delete the mid tier caches
for _, cg := range mids {
// Retrieve the CacheGroup by name so we can get the id for the Update
resp, _, err := TOSession.GetCacheGroupByName(cg.Name)
if err != nil {
t.Errorf("cannot GET CacheGroup by name: %v - %v\n", cg.Name, err)
failed = true
}
if len(resp) > 0 {
respCG := resp[0]
_, _, err := TOSession.DeleteCacheGroupByID(respCG.ID)
if err != nil {
t.Errorf("cannot DELETE CacheGroup by name: '%s' %v\n", respCG.Name, err)
failed = true
}

// Retrieve the CacheGroup to see if it got deleted
cgs, _, err := TOSession.GetCacheGroupByName(cg.Name)
if err != nil {
t.Errorf("error deleting CacheGroup name: %s\n", err.Error())
failed = true
}
if len(cgs) > 0 {
t.Errorf("expected CacheGroup name: %s to be deleted\n", cg.Name)
failed = true
}
}
}

if !failed {
log.Debugln("DeleteTestCacheGroups() PASSED: ")
}
}
Loading