You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by de...@apache.org on 2018/05/24 15:31:30 UTC
[incubator-trafficcontrol] 08/08: Remove non-nullable Origin struct,
rename OriginNullable to Origin
This is an automated email from the ASF dual-hosted git repository.
dewrich pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-trafficcontrol.git
commit 6e675a963992616424ccafc1e4a93d905f420efe
Author: Rawlin Peters <ra...@comcast.com>
AuthorDate: Tue May 22 17:12:57 2018 -0600
Remove non-nullable Origin struct, rename OriginNullable to Origin
---
lib/go-tc/v13/origins.go | 100 +--------------------
traffic_ops/client/v13/origin.go | 14 +--
traffic_ops/testing/api/v13/traffic_control.go | 2 +-
traffic_ops/traffic_ops_golang/origin/origins.go | 14 +--
.../traffic_ops_golang/origin/origins_test.go | 47 +++++-----
traffic_ops/traffic_ops_golang/utils/ptr.go | 42 +++++++++
6 files changed, 84 insertions(+), 135 deletions(-)
diff --git a/lib/go-tc/v13/origins.go b/lib/go-tc/v13/origins.go
index bff90c3..fb07ef2 100644
--- a/lib/go-tc/v13/origins.go
+++ b/lib/go-tc/v13/origins.go
@@ -24,38 +24,16 @@ import (
*/
type OriginsResponse struct {
- Response []OriginNullable `json:"response"`
+ Response []Origin `json:"response"`
}
// OriginDetailResponse is the JSON object returned for a single origin
type OriginDetailResponse struct {
- Response OriginNullable `json:"response"`
+ Response Origin `json:"response"`
tc.Alerts
}
type Origin struct {
- Cachegroup string `json:"cachegroup" db:"cachegroup"`
- CachegroupID int `json:"cachegroupId" db:"cachegroup_id"`
- Coordinate string `json:"coordinate" db:"coordinate"`
- CoordinateID int `json:"coordinateId" db:"coordinate_id"`
- DeliveryService string `json:"deliveryService" db:"deliveryservice"`
- DeliveryServiceID int `json:"deliveryServiceId" db:"deliveryservice_id"`
- FQDN string `json:"fqdn" db:"fqdn"`
- ID int `json:"id" db:"id"`
- IP6Address string `json:"ip6Address" db:"ip6_address"`
- IPAddress string `json:"ipAddress" db:"ip_address"`
- IsPrimary bool `json:"isPrimary" db:"is_primary"`
- LastUpdated tc.TimeNoMod `json:"lastUpdated" db:"last_updated"`
- Name string `json:"name" db:"name"`
- Port int `json:"port" db:"port"`
- Profile string `json:"profile" db:"profile"`
- ProfileID int `json:"profileId" db:"profile_id"`
- Protocol string `json:"protocol" db:"protocol"`
- Tenant string `json:"tenant" db:"tenant"`
- TenantID int `json:"tenantId" db:"tenant_id"`
-}
-
-type OriginNullable struct {
Cachegroup *string `json:"cachegroup" db:"cachegroup"`
CachegroupID *int `json:"cachegroupId" db:"cachegroup_id"`
Coordinate *string `json:"coordinate" db:"coordinate"`
@@ -75,76 +53,4 @@ type OriginNullable struct {
Protocol *string `json:"protocol" db:"protocol"`
Tenant *string `json:"tenant" db:"tenant"`
TenantID *int `json:"tenantId" db:"tenant_id"`
-}
-
-type OriginBuilder struct {
- originBuild OriginNullable
-}
-
-func NewOriginBuilder() *OriginBuilder {
- return &OriginBuilder{}
-}
-
-func (o *OriginBuilder) Build() OriginNullable {
- return o.originBuild
-}
-
-func (o *OriginBuilder) CachegroupID(id int) *OriginBuilder {
- o.originBuild.CachegroupID = &id
- return o
-}
-
-func (o *OriginBuilder) CoordinateID(id int) *OriginBuilder {
- o.originBuild.CoordinateID = &id
- return o
-}
-
-func (o *OriginBuilder) DeliveryServiceID(id int) *OriginBuilder {
- o.originBuild.DeliveryServiceID = &id
- return o
-}
-
-func (o *OriginBuilder) FQDN(fqdn string) *OriginBuilder {
- o.originBuild.FQDN = &fqdn
- return o
-}
-
-func (o *OriginBuilder) IP6Address(ip string) *OriginBuilder {
- o.originBuild.IP6Address = &ip
- return o
-}
-
-func (o *OriginBuilder) IPAddress(ip string) *OriginBuilder {
- o.originBuild.IPAddress = &ip
- return o
-}
-
-func (o *OriginBuilder) IsPrimary(isPrimary bool) *OriginBuilder {
- o.originBuild.IsPrimary = &isPrimary
- return o
-}
-
-func (o *OriginBuilder) Name(name string) *OriginBuilder {
- o.originBuild.Name = &name
- return o
-}
-
-func (o *OriginBuilder) Port(port int) *OriginBuilder {
- o.originBuild.Port = &port
- return o
-}
-
-func (o *OriginBuilder) ProfileID(id int) *OriginBuilder {
- o.originBuild.ProfileID = &id
- return o
-}
-
-func (o *OriginBuilder) Protocol(proto string) *OriginBuilder {
- o.originBuild.Protocol = &proto
- return o
-}
-
-func (o *OriginBuilder) TenantID(id int) *OriginBuilder {
- o.originBuild.TenantID = &id
- return o
-}
+}
\ No newline at end of file
diff --git a/traffic_ops/client/v13/origin.go b/traffic_ops/client/v13/origin.go
index fa313b7..db95db9 100644
--- a/traffic_ops/client/v13/origin.go
+++ b/traffic_ops/client/v13/origin.go
@@ -30,7 +30,7 @@ const (
)
// Create an Origin
-func (to *Session) CreateOrigin(origin v13.OriginNullable) (*v13.OriginDetailResponse, ReqInf, error) {
+func (to *Session) CreateOrigin(origin v13.Origin) (*v13.OriginDetailResponse, ReqInf, error) {
var remoteAddr net.Addr
reqBody, err := json.Marshal(origin)
@@ -51,7 +51,7 @@ func (to *Session) CreateOrigin(origin v13.OriginNullable) (*v13.OriginDetailRes
}
// Update an Origin by ID
-func (to *Session) UpdateOriginByID(id int, origin v13.OriginNullable) (*v13.OriginDetailResponse, ReqInf, error) {
+func (to *Session) UpdateOriginByID(id int, origin v13.Origin) (*v13.OriginDetailResponse, ReqInf, error) {
var remoteAddr net.Addr
reqBody, err := json.Marshal(origin)
@@ -73,7 +73,7 @@ func (to *Session) UpdateOriginByID(id int, origin v13.OriginNullable) (*v13.Ori
}
// GET a list of Origins by a query parameter string
-func (to *Session) GetOriginsByQueryParams(queryParams string) ([]v13.OriginNullable, ReqInf, error) {
+func (to *Session) GetOriginsByQueryParams(queryParams string) ([]v13.Origin, ReqInf, error) {
URI := API_v13_Origins + queryParams
resp, remoteAddr, err := to.request(http.MethodGet, URI, nil)
reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
@@ -91,22 +91,22 @@ func (to *Session) GetOriginsByQueryParams(queryParams string) ([]v13.OriginNull
}
// Returns a list of Origins
-func (to *Session) GetOrigins() ([]v13.OriginNullable, ReqInf, error) {
+func (to *Session) GetOrigins() ([]v13.Origin, ReqInf, error) {
return to.GetOriginsByQueryParams("")
}
// GET an Origin by the Origin ID
-func (to *Session) GetOriginByID(id int) ([]v13.OriginNullable, ReqInf, error) {
+func (to *Session) GetOriginByID(id int) ([]v13.Origin, ReqInf, error) {
return to.GetOriginsByQueryParams(fmt.Sprintf("?id=%d", id))
}
// GET an Origin by the Origin name
-func (to *Session) GetOriginByName(name string) ([]v13.OriginNullable, ReqInf, error) {
+func (to *Session) GetOriginByName(name string) ([]v13.Origin, ReqInf, error) {
return to.GetOriginsByQueryParams(fmt.Sprintf("?name=%s", name))
}
// GET a list of Origins by Delivery Service ID
-func (to *Session) GetOriginsByDeliveryServiceID(id int) ([]v13.OriginNullable, ReqInf, error) {
+func (to *Session) GetOriginsByDeliveryServiceID(id int) ([]v13.Origin, ReqInf, error) {
return to.GetOriginsByQueryParams(fmt.Sprintf("?deliveryservice=%d", id))
}
diff --git a/traffic_ops/testing/api/v13/traffic_control.go b/traffic_ops/testing/api/v13/traffic_control.go
index c2dd5b9..e456597 100644
--- a/traffic_ops/testing/api/v13/traffic_control.go
+++ b/traffic_ops/testing/api/v13/traffic_control.go
@@ -30,7 +30,7 @@ type TrafficControl struct {
DeliveryServices []v12.DeliveryService `json:"deliveryservices"`
Divisions []v12.Division `json:"divisions"`
Coordinates []v13.Coordinate `json:"coordinates"`
- Origins []v13.OriginNullable `json:"origins"`
+ Origins []v13.Origin `json:"origins"`
Profiles []v13.Profile `json:"profiles"`
Parameters []v12.Parameter `json:"parameters"`
ProfileParameters []v13.ProfileParameter `json:"profileParameters"`
diff --git a/traffic_ops/traffic_ops_golang/origin/origins.go b/traffic_ops/traffic_ops_golang/origin/origins.go
index 3c5f814..f514043 100644
--- a/traffic_ops/traffic_ops_golang/origin/origins.go
+++ b/traffic_ops/traffic_ops_golang/origin/origins.go
@@ -41,7 +41,7 @@ import (
)
//we need a type alias to define functions on
-type TOOrigin v13.OriginNullable
+type TOOrigin v13.Origin
//the refType is passed into the handlers where a copy of its type is used to decode the json.
var refType = TOOrigin{}
@@ -136,9 +136,9 @@ func (origin *TOOrigin) IsTenantAuthorized(user auth.CurrentUser, db *sqlx.DB) (
return true, nil
}
-// filterAuthorized will filter a slice of OriginNullables based upon tenant. It assumes that tenancy is enabled
-func filterAuthorized(origins []v13.OriginNullable, user auth.CurrentUser, db *sqlx.DB) ([]v13.OriginNullable, error) {
- newOrigins := []v13.OriginNullable{}
+// filterAuthorized will filter a slice of Origins based upon tenant. It assumes that tenancy is enabled
+func filterAuthorized(origins []v13.Origin, user auth.CurrentUser, db *sqlx.DB) ([]v13.Origin, error) {
+ newOrigins := []v13.Origin{}
for _, origin := range origins {
if origin.TenantID == nil {
if origin.ID == nil {
@@ -190,7 +190,7 @@ func (origin *TOOrigin) Read(db *sqlx.DB, params map[string]string, user auth.Cu
return returnable, nil, tc.NoError
}
-func getOrigins(params map[string]string, db *sqlx.DB, privLevel int) ([]v13.OriginNullable, []error, tc.ApiErrorType) {
+func getOrigins(params map[string]string, db *sqlx.DB, privLevel int) ([]v13.Origin, []error, tc.ApiErrorType) {
var rows *sqlx.Rows
var err error
@@ -220,10 +220,10 @@ func getOrigins(params map[string]string, db *sqlx.DB, privLevel int) ([]v13.Ori
}
defer rows.Close()
- origins := []v13.OriginNullable{}
+ origins := []v13.Origin{}
for rows.Next() {
- var s v13.OriginNullable
+ var s v13.Origin
if err = rows.StructScan(&s); err != nil {
return nil, []error{fmt.Errorf("getting origins: %v", err)}, tc.SystemError
}
diff --git a/traffic_ops/traffic_ops_golang/origin/origins_test.go b/traffic_ops/traffic_ops_golang/origin/origins_test.go
index e1e3305..c7d522a 100644
--- a/traffic_ops/traffic_ops_golang/origin/origins_test.go
+++ b/traffic_ops/traffic_ops_golang/origin/origins_test.go
@@ -32,6 +32,7 @@ import (
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/api"
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/auth"
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/test"
+ "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/utils"
"github.com/jmoiron/sqlx"
"gopkg.in/DATA-DOG/go-sqlmock.v1"
@@ -40,36 +41,36 @@ import (
func getTestOrigins() []v13.Origin {
origins := []v13.Origin{}
testOrigin := v13.Origin{
- Cachegroup: "Cachegroup",
- CachegroupID: 1,
- Coordinate: "originCoordinate",
- CoordinateID: 1,
- DeliveryService: "testDS",
- DeliveryServiceID: 1,
- FQDN: "origin.cdn.net",
- ID: 1,
- IP6Address: "dead:beef:cafe::42",
- IPAddress: "10.2.3.4",
- IsPrimary: false,
- LastUpdated: tc.TimeNoMod{Time: time.Now()},
- Name: "originName",
- Port: 443,
- Profile: "profile",
- ProfileID: 1,
- Protocol: "https",
- Tenant: "tenantName",
- TenantID: 1,
+ Cachegroup: utils.StrPtr("Cachegroup"),
+ CachegroupID: utils.IntPtr(1),
+ Coordinate: utils.StrPtr("originCoordinate"),
+ CoordinateID: utils.IntPtr(1),
+ DeliveryService: utils.StrPtr("testDS"),
+ DeliveryServiceID: utils.IntPtr(1),
+ FQDN: utils.StrPtr("origin.cdn.net"),
+ ID: utils.IntPtr(1),
+ IP6Address: utils.StrPtr("dead:beef:cafe::42"),
+ IPAddress: utils.StrPtr("10.2.3.4"),
+ IsPrimary: utils.BoolPtr(false),
+ LastUpdated: utils.NewTimeNoMod(),
+ Name: utils.StrPtr("originName"),
+ Port: utils.IntPtr(443),
+ Profile: utils.StrPtr("profile"),
+ ProfileID: utils.IntPtr(1),
+ Protocol: utils.StrPtr("https"),
+ Tenant: utils.StrPtr("tenantName"),
+ TenantID: utils.IntPtr(1),
}
origins = append(origins, testOrigin)
testOrigin2 := testOrigin
- testOrigin2.FQDN = "origin2.cdn.com"
- testOrigin2.Name = "origin2"
+ testOrigin2.FQDN = utils.StrPtr("origin2.cdn.com")
+ testOrigin2.Name = utils.StrPtr("origin2")
origins = append(origins, testOrigin2)
testOrigin3 := testOrigin
- testOrigin3.FQDN = "origin3.cdn.org"
- testOrigin3.Name = "origin3"
+ testOrigin3.FQDN = utils.StrPtr("origin3.cdn.org")
+ testOrigin3.Name = utils.StrPtr("origin3")
origins = append(origins, testOrigin3)
return origins
diff --git a/traffic_ops/traffic_ops_golang/utils/ptr.go b/traffic_ops/traffic_ops_golang/utils/ptr.go
new file mode 100644
index 0000000..066a083
--- /dev/null
+++ b/traffic_ops/traffic_ops_golang/utils/ptr.go
@@ -0,0 +1,42 @@
+package utils
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+ import (
+ "time"
+
+ "github.com/apache/incubator-trafficcontrol/lib/go-tc"
+ )
+
+ func StrPtr(str string) *string {
+ return &str
+}
+
+func IntPtr(i int) *int {
+ return &i
+}
+
+func BoolPtr(b bool) *bool {
+ return &b
+}
+
+func NewTimeNoMod() *tc.TimeNoMod {
+ return &tc.TimeNoMod{Time: time.Now()}
+}
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
dewrich@apache.org.