/
leadership.go
108 lines (93 loc) · 3.45 KB
/
leadership.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
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package common
import (
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/juju/api/base"
"github.com/juju/juju/rpc/params"
)
const leadershipFacade = "LeadershipPinning"
// LeadershipPinningAPI provides common client-side API functions
// for manipulating and querying application leadership pinning.
type LeadershipPinningAPI struct {
facade base.FacadeCaller
}
// NewLeadershipPinningAPI creates and returns a new leadership API client.
func NewLeadershipPinningAPI(caller base.APICaller) *LeadershipPinningAPI {
facadeCaller := base.NewFacadeCaller(
caller,
leadershipFacade,
)
return NewLeadershipPinningAPIFromFacade(facadeCaller)
}
// NewLeadershipPinningAPIFromFacade creates and returns a new leadership API
// client based on the input client facade and facade caller.
func NewLeadershipPinningAPIFromFacade(facade base.FacadeCaller) *LeadershipPinningAPI {
return &LeadershipPinningAPI{
facade: facade,
}
}
// PinnedLeadership returns a collection of application names for which
// leadership is currently pinned, with the entities requiring each
// application's pinned behaviour.
func (a *LeadershipPinningAPI) PinnedLeadership() (map[string][]names.Tag, error) {
var callResult params.PinnedLeadershipResult
err := a.facade.FacadeCall("PinnedLeadership", nil, &callResult)
if err != nil {
return nil, errors.Trace(err)
}
if callResult.Error != nil {
return nil, errors.Trace(callResult.Error)
}
pinned := make(map[string][]names.Tag, len(callResult.Result))
for app, entities := range callResult.Result {
entityTags := make([]names.Tag, len(entities))
for i, e := range entities {
tag, err := names.ParseTag(e)
if err != nil {
return nil, errors.Trace(err)
}
entityTags[i] = tag
}
pinned[app] = entityTags
}
return pinned, nil
}
// PinMachineApplications pins leadership for applications represented by units
// running on the local machine.
// If the caller is not a machine agent, an error will be returned.
// The return is a collection of applications determined to be running on the
// machine, with the result of each individual pin operation.
func (a *LeadershipPinningAPI) PinMachineApplications() (map[string]error, error) {
res, err := a.pinMachineAppsOps("PinMachineApplications")
return res, errors.Trace(err)
}
// UnpinMachineApplications pins leadership for applications represented by
// units running on the local machine.
// If the caller is not a machine agent, an error will be returned.
// The return is a collection of applications determined to be running on the
// machine, with the result of each individual unpin operation.
func (a *LeadershipPinningAPI) UnpinMachineApplications() (map[string]error, error) {
res, err := a.pinMachineAppsOps("UnpinMachineApplications")
return res, errors.Trace(err)
}
// pinMachineAppsOps makes a facade call to the input method name and
// transforms the response into map.
func (a *LeadershipPinningAPI) pinMachineAppsOps(callName string) (map[string]error, error) {
var callResult params.PinApplicationsResults
err := a.facade.FacadeCall(callName, nil, &callResult)
if err != nil {
return nil, errors.Trace(err)
}
callResults := callResult.Results
result := make(map[string]error, len(callResults))
for _, res := range callResults {
var appErr error
if res.Error != nil {
appErr = res.Error
}
result[res.ApplicationName] = appErr
}
return result, nil
}