~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/provider/joyent/config.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
import (
7
7
        "fmt"
8
 
        "io/ioutil"
9
8
        "net/url"
10
9
        "os"
11
10
        "strings"
12
11
 
13
12
        "github.com/juju/errors"
14
13
        "github.com/juju/schema"
15
 
        "github.com/juju/utils"
16
14
 
17
15
        "github.com/juju/juju/environs/config"
18
16
)
19
17
 
20
18
const (
21
 
        SdcAccount          = "SDC_ACCOUNT"
22
 
        SdcKeyId            = "SDC_KEY_ID"
23
 
        SdcUrl              = "SDC_URL"
24
 
        MantaUser           = "MANTA_USER"
25
 
        MantaKeyId          = "MANTA_KEY_ID"
26
 
        MantaUrl            = "MANTA_URL"
27
 
        MantaPrivateKeyFile = "MANTA_PRIVATE_KEY_FILE"
 
19
        SdcAccount = "SDC_ACCOUNT"
 
20
        SdcKeyId   = "SDC_KEY_ID"
 
21
        SdcUrl     = "SDC_URL"
28
22
 
29
23
        sdcUser        = "sdc-user"
30
24
        sdcKeyId       = "sdc-key-id"
31
25
        sdcUrl         = "sdc-url"
32
 
        mantaUser      = "manta-user"
33
 
        mantaKeyId     = "manta-key-id"
34
 
        mantaUrl       = "manta-url"
35
26
        privateKeyPath = "private-key-path"
36
27
        algorithm      = "algorithm"
37
 
        controlDir     = "control-dir"
38
28
        privateKey     = "private-key"
39
29
)
40
30
 
41
31
var environmentVariables = map[string]string{
42
 
        sdcUser:        SdcAccount,
43
 
        sdcKeyId:       SdcKeyId,
44
 
        sdcUrl:         SdcUrl,
45
 
        mantaUser:      MantaUser,
46
 
        mantaKeyId:     MantaKeyId,
47
 
        mantaUrl:       MantaUrl,
48
 
        privateKeyPath: MantaPrivateKeyFile,
 
32
        sdcUser:  SdcAccount,
 
33
        sdcKeyId: SdcKeyId,
 
34
        sdcUrl:   SdcUrl,
49
35
}
50
36
 
51
37
var configFields = schema.Fields{
52
 
        sdcUser:        schema.String(),
53
 
        sdcKeyId:       schema.String(),
54
 
        sdcUrl:         schema.String(),
55
 
        mantaUser:      schema.String(),
56
 
        mantaKeyId:     schema.String(),
57
 
        mantaUrl:       schema.String(),
58
 
        privateKeyPath: schema.String(),
59
 
        algorithm:      schema.String(),
60
 
        controlDir:     schema.String(),
61
 
        privateKey:     schema.String(),
 
38
        sdcUser:    schema.String(),
 
39
        sdcKeyId:   schema.String(),
 
40
        sdcUrl:     schema.String(),
 
41
        algorithm:  schema.String(),
 
42
        privateKey: schema.String(),
62
43
}
63
44
 
64
45
var configDefaults = schema.Defaults{
65
 
        sdcUrl:         "https://us-west-1.api.joyentcloud.com",
66
 
        mantaUrl:       "https://us-east.manta.joyent.com",
67
 
        algorithm:      "rsa-sha256",
68
 
        privateKeyPath: schema.Omit,
69
 
        sdcUser:        schema.Omit,
70
 
        sdcKeyId:       schema.Omit,
71
 
        mantaUser:      schema.Omit,
72
 
        mantaKeyId:     schema.Omit,
73
 
        privateKey:     schema.Omit,
 
46
        sdcUrl:     "https://us-west-1.api.joyentcloud.com",
 
47
        algorithm:  "rsa-sha256",
 
48
        sdcUser:    schema.Omit,
 
49
        sdcKeyId:   schema.Omit,
 
50
        privateKey: schema.Omit,
74
51
}
75
52
 
76
53
var requiredFields = []string{
77
54
        sdcUrl,
78
 
        mantaUrl,
79
55
        algorithm,
80
56
        sdcUser,
81
57
        sdcKeyId,
82
 
        mantaUser,
83
 
        mantaKeyId,
84
58
        // privatekey and privatekeypath are handled separately
85
59
}
86
60
 
87
61
var configSecretFields = []string{
88
62
        sdcUser,
89
63
        sdcKeyId,
90
 
        mantaUser,
91
 
        mantaKeyId,
92
64
        privateKey,
93
65
}
94
66
 
95
67
var configImmutableFields = []string{
96
68
        sdcUrl,
97
 
        mantaUrl,
98
 
        privateKeyPath,
99
69
        privateKey,
100
70
        algorithm,
101
71
}
135
105
                        if localEnvVariable != "" {
136
106
                                envConfig.attrs[field] = localEnvVariable
137
107
                        } else {
138
 
                                if field != privateKeyPath {
139
 
                                        return nil, fmt.Errorf("cannot get %s value from model variable %s", field, envVar)
140
 
                                }
 
108
                                return nil, fmt.Errorf("cannot get %s value from model variable %s", field, envVar)
141
109
                        }
142
110
                }
143
111
        }
144
112
 
145
 
        if err := ensurePrivateKeyOrPath(envConfig); err != nil {
 
113
        if err := ensurePrivateKey(envConfig); err != nil {
146
114
                return nil, err
147
115
        }
148
116
 
149
 
        // Now that we've ensured private-key-path is properly set, we go back and set
150
 
        // up the private key - this is used to sign requests.
151
 
        if nilOrEmptyString(envConfig.attrs[privateKey]) {
152
 
                keyFile, err := utils.NormalizePath(envConfig.attrs[privateKeyPath].(string))
153
 
                if err != nil {
154
 
                        return nil, err
155
 
                }
156
 
                priv, err := ioutil.ReadFile(keyFile)
157
 
                if err != nil {
158
 
                        return nil, err
159
 
                }
160
 
                envConfig.attrs[privateKey] = string(priv)
161
 
        }
162
 
 
163
117
        // Check for missing fields.
164
118
        for _, field := range requiredFields {
165
119
                if nilOrEmptyString(envConfig.attrs[field]) {
169
123
        return envConfig, nil
170
124
}
171
125
 
172
 
// Ensure private-key-path is set.
173
 
func ensurePrivateKeyOrPath(envConfig *environConfig) error {
174
 
        if !nilOrEmptyString(envConfig.attrs[privateKeyPath]) {
175
 
                return nil
176
 
        }
177
 
        if path := os.Getenv(environmentVariables[privateKeyPath]); path != "" {
178
 
                envConfig.attrs[privateKeyPath] = path
179
 
                return nil
180
 
        }
 
126
// Ensure private-key is set.
 
127
func ensurePrivateKey(envConfig *environConfig) error {
181
128
        if !nilOrEmptyString(envConfig.attrs[privateKey]) {
182
129
                return nil
183
130
        }
184
 
 
185
131
        return errors.New("no ssh private key specified in joyent configuration")
186
132
}
187
133
 
206
152
        return ecfg.attrs[sdcKeyId].(string)
207
153
}
208
154
 
209
 
func (ecfg *environConfig) mantaUrl() string {
210
 
        return ecfg.attrs[mantaUrl].(string)
211
 
}
212
 
 
213
 
func (ecfg *environConfig) mantaUser() string {
214
 
        return ecfg.attrs[mantaUser].(string)
215
 
}
216
 
 
217
 
func (ecfg *environConfig) mantaKeyId() string {
218
 
        return ecfg.attrs[mantaKeyId].(string)
219
 
}
220
 
 
221
155
func (ecfg *environConfig) privateKey() string {
222
156
        if v, ok := ecfg.attrs[privateKey]; ok {
223
157
                return v.(string)
229
163
        return ecfg.attrs[algorithm].(string)
230
164
}
231
165
 
232
 
func (c *environConfig) controlDir() string {
233
 
        return c.attrs[controlDir].(string)
234
 
}
235
 
 
236
 
func (c *environConfig) ControlDir() string {
237
 
        return c.controlDir()
238
 
}
239
 
 
240
166
func (ecfg *environConfig) SdcUrl() string {
241
167
        return ecfg.sdcUrl()
242
168
}