-
Notifications
You must be signed in to change notification settings - Fork 0
/
almanac.go
142 lines (131 loc) · 4.86 KB
/
almanac.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
package phabricator
import (
"encoding/json"
)
// GetServicesFuture Returns an Async FutureAlmanac
func (p *Phabricator) GetServicesFuture() (FutureAlmanac, FutureError) {
fAlmanac := make(FutureAlmanac)
fError := make(FutureError)
request := p.NewRequest()
queryList := make([]Query, 0)
attachments := make(map[string]string)
attachments["properties"] = "1"
attachments["projects"] = "1"
attachments["bindings"] = "1"
queryList = append(queryList, Query{"map", "attachments", attachments})
queryList = append(queryList, Query{"string", "limit", "100"})
request.AddValues(queryList)
request.SetMethod("almanac.service.search")
go func(request Request) {
resp, err := request.Send(p)
var services Almanac
err = json.Unmarshal(resp, &services)
fAlmanac <- services
fError <- err
}(request)
return fAlmanac, fError
}
// GetServicesAsync Returns the list of services asynchronously
func (p *Phabricator) GetServicesAsync() (Almanac, error) {
fAlmanac, fError := p.GetServicesFuture()
return <-fAlmanac, <-fError
}
// GetDeviceFuture Returns an Async FutureAlmanac
func (p *Phabricator) GetDeviceFuture(hostName string) (FutureAlmanac, FutureError) {
fd := make(FutureAlmanac)
ferr := make(FutureError)
request := p.NewRequest()
queryList := make([]Query, 0)
attachments := make(map[string]string)
attachments["properties"] = "1"
attachments["projects"] = "1"
constraints := make(map[string][]string)
nameConstraint := []string{hostName}
constraints["names"] = nameConstraint
queryList = append(queryList, Query{"map", "attachments", attachments})
queryList = append(queryList, Query{"mapArray", "constraints", constraints})
queryList = append(queryList, Query{"string", "limit", "100"})
request.SetMethod("almanac.device.search")
request.AddValues(queryList)
go func(request Request) {
resp, err := request.Send(p)
var device Almanac
err = json.Unmarshal(resp, &device)
fd <- device
ferr <- err
}(request)
return fd, ferr
}
// GetDeviceAsync Returns the device information asynchronously
func (p *Phabricator) GetDeviceAsync(hostName string) (Almanac, error) {
fDevice, fError := p.GetDeviceFuture(hostName)
return <-fDevice, <-fError
}
// GetProperties Returns the Properties for the result of Almanac Query
func (almanac *Almanac) GetProperties() (properties []Property) {
for _, v := range almanac.Result.Data {
properties = append(properties, v.Attachments.Properties.Properties...)
}
return properties
}
// GetBindings Returns the bindings for the result of Almanac Query
func (almanac *Almanac) GetBindings() (bindings []Binding) {
for _, v := range almanac.Result.Data {
bindings = append(bindings, v.Attachments.Bindings.Bindings...)
}
return bindings
}
// GetDevices returns the list of Devices from Almanac
// Backward compatibility
func GetDevices(request *Request) (devices Almanac, err error) {
queryList := make([]Query, 0)
attachments := make(map[string]string)
attachments["properties"] = "1"
attachments["projects"] = "1"
queryList = append(queryList, Query{"map", "attachments", attachments})
queryList = append(queryList, Query{"string", "limit", "100"})
request.SetMethod("almanac.device.search")
request.AddValues(queryList)
resp, err := SendRequest(request)
err = json.Unmarshal(resp, &devices)
return devices, err
}
// GetDevice returns the specification for the given device
// Backward compatibility
func GetDevice(request *Request, hostName string) (device Almanac, err error) {
queryList := make([]Query, 0)
attachments := make(map[string]string)
attachments["properties"] = "1"
attachments["projects"] = "1"
constraints := make(map[string][]string)
nameConstraint := []string{hostName}
constraints["names"] = nameConstraint
queryList = append(queryList, Query{"map", "attachments", attachments})
queryList = append(queryList, Query{"mapArray", "constraints", constraints})
queryList = append(queryList, Query{"string", "limit", "100"})
request.SetMethod("almanac.device.search")
request.AddValues(queryList)
resp, err := SendRequest(request)
err = json.Unmarshal(resp, &device)
return device, err
}
// GetService returns the specification for the given service
// Backward compatibility
func GetService(request *Request, serviceName string) (service Almanac, err error) {
queryList := make([]Query, 0)
attachments := make(map[string]string)
attachments["properties"] = "1"
attachments["projects"] = "1"
attachments["bindings"] = "1"
constraints := make(map[string][]string)
nameConstraint := []string{serviceName}
constraints["names"] = nameConstraint
queryList = append(queryList, Query{"map", "attachments", attachments})
queryList = append(queryList, Query{"mapArray", "constraints", constraints})
queryList = append(queryList, Query{"string", "limit", "100"})
request.SetMethod("almanac.service.search")
request.AddValues(queryList)
resp, err := SendRequest(request)
err = json.Unmarshal(resp, &service)
return service, err
}