Merge lp:~axwalk/juju-core/state-addmachineparams-expose into lp:~go-bot/juju-core/trunk

Proposed by Andrew Wilkins
Status: Merged
Approved by: Andrew Wilkins
Approved revision: no longer in the source branch.
Merged at revision: 1704
Proposed branch: lp:~axwalk/juju-core/state-addmachineparams-expose
Merge into: lp:~go-bot/juju-core/trunk
Diff against target: 258 lines (+90/-48)
4 files modified
cmd/jujud/machine_test.go (+6/-3)
environs/bootstrap/bootstrap.go (+8/-1)
state/state.go (+30/-29)
state/state_test.go (+46/-15)
To merge this branch: bzr merge lp:~axwalk/juju-core/state-addmachineparams-expose
Reviewer Review Type Date Requested Status
Juju Engineering Pending
Review via email: mp+181488@code.launchpad.net

Commit message

state: modify InjectMachine to take params struct

InjectMachine is modified to take AddMachineParams
struct, and that struct's fields are now all
exported. Additionally, AddMachineWithConstraints
(which already took the params struct), now checks
that the formerly non-exported fields are blank
upon entry.

These changes are to accommodate manual provisioning,
where we want to inject a machine into state, but
set the nonce to something other than the bootstrap
nonce. Additionally, we are likely to add provider
type into the machine document soon.

https://codereview.appspot.com/13169043/

Description of the change

state: modify InjectMachine to take params struct

InjectMachine is modified to take AddMachineParams
struct, and that struct's fields are now all
exported. Additionally, AddMachineWithConstraints
(which already took the params struct), now checks
that the formerly non-exported fields are blank
upon entry.

These changes are to accommodate manual provisioning,
where we want to inject a machine into state, but
set the nonce to something other than the bootstrap
nonce. Additionally, we are likely to add provider
type into the machine document soon.

https://codereview.appspot.com/13169043/

To post a comment you must log in.
Revision history for this message
Andrew Wilkins (axwalk) wrote :

Reviewers: mp+181488_code.launchpad.net,

Message:
Please take a look.

Description:
state: modify InjectMachine to take params struct

InjectMachine is modified to take AddMachineParams
struct, and that struct's fields are now all
exported. Additionally, AddMachineWithConstraints
(which already took the params struct), now checks
that the formerly non-exported fields are blank
upon entry.

These changes are to accommodate manual provisioning,
where we want to inject a machine into state, but
set the nonce to something other than the bootstrap
nonce. Additionally, we are likely to add provider
type into the machine document soon.

https://code.launchpad.net/~axwalk/juju-core/state-addmachineparams-expose/+merge/181488

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/13169043/

Affected files:
   A [revision details]
   M cmd/jujud/machine_test.go
   M environs/bootstrap.go
   M state/state.go
   M state/state_test.go

Revision history for this message
William Reade (fwereade) wrote :

LGTM; I'll leave the naming question to your judgment.

https://codereview.appspot.com/13169043/diff/1/state/state.go
File state/state.go (left):

https://codereview.appspot.com/13169043/diff/1/state/state.go#oldcode273
state/state.go:273: type AddMachineParams struct {
Quibble quibble, should we call it something like MachineSpec? It's not
really Add-specific.

https://codereview.appspot.com/13169043/

Revision history for this message
Andrew Wilkins (axwalk) wrote :

On 2013/08/22 16:33:17, fwereade wrote:
> LGTM; I'll leave the naming question to your judgment.

Thanks for reviewing.

> https://codereview.appspot.com/13169043/diff/1/state/state.go
> File state/state.go (left):

https://codereview.appspot.com/13169043/diff/1/state/state.go#oldcode273
> state/state.go:273: type AddMachineParams struct {
> Quibble quibble, should we call it something like MachineSpec? It's
not really
> Add-specific.

This crossed my mind too, but I'm going to leave it. I want to keep it
xParams, because that's what it is: a parameter structure. I wouldn't
want to inadvertently suggest that the structure could be used for other
purposes.

If this becomes contentious, we could consolidate all AddMachine* and
InjectMachine functions to one function: AddMachine(params
AddMachineParams, mode AddMachineMode), where mode tells it what type of
addition it is. Alternatively, that can be conveyed in the presence/lack
of instance ID, though that's a bit more subtle and error prone.

https://codereview.appspot.com/13169043/

Revision history for this message
Go Bot (go-bot) wrote :

Attempt to merge into lp:juju-core failed due to conflicts:

text conflict in cmd/jujud/machine_test.go

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'cmd/jujud/machine_test.go'
2--- cmd/jujud/machine_test.go 2013-08-22 23:29:46 +0000
3+++ cmd/jujud/machine_test.go 2013-08-23 07:58:33 +0000
4@@ -13,11 +13,9 @@
5 "launchpad.net/juju-core/agent"
6 "launchpad.net/juju-core/charm"
7 "launchpad.net/juju-core/cmd"
8- "launchpad.net/juju-core/constraints"
9 "launchpad.net/juju-core/container/lxc"
10 envtesting "launchpad.net/juju-core/environs/testing"
11 "launchpad.net/juju-core/errors"
12- "launchpad.net/juju-core/instance"
13 "launchpad.net/juju-core/names"
14 "launchpad.net/juju-core/provider/dummy"
15 "launchpad.net/juju-core/state"
16@@ -68,7 +66,12 @@
17 // machine agent's directory. It returns the new machine, the
18 // agent's configuration and the tools currently running.
19 func (s *MachineSuite) primeAgent(c *gc.C, jobs ...state.MachineJob) (m *state.Machine, config agent.Config, tools *tools.Tools) {
20- m, err := s.State.InjectMachine("series", constraints.Value{}, "ardbeg-0", instance.HardwareCharacteristics{}, jobs...)
21+ m, err := s.State.InjectMachine(&state.AddMachineParams{
22+ Series: "series",
23+ InstanceId: "ardbeg-0",
24+ Nonce: state.BootstrapNonce,
25+ Jobs: jobs,
26+ })
27 c.Assert(err, gc.IsNil)
28 err = m.SetMongoPassword(initialMachinePassword)
29 c.Assert(err, gc.IsNil)
30
31=== modified file 'environs/bootstrap/bootstrap.go'
32--- environs/bootstrap/bootstrap.go 2013-08-22 23:39:47 +0000
33+++ environs/bootstrap/bootstrap.go 2013-08-23 07:58:33 +0000
34@@ -108,7 +108,14 @@
35 }
36
37 logger.Debugf("create bootstrap machine in state")
38- m, err := st.InjectMachine(version.Current.Series, cons, instId, characteristics, jobs...)
39+ m, err := st.InjectMachine(&state.AddMachineParams{
40+ Series: version.Current.Series,
41+ Nonce: state.BootstrapNonce,
42+ Constraints: cons,
43+ InstanceId: instId,
44+ HardwareCharacteristics: characteristics,
45+ Jobs: jobs,
46+ })
47 if err != nil {
48 return err
49 }
50
51=== modified file 'state/state.go'
52--- state/state.go 2013-08-22 22:24:34 +0000
53+++ state/state.go 2013-08-23 07:58:33 +0000
54@@ -170,6 +170,12 @@
55 // supplied series. The machine's constraints and other configuration will be taken from
56 // the supplied params struct.
57 func (st *State) AddMachineWithConstraints(params *AddMachineParams) (m *Machine, err error) {
58+ if params.InstanceId != "" {
59+ return nil, fmt.Errorf("cannot specify an instance id when adding a new machine")
60+ }
61+ if params.Nonce != "" {
62+ return nil, fmt.Errorf("cannot specify a nonce when adding a new machine")
63+ }
64
65 // TODO(wallyworld) - if a container is required, and when the actual machine characteristics
66 // are made available, we need to check the machine constraints to ensure the container can be
67@@ -180,20 +186,15 @@
68 }
69
70 // InjectMachine adds a new machine, corresponding to an existing provider
71-// instance, configured to run the supplied jobs on the supplied series, using
72-// the specified constraints.
73-func (st *State) InjectMachine(series string, cons constraints.Value, instanceId instance.Id, hc instance.HardwareCharacteristics, jobs ...MachineJob) (m *Machine, err error) {
74- if instanceId == "" {
75+// instance, configured according to the supplied params struct.
76+func (st *State) InjectMachine(params *AddMachineParams) (m *Machine, err error) {
77+ if params.InstanceId == "" {
78 return nil, fmt.Errorf("cannot inject a machine without an instance id")
79 }
80- return st.addMachine(&AddMachineParams{
81- Series: series,
82- Constraints: cons,
83- instanceId: instanceId,
84- characteristics: hc,
85- nonce: BootstrapNonce,
86- Jobs: jobs,
87- })
88+ if params.Nonce == "" {
89+ return nil, fmt.Errorf("cannot inject a machine without a nonce")
90+ }
91+ return st.addMachine(params)
92 }
93
94 // containerRefParams specify how a machineContainers document is to be created.
95@@ -273,14 +274,14 @@
96
97 // AddMachineParams encapsulates the parameters used to create a new machine.
98 type AddMachineParams struct {
99- Series string
100- Constraints constraints.Value
101- ParentId string
102- ContainerType instance.ContainerType
103- instanceId instance.Id
104- characteristics instance.HardwareCharacteristics
105- nonce string
106- Jobs []MachineJob
107+ Series string
108+ Constraints constraints.Value
109+ ParentId string
110+ ContainerType instance.ContainerType
111+ InstanceId instance.Id
112+ HardwareCharacteristics instance.HardwareCharacteristics
113+ Nonce string
114+ Jobs []MachineJob
115 }
116
117 // addMachineContainerOps returns txn operations and associated Mongo records used to create a new machine,
118@@ -291,14 +292,14 @@
119 // 2. AssignToNewMachine, which is used to create a new machine on which to deploy a unit.
120 func (st *State) addMachineContainerOps(params *AddMachineParams, cons constraints.Value) ([]txn.Op, *instanceData, *containerRefParams, error) {
121 var instData *instanceData
122- if params.instanceId != "" {
123+ if params.InstanceId != "" {
124 instData = &instanceData{
125- InstanceId: params.instanceId,
126- Arch: params.characteristics.Arch,
127- Mem: params.characteristics.Mem,
128- RootDisk: params.characteristics.RootDisk,
129- CpuCores: params.characteristics.CpuCores,
130- CpuPower: params.characteristics.CpuPower,
131+ InstanceId: params.InstanceId,
132+ Arch: params.HardwareCharacteristics.Arch,
133+ Mem: params.HardwareCharacteristics.Mem,
134+ RootDisk: params.HardwareCharacteristics.RootDisk,
135+ CpuCores: params.HardwareCharacteristics.CpuCores,
136+ CpuPower: params.HardwareCharacteristics.CpuPower,
137 }
138 }
139 var ops []txn.Op
140@@ -356,8 +357,8 @@
141 Clean: true,
142 }
143 if mdoc.ContainerType == "" {
144- mdoc.InstanceId = params.instanceId
145- mdoc.Nonce = params.nonce
146+ mdoc.InstanceId = params.InstanceId
147+ mdoc.Nonce = params.Nonce
148 }
149 mdoc, machineOps, err := st.addMachineOps(mdoc, instData, cons, containerParams)
150 if err != nil {
151
152=== modified file 'state/state_test.go'
153--- state/state_test.go 2013-08-22 21:59:32 +0000
154+++ state/state_test.go 2013-08-23 07:58:33 +0000
155@@ -279,7 +279,15 @@
156 Constraints: cons,
157 Jobs: oneJob,
158 }
159+ params.InstanceId = "id"
160 m, err := s.State.AddMachineWithConstraints(&params)
161+ c.Assert(err, gc.ErrorMatches, "cannot specify an instance id when adding a new machine")
162+ params.InstanceId = ""
163+ params.Nonce = "nonce"
164+ m, err = s.State.AddMachineWithConstraints(&params)
165+ c.Assert(err, gc.ErrorMatches, "cannot specify a nonce when adding a new machine")
166+ params.Nonce = ""
167+ m, err = s.State.AddMachineWithConstraints(&params)
168 c.Assert(err, gc.IsNil)
169 c.Assert(m.Id(), gc.Equals, "0/lxc/0")
170 c.Assert(m.Series(), gc.Equals, "series")
171@@ -307,12 +315,23 @@
172 }
173
174 func (s *StateSuite) TestInjectMachineErrors(c *gc.C) {
175- hc := instance.HardwareCharacteristics{}
176- _, err := s.State.InjectMachine("", emptyCons, instance.Id("i-minvalid"), hc, state.JobHostUnits)
177+ injectMachine := func(series, instanceId, nonce string, jobs ...state.MachineJob) error {
178+ params := &state.AddMachineParams{
179+ InstanceId: instance.Id(instanceId),
180+ Series: series,
181+ Nonce: nonce,
182+ Jobs: jobs,
183+ }
184+ _, err := s.State.InjectMachine(params)
185+ return err
186+ }
187+ err := injectMachine("", "i-minvalid", state.BootstrapNonce, state.JobHostUnits)
188 c.Assert(err, gc.ErrorMatches, "cannot add a new machine: no series specified")
189- _, err = s.State.InjectMachine("series", emptyCons, instance.Id(""), hc, state.JobHostUnits)
190+ err = injectMachine("series", "", state.BootstrapNonce, state.JobHostUnits)
191 c.Assert(err, gc.ErrorMatches, "cannot inject a machine without an instance id")
192- _, err = s.State.InjectMachine("series", emptyCons, instance.Id("i-mlazy"), hc)
193+ err = injectMachine("series", "i-minvalid", "", state.JobHostUnits)
194+ c.Assert(err, gc.ErrorMatches, "cannot inject a machine without a nonce")
195+ err = injectMachine("series", state.BootstrapNonce, "i-mlazy")
196 c.Assert(err, gc.ErrorMatches, "cannot add a new machine: no jobs specified")
197 }
198
199@@ -321,23 +340,30 @@
200 arch := "amd64"
201 mem := uint64(1024)
202 disk := uint64(1024)
203- hc := instance.HardwareCharacteristics{
204- Arch: &arch,
205- Mem: &mem,
206- RootDisk: &disk,
207+ params := &state.AddMachineParams{
208+ Series: "series",
209+ Constraints: cons,
210+ InstanceId: instance.Id("i-mindustrious"),
211+ Nonce: state.BootstrapNonce,
212+ HardwareCharacteristics: instance.HardwareCharacteristics{
213+ Arch: &arch,
214+ Mem: &mem,
215+ RootDisk: &disk,
216+ },
217+ Jobs: []state.MachineJob{state.JobHostUnits, state.JobManageEnviron},
218 }
219- m, err := s.State.InjectMachine("series", cons, instance.Id("i-mindustrious"), hc, state.JobHostUnits, state.JobManageEnviron)
220+ m, err := s.State.InjectMachine(params)
221 c.Assert(err, gc.IsNil)
222- c.Assert(m.Jobs(), gc.DeepEquals, []state.MachineJob{state.JobHostUnits, state.JobManageEnviron})
223+ c.Assert(m.Jobs(), gc.DeepEquals, params.Jobs)
224 instanceId, err := m.InstanceId()
225 c.Assert(err, gc.IsNil)
226- c.Assert(instanceId, gc.Equals, instance.Id("i-mindustrious"))
227+ c.Assert(instanceId, gc.Equals, params.InstanceId)
228 mcons, err := m.Constraints()
229 c.Assert(err, gc.IsNil)
230 c.Assert(cons, gc.DeepEquals, mcons)
231 characteristics, err := m.HardwareCharacteristics()
232 c.Assert(err, gc.IsNil)
233- c.Assert(*characteristics, gc.DeepEquals, hc)
234+ c.Assert(*characteristics, gc.DeepEquals, params.HardwareCharacteristics)
235
236 // Make sure the bootstrap nonce value is set.
237 c.Assert(m.CheckProvisioned(state.BootstrapNonce), gc.Equals, true)
238@@ -345,12 +371,17 @@
239
240 func (s *StateSuite) TestAddContainerToInjectedMachine(c *gc.C) {
241 oneJob := []state.MachineJob{state.JobHostUnits}
242- hc := instance.HardwareCharacteristics{}
243- m0, err := s.State.InjectMachine("series", emptyCons, instance.Id("i-mindustrious"), hc, state.JobHostUnits, state.JobManageEnviron)
244+ params := state.AddMachineParams{
245+ Series: "series",
246+ InstanceId: instance.Id("i-mindustrious"),
247+ Nonce: state.BootstrapNonce,
248+ Jobs: []state.MachineJob{state.JobHostUnits, state.JobManageEnviron},
249+ }
250+ m0, err := s.State.InjectMachine(&params)
251 c.Assert(err, gc.IsNil)
252
253 // Add first container.
254- params := state.AddMachineParams{
255+ params = state.AddMachineParams{
256 ParentId: "0",
257 ContainerType: instance.LXC,
258 Series: "series",

Subscribers

People subscribed via source and target branches

to status/vote changes: