github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/upgrades/contexts.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package upgrades 5 6 import ( 7 "github.com/juju/juju/agent" 8 "github.com/juju/juju/api" 9 "github.com/juju/juju/state" 10 ) 11 12 // Context provides the dependencies used when executing upgrade steps. 13 type Context interface { 14 // APIState returns an API connection to state. 15 APIState() *api.State 16 17 // State returns a connection to state. This will be non-nil 18 // only in the context of a state server. 19 State() *state.State 20 21 // AgentConfig returns the agent config for the machine that is being 22 // upgraded. 23 AgentConfig() agent.ConfigSetter 24 25 // StateContext returns a new Context suitable for State-based 26 // upgrade steps. 27 StateContext() Context 28 29 // APIContext returns a new Context suitable for API-based upgrade 30 // steps. 31 APIContext() Context 32 } 33 34 // NewContext returns a new upgrade context. 35 func NewContext(agentConfig agent.ConfigSetter, api *api.State, st *state.State) Context { 36 return &upgradeContext{ 37 agentConfig: agentConfig, 38 api: api, 39 st: st, 40 } 41 } 42 43 // upgradeContext is a default Context implementation. 44 type upgradeContext struct { 45 agentConfig agent.ConfigSetter 46 api *api.State 47 st *state.State 48 } 49 50 // APIState is defined on the Context interface. 51 // 52 // This will panic if called on a Context returned by StateContext. 53 func (c *upgradeContext) APIState() *api.State { 54 if c.api == nil { 55 panic("API not available from this context") 56 } 57 return c.api 58 } 59 60 // State is defined on the Context interface. 61 // 62 // This will panic if called on a Context returned by APIContext. 63 func (c *upgradeContext) State() *state.State { 64 if c.st == nil { 65 panic("State not available from this context") 66 } 67 return c.st 68 } 69 70 // AgentConfig is defined on the Context interface. 71 func (c *upgradeContext) AgentConfig() agent.ConfigSetter { 72 return c.agentConfig 73 } 74 75 // StateContext is defined on the Context interface. 76 func (c *upgradeContext) StateContext() Context { 77 return &upgradeContext{ 78 agentConfig: c.agentConfig, 79 st: c.st, 80 } 81 } 82 83 // APIContext is defined on the Context interface. 84 func (c *upgradeContext) APIContext() Context { 85 return &upgradeContext{ 86 agentConfig: c.agentConfig, 87 api: c.api, 88 } 89 }