~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/provider/cloudsigma/environ.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 2015 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package cloudsigma
 
5
 
 
6
import (
 
7
        "sync"
 
8
 
 
9
        "github.com/altoros/gosigma"
 
10
        "github.com/juju/errors"
 
11
        "github.com/juju/utils/arch"
 
12
 
 
13
        "github.com/juju/juju/constraints"
 
14
        "github.com/juju/juju/environs"
 
15
        "github.com/juju/juju/environs/config"
 
16
        "github.com/juju/juju/environs/simplestreams"
 
17
        "github.com/juju/juju/instance"
 
18
        "github.com/juju/juju/provider/common"
 
19
)
 
20
 
 
21
const (
 
22
        CloudsigmaCloudImagesURLTemplate = "https://%v.cloudsigma.com/"
 
23
)
 
24
 
 
25
// This file contains the core of the Environ implementation.
 
26
type environ struct {
 
27
        name   string
 
28
        cloud  environs.CloudSpec
 
29
        client *environClient
 
30
 
 
31
        lock      sync.Mutex
 
32
        archMutex sync.Mutex
 
33
 
 
34
        ecfg                   *environConfig
 
35
        supportedArchitectures []string
 
36
}
 
37
 
 
38
// Name returns the Environ's name.
 
39
func (env *environ) Name() string {
 
40
        return env.name
 
41
}
 
42
 
 
43
// Provider returns the EnvironProvider that created this Environ.
 
44
func (*environ) Provider() environs.EnvironProvider {
 
45
        return providerInstance
 
46
}
 
47
 
 
48
// SetConfig updates the Environ's configuration.
 
49
//
 
50
// Calls to SetConfig do not affect the configuration of values previously obtained
 
51
// from Storage.
 
52
func (env *environ) SetConfig(cfg *config.Config) error {
 
53
        env.lock.Lock()
 
54
        defer env.lock.Unlock()
 
55
 
 
56
        ecfg, err := validateConfig(cfg, env.ecfg)
 
57
        if err != nil {
 
58
                return errors.Trace(err)
 
59
        }
 
60
        env.ecfg = ecfg
 
61
 
 
62
        return nil
 
63
}
 
64
 
 
65
// Config returns the configuration data with which the Environ was created.
 
66
// Note that this is not necessarily current; the canonical location
 
67
// for the configuration data is stored in the state.
 
68
func (env *environ) Config() *config.Config {
 
69
        return env.ecfg.Config
 
70
}
 
71
 
 
72
// PrepareForBootstrap is part of the Environ interface.
 
73
func (env *environ) PrepareForBootstrap(ctx environs.BootstrapContext) error {
 
74
        logger.Infof("preparing model %q", env.name)
 
75
        return nil
 
76
}
 
77
 
 
78
// Create is part of the Environ interface.
 
79
func (env *environ) Create(environs.CreateParams) error {
 
80
        return nil
 
81
}
 
82
 
 
83
// Bootstrap initializes the state for the environment, possibly
 
84
// starting one or more instances.  If the configuration's
 
85
// AdminSecret is non-empty, the administrator password on the
 
86
// newly bootstrapped state will be set to a hash of it (see
 
87
// utils.PasswordHash), When first connecting to the
 
88
// environment via the juju package, the password hash will be
 
89
// automatically replaced by the real password.
 
90
//
 
91
// The supplied constraints are used to choose the initial instance
 
92
// specification, and will be stored in the new environment's state.
 
93
//
 
94
// Bootstrap is responsible for selecting the appropriate tools,
 
95
// and setting the agent-version configuration attribute prior to
 
96
// bootstrapping the environment.
 
97
func (env *environ) Bootstrap(ctx environs.BootstrapContext, params environs.BootstrapParams) (*environs.BootstrapResult, error) {
 
98
        return common.Bootstrap(ctx, env, params)
 
99
}
 
100
 
 
101
func (e *environ) ControllerInstances(controllerUUID string) ([]instance.Id, error) {
 
102
        return e.client.getControllerIds()
 
103
}
 
104
 
 
105
// Destroy shuts down all known machines and destroys the
 
106
// rest of the environment. Note that on some providers,
 
107
// very recently started instances may not be destroyed
 
108
// because they are not yet visible.
 
109
//
 
110
// When Destroy has been called, any Environ referring to the
 
111
// same remote environment may become invalid
 
112
func (env *environ) Destroy() error {
 
113
        // You can probably ignore this method; the common implementation should work.
 
114
        return common.Destroy(env)
 
115
}
 
116
 
 
117
// DestroyController implements the Environ interface.
 
118
func (env *environ) DestroyController(controllerUUID string) error {
 
119
        // TODO(wallyworld): destroy hosted model resources
 
120
        return env.Destroy()
 
121
}
 
122
 
 
123
// PrecheckInstance performs a preflight check on the specified
 
124
// series and constraints, ensuring that they are possibly valid for
 
125
// creating an instance in this environment.
 
126
//
 
127
// PrecheckInstance is best effort, and not guaranteed to eliminate
 
128
// all invalid parameters. If PrecheckInstance returns nil, it is not
 
129
// guaranteed that the constraints are valid; if a non-nil error is
 
130
// returned, then the constraints are definitely invalid.
 
131
func (env *environ) PrecheckInstance(series string, cons constraints.Value, placement string) error {
 
132
        return nil
 
133
}
 
134
 
 
135
// Region is specified in the HasRegion interface.
 
136
func (env *environ) Region() (simplestreams.CloudSpec, error) {
 
137
        return simplestreams.CloudSpec{
 
138
                Region:   env.cloud.Region,
 
139
                Endpoint: env.cloud.Endpoint,
 
140
        }, nil
 
141
}
 
142
 
 
143
func (env *environ) MetadataLookupParams(region string) (*simplestreams.MetadataLookupParams, error) {
 
144
        env.lock.Lock()
 
145
        defer env.lock.Unlock()
 
146
        return &simplestreams.MetadataLookupParams{
 
147
                Region:        region,
 
148
                Endpoint:      gosigma.ResolveEndpoint(region),
 
149
                Architectures: arch.AllSupportedArches,
 
150
                Series:        config.PreferredSeries(env.ecfg),
 
151
        }, nil
 
152
}