/
client.go
141 lines (124 loc) · 2.74 KB
/
client.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"errors"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
var (
noUnix = errors.New(`Unix connections not currently supported.`)
noTarget = errors.New(`Unspecified client target. Please set the host parameter.`)
)
func ClientCreatePolicy(c *cli.Context) error {
unix, target, err := processClientTarget(c)
if err != nil {
return err
}
if unix {
return noUnix
} else {
name := c.String(`name`)
image := c.String(`image`)
criteria := c.String(`criteria`)
frequency := c.String(`frequency`)
probability := c.String(`probability`)
failure := c.String(`failure`)
resp, err := http.PostForm(fmt.Sprintf("http://%s/policy/", target), url.Values{
`name`: {name},
`image`: {image},
`criteria`: {criteria},
`frequency`: {frequency},
`probability`: {probability},
`failures`: {failure},
})
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Printf("%s", body)
}
return nil
}
func RequestPolicyList(c *cli.Context) error {
unix, target, err := processClientTarget(c)
if err != nil {
return err
}
if unix {
return noUnix
} else {
resp, err := http.Get(fmt.Sprintf("http://%s/policy/", target))
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Printf("%s", body)
}
return nil
}
func RequestInjectorList(c *cli.Context) error {
unix, target, err := processClientTarget(c)
if err != nil {
return err
}
if unix {
return noUnix
} else {
resp, err := http.Get(fmt.Sprintf("http://%s/injector/", target))
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Printf("%s", body)
}
return nil
}
func DeletePolicy(c *cli.Context) error {
unix, target, err := processClientTarget(c)
if err != nil {
return err
}
if unix {
return noUnix
} else {
var name string
if c.NArg() > 0 {
name = c.Args()[0]
}
client := &http.Client{}
req, err := http.NewRequest("DELETE", fmt.Sprintf("http://%s/policy/%s", target, name), nil)
if err != nil {
log.Fatal(err)
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Printf("%s", body)
}
return nil
}
func processClientTarget(c *cli.Context) (isUnix bool, target string, err error) {
for _, v := range c.GlobalStringSlice(`host`) {
if strings.HasPrefix(v, "tcp://") {
isUnix = false
target = strings.TrimPrefix(v, "tcp://")
err = nil
}
if strings.HasPrefix(v, "unix://") {
isUnix = true
target = strings.TrimPrefix(v, "unix://")
err = nil
}
}
return
}