This repository has been archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
portObjectGroup.go
233 lines (195 loc) · 4.65 KB
/
portObjectGroup.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package goftd
import (
"encoding/json"
"fmt"
"strconv"
"github.com/golang/glog"
)
// PortObjectGroup Port Object Group
type PortObjectGroup struct {
ReferenceObject
Description string `json:"description,omitempty"`
IsSystemDefined bool `json:"isSystemDefined,omitempty"`
Objects []*ReferenceObject `json:"objects,omitempty"`
Links *Links `json:"links,omitempty"`
}
// Reference Returns a reference object
func (p *PortObjectGroup) Reference() *ReferenceObject {
r := ReferenceObject{
ID: p.ID,
Name: p.Name,
Version: p.Version,
Type: p.Type,
}
return &r
}
// GetPortObjectGroups Get all the port object groups within the limit specified
func (f *FTD) GetPortObjectGroups(limit int) ([]*PortObjectGroup, error) {
var err error
filter := make(map[string]string)
filter["limit"] = strconv.Itoa(limit)
endpoint := apiPortObjectGroupsEndpoint
data, err := f.Get(endpoint, filter)
if err != nil {
return nil, err
}
var v struct {
Items []*PortObjectGroup `json:"items"`
//Paging *Paging `json:"paging"`
}
err = json.Unmarshal(data, &v)
if err != nil {
if f.debug {
glog.Errorf("Error: %s\n", err)
}
return nil, err
}
return v.Items, nil
}
func (f *FTD) getPortObjectGroupBy(filterString string) ([]*PortObjectGroup, error) {
var err error
filter := make(map[string]string)
filter["filter"] = filterString
endpoint := apiPortObjectGroupsEndpoint
data, err := f.Get(endpoint, filter)
if err != nil {
return nil, err
}
var v struct {
Items []*PortObjectGroup `json:"items"`
}
err = json.Unmarshal(data, &v)
if err != nil {
if f.debug {
glog.Errorf("Error: %s\n", err)
}
return nil, err
}
return v.Items, nil
}
// CreatePortObjectGroup Create a new port object group
func (f *FTD) CreatePortObjectGroup(g *PortObjectGroup, duplicateAction int) error {
var err error
g.Type = "portobjectgroup"
endpoint := apiPortObjectGroupsEndpoint
_, err = f.Post(endpoint, g)
if err != nil {
ftdErr := err.(*FTDError)
//spew.Dump(ftdErr)
if len(ftdErr.Message) > 0 && (ftdErr.Message[0].Code == "duplicateName" || ftdErr.Message[0].Code == "newInstanceWithDuplicateId") {
if f.debug {
glog.Warningf("This is a duplicate\n")
}
if duplicateAction == DuplicateActionError {
return err
}
} else {
if f.debug {
glog.Errorf("Error: %s\n", err)
}
return err
}
}
query := fmt.Sprintf("name:%s", g.Name)
obj, err := f.getPortObjectGroupBy(query)
if err != nil {
if f.debug {
glog.Errorf("Error: %s\n", err)
}
return err
}
var o *PortObjectGroup
if len(obj) == 1 {
o = obj[0]
} else {
if f.debug {
glog.Errorf("Error: length of object is not 1\n")
}
return err
}
switch duplicateAction {
case DuplicateActionReplace:
o.Objects = g.Objects
err = f.UpdatePortObjectGroup(o)
if err != nil {
if f.debug {
glog.Errorf("Error: %s\n", err)
}
return err
}
}
*g = *o
return nil
}
// DeletePortObjectGroup Delete a port object group
func (f *FTD) DeletePortObjectGroup(g *PortObjectGroup) error {
var err error
endpoint := fmt.Sprintf("%s/%s", apiPortObjectGroupsEndpoint, g.ID)
err = f.Delete(endpoint)
if err != nil {
if f.debug {
glog.Errorf("Error: %s\n", err)
}
return err
}
return nil
}
// UpdatePortObjectGroup Updates a port object group
func (f *FTD) UpdatePortObjectGroup(g *PortObjectGroup) error {
var err error
endpoint := fmt.Sprintf("%s/%s", apiPortObjectGroupsEndpoint, g.ID)
data, err := f.Put(endpoint, g)
if err != nil {
if f.debug {
glog.Errorf("Error: %s\n", err)
}
return err
}
err = json.Unmarshal(data, &g)
if err != nil {
if f.debug {
glog.Errorf("Error: %s\n", err)
}
return err
}
return nil
}
// AddToPortObjectGroup Add a Port to an Object Group
func (f *FTD) AddToPortObjectGroup(g *PortObjectGroup, p *PortObject) error {
var err error
for k := range g.Objects {
if g.Objects[k].ID == p.ID {
if f.debug {
glog.Errorf("object already in object group\n")
return fmt.Errorf("object already in object group")
}
}
}
g.Objects = append(g.Objects, p.Reference())
err = f.UpdatePortObjectGroup(g)
if err != nil {
if f.debug {
glog.Errorf("Error: %s\n", err)
}
return err
}
return nil
}
// DeleteFromPortObjectGroup Deletes a Port from an Object Group
func (f *FTD) DeleteFromPortObjectGroup(g *PortObjectGroup, p *PortObject) error {
var err error
for k := range g.Objects {
if g.Objects[k].ID == p.ID {
g.Objects = append(g.Objects[:k], g.Objects[k+1:]...)
break
}
}
err = f.UpdatePortObjectGroup(g)
if err != nil {
if f.debug {
glog.Errorf("Error: %s\n", err)
}
return err
}
return nil
}