1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
"launchpad.net/juju-core/instance"
8
"launchpad.net/juju-core/state"
9
"launchpad.net/juju-core/state/api/params"
12
// InstanceIdGetter implements a common InstanceId method for use by
14
type InstanceIdGetter struct {
16
getCanRead GetAuthFunc
19
// NewInstanceIdGetter returns a new InstanceIdGetter. The GetAuthFunc
20
// will be used on each invocation of InstanceId to determine current
22
func NewInstanceIdGetter(st state.EntityFinder, getCanRead GetAuthFunc) *InstanceIdGetter {
23
return &InstanceIdGetter{
25
getCanRead: getCanRead,
29
func (ig *InstanceIdGetter) getInstanceId(tag string) (instance.Id, error) {
30
entity0, err := ig.st.FindEntity(tag)
34
entity, ok := entity0.(state.InstanceIdGetter)
36
return "", NotSupportedError(tag, "instance id")
38
return entity.InstanceId()
41
// InstanceId returns the provider specific instance id for each given
42
// machine or an CodeNotProvisioned error, if not set.
43
func (ig *InstanceIdGetter) InstanceId(args params.Entities) (params.StringResults, error) {
44
result := params.StringResults{
45
Results: make([]params.StringResult, len(args.Entities)),
47
canRead, err := ig.getCanRead()
51
for i, entity := range args.Entities {
53
if canRead(entity.Tag) {
54
var instanceId instance.Id
55
instanceId, err = ig.getInstanceId(entity.Tag)
57
result.Results[i].Result = string(instanceId)
60
result.Results[i].Error = ServerError(err)