You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by mi...@apache.org on 2018/05/22 02:47:10 UTC

[incubator-trafficcontrol] branch master updated: Add TO Go staticdnsentries endpoint

This is an automated email from the ASF dual-hosted git repository.

mitchell852 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new fe96ec8  Add TO Go staticdnsentries endpoint
fe96ec8 is described below

commit fe96ec850c235bb506a19224f3d634cf8bbaa84b
Author: Robert Butts <ro...@apache.org>
AuthorDate: Mon May 7 15:14:07 2018 -0600

    Add TO Go staticdnsentries endpoint
---
 lib/go-tc/crconfig.go                              |  4 +-
 lib/go-tc/staticdnsentry.go                        | 29 ++++++++
 .../traffic_ops_golang/crconfig/deliveryservice.go |  6 +-
 .../crconfig/deliveryservice_test.go               | 12 ++--
 traffic_ops/traffic_ops_golang/routes.go           |  3 +
 .../staticdnsentry/staticdnsentry.go               | 77 ++++++++++++++++++++++
 6 files changed, 120 insertions(+), 11 deletions(-)

diff --git a/lib/go-tc/crconfig.go b/lib/go-tc/crconfig.go
index aee4976..6683271 100644
--- a/lib/go-tc/crconfig.go
+++ b/lib/go-tc/crconfig.go
@@ -123,14 +123,14 @@ type CRConfigDeliveryService struct {
 	DeepCachingType          *DeepCachingType                      `json:"deepCachingType"`
 	GeoEnabled               []CRConfigGeoEnabled                  `json:"geoEnabled,omitempty"`
 	GeoLimitRedirectURL      *string                               `json:"geoLimitRedirectURL,omitempty"`
-	StaticDNSEntries         []StaticDNSEntry                      `json:"staticDnsEntries,omitempty"`
+	StaticDNSEntries         []CRConfigStaticDNSEntry              `json:"staticDnsEntries,omitempty"`
 }
 
 type CRConfigGeoEnabled struct {
 	CountryCode string `json:"countryCode"`
 }
 
-type StaticDNSEntry struct {
+type CRConfigStaticDNSEntry struct {
 	Name  string `json:"name"`
 	TTL   int    `json:"ttl"`
 	Type  string `json:"type"`
diff --git a/lib/go-tc/staticdnsentry.go b/lib/go-tc/staticdnsentry.go
new file mode 100644
index 0000000..d27610b
--- /dev/null
+++ b/lib/go-tc/staticdnsentry.go
@@ -0,0 +1,29 @@
+package tc
+
+/*
+ * 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.
+ */
+
+type StaticDNSEntry struct {
+	Address         string `json:"address" db:"address"`
+	CacheGroup      string `json:"cachegroup" db:"cachegroup"`
+	DeliveryService string `json:"deliveryservice" db:"dsname"`
+	Host            string `json:"host" db:"host"`
+	TTL             int64  `json:"ttl" db:"ttl"`
+	Type            string `json:"type" db:"type"`
+}
diff --git a/traffic_ops/traffic_ops_golang/crconfig/deliveryservice.go b/traffic_ops/traffic_ops_golang/crconfig/deliveryservice.go
index 55cf884..28913bd 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/deliveryservice.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/deliveryservice.go
@@ -341,8 +341,8 @@ and d.active = true
 	return dses, nil
 }
 
-func getStaticDNSEntries(cdn string, db *sql.DB) (map[tc.DeliveryServiceName][]tc.StaticDNSEntry, error) {
-	entries := map[tc.DeliveryServiceName][]tc.StaticDNSEntry{}
+func getStaticDNSEntries(cdn string, db *sql.DB) (map[tc.DeliveryServiceName][]tc.CRConfigStaticDNSEntry, error) {
+	entries := map[tc.DeliveryServiceName][]tc.CRConfigStaticDNSEntry{}
 
 	q := `
 select d.xml_id as ds, e.host as name, e.ttl, e.address as value, t.name as type
@@ -368,7 +368,7 @@ and d.active = true
 			return nil, errors.New("scanning static DNS entries: " + err.Error())
 		}
 		ttype = strings.Replace(ttype, "_RECORD", "", -1)
-		entries[tc.DeliveryServiceName(ds)] = append(entries[tc.DeliveryServiceName(ds)], tc.StaticDNSEntry{Name: name, TTL: ttl, Value: value, Type: ttype})
+		entries[tc.DeliveryServiceName(ds)] = append(entries[tc.DeliveryServiceName(ds)], tc.CRConfigStaticDNSEntry{Name: name, TTL: ttl, Value: value, Type: ttype})
 	}
 	return entries, nil
 }
diff --git a/traffic_ops/traffic_ops_golang/crconfig/deliveryservice_test.go b/traffic_ops/traffic_ops_golang/crconfig/deliveryservice_test.go
index eede045..e624433 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/deliveryservice_test.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/deliveryservice_test.go
@@ -133,8 +133,8 @@ func randDS() tc.CRConfigDeliveryService {
 		DeepCachingType: nil,
 		GeoEnabled:      nil,
 		// GeoLimitRedirectURL: randStr(),
-		StaticDNSEntries: []tc.StaticDNSEntry{
-			tc.StaticDNSEntry{
+		StaticDNSEntries: []tc.CRConfigStaticDNSEntry{
+			tc.CRConfigStaticDNSEntry{
 				Name:  *randStr(),
 				TTL:   *randInt(),
 				Type:  *randStr(),
@@ -343,17 +343,17 @@ func TestGetDSRegexesDomains(t *testing.T) {
 	}
 }
 
-func ExpectedGetStaticDNSEntries(expectedMakeDSes map[string]tc.CRConfigDeliveryService) map[tc.DeliveryServiceName][]tc.StaticDNSEntry {
-	expected := map[tc.DeliveryServiceName][]tc.StaticDNSEntry{}
+func ExpectedGetStaticDNSEntries(expectedMakeDSes map[string]tc.CRConfigDeliveryService) map[tc.DeliveryServiceName][]tc.CRConfigStaticDNSEntry {
+	expected := map[tc.DeliveryServiceName][]tc.CRConfigStaticDNSEntry{}
 	for dsName, ds := range expectedMakeDSes {
 		for _, entry := range ds.StaticDNSEntries {
-			expected[tc.DeliveryServiceName(dsName)] = append(expected[tc.DeliveryServiceName(dsName)], tc.StaticDNSEntry{Name: entry.Name, TTL: entry.TTL, Value: entry.Value, Type: entry.Type})
+			expected[tc.DeliveryServiceName(dsName)] = append(expected[tc.DeliveryServiceName(dsName)], tc.CRConfigStaticDNSEntry{Name: entry.Name, TTL: entry.TTL, Value: entry.Value, Type: entry.Type})
 		}
 	}
 	return expected
 }
 
-func MockGetStaticDNSEntries(mock sqlmock.Sqlmock, expected map[tc.DeliveryServiceName][]tc.StaticDNSEntry, cdn string) {
+func MockGetStaticDNSEntries(mock sqlmock.Sqlmock, expected map[tc.DeliveryServiceName][]tc.CRConfigStaticDNSEntry, cdn string) {
 	rows := sqlmock.NewRows([]string{"ds", "name", "ttl", "value", "type"})
 	for dsName, entries := range expected {
 		for _, entry := range entries {
diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go
index ea54353..8858c32 100644
--- a/traffic_ops/traffic_ops_golang/routes.go
+++ b/traffic_ops/traffic_ops_golang/routes.go
@@ -51,6 +51,7 @@ import (
 	"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/region"
 	"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/role"
 	"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/server"
+	"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/staticdnsentry"
 	"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/status"
 	"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/systeminfo"
 	"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/types"
@@ -237,6 +238,8 @@ func Routes(d ServerData) ([]Route, []RawRoute, http.Handler, error) {
 		{1.3, http.MethodPost, `servers/{id}/deliveryservices$`, server.AssignDeliveryServicesToServerHandler(d.DB), auth.PrivLevelOperations, Authenticated, nil},
 		{1.3, http.MethodGet, `servers/{host_name}/update_status$`, server.GetServerUpdateStatusHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
 
+		{1.1, http.MethodGet, `staticdnsentries/?(\.json)?$`, api.ReadHandler(staticdnsentry.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
+
 		//ProfileParameters
 		{1.1, http.MethodGet, `profiles/{id}/parameters/?(\.json)?$`, profileparameter.GetProfileID(d.DB.DB), auth.PrivLevelReadOnly, Authenticated, nil},
 		{1.1, http.MethodGet, `profiles/{id}/unassigned_parameters/?(\.json)?$`, profileparameter.GetUnassigned(d.DB.DB), auth.PrivLevelReadOnly, Authenticated, nil},
diff --git a/traffic_ops/traffic_ops_golang/staticdnsentry/staticdnsentry.go b/traffic_ops/traffic_ops_golang/staticdnsentry/staticdnsentry.go
new file mode 100644
index 0000000..6308ba4
--- /dev/null
+++ b/traffic_ops/traffic_ops_golang/staticdnsentry/staticdnsentry.go
@@ -0,0 +1,77 @@
+package staticdnsentry
+
+/*
+ * 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 (
+	"github.com/apache/incubator-trafficcontrol/lib/go-log"
+	"github.com/apache/incubator-trafficcontrol/lib/go-tc"
+	"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/auth"
+	"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
+	"github.com/jmoiron/sqlx"
+)
+
+type TOStaticDNSEntry tc.StaticDNSEntry
+
+func GetRefType() *TOStaticDNSEntry { return &TOStaticDNSEntry{} }
+
+func (staticDNSEntry *TOStaticDNSEntry) Read(db *sqlx.DB, parameters map[string]string, user auth.CurrentUser) ([]interface{}, []error, tc.ApiErrorType) {
+	queryParamsToQueryCols := map[string]dbhelpers.WhereColumnInfo{
+		"deliveryservice": dbhelpers.WhereColumnInfo{"deliveryservice", nil}, // order by
+	}
+	where, orderBy, queryValues, errs := dbhelpers.BuildWhereAndOrderBy(parameters, queryParamsToQueryCols)
+	if len(errs) > 0 {
+		log.Errorf("Data Conflict Error")
+		return nil, errs, tc.DataConflictError
+	}
+	query := selectQuery() + where + orderBy
+	log.Debugln("Query is ", query)
+	rows, err := db.NamedQuery(query, queryValues)
+	if err != nil {
+		log.Errorf("Error querying StaticDNSEntries: %v", err)
+		return nil, []error{tc.DBError}, tc.SystemError
+	}
+	defer rows.Close()
+	staticDNSEntries := []interface{}{}
+	for rows.Next() {
+		s := tc.StaticDNSEntry{}
+		if err = rows.StructScan(&s); err != nil {
+			log.Errorln("error parsing StaticDNSEntry rows: " + err.Error())
+			return nil, []error{tc.DBError}, tc.SystemError
+		}
+		staticDNSEntries = append(staticDNSEntries, s)
+	}
+	return staticDNSEntries, []error{}, tc.NoError
+}
+
+func selectQuery() string {
+	return `
+SELECT
+ds.xml_id as dsname,
+sde.host,
+sde.ttl,
+sde.address,
+tp.name as type,
+cg.name as cachegroup
+FROM staticdnsentry as sde
+JOIN type as tp on sde.type = tp.id
+JOIN cachegroup as cg ON sde.cachegroup = cg.id
+JOIN deliveryservice as ds on sde.deliveryservice = ds.id
+`
+}

-- 
To stop receiving notification emails like this one, please contact
mitchell852@apache.org.