-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcmd_target.go
166 lines (149 loc) · 3.8 KB
/
cmd_target.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
package omp
import (
"encoding/xml"
"strings"
)
// OMPtarget .
type OMPtarget struct {
Text string `xml:",chardata"`
ID string `xml:"id,attr,omitempty"`
Name string `xml:"name,omitempty"`
Comment string `xml:"comment,omitempty"`
CreationTime string `xml:"creation_time,omitempty"`
ModificationTime string `xml:"modification_time,omitempty"`
Writable string `xml:"writable,omitempty"`
InUse string `xml:"in_use,omitempty"`
UserTags *UserTags `xml:"user_tags,omitempty"`
Hosts string `xml:"hosts,omitempty"`
MaxHosts string `xml:"max_hosts,omitempty"`
SSHCredential *struct {
Text string `xml:",chardata"`
ID string `xml:"id,attr,omitempty"`
Name string `xml:"name,omitempty"`
} `xml:"ssh_credential,omitempty"`
SmbCredential *struct {
Text string `xml:",chardata"`
ID string `xml:"id,attr,omitempty"`
Name string `xml:"name,omitempty"`
} `xml:"smb_credential,omitempty"`
EsxiCredential *struct {
Text string `xml:",chardata"`
ID string `xml:"id,attr,omitempty"`
Name string `xml:"name,omitempty"`
} `xml:"esxi_credential,omitempty"`
}
// Target .
type Target struct {
Text string `xml:",chardata"`
ID string `xml:"id,attr,omitempty"`
Name string `xml:"name,omitempty"`
Hosts []string
ExcludeHosts []string
}
// CreateTarget .
type CreateTarget struct {
requestBase
XMLName xml.Name `xml:"create_target"`
Name string `xml:"name,omitempty"`
Hosts string `xml:"hosts,omitempty"`
}
type responseCreateTarget struct {
responseBase
ID string `xml:"id,attr,omitempty"`
}
// ModifyTarget .
type ModifyTarget struct {
requestBase
XMLName xml.Name `xml:"modify_target"`
ID string `xml:"target_id,attr,omitempty"`
Name string `xml:"name,omitempty"`
Hosts string `xml:"hosts,omitempty"`
ExcludeHosts string `xml:"exclude_hosts,omitempty"`
}
type responseModifyTarget struct {
responseBase
}
// GetTargets .
type GetTargets struct {
requestBase
XMLName xml.Name `xml:"get_targets"`
}
// GetTargetsResponse .
type GetTargetsResponse struct {
responseBase
XMLName xml.Name `xml:"get_targets_response"`
Text string `xml:",chardata"`
Target []OMPtarget `xml:"target,omitempty"`
}
// DeleteTarget .
type DeleteTarget struct {
requestBase
XMLName xml.Name `xml:"delete_target"`
ID string `xml:"target_id,attr,omitempty"`
}
type responseDeleteTarget struct {
responseBase
}
// CreateTarget create a target and return its ID
func (c *Connector) CreateTarget(t *Target) (string, error) {
req := CreateTarget{
Name: t.Name,
Hosts: strings.Join(t.Hosts, ","),
}
resp := &responseCreateTarget{}
err := c.doRequest(req, resp)
if err != nil {
return "", err
}
return resp.ID, nil
}
// ModifyTarget .
func (c *Connector) ModifyTarget(t *Target) error {
req := ModifyTarget{
ID: t.ID,
}
if t.Hosts != nil {
req.Hosts = strings.Join(t.Hosts, ",")
}
if t.ExcludeHosts != nil {
req.ExcludeHosts = strings.Join(t.ExcludeHosts, ",")
}
resp := &responseModifyTarget{}
err := c.doRequest(req, resp)
if err != nil {
return err
}
return nil
}
// DeleteTarget .
func (c *Connector) DeleteTarget(id string) error {
req := DeleteTarget{
ID: id,
}
resp := &responseDeleteTarget{}
err := c.doRequest(req, resp)
if err != nil {
return err
}
return nil
}
// GetTargets .
func (c *Connector) GetTargets() ([]OMPtarget, error) {
req := GetTargets{}
res := []OMPtarget{}
resp := &GetTargetsResponse{}
err := c.doRequest(req, resp)
if err != nil {
return res, err
}
targets := resp.Target
for i := range targets {
config := OMPtarget{
ID: targets[i].ID,
Name: targets[i].Name,
Hosts: targets[i].Hosts,
}
res = append(res, config)
}
return res, nil
}