launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/provider/maas/config.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package maas 5 6 import ( 7 "errors" 8 "fmt" 9 "net/url" 10 "strings" 11 12 "launchpad.net/juju-core/environs/config" 13 "launchpad.net/juju-core/schema" 14 ) 15 16 var configFields = schema.Fields{ 17 "maas-server": schema.String(), 18 // maas-oauth is a colon-separated triplet of: 19 // consumer-key:resource-token:resource-secret 20 "maas-oauth": schema.String(), 21 // maas-agent-name is an optional UUID to group the instances 22 // acquired from MAAS, to support multiple environments per MAAS user. 23 "maas-agent-name": schema.String(), 24 } 25 var configDefaults = schema.Defaults{ 26 // For backward-compatibility, maas-agent-name is the empty string 27 // by default. However, new environments should all use a UUID. 28 "maas-agent-name": "", 29 } 30 31 type maasEnvironConfig struct { 32 *config.Config 33 attrs map[string]interface{} 34 } 35 36 func (cfg *maasEnvironConfig) maasServer() string { 37 return cfg.attrs["maas-server"].(string) 38 } 39 40 func (cfg *maasEnvironConfig) maasOAuth() string { 41 return cfg.attrs["maas-oauth"].(string) 42 } 43 44 func (cfg *maasEnvironConfig) maasAgentName() string { 45 if uuid, ok := cfg.attrs["maas-agent-name"].(string); ok { 46 return uuid 47 } 48 return "" 49 } 50 51 func (prov maasEnvironProvider) newConfig(cfg *config.Config) (*maasEnvironConfig, error) { 52 validCfg, err := prov.Validate(cfg, nil) 53 if err != nil { 54 return nil, err 55 } 56 result := new(maasEnvironConfig) 57 result.Config = validCfg 58 result.attrs = validCfg.UnknownAttrs() 59 return result, nil 60 } 61 62 var errMalformedMaasOAuth = errors.New("malformed maas-oauth (3 items separated by colons)") 63 64 func (prov maasEnvironProvider) Validate(cfg, oldCfg *config.Config) (*config.Config, error) { 65 // Validate base configuration change before validating MAAS specifics. 66 err := config.Validate(cfg, oldCfg) 67 if err != nil { 68 return nil, err 69 } 70 71 validated, err := cfg.ValidateUnknownAttrs(configFields, configDefaults) 72 if err != nil { 73 return nil, err 74 } 75 if oldCfg != nil { 76 oldAttrs := oldCfg.UnknownAttrs() 77 validMaasAgentName := false 78 if oldName, ok := oldAttrs["maas-agent-name"]; !ok || oldName == nil { 79 // If maas-agent-name was nil (because the config was 80 // generated pre-1.16.2 the only correct value for it is "" 81 // See bug #1256179 82 validMaasAgentName = (validated["maas-agent-name"] == "") 83 } else { 84 validMaasAgentName = (validated["maas-agent-name"] == oldName) 85 } 86 if !validMaasAgentName { 87 return nil, fmt.Errorf("cannot change maas-agent-name") 88 } 89 } 90 envCfg := new(maasEnvironConfig) 91 envCfg.Config = cfg 92 envCfg.attrs = validated 93 server := envCfg.maasServer() 94 serverURL, err := url.Parse(server) 95 if err != nil || serverURL.Scheme == "" || serverURL.Host == "" { 96 return nil, fmt.Errorf("malformed maas-server URL '%v': %s", server, err) 97 } 98 oauth := envCfg.maasOAuth() 99 if strings.Count(oauth, ":") != 2 { 100 return nil, errMalformedMaasOAuth 101 } 102 return cfg.Apply(envCfg.attrs) 103 }