/
federation.go
91 lines (78 loc) · 2.77 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
package artifactory
import (
"encoding/json"
)
const federationMirrorsLagEndpoint = "federation/status/mirrorsLag"
const federationUnavailableMirrorsEndpoint = "federation/status/unavailableMirrors"
// IsFederationEnabled checks one of the federation endpoints to see if federation is enabled
func (c *Client) IsFederationEnabled() bool {
_, err := c.FetchHTTP(federationUnavailableMirrorsEndpoint)
if err != nil {
return false
}
return true
}
// MirrorLag represents single element of API respond from federation/status/mirrorsLag endpoint
type MirrorLag struct {
LocalRepoKey string `json:"localRepoKey"`
RemoteUrl string `json:"remoteUrl"`
RemoteRepoKey string `json:"remoteRepoKey"`
LagInMS int `json:"lagInMS"`
}
type MirrorLags struct {
MirrorLags []MirrorLag
NodeId string
}
// FetchMirrorLags makes the API call to federation/status/mirrorsLag endpoint and returns []MirrorLag
func (c *Client) FetchMirrorLags() (MirrorLags, error) {
var mirrorLags MirrorLags
c.logger.Debug("Fetching mirror lags")
resp, err := c.FetchHTTP(federationMirrorsLagEndpoint)
if err != nil {
if err.(*APIError).status == 404 {
return mirrorLags, nil
}
return mirrorLags, err
}
mirrorLags.NodeId = resp.NodeId
if err := json.Unmarshal(resp.Body, &mirrorLags.MirrorLags); err != nil {
c.logger.Error("There was an issue when try to unmarshal mirror lags respond")
return mirrorLags, &UnmarshalError{
message: err.Error(),
endpoint: federationMirrorsLagEndpoint,
}
}
return mirrorLags, nil
}
// UnavailableMirror represents single element of API respond from federation/status/unavailableMirrors endpoint
type UnavailableMirror struct {
LocalRepoKey string `json:"localRepoKey"`
RemoteUrl string `json:"remoteUrl"`
RemoteRepoKey string `json:"remoteRepoKey"`
Status string `json:"status"`
}
type UnavailableMirrors struct {
UnavailableMirrors []UnavailableMirror
NodeId string
}
// FetchUnavailableMirrors makes the API call to federation/status/unavailableMirrors endpoint and returns []UnavailableMirror
func (c *Client) FetchUnavailableMirrors() (UnavailableMirrors, error) {
var unavailableMirrors UnavailableMirrors
c.logger.Debug("Fetching unavailable mirrors")
resp, err := c.FetchHTTP(federationUnavailableMirrorsEndpoint)
if err != nil {
if err.(*APIError).status == 404 {
return unavailableMirrors, nil
}
return unavailableMirrors, err
}
unavailableMirrors.NodeId = resp.NodeId
if err := json.Unmarshal(resp.Body, &unavailableMirrors.UnavailableMirrors); err != nil {
c.logger.Error("There was an issue when try to unmarshal unavailable mirrors respond")
return unavailableMirrors, &UnmarshalError{
message: err.Error(),
endpoint: federationUnavailableMirrorsEndpoint,
}
}
return unavailableMirrors, nil
}