forked from Telmate/proxmox-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_sdn_vnet.go
100 lines (88 loc) · 2.47 KB
/
config_sdn_vnet.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
package proxmox
import (
"encoding/json"
"fmt"
"regexp"
)
type ConfigSDNVNet struct {
VNet string `json:"vnet"`
Zone string `json:"zone"`
Alias string `json:"alias,omitempty"`
Delete string `json:"delete,omitempty"`
Tag int `json:"tag,omitempty"`
VLANAware bool `json:"vlanaware,omitempty"`
// Digest allows for a form of optimistic locking
Digest string `json:"digest,omitempty"`
}
func NewConfigSDNVNetFromJson(input []byte) (config *ConfigSDNVNet, err error) {
config = &ConfigSDNVNet{}
err = json.Unmarshal([]byte(input), config)
return
}
func (config *ConfigSDNVNet) CreateWithValidate(id string, client *Client) (err error) {
err = config.Validate(id, true, client)
if err != nil {
return
}
return config.Create(id, client)
}
func (config *ConfigSDNVNet) Create(id string, client *Client) (err error) {
config.VNet = id
params := config.mapToApiValues()
return client.CreateSDNVNet(params)
}
func (config *ConfigSDNVNet) UpdateWithValidate(id string, client *Client) (err error) {
err = config.Validate(id, false, client)
if err != nil {
return
}
return config.Update(id, client)
}
func (config *ConfigSDNVNet) Update(id string, client *Client) (err error) {
config.VNet = id
params := config.mapToApiValues()
err = client.UpdateSDNVNet(id, params)
if err != nil {
params, _ := json.Marshal(¶ms)
return fmt.Errorf("error updating SDN VNet: %v, (params: %v)", err, string(params))
}
return
}
func (c *ConfigSDNVNet) Validate(id string, create bool, client *Client) (err error) {
exists, err := client.CheckSDNVNetExistance(id)
if err != nil {
return
}
if exists && create {
return ErrorItemExists(id, "vnet")
}
if !exists && !create {
return ErrorItemNotExists(id, "vnet")
}
zoneExists, err := client.CheckSDNZoneExistance(c.Zone)
if err != nil {
return
}
if !zoneExists {
return fmt.Errorf("vnet must be associated to an existing zone. zone %s could not be found", c.Zone)
}
if c.Alias != "" {
regex, _ := regexp.Compile(`^(?i:[\(\)-_.\w\d\s]{0,256})$`)
if !regex.Match([]byte(c.Alias)) {
return fmt.Errorf(`alias must match the validation regular expression: ^(?i:[\(\)-_.\w\d\s]{0,256})$`)
}
}
err = ValidateIntGreater(0, c.Tag, "tag")
if err != nil {
return
}
return
}
func (config *ConfigSDNVNet) mapToApiValues() (params map[string]interface{}) {
d, _ := json.Marshal(config)
json.Unmarshal(d, ¶ms)
if v, has := params["vlanaware"]; has {
params["vlanaware"] = Btoi(v.(bool))
}
return
}