github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/state/stateenvirons/config.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package stateenvirons 5 6 import ( 7 "github.com/juju/errors" 8 "gopkg.in/juju/names.v2" 9 10 "github.com/juju/juju/caas" 11 "github.com/juju/juju/cloud" 12 "github.com/juju/juju/environs" 13 "github.com/juju/juju/state" 14 ) 15 16 // EnvironConfigGetter implements environs.EnvironConfigGetter 17 // in terms of a *state.State. 18 // TODO - CAAS(externalreality): Once cloud methods are migrated 19 // to model EnvironConfigGetter will no longer need to contain 20 // both state and model but only model. 21 type EnvironConfigGetter struct { 22 *state.State 23 *state.Model 24 } 25 26 // CloudSpec implements environs.EnvironConfigGetter. 27 func (g EnvironConfigGetter) CloudSpec() (environs.CloudSpec, error) { 28 cloudName := g.Model.Cloud() 29 regionName := g.Model.CloudRegion() 30 credentialTag, _ := g.Model.CloudCredential() 31 return CloudSpec(g.State, cloudName, regionName, credentialTag) 32 } 33 34 // CloudSpec returns an environs.CloudSpec from a *state.State, 35 // given the cloud, region and credential names. 36 func CloudSpec( 37 accessor state.CloudAccessor, 38 cloudName, regionName string, 39 credentialTag names.CloudCredentialTag, 40 ) (environs.CloudSpec, error) { 41 modelCloud, err := accessor.Cloud(cloudName) 42 if err != nil { 43 return environs.CloudSpec{}, errors.Trace(err) 44 } 45 46 var credential *cloud.Credential 47 if credentialTag != (names.CloudCredentialTag{}) { 48 credentialValue, err := accessor.CloudCredential(credentialTag) 49 if err != nil { 50 return environs.CloudSpec{}, errors.Trace(err) 51 } 52 cloudCredential := cloud.NewNamedCredential(credentialValue.Name, 53 cloud.AuthType(credentialValue.AuthType), 54 credentialValue.Attributes, 55 credentialValue.Revoked, 56 ) 57 credential = &cloudCredential 58 } 59 60 return environs.MakeCloudSpec(modelCloud, regionName, credential) 61 } 62 63 // NewEnvironFunc defines the type of a function that, given a state.State, 64 // returns a new Environ. 65 type NewEnvironFunc func(*state.State) (environs.Environ, error) 66 67 // GetNewEnvironFunc returns a NewEnvironFunc, that constructs Environs 68 // using the given environs.NewEnvironFunc. 69 func GetNewEnvironFunc(newEnviron environs.NewEnvironFunc) NewEnvironFunc { 70 return func(st *state.State) (environs.Environ, error) { 71 m, err := st.Model() 72 if err != nil { 73 return nil, errors.Trace(err) 74 } 75 g := EnvironConfigGetter{st, m} 76 return environs.GetEnviron(g, newEnviron) 77 } 78 } 79 80 // NewCAASBrokerFunc defines the type of a function that, given a state.State, 81 // returns a new CAAS broker. 82 type NewCAASBrokerFunc func(*state.State) (caas.Broker, error) 83 84 // GetNewCAASBrokerFunc returns a NewCAASBrokerFunc, that constructs CAAS brokers 85 // using the given caas.NewContainerBrokerFunc. 86 func GetNewCAASBrokerFunc(newBroker caas.NewContainerBrokerFunc) NewCAASBrokerFunc { 87 return func(st *state.State) (caas.Broker, error) { 88 m, err := st.Model() 89 if err != nil { 90 return nil, errors.Trace(err) 91 } 92 g := EnvironConfigGetter{st, m} 93 cloudSpec, err := g.CloudSpec() 94 if err != nil { 95 return nil, errors.Trace(err) 96 } 97 cfg, err := g.ModelConfig() 98 if err != nil { 99 return nil, errors.Trace(err) 100 } 101 return newBroker(environs.OpenParams{ 102 Cloud: cloudSpec, 103 Config: cfg, 104 }) 105 } 106 }