forked from floshodan/hrobot-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vswitch.go
194 lines (159 loc) · 4.48 KB
/
vswitch.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
package hrobot
import (
"context"
"fmt"
"log"
"net/url"
"reflect"
"github.com/Mats4k/hrobot-go/hrobot/schema"
"github.com/google/go-querystring/query"
)
type VSwitch struct {
ID int
Name string
Vlan int
Cancelled bool
}
type VSwitchSingle struct {
ID int
Name string
Vlan int
Cancelled bool
Server []interface{}
Subnet []interface{}
CloudNetwork []interface{}
}
type VSwitchServer struct {
ServerIP string
ServerIpv6Net string
ServerNumber int
Status string
}
type VSwitchClient struct {
client *Client
}
func (c *VSwitchClient) GetVSwitchList(ctx context.Context) ([]*VSwitch, *Response, error) {
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/vswitch/"), nil)
if err != nil {
return nil, nil, err
}
var body schema.VSwitchList
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
vswitchlist := make([]*VSwitch, 0, len(body))
for i := range body {
vswitch := VswitchFromSchema(body[i])
vswitchlist = append(vswitchlist, vswitch)
}
return vswitchlist, resp, nil
}
func (c *VSwitchClient) AddVSwitch(ctx context.Context, opt *AddvSwitchOps) (*VSwitchSingle, *Response, error) {
params, _ := query.Values(opt)
req, err := c.client.NewRequest(ctx, "POST", "/vswitch", params)
if err != nil {
return nil, nil, err
}
var body schema.VSwitchSingle
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
return VSwitchSingleFromSchema(body), resp, nil
}
func (c *VSwitchClient) GetVSwitchById(ctx context.Context, id string) (*VSwitchSingle, *Response, error) {
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/vswitch/%s", id), nil)
if err != nil {
return nil, nil, err
}
var body schema.VSwitchSingle
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
return VSwitchSingleFromSchema(body), resp, nil
}
func (c *VSwitchClient) UpdateVSwitchById(ctx context.Context, id string, opt *AddvSwitchOps) (*VSwitchSingle, *Response, error) {
params, _ := query.Values(opt)
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("/vswitch/%s", id), params)
if err != nil {
return nil, nil, err
}
var body schema.VSwitchSingle
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
return VSwitchSingleFromSchema(body), resp, nil
}
// cancellation_date format yyyy-MM-dd
func (c *VSwitchClient) CancelVSwitch(ctx context.Context, id string, cancellation_date string) (*Response, error) {
params := url.Values{}
params.Add("cancellation_date", cancellation_date)
fmt.Println(params.Encode())
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/vswitch/%s", id), params)
if err != nil {
return nil, err
}
var body schema.VSwitchSingle
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, err
}
return resp, nil
}
// adds server to vswitch, server can be server number or ip
func (c *VSwitchClient) AddToServer(ctx context.Context, id string, server interface{}) (*Response, error) {
params := stringToURLValue(server, "server[]")
//params.Add("cancellation_date", server[0])
fmt.Println(params.Encode())
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("/vswitch/%s/server", id), params)
if err != nil {
return nil, err
}
resp, err := c.client.Do(req, nil)
if err != nil {
return nil, err
}
return resp, nil
}
// removes server by servernumber or ip from vswitch
func (c *VSwitchClient) RemoveServer(ctx context.Context, id string, server interface{}) (*Response, error) {
params := stringToURLValue(server, "server[]")
//params.Add("cancellation_date", server[0])
fmt.Println(params.Encode())
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/vswitch/%s/server", id), params)
if err != nil {
return nil, err
}
resp, err := c.client.Do(req, nil)
if err != nil {
return nil, err
}
return resp, nil
}
// converts a string or slice of strings to url value
func stringToURLValue(value interface{}, key string) url.Values {
params := url.Values{}
switch value.(type) {
case string:
s := reflect.ValueOf(value)
params.Add(key, s.String())
return params
case []string:
s := reflect.ValueOf(value)
for i := 0; i < s.Len(); i++ {
params.Add(key, s.Index(i).String())
}
return params
default:
log.Fatalf("%v is not a string or slice of strings \n ", value)
return nil
}
}
// Vlan_ID has to be in range: 4000 to 4091
type AddvSwitchOps struct {
Name string `url:"name"`
Vlan_ID int `url:"vlan"`
}