11package rabbithole
22
33import (
4- "encoding/json"
54 "net/http"
6- "net/url"
75)
86
97// Federation definition: additional arguments
@@ -24,49 +22,132 @@ type FederationDefinition struct {
2422
2523// Represents a configured Federation upstream.
2624type FederationUpstream struct {
25+ Name string `json:"name"`
26+ Vhost string `json:"vhost"`
27+ Component string `json:"component"`
2728 Definition FederationDefinition `json:"value"`
2829}
2930
31+ const FederationUpstreamComponent string = "federation-upstream"
32+
3033//
31- // PUT /api/parameters/federation-upstream/{vhost}/{upstream}
34+ // GET /api/parameters/federation-upstream
3235//
3336
34- // Updates a federation upstream
35- func (c * Client ) PutFederationUpstream (vhost string , upstreamName string , fDef FederationDefinition ) (res * http.Response , err error ) {
36- fedUp := FederationUpstream {
37- Definition : fDef ,
38- }
39- body , err := json .Marshal (fedUp )
37+ // ListFederationUpstreams returns a list of all federation upstreams.
38+ func (c * Client ) ListFederationUpstreams () (ups []FederationUpstream , err error ) {
39+ params , err := c .ListRuntimeParametersFor (FederationUpstreamComponent )
4040 if err != nil {
4141 return nil , err
4242 }
4343
44- req , err := newRequestWithBody (c , "PUT" , "parameters/federation-upstream/" + url .PathEscape (vhost )+ "/" + url .PathEscape (upstreamName ), body )
44+ for _ , p := range params {
45+ up := paramToUpstream (& p )
46+ ups = append (ups , * up )
47+ }
48+ return ups , nil
49+ }
50+
51+ //
52+ // GET /api/parameters/federation-upstream/{vhost}
53+ //
54+
55+ // ListFederationUpstreamsIn returns a list of all federation upstreams in a vhost.
56+ func (c * Client ) ListFederationUpstreamsIn (vhost string ) (ups []FederationUpstream , err error ) {
57+ params , err := c .ListRuntimeParametersIn (FederationUpstreamComponent , vhost )
4558 if err != nil {
4659 return nil , err
4760 }
4861
49- if res , err = executeRequest (c , req ); err != nil {
62+ for _ , p := range params {
63+ up := paramToUpstream (& p )
64+ ups = append (ups , * up )
65+ }
66+ return ups , nil
67+ }
68+
69+ //
70+ // GET /api/parameters/federation-upstream/{vhost}/{upstream}
71+ //
72+
73+ // GetFederationUpstream returns information about a federation upstream.
74+ func (c * Client ) GetFederationUpstream (vhost , name string ) (up * FederationUpstream , err error ) {
75+ p , err := c .GetRuntimeParameter (FederationUpstreamComponent , vhost , name )
76+ if err != nil {
5077 return nil , err
5178 }
79+ return paramToUpstream (p ), nil
80+ }
5281
53- return res , nil
82+ //
83+ // PUT /api/parameters/federation-upstream/{vhost}/{upstream}
84+ //
85+
86+ // PutFederationUpstream creates or updates a federation upstream configuration.
87+ func (c * Client ) PutFederationUpstream (vhost , name string , def FederationDefinition ) (res * http.Response , err error ) {
88+ return c .PutRuntimeParameter (FederationUpstreamComponent , vhost , name , def )
5489}
5590
5691//
5792// DELETE /api/parameters/federation-upstream/{vhost}/{name}
5893//
5994
60- // Deletes a federation upstream.
61- func (c * Client ) DeleteFederationUpstream (vhost , upstreamName string ) (res * http.Response , err error ) {
62- req , err := newRequestWithBody (c , "DELETE" , "parameters/federation-upstream/" + url .PathEscape (vhost )+ "/" + url .PathEscape (upstreamName ), nil )
63- if err != nil {
64- return nil , err
95+ // DeleteFederationUpstream removes a federation upstream.
96+ func (c * Client ) DeleteFederationUpstream (vhost , name string ) (res * http.Response , err error ) {
97+ return c .DeleteRuntimeParameter (FederationUpstreamComponent , vhost , name )
98+ }
99+
100+ // paramToUpstream maps from a RuntimeParameter structure to a FederationUpstream structure.
101+ func paramToUpstream (p * RuntimeParameter ) (up * FederationUpstream ) {
102+ up = & FederationUpstream {
103+ Name : p .Name ,
104+ Vhost : p .Vhost ,
105+ Component : p .Component ,
65106 }
66107
67- if res , err = executeRequest (c , req ); err != nil {
68- return nil , err
108+ def := FederationDefinition {}
109+ m := p .Value .(map [string ]interface {})
110+
111+ if v , ok := m ["uri" ].(string ); ok {
112+ def .Uri = v
113+ }
114+
115+ if v , ok := m ["expires" ].(float64 ); ok {
116+ def .Expires = int (v )
117+ }
118+
119+ if v , ok := m ["message-ttl" ].(float64 ); ok {
120+ def .MessageTTL = int32 (v )
121+ }
122+
123+ if v , ok := m ["max-hops" ].(float64 ); ok {
124+ def .MaxHops = int (v )
125+ }
126+
127+ if v , ok := m ["prefetch-count" ].(float64 ); ok {
128+ def .PrefetchCount = int (v )
129+ }
130+
131+ if v , ok := m ["reconnect-delay" ].(float64 ); ok {
132+ def .ReconnectDelay = int (v )
133+ }
134+
135+ if v , ok := m ["ack-mode" ].(string ); ok {
136+ def .AckMode = v
137+ }
138+
139+ if v , ok := m ["trust-user-id" ].(bool ); ok {
140+ def .TrustUserId = v
141+ }
142+
143+ if v , ok := m ["exchange" ].(string ); ok {
144+ def .Exchange = v
145+ }
146+
147+ if v , ok := m ["queue" ].(string ); ok {
148+ def .Queue = v
69149 }
70150
71- return res , nil
151+ up .Definition = def
152+ return up
72153}
0 commit comments