~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/apiserver/common/machine.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2015 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package common
 
5
 
 
6
import (
 
7
        "github.com/juju/errors"
 
8
 
 
9
        "github.com/juju/juju/state"
 
10
        "github.com/juju/juju/state/multiwatcher"
 
11
)
 
12
 
 
13
// StateJobs translates a slice of multiwatcher jobs to their equivalents in state.
 
14
func StateJobs(jobs []multiwatcher.MachineJob) ([]state.MachineJob, error) {
 
15
        newJobs := make([]state.MachineJob, len(jobs))
 
16
        for i, job := range jobs {
 
17
                newJob, err := machineJobFromParams(job)
 
18
                if err != nil {
 
19
                        return nil, err
 
20
                }
 
21
                newJobs[i] = newJob
 
22
        }
 
23
        return newJobs, nil
 
24
}
 
25
 
 
26
// machineJobFromParams returns the job corresponding to multiwatcher.MachineJob.
 
27
func machineJobFromParams(job multiwatcher.MachineJob) (state.MachineJob, error) {
 
28
        switch job {
 
29
        case multiwatcher.JobHostUnits:
 
30
                return state.JobHostUnits, nil
 
31
        case multiwatcher.JobManageModel:
 
32
                return state.JobManageModel, nil
 
33
        default:
 
34
                return -1, errors.Errorf("invalid machine job %q", job)
 
35
        }
 
36
}
 
37
 
 
38
type origStateInterface interface {
 
39
        Machine(string) (*state.Machine, error)
 
40
}
 
41
 
 
42
type stateInterface interface {
 
43
        Machine(string) (Machine, error)
 
44
}
 
45
 
 
46
type stateShim struct {
 
47
        origStateInterface
 
48
}
 
49
 
 
50
func (st *stateShim) Machine(id string) (Machine, error) {
 
51
        return st.origStateInterface.Machine(id)
 
52
}
 
53
 
 
54
type Machine interface {
 
55
        Life() state.Life
 
56
        ForceDestroy() error
 
57
        Destroy() error
 
58
}
 
59
 
 
60
func DestroyMachines(st origStateInterface, force bool, ids ...string) error {
 
61
        return destroyMachines(&stateShim{st}, force, ids...)
 
62
}
 
63
 
 
64
func destroyMachines(st stateInterface, force bool, ids ...string) error {
 
65
        var errs []string
 
66
        for _, id := range ids {
 
67
                machine, err := st.Machine(id)
 
68
                switch {
 
69
                case errors.IsNotFound(err):
 
70
                        err = errors.Errorf("machine %s does not exist", id)
 
71
                case err != nil:
 
72
                case force:
 
73
                        err = machine.ForceDestroy()
 
74
                case machine.Life() != state.Alive:
 
75
                        continue
 
76
                default:
 
77
                        err = machine.Destroy()
 
78
                }
 
79
                if err != nil {
 
80
                        errs = append(errs, err.Error())
 
81
                }
 
82
        }
 
83
        return DestroyErr("machines", ids, errs)
 
84
}