-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_system.go
78 lines (63 loc) · 2.07 KB
/
client_system.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package containerstation
import (
"context"
"fmt"
"net/http"
)
//SystemInformation returns information on the system that is running container station.
func (c Client) SystemInformation(ctx context.Context) (*SystemInformation, error) {
const apiEndpoint = `/containerstation/api/v1/system`
req, err := http.NewRequest(http.MethodGet, *c.baseURL+apiEndpoint, nil)
i, err := c.boilerplateHTTP(ctx, req, err, sysInformationType, nil)
si, _ := i.(*SystemInformation)
return si, err
}
//ResourceUsage returns resource usage information on the system that is running container station.
func (c Client) ResourceUsage(ctx context.Context) (*ResourceUsage, error) {
const apiEndpoint = `/containerstation/api/v1/system/resource`
req, err := http.NewRequest(http.MethodGet, *c.baseURL+apiEndpoint, nil)
i, err := c.boilerplateHTTP(ctx, req, err, resUsageType, nil)
ru, _ := i.(*ResourceUsage)
return ru, err
}
type protocol int
const (
//TCP enum to be used with client.NetworkPort
TCP protocol = iota
//UDP enum to be used with client.NetworkPort
UDP
)
func (p protocol) String() string {
switch p {
case TCP:
return "tcp"
case UDP:
return "udp"
default:
return fmt.Sprintf("<unknown protocol %d>", p)
}
}
func (p protocol) isValid() bool {
switch p {
case TCP, UDP:
return true
default:
return false
}
}
//NetworkPort reports whether or not a given protocol and port are being used.
//Protocol must be one of TCP or UDP constants defined in this package.
//If an invalid protocol or port is set an error is returned.
func (c Client) NetworkPort(ctx context.Context, proto protocol, port int) (bool, error) {
const apiEndpoint = `/containerstation/api/v1/system/port/%s/%d`
if port < 1 || !proto.isValid() {
return false, fmt.Errorf("containerstation: Bad port or protocol in call to NetworkPort")
}
req, err := http.NewRequest(http.MethodGet, *c.baseURL+fmt.Sprintf(apiEndpoint, proto, port), nil)
i, err := c.boilerplateHTTP(ctx, req, err, npUsedType, nil)
var toReturn bool
if np, ok := i.(*networkPortUsed); ok {
toReturn = np.Used
}
return toReturn, err
}