-
Notifications
You must be signed in to change notification settings - Fork 227
/
config_sdn_dns.go
91 lines (81 loc) · 2.33 KB
/
config_sdn_dns.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
package proxmox
import (
"encoding/json"
"fmt"
)
// ConfigSDNDNS describes the SDN DNS configurable element
type ConfigSDNDNS struct {
DNS string `json:"dns"`
Key string `json:"key"`
Type string `json:"type"`
URL string `json:"url"`
TTL int `json:"ttl,omitempty"`
// The SDN Plugin schema contains ReverseV6Mask attribute while the
// PowerDNS plugin schema contains the ReverseMaskV6 attribute
// This is probably a bug that crept into the Proxmox implementation.a
// Checked in libpve-network-perl=0.7.3
ReverseMaskV6 int `json:"reversemaskv6,omitempty"`
ReverseV6Mask int `json:"reversev6mask,omitempty"`
// Digest allows for a form of optimistic locking
Digest string `json:"digest,omitempty"`
}
func NewConfigSDNDNSFromJson(input []byte) (config *ConfigSDNDNS, err error) {
config = &ConfigSDNDNS{}
err = json.Unmarshal([]byte(input), config)
return
}
func (config *ConfigSDNDNS) CreateWithValidate(id string, client *Client) (err error) {
err = config.Validate(id, true, client)
if err != nil {
return
}
return config.Create(id, client)
}
func (config *ConfigSDNDNS) Create(id string, client *Client) (err error) {
config.DNS = id
params := config.mapToApiValues()
return client.CreateSDNDNS(params)
}
func (config *ConfigSDNDNS) UpdateWithValidate(id string, client *Client) (err error) {
err = config.Validate(id, false, client)
if err != nil {
return
}
return config.Update(id, client)
}
func (config *ConfigSDNDNS) Update(id string, client *Client) (err error) {
config.DNS = id
params := config.mapToApiValues()
err = client.UpdateSDNDNS(id, params)
if err != nil {
params, _ := json.Marshal(¶ms)
return fmt.Errorf("error updating SDN DNS: %v, (params: %v)", err, string(params))
}
return
}
func (c *ConfigSDNDNS) Validate(id string, create bool, client *Client) (err error) {
exists, err := client.CheckSDNDNSExistance(id)
if err != nil {
return
}
if exists && create {
return ErrorItemExists(id, "dns")
}
if !exists && !create {
return ErrorItemNotExists(id, "dns")
}
err = ValidateStringInArray([]string{"powerdns"}, c.Type, "type")
if err != nil {
return
}
err = ValidateIntGreater(0, c.TTL, "ttl")
if err != nil {
return
}
return
}
func (config *ConfigSDNDNS) mapToApiValues() (params map[string]interface{}) {
d, _ := json.Marshal(config)
json.Unmarshal(d, ¶ms)
return
}