Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(120)

Side by Side Diff: environs/ec2/config.go

Issue 6343107: environs: add configuration attributes and access
Patch Set: environs: add configuration attributes and access Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « environs/dummy/environs.go ('k') | environs/ec2/config_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 package ec2 1 package ec2
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "launchpad.net/goamz/aws" 5 "launchpad.net/goamz/aws"
6 "launchpad.net/juju-core/environs" 6 "launchpad.net/juju-core/environs"
7 "launchpad.net/juju-core/environs/config"
7 "launchpad.net/juju-core/schema" 8 "launchpad.net/juju-core/schema"
8 ) 9 )
9 10
10 // providerConfig is a placeholder for any config information 11 // providerConfig is a placeholder for any config information
11 // that we will have in a configuration file. 12 // that we will have in a configuration file.
12 type providerConfig struct { 13 type providerConfig struct {
13 » name string 14 » *config.Config
14 region string 15 region string
15 auth aws.Auth 16 auth aws.Auth
16 bucket string 17 bucket string
17 publicBucket string 18 publicBucket string
18 authorizedKeys string 19 authorizedKeys string
20 defaultSeries string
19 } 21 }
20 22
21 var configChecker = schema.StrictFieldMap( 23 var configChecker = schema.StrictFieldMap(
22 schema.Fields{ 24 schema.Fields{
23 "name": schema.String(),
24 "type": schema.Const("ec2"),
25 "access-key": schema.String(), 25 "access-key": schema.String(),
26 "secret-key": schema.String(), 26 "secret-key": schema.String(),
27 "region": schema.String(), 27 "region": schema.String(),
28 "control-bucket": schema.String(), 28 "control-bucket": schema.String(),
29 "public-bucket": schema.String(), 29 "public-bucket": schema.String(),
30 "authorized-keys": schema.String(), 30 "authorized-keys": schema.String(),
31 "authorized-keys-path": schema.String(), 31 "authorized-keys-path": schema.String(),
32 "default-series": schema.String(),
32 }, []string{ 33 }, []string{
33 "access-key", 34 "access-key",
34 "secret-key", 35 "secret-key",
35 "region", 36 "region",
36 "authorized-keys", 37 "authorized-keys",
37 "authorized-keys-path", 38 "authorized-keys-path",
38 "public-bucket", 39 "public-bucket",
40 "default-series",
39 }, 41 },
40 ) 42 )
41 43
42 func (p environProvider) NewConfig(config map[string]interface{}) (cfg environs. EnvironConfig, err error) { 44 func (p environProvider) NewConfig(attrs map[string]interface{}) (environs.Envir onConfig, error) {
43 » v, err := configChecker.Coerce(config, nil) 45 » ccfg, attrs, err := config.New(attrs)
46 » if err != nil {
47 » » return nil, err
48 » }
49 » return p.newConfig(ccfg, attrs)
50 }
51
52 func (p environProvider) newConfig(ccfg *config.Config, attrs map[string]interfa ce{}) (cfg environs.EnvironConfig, err error) {
53 » if ccfg.Type() != "ec2" {
54 » » return nil, fmt.Errorf("unexpected environment type %q", ccfg.Ty pe())
55 » }
56 » v, err := configChecker.Coerce(attrs, nil)
44 if err != nil { 57 if err != nil {
45 return nil, err 58 return nil, err
46 } 59 }
47 m := v.(schema.MapType) 60 m := v.(schema.MapType)
48 var c providerConfig 61 var c providerConfig
49 62
50 » c.name = m["name"].(string) 63 » c.Config = ccfg
51 c.bucket = m["control-bucket"].(string) 64 c.bucket = m["control-bucket"].(string)
52 c.publicBucket = maybeString(m["public-bucket"], "") 65 c.publicBucket = maybeString(m["public-bucket"], "")
53 c.auth.AccessKey = maybeString(m["access-key"], "") 66 c.auth.AccessKey = maybeString(m["access-key"], "")
54 c.auth.SecretKey = maybeString(m["secret-key"], "") 67 c.auth.SecretKey = maybeString(m["secret-key"], "")
55 if c.auth.AccessKey == "" || c.auth.SecretKey == "" { 68 if c.auth.AccessKey == "" || c.auth.SecretKey == "" {
56 if c.auth.AccessKey != "" { 69 if c.auth.AccessKey != "" {
57 return nil, fmt.Errorf("environment has access-key but n o secret-key") 70 return nil, fmt.Errorf("environment has access-key but n o secret-key")
58 } 71 }
59 if c.auth.SecretKey != "" { 72 if c.auth.SecretKey != "" {
60 return nil, fmt.Errorf("environment has secret-key but n o access-key") 73 return nil, fmt.Errorf("environment has secret-key but n o access-key")
61 } 74 }
62 c.auth, err = aws.EnvAuth() 75 c.auth, err = aws.EnvAuth()
63 if err != nil { 76 if err != nil {
64 return 77 return
65 } 78 }
66 } 79 }
67 80
68 regionName := maybeString(m["region"], "us-east-1") 81 regionName := maybeString(m["region"], "us-east-1")
69 if _, ok := aws.Regions[regionName]; !ok { 82 if _, ok := aws.Regions[regionName]; !ok {
70 return nil, fmt.Errorf("invalid region name %q", regionName) 83 return nil, fmt.Errorf("invalid region name %q", regionName)
71 } 84 }
85 c.defaultSeries = maybeString(m["default-series"], environs.CurrentSerie s)
72 c.region = regionName 86 c.region = regionName
73 » c.authorizedKeys = maybeString(m["authorized-keys"], "") 87 » return &c, nil
74 » authorizedKeysPath := maybeString(m["authorized-keys-path"], "") 88 }
75 » if c.authorizedKeys == "" { 89
76 » » c.authorizedKeys, err = authorizedKeys(authorizedKeysPath) 90 func (cfg *providerConfig) Clone() environs.EnvironConfig {
77 » » if err != nil { 91 » newCfg := *cfg
78 » » » return nil, err 92 » newCfg.Config = newCfg.Config.Clone()
79 » » } 93 » return &newCfg
80 » } else if authorizedKeysPath != "" { 94 }
81 » » return nil, fmt.Errorf("environment has both authorized-keys and authorized-keys-path") 95
96 func (cfg *providerConfig) DefaultSeries() string {
97 » return cfg.defaultSeries
98 }
99
100 func (cfg *providerConfig) Change(attrs map[string]interface{}) error {
101 » ccfg := cfg.Config.Clone()
102 » attrs, err := ccfg.Change(attrs)
103 » if err != nil {
104 » » return err
82 } 105 }
83 » return &c, nil 106 » newAttrs := cfg.localAttrs()
107 » for k, v := range attrs {
108 » » newAttrs[k] = v
109 » }
110 » newCfg0, err := environProvider{}.newConfig(ccfg, newAttrs)
111 » if err != nil {
112 » » return err
113 » }
114 » newCfg := newCfg0.(*providerConfig)
115 » if newCfg.region != cfg.region {
116 » » return fmt.Errorf("cannot change region config field")
117 » }
118 » *cfg = *newCfg
119 » return nil
120 }
121
122 // attrs returned locally defined attributes only.
123 func (cfg *providerConfig) localAttrs() map[string]interface{} {
124 » return map[string]interface{}{
125 » » "access-key": cfg.auth.AccessKey,
126 » » "secret-key": cfg.auth.SecretKey,
127 » » "region": cfg.region,
128 » » "authorized-keys": cfg.authorizedKeys,
129 » » "control-bucket": cfg.bucket,
130 » » "public-bucket": cfg.publicBucket,
131 » » "default-series": cfg.defaultSeries,
132 » }
133 }
134
135 func (cfg *providerConfig) Attrs() map[string]interface{} {
136 » attrs := cfg.localAttrs()
137 » for k, v := range cfg.Config.Attrs() {
138 » » attrs[k] = v
139 » }
140 » return attrs
84 } 141 }
85 142
86 func maybeString(x interface{}, dflt string) string { 143 func maybeString(x interface{}, dflt string) string {
87 if x == nil { 144 if x == nil {
88 return dflt 145 return dflt
89 } 146 }
90 return x.(string) 147 return x.(string)
91 } 148 }
OLDNEW
« no previous file with comments | « environs/dummy/environs.go ('k') | environs/ec2/config_test.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld 204d58d