~juju-qa/ubuntu/yakkety/juju/2.0-rc3-again

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/environs/open.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-04-24 22:34:47 UTC
  • Revision ID: package-import@ubuntu.com-20130424223447-f0qdji7ubnyo0s71
Tags: upstream-1.10.0.1
ImportĀ upstreamĀ versionĀ 1.10.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package environs
 
2
 
 
3
import (
 
4
        "fmt"
 
5
        "launchpad.net/juju-core/environs/config"
 
6
)
 
7
 
 
8
// Open creates a new Environ using the environment configuration with the
 
9
// given name. If name is empty, the default environment will be used.
 
10
func (envs *Environs) Open(name string) (Environ, error) {
 
11
        if name == "" {
 
12
                name = envs.Default
 
13
                if name == "" {
 
14
                        return nil, fmt.Errorf("no default environment found")
 
15
                }
 
16
        }
 
17
        e, ok := envs.environs[name]
 
18
        if !ok {
 
19
                return nil, fmt.Errorf("unknown environment %q", name)
 
20
        }
 
21
        if e.err != nil {
 
22
                return nil, e.err
 
23
        }
 
24
        return New(e.config)
 
25
}
 
26
 
 
27
// NewFromName opens the environment with the given
 
28
// name from the default environments file. If the
 
29
// name is blank, the default environment will be used.
 
30
func NewFromName(name string) (Environ, error) {
 
31
        environs, err := ReadEnvirons("")
 
32
        if err != nil {
 
33
                return nil, err
 
34
        }
 
35
        return environs.Open(name)
 
36
}
 
37
 
 
38
// NewFromAttrs returns a new environment based on the provided configuration
 
39
// attributes.
 
40
func NewFromAttrs(attrs map[string]interface{}) (Environ, error) {
 
41
        cfg, err := config.New(attrs)
 
42
        if err != nil {
 
43
                return nil, err
 
44
        }
 
45
        return New(cfg)
 
46
}
 
47
 
 
48
// New returns a new environment based on the provided configuration.
 
49
func New(config *config.Config) (Environ, error) {
 
50
        p, err := Provider(config.Type())
 
51
        if err != nil {
 
52
                return nil, err
 
53
        }
 
54
        return p.Open(config)
 
55
}