forked from michaelklishin/rabbit-hole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfederation.go
158 lines (128 loc) · 4 KB
/
federation.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
package rabbithole
import (
"net/http"
)
// FederationDefinition represents settings
// that will be used by federation links.
type FederationDefinition struct {
Uri string `json:"uri"`
Expires int `json:"expires,omitempty"`
MessageTTL int32 `json:"message-ttl"`
MaxHops int `json:"max-hops"`
PrefetchCount int `json:"prefetch-count"`
ReconnectDelay int `json:"reconnect-delay"`
AckMode string `json:"ack-mode,omitempty"`
TrustUserId bool `json:"trust-user-id"`
Exchange string `json:"exchange"`
Queue string `json:"queue"`
}
// FederationUpstream represents a configured federation upstream.
type FederationUpstream struct {
Name string `json:"name"`
Vhost string `json:"vhost"`
Component string `json:"component"`
Definition FederationDefinition `json:"value"`
}
// FederationUpstreamComponent is the name of the runtime parameter component
// used by federation upstreams.
const FederationUpstreamComponent string = "federation-upstream"
//
// GET /api/parameters/federation-upstream
//
// ListFederationUpstreams returns a list of all federation upstreams.
func (c *Client) ListFederationUpstreams() (ups []FederationUpstream, err error) {
params, err := c.ListRuntimeParametersFor(FederationUpstreamComponent)
if err != nil {
return nil, err
}
for _, p := range params {
up := paramToUpstream(&p)
ups = append(ups, *up)
}
return ups, nil
}
//
// GET /api/parameters/federation-upstream/{vhost}
//
// ListFederationUpstreamsIn returns a list of all federation upstreams in a vhost.
func (c *Client) ListFederationUpstreamsIn(vhost string) (ups []FederationUpstream, err error) {
params, err := c.ListRuntimeParametersIn(FederationUpstreamComponent, vhost)
if err != nil {
return nil, err
}
for _, p := range params {
up := paramToUpstream(&p)
ups = append(ups, *up)
}
return ups, nil
}
//
// GET /api/parameters/federation-upstream/{vhost}/{upstream}
//
// GetFederationUpstream returns information about a federation upstream.
func (c *Client) GetFederationUpstream(vhost, name string) (up *FederationUpstream, err error) {
p, err := c.GetRuntimeParameter(FederationUpstreamComponent, vhost, name)
if err != nil {
return nil, err
}
return paramToUpstream(p), nil
}
//
// PUT /api/parameters/federation-upstream/{vhost}/{upstream}
//
// PutFederationUpstream creates or updates a federation upstream configuration.
func (c *Client) PutFederationUpstream(vhost, name string, def FederationDefinition) (res *http.Response, err error) {
return c.PutRuntimeParameter(FederationUpstreamComponent, vhost, name, def)
}
//
// DELETE /api/parameters/federation-upstream/{vhost}/{name}
//
// DeleteFederationUpstream removes a federation upstream.
func (c *Client) DeleteFederationUpstream(vhost, name string) (res *http.Response, err error) {
return c.DeleteRuntimeParameter(FederationUpstreamComponent, vhost, name)
}
// paramToUpstream maps from a RuntimeParameter structure to a FederationUpstream structure.
func paramToUpstream(p *RuntimeParameter) (up *FederationUpstream) {
up = &FederationUpstream{
Name: p.Name,
Vhost: p.Vhost,
Component: p.Component,
}
m, ok := p.Value.(map[string]interface{})
if !ok {
return up
}
def := FederationDefinition{}
if v, ok := m["uri"].(string); ok {
def.Uri = v
}
if v, ok := m["expires"].(float64); ok {
def.Expires = int(v)
}
if v, ok := m["message-ttl"].(float64); ok {
def.MessageTTL = int32(v)
}
if v, ok := m["max-hops"].(float64); ok {
def.MaxHops = int(v)
}
if v, ok := m["prefetch-count"].(float64); ok {
def.PrefetchCount = int(v)
}
if v, ok := m["reconnect-delay"].(float64); ok {
def.ReconnectDelay = int(v)
}
if v, ok := m["ack-mode"].(string); ok {
def.AckMode = v
}
if v, ok := m["trust-user-id"].(bool); ok {
def.TrustUserId = v
}
if v, ok := m["exchange"].(string); ok {
def.Exchange = v
}
if v, ok := m["queue"].(string); ok {
def.Queue = v
}
up.Definition = def
return up
}