forked from apache/trafficcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |