You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by el...@apache.org on 2018/06/14 17:10:18 UTC

[trafficcontrol] 03/08: CrConfig's backupLocation Json node generation logic in GoLang

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

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

commit dc88525eb54b29b62ffa1567aff7ae4640ed11cc
Author: Vijayanand Subramanian <vi...@gmail.com>
AuthorDate: Fri May 25 02:36:55 2018 -0400

    CrConfig's backupLocation Json node generation logic in GoLang
---
 lib/go-tc/cachegroupfallback.go                    | 55 ++++++++++++++++++++++
 lib/go-tc/crconfig.go                              | 13 ++++-
 .../traffic_ops_golang/crconfig/edgelocations.go   | 30 ++++++++++++
 3 files changed, 96 insertions(+), 2 deletions(-)

diff --git a/lib/go-tc/cachegroupfallback.go b/lib/go-tc/cachegroupfallback.go
new file mode 100644
index 0000000..7e1f95b
--- /dev/null
+++ b/lib/go-tc/cachegroupfallback.go
@@ -0,0 +1,55 @@
+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.
+ */
+
+// A List of CACHEGROUPFALLBACKs Response
+// swagger:response CACHEGROUPFALLBACKsResponse
+// in: body
+type CACHEGROUPFALLBACKsResponse struct {
+	// in: body
+	Response []CACHEGROUPFALLBACK `json:"response"`
+}
+
+// A Single CACHEGROUPFALLBACK Response for Update and Create to depict what changed
+// swagger:response CACHEGROUPFALLBACKResponse
+// in: body
+type CACHEGROUPFALLBACKResponse struct {
+	// in: body
+	Response CACHEGROUPFALLBACK `json:"response"`
+}
+
+// CACHEGROUPFALLBACK ...
+type CACHEGROUPFALLBACK struct {
+
+	PrimaryCgId int `json:"primaryId" db:"primary_cg"`
+	BackupCgId  int `json:"backupId" db:"backup_cg"`
+	SetOrder    int `json:"setOrder" db:"set_order"`
+
+}
+
+// CACHEGROUPFALLBACKNullable ...
+type CACHEGROUPFALLBACKNullable struct {
+
+	PrimaryCgId *int `json:"primaryId" db:"primary_cg"`
+	BackupCgId  *int `json:"backupId" db:"backup_cg"`
+	SetOrder    *int `json:"setOrder" db:"set_order"`
+}
+
diff --git a/lib/go-tc/crconfig.go b/lib/go-tc/crconfig.go
index 6683271..18133bf 100644
--- a/lib/go-tc/crconfig.go
+++ b/lib/go-tc/crconfig.go
@@ -151,9 +151,18 @@ type CRConfigDispersion struct {
 	Shuffled bool `json:"shuffled,string"`
 }
 
+
+type CRConfigBackupLocations struct {
+	FallbackToClosest bool     `json:"fallbackToClosest,string"`
+	List              []string `json:"list,omitempty"`
+
+}
+
 type CRConfigLatitudeLongitude struct {
-	Lat float64 `json:"latitude"`
-	Lon float64 `json:"longitude"`
+	Lat               float64                 `json:"latitude"`
+	Lon               float64                 `json:"longitude"`
+	BackupLocations   CRConfigBackupLocations `json:"backupLocations,omitempty"`
+
 }
 
 type CRConfigLatitudeLongitudeShort struct {
diff --git a/traffic_ops/traffic_ops_golang/crconfig/edgelocations.go b/traffic_ops/traffic_ops_golang/crconfig/edgelocations.go
index 2d43fd6..1d52356 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/edgelocations.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/edgelocations.go
@@ -57,6 +57,36 @@ and (st.name = 'REPORTED' or st.name = 'ONLINE' or st.name = 'ADMIN_DOWN')
 		if ttype == RouterTypeName {
 			routerLocs[cachegroup] = latlon
 		} else {
+			primaryCacheId := ""
+			if err := db.QueryRow(`select id from cachegroup where name = $1`, cachegroup).Scan(&primaryCacheId); err != nil {
+				return nil, nil, errors.New("Failed while retrieving from cachegroup: " + err.Error())
+			}
+
+			dbRows, err := db.Query(`select backup_cg from cachegroup_fallbacks where primary_cg = $1 order by set_order`, primaryCacheId)
+
+			if err != nil {
+				return nil, nil, errors.New("Error retrieving from cachegroup_fallbacks: " + err.Error())
+			}
+			defer dbRows.Close()
+
+			index := 0
+			for dbRows.Next() {
+				backup_id := ""
+				backup_name := ""
+				if err := dbRows.Scan(&backup_id); err != nil {
+					return nil, nil, errors.New("Error while scanning from cachegroup_fallbacks: " + err.Error())
+				}
+				if err := db.QueryRow(`select name from cachegroup where id = $1`, backup_id).Scan(&backup_name); err != nil {
+					return nil, nil, errors.New("Error scanning cachegroup: " + err.Error())
+				} else {
+					latlon.BackupLocations.List = append(latlon.BackupLocations.List, backup_name)
+					index++
+				}
+			}
+
+			 if err := dbRows.Err(); err != nil {
+				return nil, nil, errors.New("Error iterating cachegroup_fallbacks rows: " + err.Error())
+			}
 			edgeLocs[cachegroup] = latlon
 		}
 	}

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