forked from giantswarm/kemp-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentrules.go
117 lines (92 loc) · 3.71 KB
/
contentrules.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
package kempclient
import (
"encoding/xml"
"fmt"
"github.com/juju/errgo"
)
// Information about the content rules can be found in https://support.kemptechnologies.com/hc/en-us/articles/203863435-RESTful-API
const (
ContentRuleAddHeader = "1"
ContentRuleUpdateHeader = "3"
ContentRuleDeleteHeader = "2"
DeleteHeaderProtoName = "DeleteHeaderProto"
DeleteHeaderPortName = "DeleteHeaderPort"
DeleteHeaderPortValue = "X-Forwarded-Port"
DeleteHeaderProtoValue = "X-Forwarded-Proto"
)
type ContentRuleResponse struct {
Debug string `xml:",innerxml"`
XMLName xml.Name `xml:"Response"`
CR ContentRule `xml:"Success>Data"`
}
type ContentRule struct {
Name string `xml:"Name"`
Header string `xml:"Header"`
HeaderValue string `xml:"HeaderValue"`
}
func (c *Client) AddHeaderContentRule(name, headerKey, headerValue string) error {
data := ContentRuleResponse{}
ruleParameters := make(map[string]string)
// Only accepts alphanumeric names
ruleParameters["name"] = name
// Add Header to HTTP Request
ruleParameters["type"] = ContentRuleAddHeader
ruleParameters["header"] = headerKey
ruleParameters["replacement"] = headerValue
err := c.Request("addrule", ruleParameters, &data)
if err != nil {
return errgo.NoteMask(err, fmt.Sprintf("kemp unable to add content rule %s with header %s and value %s for virtual service %s '%#v'", name, headerKey, headerValue, ruleParameters), errgo.Any)
}
return nil
}
func (c *Client) AddProtoPortHeaderRequestRules() error {
data := ContentRuleResponse{}
ruleParameters := make(map[string]string)
ruleParameters["name"] = DeleteHeaderProtoName
ruleParameters["type"] = ContentRuleDeleteHeader
if err := c.Request("showrule", ruleParameters, &data); err != nil {
// The Rule doesn't exists
ruleParameters["pattern"] = DeleteHeaderProtoValue
err := c.Request("addrule", ruleParameters, &data)
if err != nil {
return errgo.NoteMask(err, fmt.Sprintf("kemp unable to add content rule %s with value %s for virtual service %s '%#v'", DeleteHeaderProtoName, DeleteHeaderProtoValue, ruleParameters), errgo.Any)
}
}
ruleParameters["name"] = DeleteHeaderPortName
if err := c.Request("showrule", ruleParameters, &data); err != nil {
ruleParameters["pattern"] = DeleteHeaderPortValue
err = c.Request("addrule", ruleParameters, &data)
if err != nil {
return errgo.NoteMask(err, fmt.Sprintf("kemp unable to add content rule %s with value %s for virtual service %s '%#v'", DeleteHeaderPortName, DeleteHeaderPortValue, ruleParameters), errgo.Any)
}
}
return nil
}
func (c *Client) UpdateHeaderContentRule(name, headerKey, headerValue string) error {
data := ContentRuleResponse{}
ruleParameters := make(map[string]string)
// Only accepts alphanumeric names
ruleParameters["name"] = name
// Update Header to HTTP Request
ruleParameters["type"] = ContentRuleUpdateHeader
ruleParameters["header"] = headerKey
ruleParameters["replacement"] = headerValue
err := c.Request("modrule", ruleParameters, &data)
if err != nil {
return errgo.NoteMask(err, fmt.Sprintf("kemp unable to update content rule %s with header %s and value %s for virtual service %s '%#v'", name, headerKey, headerValue, ruleParameters), errgo.Any)
}
return nil
}
func (c *Client) DeleteHeaderContentRule(name string) error {
data := ContentRuleResponse{}
ruleParameters := make(map[string]string)
// Only accepts alphanumeric names
ruleParameters["name"] = name
// Delete Header to HTTP Request
ruleParameters["type"] = ContentRuleDeleteHeader
err := c.Request("delrule", ruleParameters, &data)
if err != nil {
return errgo.NoteMask(err, fmt.Sprintf("kemp unable to delete content rule %s header for virtual service '%#v'", name, ruleParameters), errgo.Any)
}
return nil
}