/
endpoint.go
88 lines (69 loc) · 2.01 KB
/
endpoint.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
package native
import (
"errors"
"github.com/CyCoreSystems/ari/v5"
)
// Endpoint provides the ARI Endpoint accessors for the native client
type Endpoint struct {
client *Client
}
// Get gets a lazy handle for the endpoint entity
func (e *Endpoint) Get(key *ari.Key) *ari.EndpointHandle {
return ari.NewEndpointHandle(e.client.stamp(key), e)
}
// List lists the current endpoints and returns a list of handles
func (e *Endpoint) List(filter *ari.Key) (ex []*ari.Key, err error) {
endpoints := []struct {
Tech string `json:"technology"`
Resource string `json:"resource"`
}{}
if filter == nil {
filter = ari.NodeKey(e.client.ApplicationName(), e.client.node)
}
if err = e.client.get("/endpoints", &endpoints); err != nil {
return nil, err
}
for _, i := range endpoints {
k := e.client.stamp(ari.NewEndpointKey(i.Tech, i.Resource))
if filter.Match(k) {
ex = append(ex, k)
}
}
return
}
// ListByTech lists the current endpoints with the given technology and
// returns a list of handles.
func (e *Endpoint) ListByTech(tech string, filter *ari.Key) (ex []*ari.Key, err error) {
endpoints := []struct {
Tech string `json:"technology"`
Resource string `json:"resource"`
}{}
if filter == nil {
filter = ari.NodeKey(e.client.ApplicationName(), e.client.node)
}
if err = e.client.get("/endpoints/"+tech, &endpoints); err != nil {
return nil, err
}
for _, i := range endpoints {
k := e.client.stamp(ari.NewEndpointKey(i.Tech, i.Resource))
if filter.Match(k) {
ex = append(ex, k)
}
}
return
}
// Data retrieves the current state of the endpoint
func (e *Endpoint) Data(key *ari.Key) (*ari.EndpointData, error) {
if key == nil || key.ID == "" {
return nil, errors.New("endpoint key not supplied")
}
if key.Kind != ari.EndpointKey {
return nil, errors.New("wrong key type")
}
data := new(ari.EndpointData)
if err := e.client.get("/endpoints/"+key.ID, data); err != nil {
return nil, dataGetError(err, "endpoint", "%s", key.ID)
}
data.Key = e.client.stamp(key)
return data, nil
}