~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/state/stateenvirons/config.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 2016 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package stateenvirons
 
5
 
 
6
import (
 
7
        "github.com/juju/errors"
 
8
        "gopkg.in/juju/names.v2"
 
9
 
 
10
        "github.com/juju/juju/cloud"
 
11
        "github.com/juju/juju/environs"
 
12
        "github.com/juju/juju/state"
 
13
)
 
14
 
 
15
// EnvironConfigGetter implements environs.EnvironConfigGetter
 
16
// in terms of a *state.State.
 
17
type EnvironConfigGetter struct {
 
18
        *state.State
 
19
}
 
20
 
 
21
// CloudSpec implements environs.EnvironConfigGetter.
 
22
func (g EnvironConfigGetter) CloudSpec(tag names.ModelTag) (environs.CloudSpec, error) {
 
23
        model, err := g.GetModel(tag)
 
24
        if err != nil {
 
25
                return environs.CloudSpec{}, errors.Trace(err)
 
26
        }
 
27
        cloudName := model.Cloud()
 
28
        regionName := model.CloudRegion()
 
29
        credentialName := model.CloudCredential()
 
30
        modelOwner := model.Owner()
 
31
        return CloudSpec(g.State, cloudName, regionName, credentialName, modelOwner)
 
32
}
 
33
 
 
34
// CloudSpec returns an environs.CloudSpec from a *state.State,
 
35
// given the cloud, region and credential names.
 
36
func CloudSpec(
 
37
        accessor state.CloudAccessor,
 
38
        cloudName, regionName, credentialName string,
 
39
        credentialOwner names.UserTag,
 
40
) (environs.CloudSpec, error) {
 
41
        modelCloud, err := accessor.Cloud(cloudName)
 
42
        if err != nil {
 
43
                return environs.CloudSpec{}, errors.Trace(err)
 
44
        }
 
45
 
 
46
        var credential *cloud.Credential
 
47
        if credentialName != "" {
 
48
                credentials, err := accessor.CloudCredentials(credentialOwner, cloudName)
 
49
                if err != nil {
 
50
                        return environs.CloudSpec{}, errors.Trace(err)
 
51
                }
 
52
                var ok bool
 
53
                credentialValue, ok := credentials[credentialName]
 
54
                if !ok {
 
55
                        return environs.CloudSpec{}, errors.NotFoundf("credential %q", credentialName)
 
56
                }
 
57
                credential = &credentialValue
 
58
        }
 
59
 
 
60
        return environs.MakeCloudSpec(modelCloud, cloudName, regionName, credential)
 
61
}
 
62
 
 
63
// NewEnvironFunc defines the type of a function that, given a state.State,
 
64
// returns a new Environ.
 
65
type NewEnvironFunc func(*state.State) (environs.Environ, error)
 
66
 
 
67
// GetNewEnvironFunc returns a NewEnvironFunc, that constructs Environs
 
68
// using the given environs.NewEnvironFunc.
 
69
func GetNewEnvironFunc(newEnviron environs.NewEnvironFunc) NewEnvironFunc {
 
70
        return func(st *state.State) (environs.Environ, error) {
 
71
                g := EnvironConfigGetter{st}
 
72
                return environs.GetEnviron(g, newEnviron)
 
73
        }
 
74
}