dewrich closed pull request #1980: [Issue 1838] add `/api/1.3/about` endpoint 
and `-version` option on cmd line
URL: https://github.com/apache/incubator-trafficcontrol/pull/1980
 
 
   

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 ef8093913..326bcf635 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 "-B 0x`git rev-parse HEAD`" \
+      go build -ldflags "-X main.version=%{version}-%{release} -B 0x`git 
rev-parse HEAD`" \
     ) || { echo "Could not build go program at $(pwd): $!"; exit 1; }
 
 %install
diff --git a/traffic_ops/client/v13/about.go b/traffic_ops/client/v13/about.go
new file mode 100644
index 000000000..495e27843
--- /dev/null
+++ b/traffic_ops/client/v13/about.go
@@ -0,0 +1,43 @@
+/*
+
+   Licensed 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.
+*/
+
+package v13
+
+import (
+       "encoding/json"
+       "net/http"
+)
+
+const (
+       API_v13_ABOUT = "/api/1.3/about"
+)
+
+// GetAbout gets data about the TO instance
+func (to *Session) GetAbout() (map[string]string, ReqInf, error) {
+       route := API_v13_ABOUT
+       resp, remoteAddr, err := to.request(http.MethodGet, route, nil)
+       reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: 
remoteAddr}
+       if err != nil {
+               return nil, reqInf, err
+       }
+       defer resp.Body.Close()
+
+       var data map[string]string
+       if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
+               return nil, reqInf, err
+       }
+
+       return data, reqInf, nil
+}
diff --git a/traffic_ops/testing/api/v13/about_test.go 
b/traffic_ops/testing/api/v13/about_test.go
new file mode 100644
index 000000000..8e64c0cca
--- /dev/null
+++ b/traffic_ops/testing/api/v13/about_test.go
@@ -0,0 +1,28 @@
+package v13
+
+/*
+
+   Licensed 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 (
+       "testing"
+)
+
+func TestAbout(t *testing.T) {
+       m, _, err := TOSession.GetAbout()
+       if err != nil {
+               t.Errorf("error from GetAbout(): %v", err)
+       }
+       t.Logf("about: %v", m)
+}
diff --git a/traffic_ops/traffic_ops_golang/about/about.go 
b/traffic_ops/traffic_ops_golang/about/about.go
new file mode 100644
index 000000000..36703653d
--- /dev/null
+++ b/traffic_ops/traffic_ops_golang/about/about.go
@@ -0,0 +1,82 @@
+package about
+
+/*
+ * 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 (
+       "encoding/json"
+       "net/http"
+       "runtime"
+       "strings"
+)
+
+// 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"`
+}
+
+// About contains version info to be exposed by `api/.../about.json` endpoint
+var About about
+
+func splitRPMVersion(v string) (string, string, string, string) {
+
+       if v == "" {
+               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
+       s := strings.SplitN(v, "-", 3)
+       if len(s) >= 3 {
+               // 3rd field is commits.hash
+               t := strings.SplitN(s[2], ".", 2)
+               s = append(s[0:2], t...)
+       }
+       for cap(s) < 4 {
+               s = append(s, "")
+       }
+       return s[0], s[1], s[2], s[3]
+}
+
+func init() {
+       // name, version, commits, hash -- parts of rpm version string
+       n, v, c, h := splitRPMVersion(About.TrafficOpsRPMVersion)
+       About = about{
+               GoVersion:            runtime.Version(),
+               TrafficOpsRPMVersion: About.TrafficOpsRPMVersion,
+               TrafficOpsName:       n,
+               TrafficOpsVersion:    v,
+               Commits:              c,
+               CommitHash:           h,
+       }
+
+}
+
+// AboutHandler returns info about running Traffic Ops
+func AboutHandler() http.HandlerFunc {
+       return func(w http.ResponseWriter, r *http.Request) {
+               w.Header().Set("Content-Type", "application/json")
+
+               json.NewEncoder(w).Encode(About)
+       }
+}
diff --git a/traffic_ops/traffic_ops_golang/about/about_test.go 
b/traffic_ops/traffic_ops_golang/about/about_test.go
new file mode 100644
index 000000000..19f5a0c5c
--- /dev/null
+++ b/traffic_ops/traffic_ops_golang/about/about_test.go
@@ -0,0 +1,50 @@
+package about
+
+/*
+ * 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 (
+       "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", "", "", ""},
+}
+
+func TestSplitRPMVersion(t *testing.T) {
+       for s, e := range testSet {
+               t.Logf("Testing %s %v", s, e)
+               n, v, c, h := splitRPMVersion(s)
+
+               if n != e[0] {
+                       t.Errorf("expected name '%s', got '%s'", n, e[0])
+               }
+               if v != e[1] {
+                       t.Errorf("expected version '%s', got '%s'", v, e[1])
+               }
+               if c != e[2] {
+                       t.Errorf("expected commits '%s', got '%s'", c, e[2])
+               }
+               if h != e[3] {
+                       t.Errorf("expected commitHash '%s', got '%s'", h, e[3])
+               }
+       }
+}
diff --git a/traffic_ops/traffic_ops_golang/routes.go 
b/traffic_ops/traffic_ops_golang/routes.go
index 80e557f20..94cbf2317 100644
--- a/traffic_ops/traffic_ops_golang/routes.go
+++ b/traffic_ops/traffic_ops_golang/routes.go
@@ -29,6 +29,7 @@ import (
        "time"
 
        tclog "github.com/apache/incubator-trafficcontrol/lib/go-log"
+       
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/about"
        
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/api"
        
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/asn"
        
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/auth"
@@ -65,6 +66,9 @@ func Routes(d ServerData) ([]Route, http.Handler, error) {
        proxyHandler := rootHandler(d)
 
        routes := []Route{
+               //About
+               {1.3, http.MethodGet, `about/?(\.json)?$`, 
about.AboutHandler(), auth.PrivLevelReadOnly, Authenticated, nil},
+
                // Proxied routes
                //CDNs
                // explicitly passed to legacy system until fully implemented.  
Auth handled by legacy system.
diff --git a/traffic_ops/traffic_ops_golang/traffic_ops_golang.go 
b/traffic_ops/traffic_ops_golang/traffic_ops_golang.go
index af61ae8d6..dfd5f27bd 100644
--- a/traffic_ops/traffic_ops_golang/traffic_ops_golang.go
+++ b/traffic_ops/traffic_ops_golang/traffic_ops_golang.go
@@ -29,20 +29,30 @@ import (
        "time"
 
        "github.com/apache/incubator-trafficcontrol/lib/go-log"
+       
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/about"
 
        "github.com/jmoiron/sqlx"
        _ "github.com/lib/pq"
 )
 
-// Version ...
-const Version = "0.1"
+// set the version at build time: `go build -X "main.version=..."`
+var version = "development"
+
+func init() {
+       about.About.TrafficOpsRPMVersion = version
+}
 
 func main() {
+       showVersion := flag.Bool("version", false, "Show version and exit")
        configFileName := flag.String("cfg", "", "The config file path")
        dbConfigFileName := flag.String("dbcfg", "", "The db config file path")
        riakConfigFileName := flag.String("riakcfg", "", "The riak config file 
path")
        flag.Parse()
 
+       if *showVersion {
+               fmt.Println(about.About.TrafficOpsRPMVersion)
+               os.Exit(0)
+       }
        if len(os.Args) < 2 {
                flag.Usage()
                os.Exit(1)
diff --git a/traffic_ops/traffic_ops_golang/wrappers.go 
b/traffic_ops/traffic_ops_golang/wrappers.go
index 812936b2d..6f5cb044a 100644
--- a/traffic_ops/traffic_ops_golang/wrappers.go
+++ b/traffic_ops/traffic_ops_golang/wrappers.go
@@ -35,13 +35,14 @@ import (
 
        "github.com/apache/incubator-trafficcontrol/lib/go-log"
        tc "github.com/apache/incubator-trafficcontrol/lib/go-tc"
+       
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/about"
        
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/auth"
        
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/tocookie"
        "github.com/jmoiron/sqlx"
 )
 
 // ServerName - the server identifier
-const ServerName = "traffic_ops_golang" + "/" + Version
+var ServerName = "traffic_ops_golang" + "/" + about.About.TrafficOpsVersion
 
 // 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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to