You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2018/12/13 18:24:11 UTC

[GitHub] rawlinp commented on a change in pull request #3110: updated cachegroup api to include cachegroup fallbacks. updated docum…

rawlinp commented on a change in pull request #3110: updated cachegroup api to include cachegroup fallbacks. updated docum…
URL: https://github.com/apache/trafficcontrol/pull/3110#discussion_r241505240
 
 

 ##########
 File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
 ##########
 @@ -244,6 +249,88 @@ func (cg *TOCacheGroup) createLocalizationMethods() error {
 	return nil
 }
 
+func (cg *TOCacheGroup) createCacheGroupFallbacks() error {
+	if cg.Fallbacks != nil {
+		for _, fallback := range *cg.Fallbacks {
+			isValid, err := cg.isValidCacheGroupFallback(fallback)
+			if err != nil {
+				return err
+			}
+
+			if !isValid {
+				log.Errorf("the cache group " + fallback + "is not valid as a fallback.  It must exist as a cache group and be of type EDGE_LOC.")
+				return errors.New("the cache group " + fallback + "is not valid as a fallback.  It must exist as a cache group and be of type EDGE_LOC.")
+			}
+		}
+
+		deleteCgfQuery := `DELETE FROM cachegroup_fallbacks where primary_cg = $1`
+		if _, err := cg.ReqInfo.Tx.Tx.Exec(deleteCgfQuery, *cg.ID); err != nil {
+			return fmt.Errorf("unable to delete cachegroup_fallbacks for cachegroup %d: %s", *cg.ID, err.Error())
+		}
+		insertCgfQuery := `INSERT INTO cachegroup_fallbacks (primary_cg, backup_cg, set_order) VALUES ($1, $2, $3)`
+		for orderIndex, fallback := range *cg.Fallbacks {
+			var backupId int
+			query := `SELECT cachegroup.id FROM cachegroup WHERE cachegroup.name = $1;`
+			err := cg.ReqInfo.Tx.Tx.QueryRow(query, fallback).Scan(&backupId)
+			if err != nil {
+				log.Errorf("received error: %++v from cachegroup query execution", err)
+				return err
+			}
+
+			if _, err := cg.ReqInfo.Tx.Tx.Exec(insertCgfQuery, *cg.ID, backupId, orderIndex); err != nil {
+				return fmt.Errorf("unable to insert cachegroup_fallbacks for cachegroup %d: %s", *cg.ID, err.Error())
+			}
+		}
+	}
+
+	return nil
+}
+
+func (cg *TOCacheGroup) isValidCacheGroupFallback(fallbackName string) (bool, error) {
+	var isValid bool
+	query := `SELECT(
+SELECT cachegroup.id 
+FROM cachegroup 
+JOIN type on type.id = cachegroup.type 
+WHERE cachegroup.name = $1 
+AND (type.name LIKE 'EDGE%')
 
 Review comment:
   I think this should check the full type name instead: `type.name = 'EDGE_LOC'`

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services