dewrich closed pull request #2002: TO golang -- improve api/1.3/about endpoint
URL: https://github.com/apache/incubator-trafficcontrol/pull/2002
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/traffic_ops/build/traffic_ops.spec 
b/traffic_ops/build/traffic_ops.spec
index 326bcf635..ded298dc3 100644
--- a/traffic_ops/build/traffic_ops.spec
+++ b/traffic_ops/build/traffic_ops.spec
@@ -104,7 +104,7 @@ Built: %(date) by %{getenv: USER}
       #echo "go getting at $(pwd)" && \
       #go get -d -v && \
       echo "go building at $(pwd)" && \
-      go build -ldflags "-X main.version=%{version}-%{release} -B 0x`git 
rev-parse HEAD`" \
+      go build -ldflags "-X main.version=traffic_ops-%{version}-%{release} -B 
0x`git rev-parse HEAD`" \
     ) || { echo "Could not build go program at $(pwd): $!"; exit 1; }
 
 %install
diff --git a/traffic_ops/traffic_ops_golang/about/about.go 
b/traffic_ops/traffic_ops_golang/about/about.go
index 36703653d..8a4e674fd 100644
--- a/traffic_ops/traffic_ops_golang/about/about.go
+++ b/traffic_ops/traffic_ops_golang/about/about.go
@@ -28,52 +28,54 @@ import (
 
 // about allows access to the version info identified at build time
 type about struct {
-       CommitHash           string `json:"commitHash,omitempty"`
-       Commits              string `json:"commits,omitempty"`
-       GoVersion            string `json:"goVersion,omitempty"`
-       TrafficOpsName       string `json:"name,omitempty"`
-       TrafficOpsRPMVersion string `json:"trafficOpsRPMVersion,omitempty"`
-       TrafficOpsVersion    string `json:"trafficOpsVersion,omitempty"`
+       CommitHash string `json:"commitHash,omitempty"`
+       Commits    string `json:"commits,omitempty"`
+       GoVersion  string `json:"goVersion,omitempty"`
+       Release    string `json:"release"`
+       Name       string `json:"name,omitempty"`
+       RPMVersion string `json:"RPMVersion,omitempty"`
+       Version    string `json:"Version,omitempty"`
 }
 
 // About contains version info to be exposed by `api/.../about.json` endpoint
 var About about
 
-func splitRPMVersion(v string) (string, string, string, string) {
+func splitRPMVersion(v string) (string, string, string, string, string) {
 
        if v == "" {
-               return "UnknownVersion", "", "", ""
+               return "UnknownVersion", "", "", "", ""
        }
-       // RPM version is something like traffic_ops-2.3.0-8765.a0b1c3d4
-       //  -- if not of that form, Name, Version, Commits, CommitHash may be 
missing
+       // RPM version is something like traffic_ops-2.3.0-8765.a0b1c3d4.el7
+       //  -- if not of that form, Name, Version, Commits, CommitHash, Release 
may be missing
        s := strings.SplitN(v, "-", 3)
        if len(s) >= 3 {
                // 3rd field is commits.hash
-               t := strings.SplitN(s[2], ".", 2)
+               t := strings.SplitN(s[2], ".", 3)
                s = append(s[0:2], t...)
        }
-       for cap(s) < 4 {
+       for len(s) < 5 {
                s = append(s, "")
        }
-       return s[0], s[1], s[2], s[3]
+       return s[0], s[1], s[2], s[3], s[4]
 }
 
-func init() {
-       // name, version, commits, hash -- parts of rpm version string
-       n, v, c, h := splitRPMVersion(About.TrafficOpsRPMVersion)
+// SetAbout is called by main.main to store the static info for the .../about 
endpoint
+func SetAbout(s string) {
+       // name, version, commits, hash, Release -- parts of rpm version string
+       n, v, c, h, r := splitRPMVersion(s)
        About = about{
-               GoVersion:            runtime.Version(),
-               TrafficOpsRPMVersion: About.TrafficOpsRPMVersion,
-               TrafficOpsName:       n,
-               TrafficOpsVersion:    v,
-               Commits:              c,
-               CommitHash:           h,
+               CommitHash: h,
+               Commits:    c,
+               GoVersion:  runtime.Version(),
+               Release:    r,
+               Name:       n,
+               RPMVersion: s,
+               Version:    v,
        }
-
 }
 
-// AboutHandler returns info about running Traffic Ops
-func AboutHandler() http.HandlerFunc {
+// Handler returns info about running Traffic Ops
+func Handler() http.HandlerFunc {
        return func(w http.ResponseWriter, r *http.Request) {
                w.Header().Set("Content-Type", "application/json")
 
diff --git a/traffic_ops/traffic_ops_golang/about/about_test.go 
b/traffic_ops/traffic_ops_golang/about/about_test.go
index 19f5a0c5c..5aefacdfe 100644
--- a/traffic_ops/traffic_ops_golang/about/about_test.go
+++ b/traffic_ops/traffic_ops_golang/about/about_test.go
@@ -23,16 +23,16 @@ import (
        "testing"
 )
 
-var testSet = map[string][]string{
-       "test0-0.1.0-1234.01ab23cd": []string{"test0", "0.1.0", "1234", 
"01ab23cd"},
-       "test1-0.2.0":               []string{"test1", "0.2.0", "", ""},
-       "test2":                     []string{"test2", "", "", ""},
+var testSet = map[string][5]string{
+       "traffic_ops-2.3.0-8095.fd4fc11a.el7": [5]string{"traffic_ops", 
"2.3.0", "8095", "fd4fc11a", "el7"},
+       "test0-0.1.0-1234.01ab23cd.el7":       [5]string{"test0", "0.1.0", 
"1234", "01ab23cd", "el7"},
+       "test1-0.2.0":                         [5]string{"test1", "0.2.0", "", 
""},
+       "test2":                               [5]string{"test2", "", "", ""},
 }
 
 func TestSplitRPMVersion(t *testing.T) {
        for s, e := range testSet {
-               t.Logf("Testing %s %v", s, e)
-               n, v, c, h := splitRPMVersion(s)
+               n, v, c, h, a := splitRPMVersion(s)
 
                if n != e[0] {
                        t.Errorf("expected name '%s', got '%s'", n, e[0])
@@ -46,5 +46,8 @@ func TestSplitRPMVersion(t *testing.T) {
                if h != e[3] {
                        t.Errorf("expected commitHash '%s', got '%s'", h, e[3])
                }
+               if a != e[4] {
+                       t.Errorf("expected commitHash '%s', got '%s'", h, e[4])
+               }
        }
 }
diff --git a/traffic_ops/traffic_ops_golang/routes.go 
b/traffic_ops/traffic_ops_golang/routes.go
index 2ecef9236..6fdf28933 100644
--- a/traffic_ops/traffic_ops_golang/routes.go
+++ b/traffic_ops/traffic_ops_golang/routes.go
@@ -68,7 +68,7 @@ func Routes(d ServerData) ([]Route, http.Handler, error) {
 
        routes := []Route{
                //About
-               {1.3, http.MethodGet, `about/?(\.json)?$`, 
about.AboutHandler(), auth.PrivLevelReadOnly, Authenticated, nil},
+               {1.3, http.MethodGet, `about/?(\.json)?$`, about.Handler(), 
auth.PrivLevelReadOnly, Authenticated, nil},
 
                // Proxied routes
                //CDNs
diff --git a/traffic_ops/traffic_ops_golang/traffic_ops_golang.go 
b/traffic_ops/traffic_ops_golang/traffic_ops_golang.go
index dfd5f27bd..f8998bf34 100644
--- a/traffic_ops/traffic_ops_golang/traffic_ops_golang.go
+++ b/traffic_ops/traffic_ops_golang/traffic_ops_golang.go
@@ -39,7 +39,7 @@ import (
 var version = "development"
 
 func init() {
-       about.About.TrafficOpsRPMVersion = version
+       about.SetAbout(version)
 }
 
 func main() {
@@ -50,7 +50,7 @@ func main() {
        flag.Parse()
 
        if *showVersion {
-               fmt.Println(about.About.TrafficOpsRPMVersion)
+               fmt.Println(about.About.RPMVersion)
                os.Exit(0)
        }
        if len(os.Args) < 2 {
diff --git a/traffic_ops/traffic_ops_golang/wrappers.go 
b/traffic_ops/traffic_ops_golang/wrappers.go
index 6f5cb044a..93b92c7ca 100644
--- a/traffic_ops/traffic_ops_golang/wrappers.go
+++ b/traffic_ops/traffic_ops_golang/wrappers.go
@@ -42,7 +42,7 @@ import (
 )
 
 // ServerName - the server identifier
-var ServerName = "traffic_ops_golang" + "/" + about.About.TrafficOpsVersion
+var ServerName = "traffic_ops_golang" + "/" + about.About.Version
 
 // AuthBase ...
 type AuthBase struct {


 

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to