github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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 ) 10 11 // Context provides the dependencies used when executing upgrade steps. 12 type Context interface { 13 // APIState returns an API connection to state. 14 // 15 // TODO(mjs) - for 2.0, this should return a base.APICaller 16 // instead of api.Connection once the 1.x upgrade steps have been 17 // removed. Upgrade steps should not be able close the API 18 // connection. 19 APIState() api.Connection 20 21 // State returns a connection to state. This will be non-nil 22 // only in the context of a controller. 23 State() StateBackend 24 25 // AgentConfig returns the agent config for the machine that is being 26 // upgraded. 27 AgentConfig() agent.ConfigSetter 28 29 // StateContext returns a new Context suitable for State-based 30 // upgrade steps. 31 StateContext() Context 32 33 // APIContext returns a new Context suitable for API-based upgrade 34 // steps. 35 APIContext() Context 36 } 37 38 // NewContext returns a new upgrade context. 39 func NewContext( 40 agentConfig agent.ConfigSetter, 41 api api.Connection, 42 st StateBackend, 43 ) Context { 44 return &upgradeContext{ 45 agentConfig: agentConfig, 46 api: api, 47 st: st, 48 } 49 } 50 51 // upgradeContext is a default Context implementation. 52 type upgradeContext struct { 53 agentConfig agent.ConfigSetter 54 api api.Connection 55 st StateBackend 56 } 57 58 // APIState is defined on the Context interface. 59 // 60 // This will panic if called on a Context returned by StateContext. 61 func (c *upgradeContext) APIState() api.Connection { 62 if c.api == nil { 63 panic("API not available from this context") 64 } 65 return c.api 66 } 67 68 // State is defined on the Context interface. 69 // 70 // This will panic if called on a Context returned by APIContext. 71 func (c *upgradeContext) State() StateBackend { 72 if c.st == nil { 73 panic("State not available from this context") 74 } 75 return c.st 76 } 77 78 // AgentConfig is defined on the Context interface. 79 func (c *upgradeContext) AgentConfig() agent.ConfigSetter { 80 return c.agentConfig 81 } 82 83 // StateContext is defined on the Context interface. 84 func (c *upgradeContext) StateContext() Context { 85 return &upgradeContext{ 86 agentConfig: c.agentConfig, 87 st: c.st, 88 } 89 } 90 91 // APIContext is defined on the Context interface. 92 func (c *upgradeContext) APIContext() Context { 93 return &upgradeContext{ 94 agentConfig: c.agentConfig, 95 api: c.api, 96 } 97 }