/
machinemanager.go
170 lines (151 loc) · 4.66 KB
/
machinemanager.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package machinemanager
import (
"fmt"
"github.com/juju/errors"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/instance"
"github.com/juju/juju/permission"
"github.com/juju/juju/state"
)
func init() {
common.RegisterStandardFacade("MachineManager", 2, NewMachineManagerAPI)
}
// MachineManagerAPI provides access to the MachineManager API facade.
type MachineManagerAPI struct {
st stateInterface
authorizer facade.Authorizer
check *common.BlockChecker
}
var getState = func(st *state.State) stateInterface {
return stateShim{st}
}
// NewMachineManagerAPI creates a new server-side MachineManager API facade.
func NewMachineManagerAPI(
st *state.State,
resources facade.Resources,
authorizer facade.Authorizer,
) (*MachineManagerAPI, error) {
if !authorizer.AuthClient() {
return nil, common.ErrPerm
}
s := getState(st)
return &MachineManagerAPI{
st: s,
authorizer: authorizer,
check: common.NewBlockChecker(s),
}, nil
}
// AddMachines adds new machines with the supplied parameters.
func (mm *MachineManagerAPI) AddMachines(args params.AddMachines) (params.AddMachinesResults, error) {
results := params.AddMachinesResults{
Machines: make([]params.AddMachinesResult, len(args.MachineParams)),
}
canWrite, err := mm.authorizer.HasPermission(permission.WriteAccess, mm.st.ModelTag())
if err != nil {
return results, errors.Trace(err)
}
if !canWrite {
return results, common.ErrPerm
}
if err := mm.check.ChangeAllowed(); err != nil {
return results, errors.Trace(err)
}
for i, p := range args.MachineParams {
m, err := mm.addOneMachine(p)
results.Machines[i].Error = common.ServerError(err)
if err == nil {
results.Machines[i].Machine = m.Id()
}
}
return results, nil
}
func (mm *MachineManagerAPI) addOneMachine(p params.AddMachineParams) (*state.Machine, error) {
if p.ParentId != "" && p.ContainerType == "" {
return nil, fmt.Errorf("parent machine specified without container type")
}
if p.ContainerType != "" && p.Placement != nil {
return nil, fmt.Errorf("container type and placement are mutually exclusive")
}
if p.Placement != nil {
// Extract container type and parent from container placement directives.
containerType, err := instance.ParseContainerType(p.Placement.Scope)
if err == nil {
p.ContainerType = containerType
p.ParentId = p.Placement.Directive
p.Placement = nil
}
}
if p.ContainerType != "" || p.Placement != nil {
// Guard against dubious client by making sure that
// the following attributes can only be set when we're
// not using placement.
p.InstanceId = ""
p.Nonce = ""
p.HardwareCharacteristics = instance.HardwareCharacteristics{}
p.Addrs = nil
}
if p.Series == "" {
conf, err := mm.st.ModelConfig()
if err != nil {
return nil, errors.Trace(err)
}
p.Series = config.PreferredSeries(conf)
}
var placementDirective string
if p.Placement != nil {
env, err := mm.st.Model()
if err != nil {
return nil, errors.Trace(err)
}
// For 1.21 we should support both UUID and name, and with 1.22
// just support UUID
if p.Placement.Scope != env.Name() && p.Placement.Scope != env.UUID() {
return nil, fmt.Errorf("invalid model name %q", p.Placement.Scope)
}
placementDirective = p.Placement.Directive
}
volumes := make([]state.MachineVolumeParams, 0, len(p.Disks))
for _, cons := range p.Disks {
if cons.Count == 0 {
return nil, errors.Errorf("invalid volume params: count not specified")
}
// Pool and Size are validated by AddMachineX.
volumeParams := state.VolumeParams{
Pool: cons.Pool,
Size: cons.Size,
}
volumeAttachmentParams := state.VolumeAttachmentParams{}
for i := uint64(0); i < cons.Count; i++ {
volumes = append(volumes, state.MachineVolumeParams{
volumeParams, volumeAttachmentParams,
})
}
}
jobs, err := common.StateJobs(p.Jobs)
if err != nil {
return nil, errors.Trace(err)
}
template := state.MachineTemplate{
Series: p.Series,
Constraints: p.Constraints,
Volumes: volumes,
InstanceId: p.InstanceId,
Jobs: jobs,
Nonce: p.Nonce,
HardwareCharacteristics: p.HardwareCharacteristics,
Addresses: params.NetworkAddresses(p.Addrs...),
Placement: placementDirective,
}
if p.ContainerType == "" {
return mm.st.AddOneMachine(template)
}
if p.ParentId != "" {
return mm.st.AddMachineInsideMachine(template, p.ParentId, p.ContainerType)
}
return mm.st.AddMachineInsideNewMachine(template, template, p.ContainerType)
}