-
Notifications
You must be signed in to change notification settings - Fork 4
/
resource.go
119 lines (106 loc) · 3.34 KB
/
resource.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
package limacharlie
import (
"fmt"
"net/http"
"time"
)
type ResourceCategory = string
type ResourceName = string
type ResourcesByCategory map[ResourceCategory]map[ResourceName]struct{}
var ResourceCategories = struct {
API string
Replicant string
Service string
}{
API: "api",
Replicant: "replicant",
Service: "service",
}
func (org Organization) resources(verb string, request restRequest) error {
return org.client.reliableRequest(verb, fmt.Sprintf("orgs/%s/resources", org.client.options.OID), request)
}
type resourceGetResponse = map[string]map[string][]string
func (r ResourcesByCategory) duplicate() ResourcesByCategory {
dup := ResourcesByCategory{}
for resCat, resNames := range r {
names, found := dup[resCat]
if !found {
names = map[string]struct{}{}
}
for name := range resNames {
names[name] = struct{}{}
}
dup[resCat] = names
}
return dup
}
func (r *ResourcesByCategory) AddToCategory(category ResourceCategory, name ResourceName) {
cat, found := (*r)[category]
if !found {
cat = map[string]struct{}{}
}
cat[name] = struct{}{}
(*r)[category] = cat
}
func (r *ResourcesByCategory) GetForCategory(category ResourceCategory) map[ResourceName]struct{} {
resourcesForCat, found := (*r)[category]
if !found {
resourcesForCat = map[ResourceName]struct{}{}
(*r)[category] = resourcesForCat
}
return resourcesForCat
}
func (r *ResourcesByCategory) RemoveFromCategory(category ResourceCategory, name ResourceName) {
cat, found := (*r)[category]
if !found {
return
}
delete(cat, name)
(*r)[category] = cat
}
// Resources list available resources
func (org Organization) Resources() (ResourcesByCategory, error) {
resp := resourceGetResponse{}
req := makeDefaultRequest(&resp).withTimeout(10 * time.Second)
if err := org.resources(http.MethodGet, req); err != nil {
return ResourcesByCategory{}, err
}
resources := ResourcesByCategory{}
resourcesContent, found := resp["resources"]
if !found {
return resources, fmt.Errorf("resources: expected key 'resources' is missing from response")
}
for resCat, resNames := range resourcesContent {
resourcesForCat, ok := resources[resCat]
if !ok {
resourcesForCat = map[string]struct{}{}
}
for _, resName := range resNames {
resourcesForCat[resName] = struct{}{}
}
resources[resCat] = resourcesForCat
}
return resources, nil
}
// ResourceSubscribe subscribe to a resource.
// The backend call is async meaning that you will get a response right away but it might take a
// few seconds before a call to list resources shows up with the updated list.
func (org Organization) ResourceSubscribe(name ResourceName, category ResourceCategory) error {
resp := Dict{}
req := makeDefaultRequest(&resp).withTimeout(120 * time.Second).withFormData(Dict{
"res_cat": category,
"res_name": name,
})
return org.resources(http.MethodPost, req)
}
// ResourceUnsubscribe unsubscribe from a resource.
// The backend call is async meaning that you will get a response right away but it might take a
// few seconds before a call to list resources shows up with the updated list.
func (org Organization) ResourceUnsubscribe(name ResourceName, category ResourceCategory) error {
resp := Dict{}
req := makeDefaultRequest(&resp).withTimeout(120 * time.Second).withFormData(Dict{
"res_cat": category,
"res_name": name,
})
return org.resources(http.MethodDelete, req)
}