Skip to content

Commit

Permalink
add system/info.json endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dangogh authored and dewrich committed Oct 6, 2017
1 parent d728b0a commit eab444b
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 0 deletions.
26 changes: 26 additions & 0 deletions traffic_ops/tostructs/system_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package tostructs

/*
* 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.
*/

type SystemInfoResponse struct {
Response struct {
Parameters map[string]string `json:"parameters"`
} `json:"response"`
}
2 changes: 2 additions & 0 deletions traffic_ops/traffic_ops_golang/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func Routes(d ServerData) ([]Route, http.Handler, error) {
{1.2, http.MethodGet, "hwinfo-wip.json$", wrapHeaders(wrapAuthWithData(hwInfoHandler(d.DB), d.Insecure, d.Secrets[0], rd.PrivLevelStmt, HWInfoPrivLevel))},
{1.2, http.MethodGet, "parameters-wip$", wrapHeaders(wrapAuthWithData(parametersHandler(d.DB), d.Insecure, d.Secrets[0], rd.PrivLevelStmt, ParametersPrivLevel))},
{1.2, http.MethodGet, "parameters-wip.json$", wrapHeaders(wrapAuthWithData(parametersHandler(d.DB), d.Insecure, d.Secrets[0], rd.PrivLevelStmt, ParametersPrivLevel))},
{1.2, http.MethodGet, "system/info-wip$", wrapHeaders(wrapAuthWithData(systemInfoHandler(d.DB), d.Insecure, d.Secrets[0], rd.PrivLevelStmt, SystemInfoPrivLevel))},
{1.2, http.MethodGet, "system/info-wip.json$", wrapHeaders(wrapAuthWithData(systemInfoHandler(d.DB), d.Insecure, d.Secrets[0], rd.PrivLevelStmt, SystemInfoPrivLevel))},
}, rootHandler(d), nil
}

Expand Down
88 changes: 88 additions & 0 deletions traffic_ops/traffic_ops_golang/system_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package main

/*
* 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"
"fmt"
"net/http"
"net/url"

log "github.com/apache/incubator-trafficcontrol/lib/go-log"
"github.com/apache/incubator-trafficcontrol/traffic_ops/tostructs"

"github.com/jmoiron/sqlx"
)

const SystemInfoPrivLevel = 10

func systemInfoHandler(db *sqlx.DB) AuthRegexHandlerFunc {
return func(w http.ResponseWriter, r *http.Request, p PathParams, username string, privLevel int) {
handleErr := func(err error, status int) {
log.Errorf("%v %v\n", r.RemoteAddr, err)
w.WriteHeader(status)
fmt.Fprintf(w, http.StatusText(status))
}

q := r.URL.Query()
resp, err := getSystemInfoResponse(q, db, privLevel)
if err != nil {
handleErr(err, http.StatusInternalServerError)
return
}

respBts, err := json.Marshal(resp)
if err != nil {
handleErr(err, http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "%s", respBts)
}
}
func getSystemInfoResponse(q url.Values, db *sqlx.DB, privLevel int) (*tostructs.SystemInfoResponse, error) {
info, err := getSystemInfo(q, db, privLevel)
if err != nil {
return nil, fmt.Errorf("getting SystemInfo: %v", err)
}

resp := tostructs.SystemInfoResponse{}
resp.Response.Parameters = info
return &resp, nil
}

func getSystemInfo(_ url.Values, db *sqlx.DB, privLevel int) (map[string]string, error) {
// system info returns all global parameters
// no parameters on the url, but use that mechanism to get the right params from the db

v := url.Values{}
v.Set("config_file", "global")
params, err := getParameters(v, db, SystemInfoPrivLevel)
if err != nil {
return nil, err
}

info := make(map[string]string, len(params))
for _, p := range params {
info[p.Name] = p.Value
}
return info, nil
}
91 changes: 91 additions & 0 deletions traffic_ops/traffic_ops_golang/system_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

/*
* 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 (
"fmt"
"net/url"
"testing"

sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"

"github.com/apache/incubator-trafficcontrol/traffic_ops/tostructs"
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/test"
"github.com/jmoiron/sqlx"
)

var sysInfoParameters = []tostructs.Parameter{
tostructs.Parameter{
ConfigFile: "global",
ID: 1,
LastUpdated: "lastUpdated",
Name: "paramname1",
Secure: false,
Value: "val1",
},

tostructs.Parameter{
ConfigFile: "global",
ID: 2,
LastUpdated: "lastUpdated",
Name: "paramname2",
Secure: false,
Value: "val2",
},
}

func TestGetSystemInfo(t *testing.T) {
mockDB, mock, err := sqlmock.New()
defer mockDB.Close()
db := sqlx.NewDb(mockDB, "sqlmock")
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()

cols := test.ColsFromStructByTag("db", tostructs.Parameter{})
rows := sqlmock.NewRows(cols)

//TODO: drichardson - build helper to add these Rows from the struct values
// or by CSV if types get in the way
for _, ts := range sysInfoParameters {
rows = rows.AddRow(
ts.ConfigFile,
ts.ID,
ts.LastUpdated,
ts.Name,
ts.Secure,
ts.Value,
)
}

mock.ExpectQuery("SELECT.*WHERE p.config_file='?").WillReturnRows(rows)
v := url.Values{}

sysinfo, err := getSystemInfo(v, db, PrivLevelReadOnly)
if err != nil {
t.Errorf("getSystemInfo expected: nil error, actual: %v", err)
}

if len(sysinfo) != 2 {
t.Errorf("getSystemInfo expected: len(sysinfo) == 2, actual: %v", len(sysinfo))
fmt.Printf("Got %+v\n", sysinfo)
}
}

0 comments on commit eab444b

Please sign in to comment.