github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/common/firewall/state.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package firewall
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/core/status"
    10  	"github.com/juju/juju/network"
    11  	"github.com/juju/juju/state"
    12  )
    13  
    14  // State provides the subset of global state required by the
    15  // remote firewaller facade.
    16  type State interface {
    17  	state.ModelMachinesWatcher
    18  	state.ModelAccessor
    19  
    20  	WatchSubnets(func(id interface{}) bool) state.StringsWatcher
    21  
    22  	KeyRelation(string) (Relation, error)
    23  
    24  	Unit(string) (Unit, error)
    25  
    26  	Machine(string) (Machine, error)
    27  
    28  	Application(string) (Application, error)
    29  }
    30  
    31  // TODO(wallyworld) - for tests, remove when remaining firewaller tests become unit tests.
    32  func StateShim(st *state.State, m *state.Model) stateShim {
    33  	return stateShim{st, m}
    34  }
    35  
    36  // TODO - CAAS(ericclaudejones): This should contain state alone, model will be
    37  // removed once all relevant methods are moved from state to model.
    38  type stateShim struct {
    39  	*state.State
    40  	*state.Model
    41  }
    42  
    43  func (st stateShim) KeyRelation(key string) (Relation, error) {
    44  	rel, err := st.State.KeyRelation(key)
    45  	if err != nil {
    46  		return nil, errors.Trace(err)
    47  	}
    48  	return relationShim{rel}, nil
    49  }
    50  
    51  type Relation interface {
    52  	status.StatusSetter
    53  	Endpoints() []state.Endpoint
    54  	WatchUnits(applicationName string) (state.RelationUnitsWatcher, error)
    55  	WatchRelationIngressNetworks() state.StringsWatcher
    56  	WatchRelationEgressNetworks() state.StringsWatcher
    57  }
    58  
    59  type relationShim struct {
    60  	*state.Relation
    61  }
    62  
    63  func (st stateShim) Application(name string) (Application, error) {
    64  	app, err := st.State.Application(name)
    65  	if err != nil {
    66  		return nil, errors.Trace(err)
    67  	}
    68  	return applicationShim{app}, nil
    69  }
    70  
    71  type Application interface {
    72  	Name() string
    73  }
    74  
    75  type applicationShim struct {
    76  	*state.Application
    77  }
    78  
    79  type Unit interface {
    80  	Name() string
    81  	PublicAddress() (network.Address, error)
    82  	AssignedMachineId() (string, error)
    83  }
    84  
    85  func (st stateShim) Unit(name string) (Unit, error) {
    86  	return st.State.Unit(name)
    87  }
    88  
    89  type Machine interface {
    90  	Id() string
    91  	WatchAddresses() state.NotifyWatcher
    92  }
    93  
    94  func (st stateShim) Machine(id string) (Machine, error) {
    95  	return st.State.Machine(id)
    96  }