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