/
groups.go
239 lines (199 loc) · 5.27 KB
/
groups.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
234
235
236
237
238
239
package papi
import (
"encoding/json"
"fmt"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1"
edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid"
"github.com/patrickmn/go-cache"
)
// Groups represents a collection of PAPI groups
type Groups struct {
client.Resource
AccountID string `json:"accountId"`
AccountName string `json:"accountName"`
Groups struct {
Items []*Group `json:"items"`
} `json:"groups"`
}
// NewGroups creates a new Groups
func NewGroups() *Groups {
groups := &Groups{}
return groups
}
// PostUnmarshalJSON is called after JSON unmarshaling into EdgeHostnames
//
// See: jsonhooks-v1/jsonhooks.Unmarshal()
func (groups *Groups) PostUnmarshalJSON() error {
groups.Init()
for key, group := range groups.Groups.Items {
groups.Groups.Items[key].parent = groups
if err := group.PostUnmarshalJSON(); err != nil {
return err
}
}
groups.Complete <- true
return nil
}
// GetGroups populates Groups with group data
//
// API Docs: https://developer.akamai.com/api/luna/papi/resources.html#listgroups
// Endpoint: GET /papi/v1/groups/
func (groups *Groups) GetGroups(correlationid string) error {
cachegroups, found := Profilecache.Get("groups")
if found {
json.Unmarshal(cachegroups.([]byte), groups)
return nil
} else {
req, err := client.NewRequest(
Config,
"GET",
"/papi/v1/groups",
nil,
)
if err != nil {
return err
}
edge.PrintHttpRequestCorrelation(req, true, correlationid)
res, err := client.Do(Config, req)
if err != nil {
return err
}
edge.PrintHttpResponseCorrelation(res, true, correlationid)
if client.IsError(res) {
return client.NewAPIError(res)
}
if err = client.BodyJSON(res, groups); err != nil {
return err
}
byt, _ := json.Marshal(groups)
Profilecache.Set("groups", byt, cache.DefaultExpiration)
return nil
}
}
// AddGroup adds a group to a Groups collection
func (groups *Groups) AddGroup(newGroup *Group) {
if newGroup.GroupID != "" {
for key, group := range groups.Groups.Items {
if group.GroupID == newGroup.GroupID {
groups.Groups.Items[key] = newGroup
return
}
}
}
newGroup.parent = groups
groups.Groups.Items = append(groups.Groups.Items, newGroup)
}
// FindGroup finds a specific group by ID
func (groups *Groups) FindGroup(id string) (*Group, error) {
var group *Group
var groupFound bool
if id == "" {
goto err
}
for _, group = range groups.Groups.Items {
if group.GroupID == id {
groupFound = true
break
}
}
err:
if !groupFound {
return nil, fmt.Errorf("Unable to find group: \"%s\"", id)
}
return group, nil
}
// FindGroupId finds a specific group by name
// Deprecated: When there are multiple groups with same name,
// the first one is returned. Please use FindGroupsByName instead.
func (groups *Groups) FindGroupId(name string) (*Group, error) {
var group *Group
var groupFound bool
if name == "" {
goto err
}
for _, group = range groups.Groups.Items {
if group.GroupName == name {
groupFound = true
break
}
}
err:
if !groupFound {
return nil, fmt.Errorf("Unable to find group: \"%s\"", name)
}
return group, nil
}
// FindGroupsByName finds groups by name
func (groups *Groups) FindGroupsByName(name string) ([]*Group, error) {
var group *Group
var foundGroups []*Group
var groupFound bool
if name == "" {
goto err
}
for _, group = range groups.Groups.Items {
if group.GroupName == name {
foundGroups = append(foundGroups, group)
groupFound = true
}
}
err:
if !groupFound {
return nil, fmt.Errorf("Unable to find group: \"%s\"", name)
}
return foundGroups, nil
}
// Group represents a group resource
type Group struct {
client.Resource
parent *Groups
GroupName string `json:"groupName"`
GroupID string `json:"groupId"`
ParentGroupID string `json:"parentGroupId,omitempty"`
ContractIDs []string `json:"contractIds"`
}
// NewGroup creates a new Group
func NewGroup(parent *Groups) *Group {
group := &Group{
parent: parent,
}
group.Init()
return group
}
// GetGroup populates a Group
func (group *Group) GetGroup() {
groups, err := GetGroups()
if err != nil {
return
}
for _, g := range groups.Groups.Items {
if g.GroupID == group.GroupID {
group.parent = groups
group.ContractIDs = g.ContractIDs
group.GroupName = g.GroupName
group.ParentGroupID = g.ParentGroupID
group.Complete <- true
return
}
}
group.Complete <- false
}
// GetProperties retrieves all properties associated with a given group and contract
func (group *Group) GetProperties(contract *Contract) (*Properties, error) {
return GetProperties(contract, group)
}
// GetCpCodes retrieves all CP codes associated with a given group and contract
func (group *Group) GetCpCodes(contract *Contract) (*CpCodes, error) {
return GetCpCodes(contract, group)
}
// GetEdgeHostnames retrieves all Edge hostnames associated with a given group/contract
func (group *Group) GetEdgeHostnames(contract *Contract, options string, correlationid string) (*EdgeHostnames, error) {
return GetEdgeHostnames(contract, group, options)
}
// NewProperty creates a property associated with a given group/contract
func (group *Group) NewProperty(contract *Contract) (*Property, error) {
property := NewProperty(NewProperties())
property.Contract = contract
property.Group = group
return property, nil
}