forked from Telmate/proxmox-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_sdn_subnet.go
123 lines (107 loc) · 3.42 KB
/
config_sdn_subnet.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
package proxmox
import (
"encoding/json"
"fmt"
"net"
)
type ConfigSDNSubnet struct {
// For creation purposes - Subnet is a CIDR
// Once a subnet has been created, the Subnet is an identifier with the format
// "<zone>-<ip>-<mask>"
Subnet string `json:"subnet"`
DNSZonePrefix string `json:"dnszoneprefix,omitempty"`
Gateway string `json:"gateway,omitempty"`
SNAT bool `json:"snat,omitempty"`
// Delete is a string of attributes to be deleted from the object
Delete string `json:"delete,omitempty"`
// Type must always hold the string "subnet"
Type string `json:"type"`
// Digest allows for a form of optimistic locking
Digest string `json:"digest,omitempty"`
}
// NewConfigSDNSubnetFromJSON takes in a byte array from a json encoded SDN Subnet
// configuration and stores it in config.
// It returns the newly created config with the passed in configuration stored
// and an error if one occurs unmarshalling the input data.
func NewConfigSDNSubnetFromJson(input []byte) (config *ConfigSDNSubnet, err error) {
config = &ConfigSDNSubnet{}
err = json.Unmarshal([]byte(input), config)
return
}
func (config *ConfigSDNSubnet) CreateWithValidate(vnet, id string, client *Client) (err error) {
err = config.Validate(vnet, id, true, client)
if err != nil {
return
}
return config.Create(vnet, id, client)
}
func (config *ConfigSDNSubnet) Create(vnet, id string, client *Client) (err error) {
config.Subnet = id
config.Type = "subnet"
params := config.mapToApiValues()
return client.CreateSDNSubnet(vnet, params)
}
func (config *ConfigSDNSubnet) UpdateWithValidate(vnet, id string, client *Client) (err error) {
err = config.Validate(vnet, id, false, client)
if err != nil {
return
}
return config.Update(vnet, id, client)
}
func (config *ConfigSDNSubnet) Update(vnet, id string, client *Client) (err error) {
config.Subnet = id
config.Type = "" // For some reason, this shouldn't be sent on update. Only on create.
params := config.mapToApiValues()
err = client.UpdateSDNSubnet(vnet, id, params)
if err != nil {
params, _ := json.Marshal(¶ms)
return fmt.Errorf("error updating SDN Subnet: %v, (params: %v)", err, string(params))
}
return
}
func (c *ConfigSDNSubnet) Validate(vnet, id string, create bool, client *Client) (err error) {
vnetExists, err := client.CheckSDNVNetExistance(vnet)
if err != nil {
return
}
if !vnetExists {
return fmt.Errorf("subnet must be created in an existing vnet. vnet (%s) wasn't found", vnet)
}
exists, err := client.CheckSDNSubnetExistance(vnet, id)
if err != nil {
return
}
if exists && create {
return ErrorItemExists(id, "subnet")
}
if !exists && !create {
return ErrorItemNotExists(id, "subnet")
}
// if this is an update, the Subnet is an identifier of the form <zone>-<ip>-<mask>
// and therefore shouldn't be validated or changed
if create {
// Make sure that the CIDR is actually a valid CIDR
_, _, err = net.ParseCIDR(c.Subnet)
if err != nil {
return
}
}
if c.Gateway != "" {
ip := net.ParseIP(c.Gateway)
if ip == nil {
return fmt.Errorf("error gateway (%s) is not a valid IP", c.Gateway)
}
}
return
}
func (config *ConfigSDNSubnet) mapToApiValues() (params map[string]interface{}) {
d, _ := json.Marshal(config)
json.Unmarshal(d, ¶ms)
if v, has := params["snat"]; has {
params["snat"] = Btoi(v.(bool))
}
// Remove the subnet and vnet (path parameters) from the map
delete(params, "subnet")
delete(params, "vnet")
return
}