This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
forked from AviatrixSystems/terraform-provider-aviatrix
/
controller.go
241 lines (216 loc) · 7.59 KB
/
controller.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package goaviatrix
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/url"
)
// Controller Http Access enabled get result struct
type ControllerHttpAccessResp struct {
Return bool `json:"return"`
Result string `json:"results"`
Reason string `json:"reason"`
}
type GetSecurityGroupManagementResp struct {
Return bool `json:"return"`
Results SecurityGroupInfo `json:"results"`
Reason string `json:"reason"`
}
type SecurityGroupInfo struct {
State string `json:"state"`
AccountName string `json:"account_name"`
Response string `json:"response"`
}
func (c *Client) EnableHttpAccess() error {
url := "?CID=%s&action=config_http_access&operation=enable"
path := c.baseURL + fmt.Sprintf(url, c.CID)
resp, err := c.Get(path, nil)
if err != nil {
return err
}
var data APIResp
if err = json.NewDecoder(resp.Body).Decode(&data); err != nil {
return err
}
if !data.Return {
log.Printf("[ERROR] Error invoking controller %s", data.Reason)
return errors.New(data.Reason)
}
return nil
}
func (c *Client) DisableHttpAccess() error {
url := "?CID=%s&action=config_http_access&operation=disable"
path := c.baseURL + fmt.Sprintf(url, c.CID)
resp, err := c.Get(path, nil)
if err != nil {
return err
}
var data APIResp
if err = json.NewDecoder(resp.Body).Decode(&data); err != nil {
return err
}
if !data.Return {
log.Printf("[ERROR] Error invoking controller %s", data.Reason)
return errors.New(data.Reason)
}
return nil
}
func (c *Client) GetHttpAccessEnabled() (string, error) {
url := "?CID=%s&action=config_http_access&operation=get"
path := c.baseURL + fmt.Sprintf(url, c.CID)
resp, err := c.Get(path, nil)
if err != nil {
return "", err
}
var data ControllerHttpAccessResp
if err = json.NewDecoder(resp.Body).Decode(&data); err != nil {
return "", err
}
if !data.Return {
log.Printf("[ERROR] Error invoking controller %s", data.Reason)
return "", errors.New(data.Reason)
}
result := data.Result
return result, nil
}
func (c *Client) EnableExceptionRule() error {
Url, err := url.Parse(c.baseURL)
if err != nil {
return errors.New("url Parsing failed for enable_fqdn_exception_rule " + err.Error())
}
enableFqdnExceptionRule := url.Values{}
enableFqdnExceptionRule.Add("CID", c.CID)
enableFqdnExceptionRule.Add("action", "enable_fqdn_exception_rule")
Url.RawQuery = enableFqdnExceptionRule.Encode()
resp, err := c.Get(Url.String(), nil)
if err != nil {
return errors.New("HTTP Get enable_fqdn_exception_rule failed: " + err.Error())
}
var data APIResp
if err = json.NewDecoder(resp.Body).Decode(&data); err != nil {
return errors.New("Json Decode detach_fqdn_filter_tag_from_gw failed: " + err.Error())
}
if !data.Return {
return errors.New("Rest API detach_fqdn_filter_tag_from_gw Get failed: " + data.Reason)
}
return nil
}
func (c *Client) DisableExceptionRule() error {
Url, err := url.Parse(c.baseURL)
if err != nil {
return errors.New("url Parsing failed for disable_fqdn_exception_rule " + err.Error())
}
disableFqdnExceptionRule := url.Values{}
disableFqdnExceptionRule.Add("CID", c.CID)
disableFqdnExceptionRule.Add("action", "disable_fqdn_exception_rule")
Url.RawQuery = disableFqdnExceptionRule.Encode()
resp, err := c.Get(Url.String(), nil)
if err != nil {
return errors.New("HTTP Get disable_fqdn_exception_rule failed: " + err.Error())
}
var data APIResp
if err = json.NewDecoder(resp.Body).Decode(&data); err != nil {
return errors.New("Json Decode disable_fqdn_exception_rule failed: " + err.Error())
}
if !data.Return {
return errors.New("Rest API disable_fqdn_exception_rule Get failed: " + data.Reason)
}
return nil
}
func (c *Client) GetExceptionRuleStatus() (bool, error) {
Url, err := url.Parse(c.baseURL)
if err != nil {
return false, errors.New("url Parsing failed for get_fqdn_exception_rule_status " + err.Error())
}
getFqdnExceptionRuleStatus := url.Values{}
getFqdnExceptionRuleStatus.Add("CID", c.CID)
getFqdnExceptionRuleStatus.Add("action", "get_fqdn_exception_rule_status")
Url.RawQuery = getFqdnExceptionRuleStatus.Encode()
resp, err := c.Get(Url.String(), nil)
if err != nil {
return false, errors.New("HTTP Get get_fqdn_exception_rule_status failed: " + err.Error())
}
data := GetFqdnExceptionRuleResp{
Return: false,
Results: "",
Reason: "",
}
if err = json.NewDecoder(resp.Body).Decode(&data); err != nil {
return false, errors.New("Json Decode get_fqdn_exception_rule_status failed: " + err.Error())
}
if !data.Return {
return false, errors.New("Rest API get_fqdn_exception_rule_status Get failed: " + data.Reason)
}
if data.Results == "disabled" {
return false, nil
}
return true, nil
}
func (c *Client) EnableSecurityGroupManagement(account string) error {
Url, err := url.Parse(c.baseURL)
if err != nil {
return errors.New("url Parsing failed for enable_controller_security_group_management " + err.Error())
}
enableSecurityGroupManagement := url.Values{}
enableSecurityGroupManagement.Add("CID", c.CID)
enableSecurityGroupManagement.Add("action", "enable_controller_security_group_management")
enableSecurityGroupManagement.Add("access_account_name", account)
Url.RawQuery = enableSecurityGroupManagement.Encode()
resp, err := c.Get(Url.String(), nil)
if err != nil {
return errors.New("HTTP Get enable_controller_security_group_management failed: " + err.Error())
}
var data APIResp
if err = json.NewDecoder(resp.Body).Decode(&data); err != nil {
return errors.New("Json Decode enable_controller_security_group_management failed: " + err.Error())
}
if !data.Return {
return errors.New("Rest API enable_controller_security_group_management Get failed: " + data.Reason)
}
return nil
}
func (c *Client) DisableSecurityGroupManagement() error {
Url, err := url.Parse(c.baseURL)
if err != nil {
return errors.New("url Parsing failed for disable_controller_security_group_management " + err.Error())
}
disableSecurityGroupManagement := url.Values{}
disableSecurityGroupManagement.Add("CID", c.CID)
disableSecurityGroupManagement.Add("action", "disable_controller_security_group_management")
Url.RawQuery = disableSecurityGroupManagement.Encode()
resp, err := c.Get(Url.String(), nil)
if err != nil {
return errors.New("HTTP Get disable_controller_security_group_management failed: " + err.Error())
}
var data APIResp
if err = json.NewDecoder(resp.Body).Decode(&data); err != nil {
return errors.New("Json Decode disable_controller_security_group_management failed: " + err.Error())
}
if !data.Return {
return errors.New("Rest API disable_controller_security_group_management Get failed: " + data.Reason)
}
return nil
}
func (c *Client) GetSecurityGroupManagementStatus() (*SecurityGroupInfo, error) {
Url, err := url.Parse(c.baseURL)
if err != nil {
return nil, errors.New("url Parsing failed for get_controller_security_group_management_status " + err.Error())
}
getSecurityGroupManagementStatus := url.Values{}
getSecurityGroupManagementStatus.Add("CID", c.CID)
getSecurityGroupManagementStatus.Add("action", "get_controller_security_group_management_status")
Url.RawQuery = getSecurityGroupManagementStatus.Encode()
resp, err := c.Get(Url.String(), nil)
if err != nil {
return nil, errors.New("HTTP Get get_controller_security_group_management_status failed: " + err.Error())
}
var data GetSecurityGroupManagementResp
if err = json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, errors.New("Json Decode get_controller_security_group_management_status failed: " + err.Error())
}
if !data.Return {
return nil, errors.New("Rest API get_controller_security_group_management_status Get failed: " + data.Reason)
}
return &data.Results, nil
}