Skip to content

Commit 41fd3d1

Browse files
guzzijasonjhg03a
authored andcommitted
Add support for servercheck updates to go client (#3470)
* Add support for servercheck updates to go client * minor formatting * remove cruft * go fmt * satisfy ReqInf response requirement * add servercheck test * Add GetCheckData() Supports GET of servercheck data
1 parent 4a6f1e6 commit 41fd3d1

File tree

3 files changed

+243
-0
lines changed

3 files changed

+243
-0
lines changed

lib/go-tc/serverchecks.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package tc
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
// Health monitor checks for servers
23+
// A Single Servercheck Response for Update and Create to depict what changed
24+
// swagger:response ServercheckResponse
25+
// in: body
26+
type ServercheckResponse struct {
27+
// in: body
28+
Response Servercheck `json:"response"`
29+
}
30+
31+
// A list of Servercheck Responses
32+
// swagger:response ServerchecksResponse
33+
// in: body
34+
type ServerchecksResponse struct {
35+
// in: body
36+
Response []Servercheck `json:"response"`
37+
}
38+
39+
// A Single Servercheck struct for GET response
40+
// swagger:model Servercheck
41+
type Servercheck struct {
42+
43+
// The Servercheck response data
44+
//
45+
// Admin state of the checked server
46+
AdminState string `json:"adminState"`
47+
48+
// Cache group the checked server belongs to
49+
CacheGroup string `json:"cacheGroup"`
50+
51+
// ID number of the checked server
52+
ID int `json:"id"`
53+
54+
// Hostname of the checked server
55+
HostName string `json:"hostName"`
56+
57+
// Reval pending flag for checked server
58+
RevalPending bool `json:"revalPending"`
59+
60+
// Profile name of checked server
61+
Profile string `json:"profile"`
62+
63+
// Traffic Control type of the checked server
64+
Type string `json:"type"`
65+
66+
// Update pending flag for the checked server
67+
UpdPending bool `json:"updPending"`
68+
69+
// Various check types
70+
Checks struct {
71+
72+
// IPv4 production interface (legacy name)
73+
Iface10G int `json:"10G"`
74+
75+
// IPv6 production interface (legacy name)
76+
Iface10G6 int `json:"10G6"`
77+
78+
// Cache Disk Usage
79+
CDU int `json:"CDU"`
80+
81+
// Cache Hit Ratio
82+
CHR int `json:"CHR"`
83+
84+
// DSCP check
85+
DSCP int `json:"DSCP"`
86+
87+
// DNS check
88+
FQDN int `json:"FQDN"`
89+
90+
// Out-of-band (BMC) interface check
91+
ILO int `json:"ILO"`
92+
93+
// IPv4 production interface (new name)
94+
IPv4 int `json:"IPv4"`
95+
96+
// IPv6 production interface (new name)
97+
IPv6 int `json:"IPv6"`
98+
99+
// MTU check
100+
MTU int `json:"MTU"`
101+
102+
// ORT check
103+
ORT int `json:"ORT"`
104+
105+
// Traffic Router status for checked server
106+
RTR int `json:"RTR"`
107+
} `json:"checks"`
108+
}
109+
110+
// A Single Servercheck struct for Update and Create to depict what changed
111+
// swagger:model ServercheckPost
112+
type ServercheckPost struct {
113+
114+
// The Servercheck data to submit
115+
//
116+
// Name of the server check type
117+
//
118+
// required: true
119+
Name string `json:"servercheck_short_name" db:"servercheck_short_name"`
120+
121+
// ID of the server
122+
//
123+
// required: true
124+
ID int `json:"id" db:"id"`
125+
126+
// Name of the server
127+
HostName string `json:"name" db:"name"`
128+
129+
// Value of the check result
130+
//
131+
// required: true
132+
Value int `json:"value" db:"value"`
133+
}
134+
135+
type ServercheckPostNullable struct {
136+
Name string `json:"servercheck_short_name" db:"servercheck_short_name"`
137+
ID int `json:"id" db:"id"`
138+
Value int `json:"value" db:"value"`
139+
}
140+
141+
type ServercheckPostResponse struct {
142+
Alerts []Alert `json:"alerts"`
143+
Response DeliveryServiceUserPost `json:"response"`
144+
}

traffic_ops/client/servercheck.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Licensed under the Apache License, Version 2.0 (the "License");
3+
you may not use this file except in compliance with the License.
4+
You may obtain a copy of the License at
5+
http://www.apache.org/licenses/LICENSE-2.0
6+
Unless required by applicable law or agreed to in writing, software
7+
distributed under the License is distributed on an "AS IS" BASIS,
8+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
See the License for the specific language governing permissions and
10+
limitations under the License.
11+
*/
12+
13+
package client
14+
15+
import (
16+
"encoding/json"
17+
"net"
18+
19+
"github.com/apache/trafficcontrol/lib/go-tc"
20+
)
21+
22+
const API_V13_SERVERCHECK = "/api/1.3/servercheck"
23+
const API_V13_SERVERCHECK_GET = "/api/1.3/servers/checks"
24+
25+
// Update a Server Check Status
26+
func (to *Session) UpdateCheckStatus(status tc.ServercheckPostNullable) (*tc.ServercheckPostResponse, ReqInf, error) {
27+
uri := API_V13_SERVERCHECK
28+
var remoteAddr net.Addr
29+
reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
30+
jsonReq, err := json.Marshal(status)
31+
if err != nil {
32+
return nil, reqInf, err
33+
}
34+
resp := tc.ServercheckPostResponse{}
35+
reqInf, err = post(to, uri, jsonReq, &resp)
36+
if err != nil {
37+
return nil, reqInf, err
38+
}
39+
return &resp, reqInf, nil
40+
}
41+
42+
// Get Server Check Data
43+
func (to *Session) GetCheckData() (*tc.ServerchecksResponse, ReqInf, error) {
44+
uri := API_V13_SERVERCHECK_GET
45+
var remoteAddr net.Addr
46+
reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
47+
resp := tc.ServerchecksResponse{}
48+
reqInf, err := get(to, uri, &resp)
49+
if err != nil {
50+
return nil, reqInf, err
51+
}
52+
return &resp, reqInf, nil
53+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package v14
2+
3+
/*
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import (
18+
"testing"
19+
20+
"github.com/apache/trafficcontrol/lib/go-log"
21+
"github.com/apache/trafficcontrol/lib/go-tc"
22+
toclient "github.com/apache/trafficcontrol/traffic_ops/client"
23+
)
24+
25+
func TestServerCheck(t *testing.T) {
26+
WithObjs(t, []TCObj{Servers}, func() {
27+
UpdateServerCheck(t)
28+
})
29+
}
30+
31+
const SessionUserName = "extension" // TODO make dynamic?
32+
33+
func UpdateServerCheck(t *testing.T) {
34+
var statusData tc.ServercheckNullable
35+
for _, server := range testData.Servers {
36+
37+
statusData.ID = server.ID
38+
statusData.Name = "DSCP"
39+
statusData.Value = 1
40+
resp, _, err := TOSession.UpdateCheckStatus(statusData)
41+
if err != nil {
42+
t.Errorf("could not set servercheck status user: %v\n", err)
43+
}
44+
log.Debugln("Response: ", resp.Alerts)
45+
}
46+
}

0 commit comments

Comments
 (0)