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

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


The following commit(s) were added to refs/heads/master by this push:
     new 20daeaa  Add t3c-preprocess cachegroup directive (#6558)
20daeaa is described below

commit 20daeaace4c01e965f175ed8cda63702a4b19734
Author: Robert O Butts <[email protected]>
AuthorDate: Mon Feb 21 16:09:44 2022 -0700

    Add t3c-preprocess cachegroup directive (#6558)
---
 CHANGELOG.md                                       |  1 +
 cache-config/t3c-preprocess/README.md              |  2 ++
 cache-config/t3c-preprocess/t3c-preprocess.go      |  6 +++++
 cache-config/t3c-preprocess/t3c-preprocess_test.go | 26 ++++++++++++++++++----
 4 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index f48ea5e..96f2fb5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -45,6 +45,7 @@ The format is based on [Keep a 
Changelog](http://keepachangelog.com/en/1.0.0/).
 - [#6405](https://github.com/apache/trafficcontrol/issues/6405) Added cache 
config version to all t3c apps and config file headers
 - Traffic Vault: Added additional flag to TV Riak (Deprecated) Util
 - Added Traffic Vault Postgres columns, a Traffic Ops API endpoint, and 
Traffic Portal page to show SSL certificate expiration information.
+- Added cache config `__CACHEGROUP__` preprocess directive, to allow injecting 
the local server's cachegroup name into any config file
 - Added support for a DS profile parameter 'LastRawRemapPre' and 
'LastRawRemapPost' which allows raw text lines to be pre or post pended to 
remap.config.
 
 ### Fixed
diff --git a/cache-config/t3c-preprocess/README.md 
b/cache-config/t3c-preprocess/README.md
index ae93e70..dc5fda0 100644
--- a/cache-config/t3c-preprocess/README.md
+++ b/cache-config/t3c-preprocess/README.md
@@ -65,6 +65,8 @@ The following directives will be replaced. These directives 
may be placed anywhe
     __FULL_HOSTNAME__   is replaced with the Server's HostName, a dot, and the 
Server's DomainName
                         from Traffic Ops (i.e. the Server's Fully Qualified 
Domain Name).
 
+    __CACHEGROUP__      is replaced with the Server's Cachegroup name from 
Traffic Ops.
+
     __RETURN__          is replaced with a newline character, and any 
whitespace before or after
                         it is removed.
 
diff --git a/cache-config/t3c-preprocess/t3c-preprocess.go 
b/cache-config/t3c-preprocess/t3c-preprocess.go
index 6f53bd3..cd95bc8 100644
--- a/cache-config/t3c-preprocess/t3c-preprocess.go
+++ b/cache-config/t3c-preprocess/t3c-preprocess.go
@@ -95,6 +95,12 @@ func PreprocessConfigFile(server *atscfg.Server, cfgFile 
string) string {
        } else {
                cfgFile = strings.Replace(cfgFile, `__FULL_HOSTNAME__`, 
*server.HostName+`.`+*server.DomainName, -1)
        }
+       if server.Cachegroup != nil && *server.Cachegroup != "" {
+               cfgFile = strings.Replace(cfgFile, `__CACHEGROUP__`, 
*server.Cachegroup, -1)
+       } else {
+               log.Errorln("Preprocessing: this server missing Cachegroup, 
cannot replace __CACHEGROUP__ directives!")
+       }
+
        cfgFile = returnRegex.ReplaceAllString(cfgFile, "\n")
        return cfgFile
 }
diff --git a/cache-config/t3c-preprocess/t3c-preprocess_test.go 
b/cache-config/t3c-preprocess/t3c-preprocess_test.go
index a68d21a..64a41e4 100644
--- a/cache-config/t3c-preprocess/t3c-preprocess_test.go
+++ b/cache-config/t3c-preprocess/t3c-preprocess_test.go
@@ -29,7 +29,7 @@ import (
 
 func TestPreprocessConfigFile(t *testing.T) {
        // the TCP port replacement is fundamentally different for 80 vs 
non-80, so test both
-       {
+       t.Run("verify port 80 replace", func(t *testing.T) {
                server := &atscfg.Server{}
                server.TCPPort = util.IntPtr(8080)
                server.Interfaces = []tc.ServerInterfaceInfoV40{}
@@ -54,9 +54,9 @@ func TestPreprocessConfigFile(t *testing.T) {
                if expected != actual {
                        t.Errorf("PreprocessConfigFile expected '%v' actual 
'%v'", expected, actual)
                }
-       }
+       })
 
-       {
+       t.Run("verify nonstandard port replace", func(t *testing.T) {
                server := &atscfg.Server{}
                server.TCPPort = util.IntPtr(80)
                server.Interfaces = []tc.ServerInterfaceInfoV40{}
@@ -82,5 +82,23 @@ func TestPreprocessConfigFile(t *testing.T) {
                if expected != actual {
                        t.Errorf("PreprocessConfigFile expected '%v' actual 
'%v'", expected, actual)
                }
-       }
+       })
+
+       t.Run("verify cachegroup replace", func(t *testing.T) {
+               server := &atscfg.Server{}
+               server.TCPPort = util.IntPtr(80)
+               server.Cachegroup = util.StrPtr("mycachegroup")
+               server.HostName = util.StrPtr("my-edge")
+               server.DomainName = util.StrPtr("example.net")
+
+               cfgFile := "abc__CACHEGROUP__defghi __RETURN__  \t __HOSTNAME__ 
jkl __FULL_HOSTNAME__ \n__SOMETHING__ __ELSE__\nmno\r\n"
+
+               actual := PreprocessConfigFile(server, cfgFile)
+
+               expected := "abcmycachegroupdefghi\nmy-edge jkl 
my-edge.example.net \n__SOMETHING__ __ELSE__\nmno\r\n"
+
+               if expected != actual {
+                       t.Errorf("PreprocessConfigFile expected '%v' actual 
'%v'", expected, actual)
+               }
+       })
 }

Reply via email to