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