github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/apiserver/application/backend.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package application 5 6 import ( 7 "gopkg.in/juju/charm.v6-unstable" 8 csparams "gopkg.in/juju/charmrepo.v2-unstable/csclient/params" 9 "gopkg.in/juju/names.v2" 10 11 "github.com/juju/juju/constraints" 12 "github.com/juju/juju/instance" 13 "github.com/juju/juju/state" 14 ) 15 16 // Backend defines the state functionality required by the application 17 // facade. For details on the methods, see the methods on state.State 18 // with the same names. 19 type Backend interface { 20 Application(string) (Application, error) 21 AddApplication(state.AddApplicationArgs) (*state.Application, error) 22 AddRelation(...state.Endpoint) (Relation, error) 23 AssignUnit(*state.Unit, state.AssignmentPolicy) error 24 AssignUnitWithPlacement(*state.Unit, *instance.Placement) error 25 Charm(*charm.URL) (Charm, error) 26 EndpointsRelation(...state.Endpoint) (Relation, error) 27 InferEndpoints(...string) ([]state.Endpoint, error) 28 Machine(string) (Machine, error) 29 ModelTag() names.ModelTag 30 Unit(string) (Unit, error) 31 } 32 33 // BlockChecker defines the block-checking functionality required by 34 // the application facade. This is implemented by 35 // apiserver/common.BlockChecker. 36 type BlockChecker interface { 37 ChangeAllowed() error 38 RemoveAllowed() error 39 } 40 41 // Application defines a subset of the functionality provided by the 42 // state.Application type, as required by the application facade. For 43 // details on the methods, see the methods on state.Application with 44 // the same names. 45 type Application interface { 46 AddUnit() (*state.Unit, error) 47 Charm() (Charm, bool, error) 48 CharmURL() (*charm.URL, bool) 49 Channel() csparams.Channel 50 ClearExposed() error 51 ConfigSettings() (charm.Settings, error) 52 Constraints() (constraints.Value, error) 53 Destroy() error 54 Endpoints() ([]state.Endpoint, error) 55 IsPrincipal() bool 56 Series() string 57 SetCharm(state.SetCharmConfig) error 58 SetConstraints(constraints.Value) error 59 SetExposed() error 60 SetMetricCredentials([]byte) error 61 SetMinUnits(int) error 62 UpdateConfigSettings(charm.Settings) error 63 } 64 65 // Charm defines a subset of the functionality provided by the 66 // state.Charm type, as required by the application facade. For 67 // details on the methods, see the methods on state.Charm with 68 // the same names. 69 type Charm interface { 70 charm.Charm 71 } 72 73 // Machine defines a subset of the functionality provided by the 74 // state.Machine type, as required by the application facade. For 75 // details on the methods, see the methods on state.Machine with 76 // the same names. 77 type Machine interface { 78 } 79 80 // Relation defines a subset of the functionality provided by the 81 // state.Relation type, as required by the application facade. For 82 // details on the methods, see the methods on state.Relation with 83 // the same names. 84 type Relation interface { 85 Destroy() error 86 Endpoint(string) (state.Endpoint, error) 87 } 88 89 // Unit defines a subset of the functionality provided by the 90 // state.Unit type, as required by the application facade. For 91 // details on the methods, see the methods on state.Unit with 92 // the same names. 93 type Unit interface { 94 Destroy() error 95 IsPrincipal() bool 96 Life() state.Life 97 } 98 99 type stateShim struct { 100 *state.State 101 } 102 103 // NewStateBackend converts a state.State into a Backend. 104 func NewStateBackend(st *state.State) Backend { 105 return stateShim{st} 106 } 107 108 // CharmToStateCharm converts a Charm into a state.Charm. This is 109 // a hack that is required until the State interface methods we 110 // deal with stop accepting state.Charms, and start accepting 111 // charm.Charm and charm.URL. 112 func CharmToStateCharm(ch Charm) *state.Charm { 113 return ch.(stateCharmShim).Charm 114 } 115 116 func (s stateShim) Application(name string) (Application, error) { 117 a, err := s.State.Application(name) 118 if err != nil { 119 return nil, err 120 } 121 return stateApplicationShim{a}, nil 122 } 123 124 func (s stateShim) AddRelation(eps ...state.Endpoint) (Relation, error) { 125 r, err := s.State.AddRelation(eps...) 126 if err != nil { 127 return nil, err 128 } 129 return stateRelationShim{r}, nil 130 } 131 132 func (s stateShim) Charm(curl *charm.URL) (Charm, error) { 133 ch, err := s.State.Charm(curl) 134 if err != nil { 135 return nil, err 136 } 137 return stateCharmShim{ch}, nil 138 } 139 140 func (s stateShim) EndpointsRelation(eps ...state.Endpoint) (Relation, error) { 141 r, err := s.State.EndpointsRelation(eps...) 142 if err != nil { 143 return nil, err 144 } 145 return stateRelationShim{r}, nil 146 } 147 148 func (s stateShim) Machine(name string) (Machine, error) { 149 m, err := s.State.Machine(name) 150 if err != nil { 151 return nil, err 152 } 153 return stateMachineShim{m}, nil 154 } 155 156 func (s stateShim) Unit(name string) (Unit, error) { 157 u, err := s.State.Unit(name) 158 if err != nil { 159 return nil, err 160 } 161 return stateUnitShim{u}, nil 162 } 163 164 type stateApplicationShim struct { 165 *state.Application 166 } 167 168 func (a stateApplicationShim) Charm() (Charm, bool, error) { 169 ch, force, err := a.Application.Charm() 170 if err != nil { 171 return nil, false, err 172 } 173 return ch, force, nil 174 } 175 176 type stateCharmShim struct { 177 *state.Charm 178 } 179 180 type stateMachineShim struct { 181 *state.Machine 182 } 183 184 type stateRelationShim struct { 185 *state.Relation 186 } 187 188 type stateUnitShim struct { 189 *state.Unit 190 }