dangogh closed pull request #1924: Implemented API tests for new routes URL: https://github.com/apache/incubator-trafficcontrol/pull/1924
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/lib/go-tc/cachegroups.go b/lib/go-tc/cachegroups.go index 8591c5d73b..7e6c1f89eb 100644 --- a/lib/go-tc/cachegroups.go +++ b/lib/go-tc/cachegroups.go @@ -26,6 +26,7 @@ type CacheGroupsResponse struct { // CacheGroup contains information about a given Cachegroup in Traffic Ops. type CacheGroup struct { + ID int `json:"id"` Name string `json:"name"` ShortName string `json:"shortName"` Latitude float64 `json:"latitude"` diff --git a/lib/go-tc/divisions.go b/lib/go-tc/divisions.go index ba4fe29d1a..fd782d8282 100644 --- a/lib/go-tc/divisions.go +++ b/lib/go-tc/divisions.go @@ -24,7 +24,7 @@ type DivisionsResponse struct { } type Division struct { - ID int `json:"id" db:"id"` - LastUpdated Time `json:"lastUpdated" db:"last_updated"` - Name string `json:"name" db:"name"` + ID int `json:"id" db:"id"` + LastUpdated TimeNoMod `json:"lastUpdated" db:"last_updated"` + Name string `json:"name" db:"name"` } diff --git a/lib/go-tc/hwinfos.go b/lib/go-tc/hwinfos.go index 022923efa9..f1a6c0a252 100644 --- a/lib/go-tc/hwinfos.go +++ b/lib/go-tc/hwinfos.go @@ -24,10 +24,10 @@ type HWInfoResponse struct { } type HWInfo struct { - Description string `json:"description" db:"description"` - ID int `json:"-" db:"id"` - LastUpdated Time `json:"lastUpdated" db:"last_updated"` - ServerHostName string `json:"serverHostName" db:"serverhostname"` - ServerID int `json:"serverId" db:"serverid"` - Val string `json:"val" db:"val"` + Description string `json:"description" db:"description"` + ID int `json:"-" db:"id"` + LastUpdated TimeNoMod `json:"lastUpdated" db:"last_updated"` + ServerHostName string `json:"serverHostName" db:"serverhostname"` + ServerID int `json:"serverId" db:"serverid"` + Val string `json:"val" db:"val"` } diff --git a/lib/go-tc/parameters.go b/lib/go-tc/parameters.go index 5751e8760d..cbf7afbeb4 100644 --- a/lib/go-tc/parameters.go +++ b/lib/go-tc/parameters.go @@ -30,7 +30,7 @@ type ParametersResponse struct { type Parameter struct { ConfigFile string `json:"configFile" db:"config_file"` ID int `json:"id" db:"id"` - LastUpdated Time `json:"lastUpdated" db:"last_updated"` + LastUpdated TimeNoMod `json:"lastUpdated" db:"last_updated"` Name string `json:"name" db:"name"` Profiles json.RawMessage `json:"profiles" db:"profiles"` Secure bool `json:"secure" db:"secure"` diff --git a/lib/go-tc/physlocations.go b/lib/go-tc/physlocations.go index 594296918f..76d6c18a97 100644 --- a/lib/go-tc/physlocations.go +++ b/lib/go-tc/physlocations.go @@ -19,6 +19,10 @@ package tc * under the License. */ +type PhysLocationsResponse struct { + Response []PhysLocation `json:"response"` +} + type PhysLocation struct { Address string `json:"address" db:"address"` City string `json:"city" db:"city"` diff --git a/lib/go-tc/regions.go b/lib/go-tc/regions.go index d32556cd11..379d56e3f9 100644 --- a/lib/go-tc/regions.go +++ b/lib/go-tc/regions.go @@ -24,9 +24,9 @@ type RegionsResponse struct { } type Region struct { - DivisionName string `json:"divisionName"` - Division int `json:"division" db:"division"` - ID int `json:"id" db:"id"` - LastUpdated Time `json:"lastUpdated" db:"last_updated"` - Name string `json:"name" db:"name"` + DivisionName string `json:"divisionName"` + Division int `json:"division" db:"division"` + ID int `json:"id" db:"id"` + LastUpdated TimeNoMod `json:"lastUpdated" db:"last_updated"` + Name string `json:"name" db:"name"` } diff --git a/lib/go-tc/servers.go b/lib/go-tc/servers.go index 24ffbc552a..123be1454d 100644 --- a/lib/go-tc/servers.go +++ b/lib/go-tc/servers.go @@ -55,7 +55,7 @@ type Server struct { IPAddress string `json:"ipAddress" db:"ip_address"` IPGateway string `json:"ipGateway" db:"ip_gateway"` IPNetmask string `json:"ipNetmask" db:"ip_netmask"` - LastUpdated Time `json:"lastUpdated" db:"last_updated"` + LastUpdated TimeNoMod `json:"lastUpdated" db:"last_updated"` MgmtIPAddress string `json:"mgmtIpAddress" db:"mgmt_ip_address"` MgmtIPGateway string `json:"mgmtIpGateway" db:"mgmt_ip_gateway"` MgmtIPNetmask string `json:"mgmtIpNetmask" db:"mgmt_ip_netmask"` diff --git a/lib/go-tc/statuses.go b/lib/go-tc/statuses.go index 4491f6869b..253b72963e 100644 --- a/lib/go-tc/statuses.go +++ b/lib/go-tc/statuses.go @@ -24,8 +24,8 @@ type StatusesResponse struct { } type Status struct { - Description string `json:"description" db:"description"` - ID int `json:"id" db:"id"` - LastUpdated Time `json:"lastUpdated" db:"last_updated"` - Name string `json:"name" db:"name"` + Description string `json:"description" db:"description"` + ID int `json:"id" db:"id"` + LastUpdated TimeNoMod `json:"lastUpdated" db:"last_updated"` + Name string `json:"name" db:"name"` } diff --git a/traffic_ops/client/cdn.go b/traffic_ops/client/cdn.go index 6bf2d93de8..d9e20e7851 100644 --- a/traffic_ops/client/cdn.go +++ b/traffic_ops/client/cdn.go @@ -25,7 +25,7 @@ import ( ) const ( - API_v2_CDNs = "/api/1.2/cdns" + API_v12_CDNs = "/api/1.2/cdns" ) // Create a CDN @@ -37,7 +37,7 @@ func (to *Session) CreateCDN(cdn tc.CDN) (tc.Alerts, ReqInf, error) { if err != nil { return tc.Alerts{}, reqInf, err } - resp, remoteAddr, err := to.request(http.MethodPost, API_v2_CDNs, reqBody) + resp, remoteAddr, err := to.request(http.MethodPost, API_v12_CDNs, reqBody) if err != nil { return tc.Alerts{}, reqInf, err } @@ -56,7 +56,7 @@ func (to *Session) UpdateCDNByID(id int, cdn tc.CDN) (tc.Alerts, ReqInf, error) if err != nil { return tc.Alerts{}, reqInf, err } - route := fmt.Sprintf("%s/%d", API_v2_CDNs, id) + route := fmt.Sprintf("%s/%d", API_v12_CDNs, id) resp, remoteAddr, err := to.request(http.MethodPut, route, reqBody) if err != nil { return tc.Alerts{}, reqInf, err @@ -69,7 +69,7 @@ func (to *Session) UpdateCDNByID(id int, cdn tc.CDN) (tc.Alerts, ReqInf, error) // Returns a list of CDNs func (to *Session) GetCDNs() ([]tc.CDN, ReqInf, error) { - resp, remoteAddr, err := to.request(http.MethodGet, API_v2_CDNs, nil) + resp, remoteAddr, err := to.request(http.MethodGet, API_v12_CDNs, nil) reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} if err != nil { return nil, reqInf, err @@ -83,7 +83,7 @@ func (to *Session) GetCDNs() ([]tc.CDN, ReqInf, error) { // GET a CDN by the CDN id func (to *Session) GetCDNByID(id int) ([]tc.CDN, ReqInf, error) { - route := fmt.Sprintf("%s/%d", API_v2_CDNs, id) + route := fmt.Sprintf("%s/%d", API_v12_CDNs, id) resp, remoteAddr, err := to.request(http.MethodGet, route, nil) reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} if err != nil { @@ -101,7 +101,7 @@ func (to *Session) GetCDNByID(id int) ([]tc.CDN, ReqInf, error) { // GET a CDN by the CDN name func (to *Session) GetCDNByName(name string) ([]tc.CDN, ReqInf, error) { - url := fmt.Sprintf("%s/name/%s", API_v2_CDNs, name) + url := fmt.Sprintf("%s/name/%s", API_v12_CDNs, name) resp, remoteAddr, err := to.request(http.MethodGet, url, nil) reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} if err != nil { @@ -119,7 +119,7 @@ func (to *Session) GetCDNByName(name string) ([]tc.CDN, ReqInf, error) { // DELETE a CDN by ID func (to *Session) DeleteCDNByID(id int) (tc.Alerts, ReqInf, error) { - route := fmt.Sprintf("%s/%d", API_v2_CDNs, id) + route := fmt.Sprintf("%s/%d", API_v12_CDNs, id) resp, remoteAddr, err := to.request(http.MethodDelete, route, nil) reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} if err != nil { @@ -132,7 +132,7 @@ func (to *Session) DeleteCDNByID(id int) (tc.Alerts, ReqInf, error) { } func (to *Session) GetCDNSSLKeys(name string) ([]tc.CDNSSLKeys, ReqInf, error) { - url := fmt.Sprintf("%s/name/%s/sslkeys", API_v2_CDNs, name) + url := fmt.Sprintf("%s/name/%s/sslkeys", API_v12_CDNs, name) resp, remoteAddr, err := to.request(http.MethodGet, url, nil) reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} if err != nil { diff --git a/traffic_ops/client/asn.go b/traffic_ops/client/v13/asn.go similarity index 99% rename from traffic_ops/client/asn.go rename to traffic_ops/client/v13/asn.go index fefeeae32a..1e65ef2134 100644 --- a/traffic_ops/client/asn.go +++ b/traffic_ops/client/v13/asn.go @@ -13,7 +13,7 @@ limitations under the License. */ -package client +package v13 import ( "encoding/json" diff --git a/traffic_ops/client/v13/cachegroup.go b/traffic_ops/client/v13/cachegroup.go new file mode 100644 index 0000000000..2203ffb7bc --- /dev/null +++ b/traffic_ops/client/v13/cachegroup.go @@ -0,0 +1,132 @@ +/* + + 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. +*/ + +package v13 + +import ( + "encoding/json" + "fmt" + "net" + "net/http" + + "github.com/apache/incubator-trafficcontrol/lib/go-tc" +) + +const ( + API_v13_CacheGroups = "/api/1.3/cachegroups" +) + +// Create a CacheGroup +func (to *Session) CreateCacheGroup(cachegroup tc.CacheGroup) (tc.Alerts, ReqInf, error) { + + var remoteAddr net.Addr + reqBody, err := json.Marshal(cachegroup) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return tc.Alerts{}, reqInf, err + } + resp, remoteAddr, err := to.request(http.MethodPost, API_v13_CacheGroups, reqBody) + if err != nil { + return tc.Alerts{}, reqInf, err + } + defer resp.Body.Close() + var alerts tc.Alerts + err = json.NewDecoder(resp.Body).Decode(&alerts) + return alerts, reqInf, nil +} + +// Update a CacheGroup by ID +func (to *Session) UpdateCacheGroupByID(id int, cachegroup tc.CacheGroup) (tc.Alerts, ReqInf, error) { + + var remoteAddr net.Addr + reqBody, err := json.Marshal(cachegroup) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return tc.Alerts{}, reqInf, err + } + route := fmt.Sprintf("%s/%d", API_v13_CacheGroups, id) + resp, remoteAddr, err := to.request(http.MethodPut, route, reqBody) + if err != nil { + return tc.Alerts{}, reqInf, err + } + defer resp.Body.Close() + var alerts tc.Alerts + err = json.NewDecoder(resp.Body).Decode(&alerts) + return alerts, reqInf, nil +} + +// Returns a list of CacheGroups +func (to *Session) GetCacheGroups() ([]tc.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 + 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) { + route := fmt.Sprintf("%s/%d", API_v13_CacheGroups, id) + resp, remoteAddr, err := to.request(http.MethodGet, route, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + + var data tc.CacheGroupsResponse + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return nil, reqInf, err + } + + return data.Response, reqInf, nil +} + +// GET a CacheGroup by the CacheGroup name +func (to *Session) GetCacheGroupByName(name string) ([]tc.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} + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + + var data tc.CacheGroupsResponse + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return nil, reqInf, err + } + + return data.Response, reqInf, nil +} + +// DELETE a CacheGroup by ID +func (to *Session) DeleteCacheGroupByID(id int) (tc.Alerts, ReqInf, error) { + route := fmt.Sprintf("%s/%d", API_v13_CacheGroups, id) + resp, remoteAddr, err := to.request(http.MethodDelete, route, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return tc.Alerts{}, reqInf, err + } + defer resp.Body.Close() + var alerts tc.Alerts + err = json.NewDecoder(resp.Body).Decode(&alerts) + return alerts, reqInf, nil +} diff --git a/traffic_ops/client/v13/cdn.go b/traffic_ops/client/v13/cdn.go new file mode 100644 index 0000000000..2396c350df --- /dev/null +++ b/traffic_ops/client/v13/cdn.go @@ -0,0 +1,149 @@ +/* + + 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. +*/ + +package v13 + +import ( + "encoding/json" + "fmt" + "net" + "net/http" + + "github.com/apache/incubator-trafficcontrol/lib/go-tc" +) + +const ( + API_v13_CDNs = "/api/1.3/cdns" +) + +// Create a CDN +func (to *Session) CreateCDN(cdn tc.CDN) (tc.Alerts, ReqInf, error) { + + var remoteAddr net.Addr + reqBody, err := json.Marshal(cdn) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return tc.Alerts{}, reqInf, err + } + resp, remoteAddr, err := to.request(http.MethodPost, API_v13_CDNs, reqBody) + if err != nil { + return tc.Alerts{}, reqInf, err + } + defer resp.Body.Close() + var alerts tc.Alerts + err = json.NewDecoder(resp.Body).Decode(&alerts) + return alerts, reqInf, nil +} + +// Update a CDN by ID +func (to *Session) UpdateCDNByID(id int, cdn tc.CDN) (tc.Alerts, ReqInf, error) { + + var remoteAddr net.Addr + reqBody, err := json.Marshal(cdn) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return tc.Alerts{}, reqInf, err + } + route := fmt.Sprintf("%s/%d", API_v13_CDNs, id) + resp, remoteAddr, err := to.request(http.MethodPut, route, reqBody) + if err != nil { + return tc.Alerts{}, reqInf, err + } + defer resp.Body.Close() + var alerts tc.Alerts + err = json.NewDecoder(resp.Body).Decode(&alerts) + return alerts, reqInf, nil +} + +// Returns a list of CDNs +func (to *Session) GetCDNs() ([]tc.CDN, ReqInf, error) { + resp, remoteAddr, err := to.request(http.MethodGet, API_v13_CDNs, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + + var data tc.CDNsResponse + err = json.NewDecoder(resp.Body).Decode(&data) + return data.Response, reqInf, nil +} + +// GET a CDN by the CDN ID +func (to *Session) GetCDNByID(id int) ([]tc.CDN, ReqInf, error) { + route := fmt.Sprintf("%s/%d", API_v13_CDNs, id) + resp, remoteAddr, err := to.request(http.MethodGet, route, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + + var data tc.CDNsResponse + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return nil, reqInf, err + } + + return data.Response, reqInf, nil +} + +// GET a CDN by the CDN name +func (to *Session) GetCDNByName(name string) ([]tc.CDN, ReqInf, error) { + url := fmt.Sprintf("%s/name/%s", API_v13_CDNs, name) + resp, remoteAddr, err := to.request(http.MethodGet, url, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + + var data tc.CDNsResponse + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return nil, reqInf, err + } + + return data.Response, reqInf, nil +} + +// DELETE a CDN by ID +func (to *Session) DeleteCDNByID(id int) (tc.Alerts, ReqInf, error) { + route := fmt.Sprintf("%s/%d", API_v13_CDNs, id) + resp, remoteAddr, err := to.request(http.MethodDelete, route, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return tc.Alerts{}, reqInf, err + } + defer resp.Body.Close() + var alerts tc.Alerts + err = json.NewDecoder(resp.Body).Decode(&alerts) + return alerts, reqInf, nil +} + +func (to *Session) GetCDNSSLKeys(name string) ([]tc.CDNSSLKeys, ReqInf, error) { + url := fmt.Sprintf("%s/name/%s/sslkeys", API_v13_CDNs, name) + resp, remoteAddr, err := to.request(http.MethodGet, url, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + + var data tc.CDNSSLKeysResponse + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return nil, reqInf, err + } + + return data.Response, reqInf, nil +} diff --git a/traffic_ops/client/deliveryservice_requests.go b/traffic_ops/client/v13/deliveryservice_requests.go similarity index 99% rename from traffic_ops/client/deliveryservice_requests.go rename to traffic_ops/client/v13/deliveryservice_requests.go index bd77b48f31..2bf3beca4a 100644 --- a/traffic_ops/client/deliveryservice_requests.go +++ b/traffic_ops/client/v13/deliveryservice_requests.go @@ -13,7 +13,7 @@ limitations under the License. */ -package client +package v13 import ( "encoding/json" diff --git a/traffic_ops/client/division.go b/traffic_ops/client/v13/division.go similarity index 79% rename from traffic_ops/client/division.go rename to traffic_ops/client/v13/division.go index 28832f9c3a..063cfe0c55 100644 --- a/traffic_ops/client/division.go +++ b/traffic_ops/client/v13/division.go @@ -13,7 +13,7 @@ limitations under the License. */ -package client +package v13 import ( "encoding/json" @@ -25,7 +25,7 @@ import ( ) const ( - API_v2_Divisions = "/api/1.3/divisions" + API_v13_Divisions = "/api/1.3/divisions" ) // Create a Division @@ -37,7 +37,7 @@ func (to *Session) CreateDivision(division tc.Division) (tc.Alerts, ReqInf, erro if err != nil { return tc.Alerts{}, reqInf, err } - resp, remoteAddr, err := to.request(http.MethodPost, API_v2_Divisions, reqBody) + resp, remoteAddr, err := to.request(http.MethodPost, API_v13_Divisions, reqBody) if err != nil { return tc.Alerts{}, reqInf, err } @@ -56,7 +56,7 @@ func (to *Session) UpdateDivisionByID(id int, division tc.Division) (tc.Alerts, if err != nil { return tc.Alerts{}, reqInf, err } - route := fmt.Sprintf("%s/%d", API_v2_Divisions, id) + route := fmt.Sprintf("%s/%d", API_v13_Divisions, id) resp, remoteAddr, err := to.request(http.MethodPut, route, reqBody) if err != nil { return tc.Alerts{}, reqInf, err @@ -69,7 +69,7 @@ func (to *Session) UpdateDivisionByID(id int, division tc.Division) (tc.Alerts, // Returns a list of Divisions func (to *Session) GetDivisions() ([]tc.Division, ReqInf, error) { - resp, remoteAddr, err := to.request(http.MethodGet, API_v2_Divisions, nil) + resp, remoteAddr, err := to.request(http.MethodGet, API_v13_Divisions, nil) reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} if err != nil { return nil, reqInf, err @@ -83,7 +83,7 @@ func (to *Session) GetDivisions() ([]tc.Division, ReqInf, error) { // GET a Division by the Division id func (to *Session) GetDivisionByID(id int) ([]tc.Division, ReqInf, error) { - route := fmt.Sprintf("%s/%d", API_v2_Divisions, id) + route := fmt.Sprintf("%s/%d", API_v13_Divisions, id) resp, remoteAddr, err := to.request(http.MethodGet, route, nil) reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} if err != nil { @@ -101,7 +101,7 @@ func (to *Session) GetDivisionByID(id int) ([]tc.Division, ReqInf, error) { // GET a Division by the Division name func (to *Session) GetDivisionByName(name string) ([]tc.Division, ReqInf, error) { - url := fmt.Sprintf("%s?name=%s", API_v2_Divisions, name) + url := fmt.Sprintf("%s?name=%s", API_v13_Divisions, name) resp, remoteAddr, err := to.request(http.MethodGet, url, nil) reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} if err != nil { @@ -117,9 +117,23 @@ func (to *Session) GetDivisionByName(name string) ([]tc.Division, ReqInf, error) return data.Response, reqInf, nil } +// DELETE a Division by Division id +func (to *Session) DeleteDivisionByID(id int) (tc.Alerts, ReqInf, error) { + route := fmt.Sprintf("%s/%d", API_v13_Divisions, id) + resp, remoteAddr, err := to.request(http.MethodDelete, route, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return tc.Alerts{}, reqInf, err + } + defer resp.Body.Close() + var alerts tc.Alerts + err = json.NewDecoder(resp.Body).Decode(&alerts) + return alerts, reqInf, nil +} + // DELETE a Division by Division name func (to *Session) DeleteDivisionByName(name string) (tc.Alerts, ReqInf, error) { - route := fmt.Sprintf("%s/name/%s", API_v2_Divisions, name) + route := fmt.Sprintf("%s/name/%s", API_v13_Divisions, name) resp, remoteAddr, err := to.request(http.MethodDelete, route, nil) reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} if err != nil { diff --git a/traffic_ops/client/v13/parameter.go b/traffic_ops/client/v13/parameter.go new file mode 100644 index 0000000000..94cf583492 --- /dev/null +++ b/traffic_ops/client/v13/parameter.go @@ -0,0 +1,169 @@ +/* + + 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. +*/ + +package v13 + +import ( + "encoding/json" + "fmt" + "net" + "net/http" + "net/url" + + "github.com/apache/incubator-trafficcontrol/lib/go-tc" +) + +const ( + API_v13_Parameters = "/api/1.3/parameters" +) + +// Create a Parameter +func (to *Session) CreateParameter(pl tc.Parameter) (tc.Alerts, ReqInf, error) { + + var remoteAddr net.Addr + reqBody, err := json.Marshal(pl) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return tc.Alerts{}, reqInf, err + } + resp, remoteAddr, err := to.request(http.MethodPost, API_v13_Parameters, reqBody) + if err != nil { + return tc.Alerts{}, reqInf, err + } + defer resp.Body.Close() + var alerts tc.Alerts + err = json.NewDecoder(resp.Body).Decode(&alerts) + return alerts, reqInf, nil +} + +// Update a Parameter by ID +func (to *Session) UpdateParameterByID(id int, pl tc.Parameter) (tc.Alerts, ReqInf, error) { + + var remoteAddr net.Addr + reqBody, err := json.Marshal(pl) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return tc.Alerts{}, reqInf, err + } + route := fmt.Sprintf("%s/%d", API_v13_Parameters, id) + resp, remoteAddr, err := to.request(http.MethodPut, route, reqBody) + if err != nil { + return tc.Alerts{}, reqInf, err + } + defer resp.Body.Close() + var alerts tc.Alerts + err = json.NewDecoder(resp.Body).Decode(&alerts) + return alerts, reqInf, nil +} + +// Returns a list of Parameters +func (to *Session) GetParameters() ([]tc.Parameter, ReqInf, error) { + resp, remoteAddr, err := to.request(http.MethodGet, API_v13_Parameters, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + + var data tc.ParametersResponse + err = json.NewDecoder(resp.Body).Decode(&data) + return data.Response, reqInf, nil +} + +// GET a Parameter by the Parameter ID +func (to *Session) GetParameterByID(id int) ([]tc.Parameter, ReqInf, error) { + route := fmt.Sprintf("%s/%d", API_v13_Parameters, id) + resp, remoteAddr, err := to.request(http.MethodGet, route, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + + var data tc.ParametersResponse + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return nil, reqInf, err + } + + return data.Response, reqInf, nil +} + +// GET a Parameter by the Parameter name +func (to *Session) GetParameterByName(name string) ([]tc.Parameter, ReqInf, error) { + URI := API_v13_Parameters + "?name=" + url.QueryEscape(name) + resp, remoteAddr, err := to.request(http.MethodGet, URI, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + + var data tc.ParametersResponse + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return nil, reqInf, err + } + + return data.Response, reqInf, nil +} + +// GET a Parameter by the Parameter ConfigFile +func (to *Session) GetParameterByConfigFile(configFile string) ([]tc.Parameter, ReqInf, error) { + URI := API_v13_Parameters + "?configFile=" + url.QueryEscape(configFile) + resp, remoteAddr, err := to.request(http.MethodGet, URI, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + + var data tc.ParametersResponse + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return nil, reqInf, err + } + + return data.Response, reqInf, nil +} + +// GET a Parameter by the Parameter Name and ConfigFile +func (to *Session) GetParameterByNameAndConfigFile(name string, configFile string) ([]tc.Parameter, ReqInf, error) { + URI := fmt.Sprintf("%s?name=%s&configFile=%s", API_v13_Parameters, url.QueryEscape(name), url.QueryEscape(configFile)) + resp, remoteAddr, err := to.request(http.MethodGet, URI, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + + var data tc.ParametersResponse + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return nil, reqInf, err + } + + return data.Response, reqInf, nil +} + +// DELETE a Parameter by ID +func (to *Session) DeleteParameterByID(id int) (tc.Alerts, ReqInf, error) { + URI := fmt.Sprintf("%s/%d", API_v13_Parameters, id) + resp, remoteAddr, err := to.request(http.MethodDelete, URI, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return tc.Alerts{}, reqInf, err + } + defer resp.Body.Close() + var alerts tc.Alerts + err = json.NewDecoder(resp.Body).Decode(&alerts) + return alerts, reqInf, nil +} diff --git a/traffic_ops/client/v13/phys_location.go b/traffic_ops/client/v13/phys_location.go new file mode 100644 index 0000000000..1083b18712 --- /dev/null +++ b/traffic_ops/client/v13/phys_location.go @@ -0,0 +1,132 @@ +/* + + 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. +*/ + +package v13 + +import ( + "encoding/json" + "fmt" + "net" + "net/http" + + "github.com/apache/incubator-trafficcontrol/lib/go-tc" +) + +const ( + API_v13_PhysLocations = "/api/1.3/physlocations" +) + +// Create a PhysLocation +func (to *Session) CreatePhysLocation(pl tc.PhysLocation) (tc.Alerts, ReqInf, error) { + + var remoteAddr net.Addr + reqBody, err := json.Marshal(pl) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return tc.Alerts{}, reqInf, err + } + resp, remoteAddr, err := to.request(http.MethodPost, API_v13_PhysLocations, reqBody) + if err != nil { + return tc.Alerts{}, reqInf, err + } + defer resp.Body.Close() + var alerts tc.Alerts + err = json.NewDecoder(resp.Body).Decode(&alerts) + return alerts, reqInf, nil +} + +// Update a PhysLocation by ID +func (to *Session) UpdatePhysLocationByID(id int, pl tc.PhysLocation) (tc.Alerts, ReqInf, error) { + + var remoteAddr net.Addr + reqBody, err := json.Marshal(pl) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return tc.Alerts{}, reqInf, err + } + route := fmt.Sprintf("%s/%d", API_v13_PhysLocations, id) + resp, remoteAddr, err := to.request(http.MethodPut, route, reqBody) + if err != nil { + return tc.Alerts{}, reqInf, err + } + defer resp.Body.Close() + var alerts tc.Alerts + err = json.NewDecoder(resp.Body).Decode(&alerts) + return alerts, reqInf, nil +} + +// Returns a list of PhysLocations +func (to *Session) GetPhysLocations() ([]tc.PhysLocation, ReqInf, error) { + resp, remoteAddr, err := to.request(http.MethodGet, API_v13_PhysLocations, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + + var data tc.PhysLocationsResponse + err = json.NewDecoder(resp.Body).Decode(&data) + return data.Response, reqInf, nil +} + +// GET a PhysLocation by the PhysLocation ID +func (to *Session) GetPhysLocationByID(id int) ([]tc.PhysLocation, ReqInf, error) { + route := fmt.Sprintf("%s/%d", API_v13_PhysLocations, id) + resp, remoteAddr, err := to.request(http.MethodGet, route, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + + var data tc.PhysLocationsResponse + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return nil, reqInf, err + } + + return data.Response, reqInf, nil +} + +// GET a PhysLocation by the PhysLocation name +func (to *Session) GetPhysLocationByName(name string) ([]tc.PhysLocation, ReqInf, error) { + url := fmt.Sprintf("%s/name/%s", API_v13_PhysLocations, name) + resp, remoteAddr, err := to.request(http.MethodGet, url, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + + var data tc.PhysLocationsResponse + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return nil, reqInf, err + } + + return data.Response, reqInf, nil +} + +// DELETE a PhysLocation by ID +func (to *Session) DeletePhysLocationByID(id int) (tc.Alerts, ReqInf, error) { + route := fmt.Sprintf("%s/%d", API_v13_PhysLocations, id) + resp, remoteAddr, err := to.request(http.MethodDelete, route, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return tc.Alerts{}, reqInf, err + } + defer resp.Body.Close() + var alerts tc.Alerts + err = json.NewDecoder(resp.Body).Decode(&alerts) + return alerts, reqInf, nil +} diff --git a/traffic_ops/client/region.go b/traffic_ops/client/v13/region.go similarity index 99% rename from traffic_ops/client/region.go rename to traffic_ops/client/v13/region.go index f3e39f16b0..2864d12fc9 100644 --- a/traffic_ops/client/region.go +++ b/traffic_ops/client/v13/region.go @@ -13,7 +13,7 @@ limitations under the License. */ -package client +package v13 import ( "encoding/json" diff --git a/traffic_ops/client/v13/session.go b/traffic_ops/client/v13/session.go new file mode 100644 index 0000000000..c117d82049 --- /dev/null +++ b/traffic_ops/client/v13/session.go @@ -0,0 +1,354 @@ +/* + + 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. +*/ + +package v13 + +import ( + "bytes" + "crypto/tls" + "encoding/json" + "errors" + "fmt" + "io/ioutil" + "net" + "net/http" + "net/http/cookiejar" + "net/http/httptrace" + "strconv" + "strings" + "sync" + "time" + + tc "github.com/apache/incubator-trafficcontrol/lib/go-tc" + + "golang.org/x/net/publicsuffix" +) + +// Session ... +type Session struct { + UserName string + Password string + URL string + Client *http.Client + cache map[string]CacheEntry + cacheMutex *sync.RWMutex + useCache bool + UserAgentStr string +} + +func NewSession(user, password, url, userAgent string, client *http.Client, useCache bool) *Session { + return &Session{ + UserName: user, + Password: password, + URL: url, + Client: client, + cache: map[string]CacheEntry{}, + cacheMutex: &sync.RWMutex{}, + useCache: useCache, + UserAgentStr: userAgent, + } +} + +const DefaultTimeout = time.Second * time.Duration(30) + +// HTTPError is returned on Update Session failure. +type HTTPError struct { + HTTPStatusCode int + HTTPStatus string + URL string + Body string +} + +// Error implements the error interface for our customer error type. +func (e *HTTPError) Error() string { + return fmt.Sprintf("%s[%d] - Error requesting Traffic Ops %s %s", e.HTTPStatus, e.HTTPStatusCode, e.URL, e.Body) +} + +// CacheEntry ... +type CacheEntry struct { + Entered int64 + Bytes []byte + RemoteAddr net.Addr +} + +// TODO JvD +const tmPollingInterval = 60 + +// loginCreds gathers login credentials for Traffic Ops. +func loginCreds(toUser string, toPasswd string) ([]byte, error) { + credentials := tc.UserCredentials{ + Username: toUser, + Password: toPasswd, + } + + js, err := json.Marshal(credentials) + if err != nil { + err := fmt.Errorf("Error creating login json: %v", err) + return nil, err + } + return js, nil +} + +// Deprecated: Login is deprecated, use LoginWithAgent instead. The `Login` function with its present signature will be removed in the next version and replaced with `Login(toURL string, toUser string, toPasswd string, insecure bool, userAgent string)`. The `LoginWithAgent` function will be removed the version after that. +func Login(toURL string, toUser string, toPasswd string, insecure bool) (*Session, error) { + s, _, err := LoginWithAgent(toURL, toUser, toPasswd, insecure, "traffic-ops-client", false, DefaultTimeout) + return s, err +} + +// login tries to log in to Traffic Ops, and set the auth cookie in the Session. Returns the IP address of the remote Traffic Ops. +func (to *Session) login() (net.Addr, error) { + credentials, err := loginCreds(to.UserName, to.Password) + if err != nil { + return nil, errors.New("creating login credentials: " + err.Error()) + } + + path := "/api/1.2/user/login" + resp, remoteAddr, err := to.rawRequest("POST", path, credentials) + resp, remoteAddr, err = to.ErrUnlessOK(resp, remoteAddr, err, path) + if err != nil { + return remoteAddr, errors.New("requesting: " + err.Error()) + } + defer resp.Body.Close() + + var alerts tc.Alerts + if err := json.NewDecoder(resp.Body).Decode(&alerts); err != nil { + return remoteAddr, errors.New("decoding response JSON: " + err.Error()) + } + + success := false + for _, alert := range alerts.Alerts { + if alert.Level == "success" && alert.Text == "Successfully logged in." { + success = true + break + } + } + + if !success { + return remoteAddr, fmt.Errorf("Login failed, alerts string: %+v", alerts) + } + + return remoteAddr, nil +} + +// Login to traffic_ops, the response should set the cookie for this session +// automatically. Start with +// to := traffic_ops.Login("user", "passwd", true) +// subsequent calls like to.GetData("datadeliveryservice") will be authenticated. +// Returns the logged in client, the remote address of Traffic Ops which was translated and used to log in, and any error. If the error is not nil, the remote address may or may not be nil, depending whether the error occurred before the login request. +func LoginWithAgent(toURL string, toUser string, toPasswd string, insecure bool, userAgent string, useCache bool, requestTimeout time.Duration) (*Session, net.Addr, error) { + options := cookiejar.Options{ + PublicSuffixList: publicsuffix.List, + } + + jar, err := cookiejar.New(&options) + if err != nil { + return nil, nil, err + } + + to := NewSession(toUser, toPasswd, toURL, userAgent, &http.Client{ + Timeout: requestTimeout, + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure}, + }, + Jar: jar, + }, useCache) + + remoteAddr, err := to.login() + if err != nil { + return nil, remoteAddr, errors.New("logging in: " + err.Error()) + } + return to, remoteAddr, nil +} + +// ErrUnlessOk returns nil and an error if the given Response's status code is anything but 200 OK. This includes reading the Response.Body and Closing it. Otherwise, the given response and error are returned unchanged. +func (to *Session) ErrUnlessOK(resp *http.Response, remoteAddr net.Addr, err error, path string) (*http.Response, net.Addr, error) { + if err != nil { + return resp, remoteAddr, err + } + if resp.StatusCode == http.StatusOK { + return resp, remoteAddr, err + } + + defer resp.Body.Close() + body, readErr := ioutil.ReadAll(resp.Body) + if readErr != nil { + return nil, remoteAddr, readErr + } + return nil, remoteAddr, errors.New(resp.Status + "[" + strconv.Itoa(resp.StatusCode) + "] - Error requesting Traffic Ops " + to.getURL(path) + " " + string(body)) +} + +func (to *Session) getURL(path string) string { return to.URL + path } + +// request performs the HTTP request to Traffic Ops, trying to refresh the cookie if an Unauthorized or Forbidden code is received. It only tries once. If the login fails, the original Unauthorized/Forbidden response is returned. If the login succeeds and the subsequent re-request fails, the re-request's response is returned even if it's another Unauthorized/Forbidden. +func (to *Session) request(method, path string, body []byte) (*http.Response, net.Addr, error) { + r, remoteAddr, err := to.rawRequest(method, path, body) + if err != nil { + return r, remoteAddr, err + } + if r.StatusCode != http.StatusUnauthorized && r.StatusCode != http.StatusForbidden { + return to.ErrUnlessOK(r, remoteAddr, err, path) + } + if _, lerr := to.login(); lerr != nil { + return to.ErrUnlessOK(r, remoteAddr, err, path) // if re-logging-in fails, return the original request's response + } + + // return second request, even if it's another Unauthorized or Forbidden. + r, remoteAddr, err = to.rawRequest(method, path, body) + return to.ErrUnlessOK(r, remoteAddr, err, path) +} + +// rawRequest performs the actual HTTP request to Traffic Ops, simply, without trying to refresh the cookie if an Unauthorized code is returned. +func (to *Session) rawRequest(method, path string, body []byte) (*http.Response, net.Addr, error) { + url := to.getURL(path) + + var req *http.Request + var err error + remoteAddr := net.Addr(nil) + + if body != nil { + req, err = http.NewRequest(method, url, bytes.NewBuffer(body)) + if err != nil { + return nil, remoteAddr, err + } + req.Header.Set("Content-Type", "application/json") + } else { + req, err = http.NewRequest(method, url, nil) + if err != nil { + return nil, remoteAddr, err + } + } + + trace := &httptrace.ClientTrace{ + GotConn: func(connInfo httptrace.GotConnInfo) { + remoteAddr = connInfo.Conn.RemoteAddr() + }, + } + req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace)) + + req.Header.Set("User-Agent", to.UserAgentStr) + + resp, err := to.Client.Do(req) + if err != nil { + return nil, remoteAddr, err + } + + return resp, remoteAddr, nil +} + +type ReqInf struct { + CacheHitStatus CacheHitStatus + RemoteAddr net.Addr +} + +type CacheHitStatus string + +const CacheHitStatusHit = CacheHitStatus("hit") +const CacheHitStatusExpired = CacheHitStatus("expired") +const CacheHitStatusMiss = CacheHitStatus("miss") +const CacheHitStatusInvalid = CacheHitStatus("") + +func (s CacheHitStatus) String() string { + return string(s) +} + +func StringToCacheHitStatus(s string) CacheHitStatus { + s = strings.ToLower(s) + switch s { + case "hit": + return CacheHitStatusHit + case "expired": + return CacheHitStatusExpired + case "miss": + return CacheHitStatusMiss + default: + return CacheHitStatusInvalid + } +} + +// setCache Sets the given cache key and value. This is threadsafe for multiple goroutines. +func (to *Session) setCache(path string, entry CacheEntry) { + if !to.useCache { + return + } + to.cacheMutex.Lock() + defer to.cacheMutex.Unlock() + to.cache[path] = entry +} + +// getCache gets the cache value at the given key, or false if it doesn't exist. This is threadsafe for multiple goroutines. +func (to *Session) getCache(path string) (CacheEntry, bool) { + to.cacheMutex.RLock() + defer to.cacheMutex.RUnlock() + cacheEntry, ok := to.cache[path] + return cacheEntry, ok +} + +//if cacheEntry, ok := to.Cache[path]; ok { + +// getBytesWithTTL gets the path, and caches in the session. Returns bytes from the cache, if found and the TTL isn't expired. Otherwise, gets it and store it in cache +func (to *Session) getBytesWithTTL(path string, ttl int64) ([]byte, ReqInf, error) { + var body []byte + var err error + var cacheHitStatus CacheHitStatus + var remoteAddr net.Addr + + getFresh := false + if cacheEntry, ok := to.getCache(path); ok { + if cacheEntry.Entered > time.Now().Unix()-ttl { + cacheHitStatus = CacheHitStatusHit + body = cacheEntry.Bytes + remoteAddr = cacheEntry.RemoteAddr + } else { + cacheHitStatus = CacheHitStatusExpired + getFresh = true + } + } else { + cacheHitStatus = CacheHitStatusMiss + getFresh = true + } + + if getFresh { + body, remoteAddr, err = to.getBytes(path) + if err != nil { + return nil, ReqInf{CacheHitStatus: CacheHitStatusInvalid, RemoteAddr: remoteAddr}, err + } + + newEntry := CacheEntry{ + Entered: time.Now().Unix(), + Bytes: body, + RemoteAddr: remoteAddr, + } + to.setCache(path, newEntry) + } + + return body, ReqInf{CacheHitStatus: cacheHitStatus, RemoteAddr: remoteAddr}, nil +} + +// GetBytes - get []bytes array for a certain path on the to session. +// returns the raw body, the remote address the Traffic Ops URL resolved to, or any error. If the error is not nil, the RemoteAddr may or may not be nil, depending whether the error occurred before the request was executed. +func (to *Session) getBytes(path string) ([]byte, net.Addr, error) { + resp, remoteAddr, err := to.request("GET", path, nil) + if err != nil { + return nil, remoteAddr, err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, remoteAddr, err + } + + return body, remoteAddr, nil +} diff --git a/traffic_ops/client/status.go b/traffic_ops/client/v13/status.go similarity index 99% rename from traffic_ops/client/status.go rename to traffic_ops/client/v13/status.go index 1145d8631c..8081b71078 100644 --- a/traffic_ops/client/status.go +++ b/traffic_ops/client/v13/status.go @@ -13,7 +13,7 @@ limitations under the License. */ -package client +package v13 import ( "encoding/json" diff --git a/traffic_ops/testing/api/status_test.go b/traffic_ops/testing/api/status_test.go deleted file mode 100644 index a2ae9fce05..0000000000 --- a/traffic_ops/testing/api/status_test.go +++ /dev/null @@ -1,106 +0,0 @@ -/* - - 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. -*/ - -package api - -import ( - "testing" - - "github.com/apache/incubator-trafficcontrol/lib/go-log" - "github.com/apache/incubator-trafficcontrol/lib/go-tc" -) - -func TestStatuses(t *testing.T) { - - CreateTestStatuses(t) - UpdateTestStatuses(t) - GetTestStatuses(t) - DeleteTestStatuses(t) - -} - -func CreateTestStatuses(t *testing.T) { - for _, status := range testData.Statuses { - resp, _, err := TOSession.CreateStatus(status) - log.Debugln("Response: ", resp) - if err != nil { - t.Errorf("could not CREATE statuss: %v\n", err) - } - } - -} - -func UpdateTestStatuses(t *testing.T) { - - firstStatus := testData.Statuses[0] - // Retrieve the Status by status so we can get the id for the Update - resp, _, err := TOSession.GetStatusByName(firstStatus.Name) - if err != nil { - t.Errorf("cannot GET Status by status: %v - %v\n", firstStatus.Name, err) - } - remoteStatus := resp[0] - expectedStatus := "OFFLINE-TEST" - remoteStatus.Name = expectedStatus - var alert tc.Alerts - alert, _, err = TOSession.UpdateStatusByID(remoteStatus.ID, remoteStatus) - if err != nil { - t.Errorf("cannot UPDATE Status by id: %v - %v\n", err, alert) - } - - // Retrieve the Status to check status got updated - resp, _, err = TOSession.GetStatusByID(remoteStatus.ID) - if err != nil { - t.Errorf("cannot GET Status by status: %v - %v\n", firstStatus.Name, err) - } - respStatus := resp[0] - if respStatus.Name != expectedStatus { - t.Errorf("results do not match actual: %s, expected: %s\n", respStatus.Name, expectedStatus) - } - -} - -func GetTestStatuses(t *testing.T) { - for _, status := range testData.Statuses { - resp, _, err := TOSession.GetStatusByName(status.Name) - if err != nil { - t.Errorf("cannot GET Status by status: %v - %v\n", err, resp) - } - } -} - -func DeleteTestStatuses(t *testing.T) { - - status := testData.Statuses[1] - // Retrieve the Status by name so we can get the id - resp, _, err := TOSession.GetStatusByName(status.Name) - if err != nil { - t.Errorf("cannot GET Status by name: %v - %v\n", status.Name, err) - } - respStatus := resp[0] - - delResp, _, err := TOSession.DeleteStatusByID(respStatus.ID) - if err != nil { - t.Errorf("cannot DELETE Status by status: %v - %v\n", err, delResp) - } - - // Retrieve the Status to see if it got deleted - statusResp, _, err := TOSession.GetStatusByName(status.Name) - if err != nil { - t.Errorf("error deleting Status status: %s\n", err.Error()) - } - if len(statusResp) > 0 { - t.Errorf("expected Status status: %s to be deleted\n", status.Name) - } -} diff --git a/traffic_ops/testing/api/todb/todb.go b/traffic_ops/testing/api/todb/todb.go index 1623b5fe98..d442cd01c3 100644 --- a/traffic_ops/testing/api/todb/todb.go +++ b/traffic_ops/testing/api/todb/todb.go @@ -80,35 +80,12 @@ func SetupTestData(cfg *config.Config, db *sql.DB) error { os.Exit(1) } - err = SetupParameters(cfg, db) - if err != nil { - fmt.Printf("\nError setting up parameter %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) - os.Exit(1) - } - - err = SetupProfiles(cfg, db) - if err != nil { - fmt.Printf("\nError setting up profile %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) - os.Exit(1) - } - - err = SetupProfileParameters(cfg, db) - if err != nil { - fmt.Printf("\nError setting up parameter %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) - os.Exit(1) - } err = SetupTypes(cfg, db) if err != nil { fmt.Printf("\nError setting up type %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) os.Exit(1) } - err = SetupCacheGroups(cfg, db) - if err != nil { - fmt.Printf("\nError setting up cachegroup %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) - os.Exit(1) - } - err = SetupDivisions(cfg, db) if err != nil { fmt.Printf("\nError setting up division %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) @@ -121,71 +98,98 @@ func SetupTestData(cfg *config.Config, db *sql.DB) error { os.Exit(1) } - err = SetupPhysLocations(cfg, db) - if err != nil { - fmt.Printf("\nError setting up phys_location %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) - os.Exit(1) - } - - err = SetupServers(cfg, db) - if err != nil { - fmt.Printf("\nError setting up server %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) - os.Exit(1) - } - - err = SetupAsns(cfg, db) - if err != nil { - fmt.Printf("\nError setting up asn %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) - os.Exit(1) - } - - err = SetupDeliveryServices(cfg, db) - if err != nil { - fmt.Printf("\nError setting up deliveryservice %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) - os.Exit(1) - } - - err = SetupRegexes(cfg, db) - if err != nil { - fmt.Printf("\nError setting up regex %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) - os.Exit(1) - } - - err = SetupDeliveryServiceRegexes(cfg, db) - if err != nil { - fmt.Printf("\nError setting up deliveryservice_regex %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) - os.Exit(1) - } - - err = SetupDeliveryServiceTmUsers(cfg, db) - if err != nil { - fmt.Printf("\nError setting up deliveryservice_tmuser %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) - os.Exit(1) - } - - err = SetupDeliveryServiceServers(cfg, db) - if err != nil { - fmt.Printf("\nError setting up deliveryservice_server %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) - os.Exit(1) - } - - err = SetupJobStatuses(cfg, db) - if err != nil { - fmt.Printf("\nError setting up job_status %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) - os.Exit(1) - } - - err = SetupJobAgents(cfg, db) - if err != nil { - fmt.Printf("\nError setting up job_agent %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) - os.Exit(1) - } - - err = SetupJobs(cfg, db) - if err != nil { - fmt.Printf("\nError setting up job %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) - os.Exit(1) - } + /* + err = SetupParameters(cfg, db) + if err != nil { + fmt.Printf("\nError setting up parameter %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) + os.Exit(1) + } + + err = SetupProfiles(cfg, db) + if err != nil { + fmt.Printf("\nError setting up profile %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) + os.Exit(1) + } + + err = SetupProfileParameters(cfg, db) + if err != nil { + fmt.Printf("\nError setting up parameter %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) + os.Exit(1) + } + + err = SetupCacheGroups(cfg, db) + if err != nil { + fmt.Printf("\nError setting up cachegroup %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) + os.Exit(1) + } + + + err = SetupPhysLocations(cfg, db) + if err != nil { + fmt.Printf("\nError setting up phys_location %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) + os.Exit(1) + } + + err = SetupServers(cfg, db) + if err != nil { + fmt.Printf("\nError setting up server %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) + os.Exit(1) + } + + err = SetupAsns(cfg, db) + if err != nil { + fmt.Printf("\nError setting up asn %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) + os.Exit(1) + } + + err = SetupDeliveryServices(cfg, db) + if err != nil { + fmt.Printf("\nError setting up deliveryservice %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) + os.Exit(1) + } + + err = SetupRegexes(cfg, db) + if err != nil { + fmt.Printf("\nError setting up regex %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) + os.Exit(1) + } + + err = SetupDeliveryServiceRegexes(cfg, db) + if err != nil { + fmt.Printf("\nError setting up deliveryservice_regex %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) + os.Exit(1) + } + + err = SetupDeliveryServiceTmUsers(cfg, db) + if err != nil { + fmt.Printf("\nError setting up deliveryservice_tmuser %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) + os.Exit(1) + } + + err = SetupDeliveryServiceServers(cfg, db) + if err != nil { + fmt.Printf("\nError setting up deliveryservice_server %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) + os.Exit(1) + } + + err = SetupJobStatuses(cfg, db) + if err != nil { + fmt.Printf("\nError setting up job_status %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) + os.Exit(1) + } + + err = SetupJobAgents(cfg, db) + if err != nil { + fmt.Printf("\nError setting up job_agent %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) + os.Exit(1) + } + + err = SetupJobs(cfg, db) + if err != nil { + fmt.Printf("\nError setting up job %s - %s, %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, err) + os.Exit(1) + } + */ return err } diff --git a/traffic_ops/testing/api/towrap/towrap.go b/traffic_ops/testing/api/towrap/towrap.go deleted file mode 100644 index d37428492c..0000000000 --- a/traffic_ops/testing/api/towrap/towrap.go +++ /dev/null @@ -1,38 +0,0 @@ -/* - - 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. -*/ - -package towrap - -import ( - "net" - "time" - - "github.com/apache/incubator-trafficcontrol/traffic_ops/client" - to "github.com/apache/incubator-trafficcontrol/traffic_ops/client" - "github.com/apache/incubator-trafficcontrol/traffic_ops/testing/api/config" -) - -func SetupSession(cfg config.Config, toURL string, toUser string, toPass string) (*client.Session, net.Addr, error) { - var err error - var session *client.Session - var netAddr net.Addr - toReqTimeout := time.Second * time.Duration(cfg.Default.Session.TimeoutInSecs) - session, netAddr, err = to.LoginWithAgent(toURL, toUser, toPass, true, "to-api-client-tests", true, toReqTimeout) - if err != nil { - return nil, nil, err - } - - return session, netAddr, err -} diff --git a/traffic_ops/testing/api/v13/cdns_test.go b/traffic_ops/testing/api/v13/cdns_test.go index 1a7d9a870d..2aba83b346 100644 --- a/traffic_ops/testing/api/v13/cdns_test.go +++ b/traffic_ops/testing/api/v13/cdns_test.go @@ -1,3 +1,5 @@ +package v13 + /* Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,8 +15,6 @@ limitations under the License. */ -package v13 - import ( "testing" diff --git a/traffic_ops/testing/api/v13/deliveryservice_requests_test.go b/traffic_ops/testing/api/v13/deliveryservice_requests_test.go index 193f03c6c9..ac8e97a682 100644 --- a/traffic_ops/testing/api/v13/deliveryservice_requests_test.go +++ b/traffic_ops/testing/api/v13/deliveryservice_requests_test.go @@ -1,3 +1,5 @@ +package v13 + /* Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,8 +15,6 @@ limitations under the License. */ -package v13 - import ( "strings" "testing" @@ -106,6 +106,8 @@ func TestDeliveryServiceRequestRules(t *testing.T) { } func TestDeliveryServiceRequestTypeFields(t *testing.T) { + CreateTestParameters(t) + dsr := testData.DeliveryServiceRequests[dsrBadTenant] alerts, _, err := TOSession.CreateDeliveryServiceRequest(dsr) if err != nil { @@ -118,6 +120,7 @@ func TestDeliveryServiceRequestTypeFields(t *testing.T) { } utils.Compare(t, expected, alerts.ToStrings()) + DeleteTestParameters(t) } diff --git a/traffic_ops/testing/api/v13/divisions_test.go b/traffic_ops/testing/api/v13/divisions_test.go new file mode 100644 index 0000000000..954fe7f300 --- /dev/null +++ b/traffic_ops/testing/api/v13/divisions_test.go @@ -0,0 +1,105 @@ +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" + "github.com/apache/incubator-trafficcontrol/lib/go-tc" +) + +func TestDivisions(t *testing.T) { + + CreateTestDivisions(t) + UpdateTestDivisions(t) + GetTestDivisions(t) + DeleteTestDivisions(t) + +} + +func CreateTestDivisions(t *testing.T) { + for _, division := range testData.Divisions { + resp, _, err := TOSession.CreateDivision(division) + log.Debugln("Response: ", resp) + if err != nil { + t.Errorf("could not CREATE division: %v\n", err) + } + } +} + +func UpdateTestDivisions(t *testing.T) { + + firstDivision := testData.Divisions[0] + // Retrieve the Division by division so we can get the id for the Update + resp, _, err := TOSession.GetDivisionByName(firstDivision.Name) + if err != nil { + t.Errorf("cannot GET Division by division: %v - %v\n", firstDivision.Name, err) + } + remoteDivision := resp[0] + expectedDivision := "division-test" + remoteDivision.Name = expectedDivision + var alert tc.Alerts + alert, _, err = TOSession.UpdateDivisionByID(remoteDivision.ID, remoteDivision) + if err != nil { + t.Errorf("cannot UPDATE Division by id: %v - %v\n", err, alert) + } + + // Retrieve the Division to check division got updated + resp, _, err = TOSession.GetDivisionByID(remoteDivision.ID) + if err != nil { + t.Errorf("cannot GET Division by division: %v - %v\n", firstDivision.Name, err) + } + respDivision := resp[0] + if respDivision.Name != expectedDivision { + t.Errorf("results do not match actual: %s, expected: %s\n", respDivision.Name, expectedDivision) + } + +} + +func GetTestDivisions(t *testing.T) { + for _, division := range testData.Divisions { + resp, _, err := TOSession.GetDivisionByName(division.Name) + if err != nil { + t.Errorf("cannot GET Division by division: %v - %v\n", err, resp) + } + } +} + +func DeleteTestDivisions(t *testing.T) { + + division := testData.Divisions[1] + // Retrieve the Division by name so we can get the id + resp, _, err := TOSession.GetDivisionByName(division.Name) + if err != nil { + t.Errorf("cannot GET Division by name: %v - %v\n", division.Name, err) + } + respDivision := resp[0] + + delResp, _, err := TOSession.DeleteDivisionByID(respDivision.ID) + if err != nil { + t.Errorf("cannot DELETE Division by division: %v - %v\n", err, delResp) + } + + // Retrieve the Division to see if it got deleted + divisionResp, _, err := TOSession.GetDivisionByName(division.Name) + if err != nil { + t.Errorf("error deleting Division division: %s\n", err.Error()) + } + if len(divisionResp) > 0 { + t.Errorf("expected Division : %s to be deleted\n", division.Name) + } +} diff --git a/traffic_ops/testing/api/v13/parameters_test.go b/traffic_ops/testing/api/v13/parameters_test.go new file mode 100644 index 0000000000..916e5460fb --- /dev/null +++ b/traffic_ops/testing/api/v13/parameters_test.go @@ -0,0 +1,115 @@ +/* + + 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. +*/ + +package v13 + +import ( + "fmt" + "testing" + + "github.com/apache/incubator-trafficcontrol/lib/go-log" + tc "github.com/apache/incubator-trafficcontrol/lib/go-tc" +) + +func TestParameters(t *testing.T) { + + CreateTestParameters(t) + //UpdateTestParameters(t) + GetTestParameters(t) + DeleteTestParameters(t) + +} + +func CreateTestParameters(t *testing.T) { + + for _, pl := range testData.Parameters { + resp, _, err := TOSession.CreateParameter(pl) + log.Debugln("Response: ", resp) + if err != nil { + t.Errorf("could not CREATE phys_locations: %v\n", err) + } + } + +} + +func UpdateTestParameters(t *testing.T) { + + firstParameter := testData.Parameters[0] + // Retrieve the Parameter by name so we can get the id for the Update + resp, _, err := TOSession.GetParameterByName(firstParameter.Name) + if err != nil { + t.Errorf("cannot GET Parameter by name: %v - %v\n", firstParameter.Name, err) + } + remoteParameter := resp[0] + expectedParameterName := "UPDATED" + remoteParameter.Name = expectedParameterName + var alert tc.Alerts + alert, _, err = TOSession.UpdateParameterByID(remoteParameter.ID, remoteParameter) + if err != nil { + t.Errorf("cannot UPDATE Parameter by id: %v - %v\n", err, alert) + } + + // Retrieve the Parameter to check Parameter name got updated + resp, _, err = TOSession.GetParameterByID(remoteParameter.ID) + if err != nil { + t.Errorf("cannot GET Parameter by name: %v - %v\n", firstParameter.Name, err) + } + respParameter := resp[0] + if respParameter.Name != expectedParameterName { + t.Errorf("results do not match actual: %s, expected: %s\n", respParameter.Name, expectedParameterName) + } + +} + +func GetTestParameters(t *testing.T) { + + for _, pl := range testData.Parameters { + resp, _, err := TOSession.GetParameterByName(pl.Name) + if err != nil { + t.Errorf("cannot GET Parameter by name: %v - %v\n", err, resp) + } + } +} + +func DeleteTestParameters(t *testing.T) { + + for _, pl := range testData.Parameters { + // Retrieve the Parameter by name so we can get the id for the Update + resp, _, err := TOSession.GetParameterByNameAndConfigFile(pl.Name, pl.ConfigFile) + if err != nil { + t.Errorf("cannot GET Parameter by name: %v - %v\n", pl.Name, err) + } + if len(resp) > 0 { + respParameter := resp[0] + + delResp, _, err := TOSession.DeleteParameterByID(respParameter.ID) + if err != nil { + t.Errorf("cannot DELETE Parameter by name: %v - %v\n", err, delResp) + } + //time.Sleep(1 * time.Second) + + // Retrieve the Parameter to see if it got deleted + pls, _, err := TOSession.GetParameterByNameAndConfigFile(pl.Name, pl.ConfigFile) + if err != nil { + t.Errorf("error deleting Parameter name: %s\n", err.Error()) + } + if len(pls) > 0 { + t.Errorf("expected Parameter Name: %s and ConfigFile: %s to be deleted\n", pl.Name, pl.ConfigFile) + } + } else { + fmt.Printf("no resp ---> %v\n", resp) + } + } +} diff --git a/traffic_ops/testing/api/v13/phys_locations_test.go b/traffic_ops/testing/api/v13/phys_locations_test.go deleted file mode 100644 index aa5f787aa2..0000000000 --- a/traffic_ops/testing/api/v13/phys_locations_test.go +++ /dev/null @@ -1,108 +0,0 @@ -/* - - 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. -*/ - -package v13 - -import ( - "testing" - - "github.com/apache/incubator-trafficcontrol/lib/go-log" - tc "github.com/apache/incubator-trafficcontrol/lib/go-tc" -) - -func TestPhysLocations(t *testing.T) { - - CreateTestPhysLocations(t) - UpdateTestPhysLocations(t) - GetTestPhysLocations(t) - DeleteTestPhysLocations(t) - -} - -func CreateTestPhysLocations(t *testing.T) { - - for _, pl := range testData.PhysLocations { - resp, _, err := TOSession.CreatePhysLocation(pl) - log.Debugln("Response: ", resp) - if err != nil { - t.Errorf("could not CREATE phys_locations: %v\n", err) - } - } - -} - -func UpdateTestPhysLocations(t *testing.T) { - - firstPhysLocation := testData.PhysLocations[0] - // Retrieve the PhysLocation by name so we can get the id for the Update - resp, _, err := TOSession.GetPhysLocationByName(firstPhysLocation.Name) - if err != nil { - t.Errorf("cannot GET PhysLocation by name: %v - %v\n", firstPhysLocation.Name, err) - } - remotePhysLocation := resp[0] - expectedPhysLocationName := "UPDATED" - remotePhysLocation.Name = expectedPhysLocationName - var alert tc.Alerts - alert, _, err = TOSession.UpdatePhysLocationByID(remotePhysLocation.ID, remotePhysLocation) - if err != nil { - t.Errorf("cannot UPDATE PhysLocation by id: %v - %v\n", err, alert) - } - - // Retrieve the PhysLocation to check PhysLocation name got updated - resp, _, err = TOSession.GetPhysLocationByID(remotePhysLocation.ID) - if err != nil { - t.Errorf("cannot GET PhysLocation by name: %v - %v\n", firstPhysLocation.Name, err) - } - respPhysLocation := resp[0] - if respPhysLocation.Name != expectedPhysLocationName { - t.Errorf("results do not match actual: %s, expected: %s\n", respPhysLocation.Name, expectedPhysLocationName) - } - -} - -func GetTestPhysLocations(t *testing.T) { - - for _, pl := range testData.PhysLocations { - resp, _, err := TOSession.GetPhysLocationByName(pl.Name) - if err != nil { - t.Errorf("cannot GET PhysLocation by name: %v - %v\n", err, resp) - } - } -} - -func DeleteTestPhysLocations(t *testing.T) { - - pl := testData.PhysLocations[1] - // Retrieve the PhysLocation by name so we can get the id for the Update - resp, _, err := TOSession.GetPhysLocationByName(pl.Name) - if err != nil { - t.Errorf("cannot GET PhysLocation by name: %v - %v\n", pl.Name, err) - } - respPhysLocation := resp[0] - - delResp, _, err := TOSession.DeletePhysLocationByID(respPhysLocation.ID) - if err != nil { - t.Errorf("cannot DELETE PhysLocation by name: %v - %v\n", err, delResp) - } - - // Retrieve the PhysLocation to see if it got deleted - pls, _, err := TOSession.GetPhysLocationByName(pl.Name) - if err != nil { - t.Errorf("error deleting PhysLocation name: %s\n", err.Error()) - } - if len(pls) > 0 { - t.Errorf("expected PhysLocation name: %s to be deleted\n", pl.Name) - } -} diff --git a/traffic_ops/testing/api/region_test.go b/traffic_ops/testing/api/v13/regions_test.go similarity index 89% rename from traffic_ops/testing/api/region_test.go rename to traffic_ops/testing/api/v13/regions_test.go index 2d0d78a51e..4d3397aec6 100644 --- a/traffic_ops/testing/api/region_test.go +++ b/traffic_ops/testing/api/v13/regions_test.go @@ -1,3 +1,5 @@ +package v13 + /* Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,10 +15,7 @@ limitations under the License. */ -package api - import ( - "fmt" "testing" "github.com/apache/incubator-trafficcontrol/lib/go-log" @@ -25,6 +24,8 @@ import ( func TestRegions(t *testing.T) { + CreateTestDivisions(t) + CreateTestRegions(t) UpdateTestRegions(t) GetTestRegions(t) @@ -33,8 +34,17 @@ func TestRegions(t *testing.T) { } func CreateTestRegions(t *testing.T) { + + // Retrieve the Division by name so we can get the ID + division := testData.Divisions[0] + resp, _, err := TOSession.GetDivisionByName(division.Name) + if err != nil { + t.Errorf("cannot GET Division by name: %v - %v\n", division.Name, err) + } + respDivision := resp[0] + for _, region := range testData.Regions { - fmt.Printf("region ---> %v\n", region) + region.Division = respDivision.ID resp, _, err := TOSession.CreateRegion(region) log.Debugln("Response: ", resp) if err != nil { diff --git a/traffic_ops/testing/api/v13/tc-fixtures.json b/traffic_ops/testing/api/v13/tc-fixtures.json index dc121fd053..33f2459ee2 100644 --- a/traffic_ops/testing/api/v13/tc-fixtures.json +++ b/traffic_ops/testing/api/v13/tc-fixtures.json @@ -1,12 +1,12 @@ { "asns": [ { - "cachegroupName": "cachegroup1", - "name": "asn1" + "asn": 8888, + "cachegroupName": "mid-northeast-group" }, { - "cachegroupName": "cachegroup2", - "name": "asn2" + "asn": 9999, + "cachegroupName": "edge_cg4" } ], "cachegroups": [ @@ -14,13 +14,15 @@ "latitude": 0, "longitude": 0, "name": "parentCacheGroup", - "parentCacheGroupName": null + "parentCacheGroupName": null, + "type": "EDGE" }, { "latitude": 0, "longitude": 0, "name": "secondaryCacheGroup", - "parentCacheGroupName": null + "parentCacheGroupName": null, + "type": "EDGE" }, { "latitude": 0, @@ -28,7 +30,8 @@ "name": "cachegroup1", "parentCacheGroupName": "parentCacheGroup", "secondaryCacheGroupName": "parentCacheGroup", - "shortName": "cg1" + "shortName": "cg1", + "type": "MID" }, { "latitude": 24.1234, @@ -36,7 +39,8 @@ "name": "cachegroup2", "parentCacheGroupName": "secondaryCacheGroup", "secondaryCacheGroupName": "secondaryCacheGroup", - "shortName": "cg2" + "shortName": "cg2", + "type": "MID" } ], "cdns": [ @@ -166,18 +170,335 @@ "name": "division2" } ], + "parameters": [ + { + "configFile": "rascal.properties", + "lastUpdated": "2018-01-19T19:01:21.455131+00:00", + "name": "health.threshold.loadavg", + "secure": false, + "value": "25.0" + }, + { + "configFile": "rascal.properties", + "lastUpdated": "2018-01-19T19:01:21.472279+00:00", + "name": "health.threshold.availableBandwidthInKbps", + "secure": false, + "value": ">1750000" + }, + { + "configFile": "rascal.properties", + "lastUpdated": "2018-01-19T19:01:21.489534+00:00", + "name": "history.count", + "secure": false, + "value": "30" + }, + { + "configFile": "url_sig_cdl-c2.config", + "lastUpdated": "2018-01-19T19:01:21.503311+00:00", + "name": "key0", + "secure": false, + "value": "HOOJ3Ghq1x4gChp3iQkqVTcPlOj8UCi3" + }, + { + "configFile": "url_sig_cdl-c2.config", + "lastUpdated": "2018-01-19T19:01:21.505157+00:00", + "name": "key1", + "secure": false, + "value": "_9LZYkRnfCS0rCBF7fTQzM9Scwlp2FhO" + }, + { + "configFile": "url_sig_cdl-c2.config", + "lastUpdated": "2018-01-19T19:01:21.508548+00:00", + "name": "key2", + "secure": false, + "value": "AFpkxfc4oTiyFSqtY6_ohjt3V80aAIxS" + }, + { + "configFile": "url_sig_cdl-c2.config", + "lastUpdated": "2018-01-19T19:01:21.401781+00:00", + "name": "key3", + "secure": false, + "value": "AL9kzs_SXaRZjPWH8G5e2m4ByTTzkzlc" + }, + { + "configFile": "url_sig_cdl-c2.config", + "lastUpdated": "2018-01-19T19:01:21.406601+00:00", + "name": "key4", + "secure": false, + "value": "poP3n3szbD1U4vx1xQXV65BvkVgWzfN8" + }, + { + "configFile": "url_sig_cdl-c2.config", + "lastUpdated": "2018-01-19T19:01:21.408784+00:00", + "name": "key5", + "secure": false, + "value": "1ir32ng4C4w137p5oq72kd2wqmIZUrya" + }, + { + "configFile": "url_sig_cdl-c2.config", + "lastUpdated": "2018-01-19T19:01:21.410854+00:00", + "name": "key6", + "secure": false, + "value": "B1qLptn2T1b_iXeTCWDcVuYvANtH139f" + }, + { + "configFile": "url_sig_cdl-c2.config", + "lastUpdated": "2018-01-19T19:01:21.412716+00:00", + "name": "key7", + "secure": false, + "value": "PiCV_5OODMzBbsNFMWsBxcQ8v1sK0TYE" + }, + { + "configFile": "url_sig_cdl-c2.config", + "lastUpdated": "2018-01-19T19:01:21.414638+00:00", + "name": "key8", + "secure": false, + "value": "Ggpv6DqXDvt2s1CETPBpNKwaLk4fTM9l" + }, + { + "configFile": "url_sig_cdl-c2.config", + "lastUpdated": "2018-01-19T19:01:21.416551+00:00", + "name": "key9", + "secure": false, + "value": "qPlVT_s6kL37aqb6hipDm4Bt55S72mI7" + }, + { + "configFile": "url_sig_cdl-c2.config", + "lastUpdated": "2018-01-19T19:01:21.418689+00:00", + "name": "key10", + "secure": false, + "value": "BsI5A9EmWrobIS1FeuOs1z9fm2t2WSBe" + }, + { + "configFile": "url_sig_cdl-c2.config", + "lastUpdated": "2018-01-19T19:01:21.420467+00:00", + "name": "key11", + "secure": false, + "value": "A54y66NCIj897GjS4yA9RrsSPtCUnQXP" + }, + { + "configFile": "url_sig_cdl-c2.config", + "lastUpdated": "2018-01-19T19:01:21.422414+00:00", + "name": "key12", + "secure": false, + "value": "2jZH0NDPSJttIr4c2KP510f47EKqTQAu" + }, + { + "configFile": "url_sig_cdl-c2.config", + "lastUpdated": "2018-01-19T19:01:21.424435+00:00", + "name": "key13", + "secure": false, + "value": "XduT2FBjBmmVID5JRB5LEf9oR5QDtBgC" + }, + { + "configFile": "url_sig_cdl-c2.config", + "lastUpdated": "2018-01-19T19:01:21.426125+00:00", + "name": "key14", + "secure": false, + "value": "D9nH0SvK_0kP5w8QNd1UFJ28ulFkFKPn" + }, + { + "configFile": "url_sig_cdl-c2.config", + "lastUpdated": "2018-01-19T19:01:21.427797+00:00", + "name": "key15", + "secure": false, + "value": "udKXWYNwbXXweaaLzaKDGl57OixnIIcm" + }, + { + "configFile": "url_sig_cdl-c2.config", + "lastUpdated": "2018-01-19T19:01:21.431062+00:00", + "name": "error_url", + "secure": false, + "value": "403" + }, + { + "configFile": "records.config", + "lastUpdated": "2018-01-19T19:01:21.432692+00:00", + "name": "CONFIG proxy.config.allocator.debug_filter", + "secure": false, + "value": "INT 0" + }, + { + "configFile": "records.config", + "lastUpdated": "2018-01-19T19:01:21.434425+00:00", + "name": "CONFIG proxy.config.allocator.enable_reclaim", + "secure": false, + "value": "INT 0" + }, + { + "configFile": "records.config", + "lastUpdated": "2018-01-19T19:01:21.435957+00:00", + "name": "CONFIG proxy.config.allocator.max_overage", + "secure": false, + "value": "INT 3" + }, + { + "configFile": "records.config", + "lastUpdated": "2018-01-19T19:01:21.437496+00:00", + "name": "CONFIG proxy.config.diags.show_location", + "secure": false, + "value": "INT 0" + }, + { + "configFile": "records.config", + "lastUpdated": "2018-01-19T19:01:21.439033+00:00", + "name": "CONFIG proxy.config.http.cache.allow_empty_doc", + "secure": false, + "value": "INT 0" + }, + { + "configFile": "records.config", + "lastUpdated": "2018-01-19T19:01:21.440502+00:00", + "name": "LOCAL proxy.config.cache.interim.storage", + "secure": false, + "value": "STRING NULL" + }, + { + "configFile": "records.config", + "lastUpdated": "2018-01-19T19:01:21.441933+00:00", + "name": "CONFIG proxy.config.http.parent_proxy.file", + "secure": false, + "value": "STRING parent.config" + }, + { + "configFile": "plugin.config", + "lastUpdated": "2018-01-19T19:01:21.447837+00:00", + "name": "astats_over_http.so", + "secure": false, + "value": "_astats 33.101.99.100,172.39.19.39,172.39.19.49,172.39.19.49,172.39.29.49" + }, + { + "configFile": "logs_xml.config", + "lastUpdated": "2018-01-19T19:01:21.461206+00:00", + "name": "LogFormat.Name", + "secure": false, + "value": "custom_ats_2" + }, + { + "configFile": "logs_xml.config", + "lastUpdated": "2018-01-19T19:01:21.462772+00:00", + "name": "LogObject.Format", + "secure": false, + "value": "custom_ats_2" + }, + { + "configFile": "logs_xml.config", + "lastUpdated": "2018-01-19T19:01:21.464259+00:00", + "name": "LogObject.Filename", + "secure": false, + "value": "custom_ats_2" + }, + { + "configFile": "records.config", + "lastUpdated": "2018-01-19T19:01:21.467349+00:00", + "name": "CONFIG proxy.config.cache.control.filename", + "secure": false, + "value": "STRING cache.config" + }, + { + "configFile": "plugin.config", + "lastUpdated": "2018-01-19T19:01:21.469075+00:00", + "name": "regex_revalidate.so", + "secure": false, + "value": "--config regex_revalidate.config" + }, + { + "configFile": "astats.config", + "lastUpdated": "2018-01-19T19:01:21.478516+00:00", + "name": "allow_ip6", + "secure": false, + "value": "::1,2033:D011:3300::336/64,2033:D011:3300::335/64,2033:D021:3300::333/64,2033:D021:3300::334/64" + }, + { + "configFile": "astats.config", + "lastUpdated": "2018-01-19T19:01:21.480143+00:00", + "name": "record_types", + "secure": false, + "value": "144" + }, + { + "configFile": "astats.config", + "lastUpdated": "2018-01-19T19:01:21.482959+00:00", + "name": "path", + "secure": false, + "value": "_astats" + }, + { + "configFile": "storage.config", + "lastUpdated": "2018-01-19T19:01:21.484501+00:00", + "name": "location", + "secure": false, + "value": "/opt/trafficserver/etc/trafficserver/" + }, + { + "configFile": "storage.config", + "lastUpdated": "2018-01-19T19:01:21.48625+00:00", + "name": "Drive_Prefix", + "secure": false, + "value": "/dev/sd" + }, + { + "configFile": "storage.config", + "lastUpdated": "2018-01-19T19:01:21.487958+00:00", + "name": "Drive_Letters", + "secure": false, + "value": "b,c,d,e,f,g,h,i,j,k,l,m,n,o" + }, + { + "configFile": "storage.config", + "lastUpdated": "2018-01-19T19:01:21.491181+00:00", + "name": "Disk_Volume", + "secure": false, + "value": "1" + }, + { + "configFile": "records.config", + "lastUpdated": "2018-01-19T19:01:21.49285+00:00", + "name": "CONFIG proxy.config.hostdb.storage_size", + "secure": false, + "value": "INT 33554432" + }, + { + "configFile": "regex_revalidate.config", + "lastUpdated": "2018-01-19T19:01:21.496195+00:00", + "name": "maxRevalDurationDays", + "secure": false, + "value": "90" + }, + { + "configFile": "whaterver.config", + "lastUpdated": "2018-01-19T19:01:21.497838+00:00", + "name": "unassigned_parameter_1", + "secure": false, + "value": "852" + }, + { + "configFile": "package", + "lastUpdated": "2018-01-19T19:01:21.499423+00:00", + "name": "trafficserver", + "secure": false, + "value": "5.3.2-765.f4354b9.el7.centos.x86_64" + }, + { + "configFile": "global", + "lastUpdated": "2018-01-19T19:01:21.501151+00:00", + "name": "use_tenancy", + "secure": false, + "value": "1" + } + ], "physLocations": [ { "address": "1234 mile high circle", "city": "Denver", "comments": null, "email": null, - "last_updated": "2018-01-19T21:19:32.081465+00:00", + "lastUpdated": "2018-01-19T21:19:32.081465+00:00", "name": "Denver", "phone": "303-111-1111", "poc": null, - "region": 100, - "short_name": "denver", + "regionId": 100, + "shortName": "denver", "state": "CO", "zip": "80202" }, @@ -186,12 +507,12 @@ "city": "Boulder", "comments": null, "email": null, - "last_updated": "2018-01-19T21:19:32.086195+00:00", + "lastUpdated": "2018-01-19T21:19:32.086195+00:00", "name": "Boulder", "phone": "303-222-2222", "poc": null, - "region": 100, - "short_name": "boulder", + "regionId": 100, + "shortName": "boulder", "state": "CO", "zip": "80301" }, @@ -200,12 +521,12 @@ "city": "Atlanta", "comments": null, "email": null, - "last_updated": "2018-01-19T21:19:32.089538+00:00", + "lastUpdated": "2018-01-19T21:19:32.089538+00:00", "name": "HotAtlanta", "phone": "404-222-2222", "poc": null, - "region": 100, - "short_name": "atlanta", + "regionId": 100, + "shortName": "atlanta", "state": "GA", "zip": "30301" } @@ -220,6 +541,32 @@ "name": "region2" } ], + "statuses": [ + { + "description": "Edge: Puts server in CCR config file in this state, but CCR will never route traffic to it. Mid: Server will not be included in parent.config files for its edge caches", + "name": "OFFLINE" + }, + { + "description": "Edge: Puts server in CCR config file in this state, and CCR will always route traffic to it. Mid: Server will be included in parent.config files for its edges", + "name": "ONLINE" + }, + { + "description": "Edge: Puts server in CCR config file in this state, and CCR will adhere to the health protocol. Mid: N/A for now", + "name": "REPORTED" + }, + { + "description": "Temporary down. Edge: XMPP client will send status OFFLINE to CCR, otherwise similar to REPORTED. Mid: Server will not be included in parent.config files for its edge caches", + "name": "ADMIN_DOWN" + }, + { + "description": "Edge: 12M will not include caches in this state in CCR config files. Mid: N/A for now", + "name": "CCR_IGNORE" + }, + { + "description": "Pre Production. Not active in any configuration.", + "name": "PRE_PROD" + } + ], "tenants": [ { "active": true, diff --git a/traffic_ops/testing/api/v13/traffic_control.go b/traffic_ops/testing/api/v13/traffic_control.go index af6637d75c..3977c48ce5 100644 --- a/traffic_ops/testing/api/v13/traffic_control.go +++ b/traffic_ops/testing/api/v13/traffic_control.go @@ -21,10 +21,11 @@ import tcapi "github.com/apache/incubator-trafficcontrol/lib/go-tc" type TrafficControl struct { ASNs []tcapi.ASN `json:"asns"` CDNs []tcapi.CDN `json:"cdns"` - Cachegroups []tcapi.CacheGroup `json:"cachegroups"` + CacheGroups []tcapi.CacheGroup `json:"cachegroups"` DeliveryServiceRequests []tcapi.DeliveryServiceRequest `json:"deliveryServiceRequests"` DeliveryServices []tcapi.DeliveryService `json:"deliveryservices"` Divisions []tcapi.Division `json:"divisions"` + Parameters []tcapi.Parameter `json:"parameters"` PhysLocations []tcapi.PhysLocation `json:"physLocations"` Regions []tcapi.Region `json:"regions"` Statuses []tcapi.Status `json:"statuses"` diff --git a/traffic_ops/testing/api/v13/traffic_ops_test.go b/traffic_ops/testing/api/v13/traffic_ops_test.go index c7e25ffb75..68fa773a02 100644 --- a/traffic_ops/testing/api/v13/traffic_ops_test.go +++ b/traffic_ops/testing/api/v13/traffic_ops_test.go @@ -1,3 +1,5 @@ +package v13 + /* Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,25 +15,24 @@ limitations under the License. */ -package v13 - import ( "database/sql" "flag" "fmt" + "net" "os" "testing" + "time" "github.com/apache/incubator-trafficcontrol/lib/go-log" - to "github.com/apache/incubator-trafficcontrol/traffic_ops/client" + "github.com/apache/incubator-trafficcontrol/traffic_ops/client/v13" "github.com/apache/incubator-trafficcontrol/traffic_ops/testing/api/config" "github.com/apache/incubator-trafficcontrol/traffic_ops/testing/api/todb" - "github.com/apache/incubator-trafficcontrol/traffic_ops/testing/api/towrap" _ "github.com/lib/pq" ) var ( - TOSession *to.Session + TOSession *v13.Session cfg config.Config testData TrafficControl ) @@ -84,7 +85,7 @@ func TestMain(m *testing.M) { os.Exit(1) } - TOSession, _, err = towrap.SetupSession(cfg, cfg.TrafficOps.URL, cfg.TrafficOps.User, cfg.TrafficOps.UserPassword) + TOSession, _, err = SetupSession(cfg, cfg.TrafficOps.URL, cfg.TrafficOps.User, cfg.TrafficOps.UserPassword) if err != nil { fmt.Printf("\nError logging into TOURL: %s TOUser: %s/%s - %v\n", cfg.TrafficOps.URL, cfg.TrafficOps.User, cfg.TrafficOps.UserPassword, err) os.Exit(1) @@ -95,3 +96,16 @@ func TestMain(m *testing.M) { os.Exit(rc) } + +func SetupSession(cfg config.Config, toURL string, toUser string, toPass string) (*v13.Session, net.Addr, error) { + var err error + var session *v13.Session + var netAddr net.Addr + toReqTimeout := time.Second * time.Duration(cfg.Default.Session.TimeoutInSecs) + session, netAddr, err = v13.LoginWithAgent(toURL, toUser, toPass, true, "to-api-v13-client-tests", true, toReqTimeout) + if err != nil { + return nil, nil, err + } + + return session, netAddr, err +} diff --git a/traffic_ops/traffic_ops_golang/api/change_log.go b/traffic_ops/traffic_ops_golang/api/change_log.go index 4136bbd64d..579d0632c8 100644 --- a/traffic_ops/traffic_ops_golang/api/change_log.go +++ b/traffic_ops/traffic_ops_golang/api/change_log.go @@ -29,12 +29,12 @@ import ( ) type ChangeLog struct { - ID int `json:"id" db:"id"` - Level string `json:"level" db:"level"` - Message string `json:"message" db:"message"` - TMUser int `json:"tmUser" db:"tm_user"` - TicketNum string `json:"ticketNum" db:"ticketnum"` - LastUpdated tc.Time `json:"lastUpdated" db:"last_updated"` + ID int `json:"id" db:"id"` + Level string `json:"level" db:"level"` + Message string `json:"message" db:"message"` + TMUser int `json:"tmUser" db:"tm_user"` + TicketNum string `json:"ticketNum" db:"ticketnum"` + LastUpdated tc.TimeNoMod `json:"lastUpdated" db:"last_updated"` } type ChangeLogger interface { diff --git a/traffic_ops/traffic_ops_golang/division/divisions.go b/traffic_ops/traffic_ops_golang/division/divisions.go index a461a3c4a2..f548912207 100644 --- a/traffic_ops/traffic_ops_golang/division/divisions.go +++ b/traffic_ops/traffic_ops_golang/division/divisions.go @@ -106,7 +106,7 @@ func (division *TODivision) Create(db *sqlx.DB, user auth.CurrentUser) (error, t defer resultRows.Close() var id int - var lastUpdated tc.Time + var lastUpdated tc.TimeNoMod rowsAffected := 0 for resultRows.Next() { rowsAffected++ @@ -208,7 +208,7 @@ func (division *TODivision) Update(db *sqlx.DB, user auth.CurrentUser) (error, t } defer resultRows.Close() - var lastUpdated tc.Time + var lastUpdated tc.TimeNoMod rowsAffected := 0 for resultRows.Next() { rowsAffected++ diff --git a/traffic_ops/traffic_ops_golang/division/divisions_test.go b/traffic_ops/traffic_ops_golang/division/divisions_test.go index 10ab0078ab..c783838204 100644 --- a/traffic_ops/traffic_ops_golang/division/divisions_test.go +++ b/traffic_ops/traffic_ops_golang/division/divisions_test.go @@ -36,7 +36,7 @@ func getTestDivisions() []tc.Division { testCase := tc.Division{ ID: 1, Name: "division1", - LastUpdated: tc.Time{Time: time.Now()}, + LastUpdated: tc.TimeNoMod{Time: time.Now()}, } regions = append(regions, testCase) diff --git a/traffic_ops/traffic_ops_golang/hwinfo_test.go b/traffic_ops/traffic_ops_golang/hwinfo_test.go index 0a2a4f899c..415f8de01b 100644 --- a/traffic_ops/traffic_ops_golang/hwinfo_test.go +++ b/traffic_ops/traffic_ops_golang/hwinfo_test.go @@ -40,7 +40,7 @@ func getTestHWInfo() []tc.HWInfo { ServerHostName: "testserver1", Description: "Description", Val: "Val", - LastUpdated: tc.Time{Time: time.Now()}, + LastUpdated: tc.TimeNoMod{Time: time.Now()}, } hwinfo = append(hwinfo, testHWInfo) diff --git a/traffic_ops/traffic_ops_golang/parameters_test.go b/traffic_ops/traffic_ops_golang/parameters_test.go index 8a26b6b048..1b9049cd93 100644 --- a/traffic_ops/traffic_ops_golang/parameters_test.go +++ b/traffic_ops/traffic_ops_golang/parameters_test.go @@ -38,7 +38,7 @@ func getTestParameters() []tc.Parameter { testParameter := tc.Parameter{ ConfigFile: "global", ID: 1, - LastUpdated: tc.Time{Time: time.Now()}, + LastUpdated: tc.TimeNoMod{Time: time.Now()}, Name: "paramname1", Profiles: json.RawMessage(`["foo","bar"]`), Secure: false, diff --git a/traffic_ops/traffic_ops_golang/region/regions.go b/traffic_ops/traffic_ops_golang/region/regions.go index 52d881d054..6719f3e2e1 100644 --- a/traffic_ops/traffic_ops_golang/region/regions.go +++ b/traffic_ops/traffic_ops_golang/region/regions.go @@ -155,7 +155,7 @@ func (region *TORegion) Update(db *sqlx.DB, user auth.CurrentUser) (error, tc.Ap } defer resultRows.Close() - var lastUpdated tc.Time + var lastUpdated tc.TimeNoMod rowsAffected := 0 for resultRows.Next() { rowsAffected++ @@ -220,7 +220,7 @@ func (region *TORegion) Create(db *sqlx.DB, user auth.CurrentUser) (error, tc.Ap defer resultRows.Close() var id int - var lastUpdated tc.Time + var lastUpdated tc.TimeNoMod rowsAffected := 0 for resultRows.Next() { rowsAffected++ diff --git a/traffic_ops/traffic_ops_golang/region/regions_test.go b/traffic_ops/traffic_ops_golang/region/regions_test.go index 0a9f28feb9..3f08cc1f7b 100644 --- a/traffic_ops/traffic_ops_golang/region/regions_test.go +++ b/traffic_ops/traffic_ops_golang/region/regions_test.go @@ -38,7 +38,7 @@ func getTestRegions() []tc.Region { DivisionName: "west", ID: 1, Name: "region1", - LastUpdated: tc.Time{Time: time.Now()}, + LastUpdated: tc.TimeNoMod{Time: time.Now()}, } regions = append(regions, testCase) diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go index 8c847bbf0e..c3450bfdf4 100644 --- a/traffic_ops/traffic_ops_golang/routes.go +++ b/traffic_ops/traffic_ops_golang/routes.go @@ -59,13 +59,7 @@ func Routes(d ServerData) ([]Route, http.Handler, error) { proxyHandler := rootHandler(d) routes := []Route{ - //ASNs - {1.3, http.MethodGet, `asns/?(\.json)?$`, api.ReadHandler(asn.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, - {1.3, http.MethodGet, `asns/{id}$`, api.ReadHandler(asn.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, - {1.3, http.MethodPut, `asns/{id}$`, api.UpdateHandler(asn.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil}, - {1.3, http.MethodPost, `asns/?$`, api.CreateHandler(asn.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil}, - {1.3, http.MethodDelete, `asns/{id}$`, api.DeleteHandler(asn.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil}, - + // Proxied routes //CDNs // explicitly passed to legacy system until fully implemented. Auth handled by legacy system. {1.2, http.MethodGet, `cdns/capacity$`, handlerToFunc(proxyHandler), 0, NoAuth, []Middleware{}}, @@ -75,8 +69,23 @@ func Routes(d ServerData) ([]Route, http.Handler, error) { {1.2, http.MethodGet, `cdns/health$`, handlerToFunc(proxyHandler), 0, NoAuth, []Middleware{}}, {1.2, http.MethodGet, `cdns/routing$`, handlerToFunc(proxyHandler), 0, NoAuth, []Middleware{}}, + //Servers + // explicitly passed to legacy system until fully implemented. Auth handled by legacy system. + {1.2, http.MethodGet, `servers/checks$`, handlerToFunc(proxyHandler), 0, NoAuth, []Middleware{}}, + {1.2, http.MethodGet, `servers/details$`, handlerToFunc(proxyHandler), 0, NoAuth, []Middleware{}}, + {1.2, http.MethodGet, `servers/status$`, handlerToFunc(proxyHandler), 0, NoAuth, []Middleware{}}, + {1.2, http.MethodGet, `servers/totals$`, handlerToFunc(proxyHandler), 0, NoAuth, []Middleware{}}, + + //Monitoring {1.2, http.MethodGet, `cdns/{name}/configs/monitoring(\.json)?$`, monitoringHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, + //ASNs + {1.3, http.MethodGet, `asns/?(\.json)?$`, api.ReadHandler(asn.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, + {1.3, http.MethodGet, `asns/{id}$`, api.ReadHandler(asn.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, + {1.3, http.MethodPut, `asns/{id}$`, api.UpdateHandler(asn.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil}, + {1.3, http.MethodPost, `asns/?$`, api.CreateHandler(asn.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil}, + {1.3, http.MethodDelete, `asns/{id}$`, api.DeleteHandler(asn.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil}, + //CDN generic handlers: {1.3, http.MethodGet, `cdns/?(\.json)?$`, api.ReadHandler(cdn.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, {1.3, http.MethodGet, `cdns/{id}$`, api.ReadHandler(cdn.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, @@ -113,7 +122,7 @@ func Routes(d ServerData) ([]Route, http.Handler, error) { {1.3, http.MethodDelete, `statuses/{id}$`, api.DeleteHandler(status.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil}, //Divisions - {1.2, http.MethodGet, `divisions/?(\.json)?$`, api.ReadHandler(division.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, + {1.3, http.MethodGet, `divisions/?(\.json)?$`, api.ReadHandler(division.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, {1.3, http.MethodGet, `divisions/{id}$`, api.ReadHandler(division.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, {1.3, http.MethodPost, `divisions/?$`, api.CreateHandler(division.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil}, @@ -130,13 +139,6 @@ func Routes(d ServerData) ([]Route, http.Handler, error) { //Parameters {1.3, http.MethodGet, `parameters/?(\.json)?$`, parametersHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, - //Servers - // explicitly passed to legacy system until fully implemented. Auth handled by legacy system. - {1.2, http.MethodGet, `servers/checks$`, handlerToFunc(proxyHandler), 0, NoAuth, []Middleware{}}, - {1.2, http.MethodGet, `servers/details$`, handlerToFunc(proxyHandler), 0, NoAuth, []Middleware{}}, - {1.2, http.MethodGet, `servers/status$`, handlerToFunc(proxyHandler), 0, NoAuth, []Middleware{}}, - {1.2, http.MethodGet, `servers/totals$`, handlerToFunc(proxyHandler), 0, NoAuth, []Middleware{}}, - {1.2, http.MethodGet, `servers/?(\.json)?$`, serversHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, {1.2, http.MethodGet, `servers/{id}$`, serversHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, {1.2, http.MethodPost, `servers/{id}/deliveryservices$`, assignDeliveryServicesToServerHandler(d.DB), auth.PrivLevelOperations, Authenticated, nil}, diff --git a/traffic_ops/traffic_ops_golang/servers_test.go b/traffic_ops/traffic_ops_golang/servers_test.go index f52d88052d..012b0d51ee 100644 --- a/traffic_ops/traffic_ops_golang/servers_test.go +++ b/traffic_ops/traffic_ops_golang/servers_test.go @@ -56,7 +56,7 @@ func getTestServers() []tc.Server { IPAddress: "ipAddress", IPGateway: "ipGateway", IPNetmask: "ipNetmask", - LastUpdated: tc.Time{Time: time.Now()}, + LastUpdated: tc.TimeNoMod{Time: time.Now()}, MgmtIPAddress: "mgmtIpAddress", MgmtIPGateway: "mgmtIpGateway", MgmtIPNetmask: "mgmtIpNetmask", diff --git a/traffic_ops/traffic_ops_golang/status/statuses.go b/traffic_ops/traffic_ops_golang/status/statuses.go index d273e03c35..ff5b395368 100644 --- a/traffic_ops/traffic_ops_golang/status/statuses.go +++ b/traffic_ops/traffic_ops_golang/status/statuses.go @@ -155,7 +155,7 @@ func (status *TOStatus) Update(db *sqlx.DB, user auth.CurrentUser) (error, tc.Ap } defer resultRows.Close() - var lastUpdated tc.Time + var lastUpdated tc.TimeNoMod rowsAffected := 0 for resultRows.Next() { rowsAffected++ @@ -222,7 +222,7 @@ func (status *TOStatus) Create(db *sqlx.DB, user auth.CurrentUser) (error, tc.Ap defer resultRows.Close() var id int - var lastUpdated tc.Time + var lastUpdated tc.TimeNoMod rowsAffected := 0 for resultRows.Next() { rowsAffected++ diff --git a/traffic_ops/traffic_ops_golang/status/statuses_test.go b/traffic_ops/traffic_ops_golang/status/statuses_test.go index ca37fa412a..cc8b80b5a1 100644 --- a/traffic_ops/traffic_ops_golang/status/statuses_test.go +++ b/traffic_ops/traffic_ops_golang/status/statuses_test.go @@ -38,7 +38,7 @@ func getTestStatuses() []tc.Status { Description: "description", ID: 1, Name: "cdn1", - LastUpdated: tc.Time{Time: time.Now()}, + LastUpdated: tc.TimeNoMod{Time: time.Now()}, } cdns = append(cdns, testStatus) diff --git a/traffic_ops/traffic_ops_golang/systeminfo/system_info_test.go b/traffic_ops/traffic_ops_golang/systeminfo/system_info_test.go index 902ea4048c..e1628d4906 100644 --- a/traffic_ops/traffic_ops_golang/systeminfo/system_info_test.go +++ b/traffic_ops/traffic_ops_golang/systeminfo/system_info_test.go @@ -37,7 +37,7 @@ var sysInfoParameters = []tc.Parameter{ tc.Parameter{ ConfigFile: "global", ID: 1, - LastUpdated: tc.Time{Time: time.Now()}, + LastUpdated: tc.TimeNoMod{Time: time.Now()}, Name: "paramname1", Profiles: json.RawMessage(`["foo","bar"]`), Secure: false, @@ -47,7 +47,7 @@ var sysInfoParameters = []tc.Parameter{ tc.Parameter{ ConfigFile: "global", ID: 2, - LastUpdated: tc.Time{Time: time.Now()}, + LastUpdated: tc.TimeNoMod{Time: time.Now()}, Name: "paramname2", Profiles: json.RawMessage(`["foo","bar"]`), Secure: false, ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services