/
uniter_v1.go
183 lines (170 loc) · 5.38 KB
/
uniter_v1.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// The uniter package implements the API interface used by the uniter
// worker. This file contains the API facade version 1.
package uniter
import (
"github.com/juju/errors"
"github.com/juju/names"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/network"
"github.com/juju/juju/state"
)
func init() {
common.RegisterStandardFacade("Uniter", 1, NewUniterAPIV1)
}
// UniterAPI implements the API version 1, used by the uniter worker.
type UniterAPIV1 struct {
uniterBaseAPI
accessMachine common.GetAuthFunc
}
// NewUniterAPIV1 creates a new instance of the Uniter API, version 1.
func NewUniterAPIV1(st *state.State, resources *common.Resources, authorizer common.Authorizer) (*UniterAPIV1, error) {
baseAPI, err := newUniterBaseAPI(st, resources, authorizer)
if err != nil {
return nil, err
}
accessMachine := func() (common.AuthFunc, error) {
switch tag := authorizer.GetAuthTag().(type) {
case names.UnitTag:
entity, err := st.Unit(tag.Id())
if err != nil {
return nil, errors.Trace(err)
}
machineId, err := entity.AssignedMachineId()
if err != nil {
return nil, errors.Trace(err)
}
machineTag := names.NewMachineTag(machineId)
return func(tag names.Tag) bool {
return tag == machineTag
}, nil
default:
return nil, errors.Errorf("expected names.UnitTag, got %T", tag)
}
}
return &UniterAPIV1{
uniterBaseAPI: *baseAPI,
accessMachine: accessMachine,
}, nil
}
// AllMachinePorts returns all opened port ranges for each given
// machine (on all networks).
func (u *UniterAPIV1) AllMachinePorts(args params.Entities) (params.MachinePortsResults, error) {
result := params.MachinePortsResults{
Results: make([]params.MachinePortsResult, len(args.Entities)),
}
canAccess, err := u.accessMachine()
if err != nil {
return params.MachinePortsResults{}, err
}
for i, entity := range args.Entities {
result.Results[i] = u.getOneMachinePorts(canAccess, entity.Tag)
}
return result, nil
}
// ServiceOwner returns the owner user for each given service tag.
func (u *UniterAPIV1) ServiceOwner(args params.Entities) (params.StringResults, error) {
result := params.StringResults{
Results: make([]params.StringResult, len(args.Entities)),
}
canAccess, err := u.accessService()
if err != nil {
return params.StringResults{}, err
}
for i, entity := range args.Entities {
tag, err := names.ParseServiceTag(entity.Tag)
if err != nil {
result.Results[i].Error = common.ServerError(common.ErrPerm)
continue
}
if !canAccess(tag) {
result.Results[i].Error = common.ServerError(common.ErrPerm)
continue
}
service, err := u.getService(tag)
if err != nil {
result.Results[i].Error = common.ServerError(err)
continue
}
result.Results[i].Result = service.GetOwnerTag()
}
return result, nil
}
// AssignedMachine returns the machine tag for each given unit tag, or
// an error satisfying params.IsCodeNotAssigned when a unit has no
// assigned machine.
func (u *UniterAPIV1) AssignedMachine(args params.Entities) (params.StringResults, error) {
result := params.StringResults{
Results: make([]params.StringResult, len(args.Entities)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.StringResults{}, err
}
for i, entity := range args.Entities {
tag, err := names.ParseUnitTag(entity.Tag)
if err != nil {
result.Results[i].Error = common.ServerError(common.ErrPerm)
continue
}
if !canAccess(tag) {
result.Results[i].Error = common.ServerError(common.ErrPerm)
continue
}
unit, err := u.getUnit(tag)
if err != nil {
result.Results[i].Error = common.ServerError(err)
continue
}
machineId, err := unit.AssignedMachineId()
if err != nil {
result.Results[i].Error = common.ServerError(err)
} else {
result.Results[i].Result = names.NewMachineTag(machineId).String()
}
}
return result, nil
}
func (u *UniterAPIV1) getMachine(tag names.MachineTag) (*state.Machine, error) {
return u.st.Machine(tag.Id())
}
func (u *UniterAPIV1) getOneMachinePorts(canAccess common.AuthFunc, machineTag string) params.MachinePortsResult {
tag, err := names.ParseMachineTag(machineTag)
if err != nil {
return params.MachinePortsResult{Error: common.ServerError(common.ErrPerm)}
}
if !canAccess(tag) {
return params.MachinePortsResult{Error: common.ServerError(common.ErrPerm)}
}
machine, err := u.getMachine(tag)
if err != nil {
return params.MachinePortsResult{Error: common.ServerError(err)}
}
allPorts, err := machine.AllPorts()
if err != nil {
return params.MachinePortsResult{Error: common.ServerError(err)}
}
var resultPorts []params.MachinePortRange
for _, ports := range allPorts {
// AllPortRanges gives a map, but apis require a stable order
// for results, so sort the port ranges.
portRangesToUnits := ports.AllPortRanges()
portRanges := make([]network.PortRange, 0, len(portRangesToUnits))
for portRange := range portRangesToUnits {
portRanges = append(portRanges, portRange)
}
network.SortPortRanges(portRanges)
for _, portRange := range portRanges {
unitName := portRangesToUnits[portRange]
resultPorts = append(resultPorts, params.MachinePortRange{
UnitTag: names.NewUnitTag(unitName).String(),
PortRange: params.FromNetworkPortRange(portRange),
})
}
}
return params.MachinePortsResult{
Ports: resultPorts,
}
}