/
instanceidgetter.go
70 lines (63 loc) · 1.82 KB
/
instanceidgetter.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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package common
import (
"github.com/juju/names"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/instance"
"github.com/juju/juju/state"
)
// InstanceIdGetter implements a common InstanceId method for use by
// various facades.
type InstanceIdGetter struct {
st state.EntityFinder
getCanRead GetAuthFunc
}
// NewInstanceIdGetter returns a new InstanceIdGetter. The GetAuthFunc
// will be used on each invocation of InstanceId to determine current
// permissions.
func NewInstanceIdGetter(st state.EntityFinder, getCanRead GetAuthFunc) *InstanceIdGetter {
return &InstanceIdGetter{
st: st,
getCanRead: getCanRead,
}
}
func (ig *InstanceIdGetter) getInstanceId(tag names.Tag) (instance.Id, error) {
entity0, err := ig.st.FindEntity(tag)
if err != nil {
return "", err
}
entity, ok := entity0.(state.InstanceIdGetter)
if !ok {
return "", NotSupportedError(tag, "instance id")
}
return entity.InstanceId()
}
// InstanceId returns the provider specific instance id for each given
// machine or an CodeNotProvisioned error, if not set.
func (ig *InstanceIdGetter) InstanceId(args params.Entities) (params.StringResults, error) {
result := params.StringResults{
Results: make([]params.StringResult, len(args.Entities)),
}
canRead, err := ig.getCanRead()
if err != nil {
return result, err
}
for i, entity := range args.Entities {
tag, err := names.ParseTag(entity.Tag)
if err != nil {
result.Results[i].Error = ServerError(ErrPerm)
continue
}
err = ErrPerm
if canRead(tag) {
var instanceId instance.Id
instanceId, err = ig.getInstanceId(tag)
if err == nil {
result.Results[i].Result = string(instanceId)
}
}
result.Results[i].Error = ServerError(err)
}
return result, nil
}