/
undertaker.go
121 lines (110 loc) · 3.86 KB
/
undertaker.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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package machineundertaker
import (
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/juju/api/base"
"github.com/juju/juju/core/network"
"github.com/juju/juju/core/watcher"
"github.com/juju/juju/rpc/params"
)
// NewWatcherFunc exists to let us test WatchMachineRemovals.
type NewWatcherFunc func(base.APICaller, params.NotifyWatchResult) watcher.NotifyWatcher
// API provides access to the machine undertaker API facade.
type API struct {
facade base.FacadeCaller
modelTag names.ModelTag
newWatcher NewWatcherFunc
}
// NewAPI creates a new client-side machine undertaker facade.
func NewAPI(caller base.APICaller, newWatcher NewWatcherFunc) (*API, error) {
modelTag, ok := caller.ModelTag()
if !ok {
return nil, errors.New("machine undertaker client requires a model API connection")
}
api := API{
facade: base.NewFacadeCaller(caller, "MachineUndertaker"),
modelTag: modelTag,
newWatcher: newWatcher,
}
return &api, nil
}
// AllMachineRemovals returns all the machines that have been marked
// ready to clean up.
func (api *API) AllMachineRemovals() ([]names.MachineTag, error) {
var results params.EntitiesResults
args := wrapEntities(api.modelTag)
err := api.facade.FacadeCall("AllMachineRemovals", &args, &results)
if err != nil {
return nil, errors.Trace(err)
}
if len(results.Results) != 1 {
return nil, errors.Errorf("expected one result, got %d", len(results.Results))
}
result := results.Results[0]
if result.Error != nil {
return nil, errors.Trace(result.Error)
}
machines := make([]names.MachineTag, len(result.Entities))
for i, entity := range result.Entities {
tag, err := names.ParseMachineTag(entity.Tag)
if err != nil {
return nil, errors.Trace(err)
}
machines[i] = tag
}
return machines, nil
}
// GetProviderInterfaceInfo gets the provider details for all of the
// interfaces for one machine.
func (api *API) GetProviderInterfaceInfo(machine names.MachineTag) ([]network.ProviderInterfaceInfo, error) {
var result params.ProviderInterfaceInfoResults
args := wrapEntities(machine)
err := api.facade.FacadeCall("GetMachineProviderInterfaceInfo", &args, &result)
if err != nil {
return nil, errors.Trace(err)
}
if len(result.Results) != 1 {
return nil, errors.Errorf("expected one result, got %d", len(result.Results))
}
item := result.Results[0]
if item.MachineTag != machine.String() {
return nil, errors.Errorf("expected interface info for %s but got %s", machine, item.MachineTag)
}
infos := make([]network.ProviderInterfaceInfo, len(item.Interfaces))
for i, info := range item.Interfaces {
infos[i].InterfaceName = info.InterfaceName
infos[i].HardwareAddress = info.MACAddress
infos[i].ProviderId = network.Id(info.ProviderId)
}
return infos, nil
}
// CompleteRemoval finishes the removal of the machine in the database
// after any provider resources are cleaned up.
func (api *API) CompleteRemoval(machine names.MachineTag) error {
args := wrapEntities(machine)
return api.facade.FacadeCall("CompleteMachineRemovals", &args, nil)
}
// WatchMachineRemovals registers to be notified when a machine
// removal is requested.
func (api *API) WatchMachineRemovals() (watcher.NotifyWatcher, error) {
var results params.NotifyWatchResults
args := wrapEntities(api.modelTag)
err := api.facade.FacadeCall("WatchMachineRemovals", &args, &results)
if err != nil {
return nil, errors.Trace(err)
}
if len(results.Results) != 1 {
return nil, errors.Errorf("expected one result, got %d", len(results.Results))
}
result := results.Results[0]
if err := result.Error; err != nil {
return nil, errors.Trace(result.Error)
}
w := api.newWatcher(api.facade.RawAPICaller(), result)
return w, nil
}
func wrapEntities(tag names.Tag) params.Entities {
return params.Entities{Entities: []params.Entity{{Tag: tag.String()}}}
}