github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/agent/caasoperator/state.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package caasoperator
     5  
     6  import (
     7  	"gopkg.in/juju/charm.v6"
     8  	"gopkg.in/juju/names.v2"
     9  
    10  	"github.com/juju/juju/core/status"
    11  	"github.com/juju/juju/environs/config"
    12  	"github.com/juju/juju/network"
    13  	"github.com/juju/juju/state"
    14  )
    15  
    16  // CAASOperatorState provides the subset of global state
    17  // required by the CAAS operator facade.
    18  type CAASOperatorState interface {
    19  	Application(string) (Application, error)
    20  	Model() (Model, error)
    21  	ModelUUID() string
    22  	FindEntity(names.Tag) (state.Entity, error)
    23  	APIHostPortsForAgents() ([][]network.HostPort, error)
    24  	Addresses() ([]string, error)
    25  	WatchAPIHostPortsForAgents() state.NotifyWatcher
    26  }
    27  
    28  // Model provides the subset of CAAS model state required
    29  // by the CAAS operator facade.
    30  type Model interface {
    31  	SetPodSpec(names.ApplicationTag, string) error
    32  	Name() string
    33  	UUID() string
    34  	Type() state.ModelType
    35  	ModelConfig() (*config.Config, error)
    36  }
    37  
    38  // Application provides the subset of application state
    39  // required by the CAAS operator facade.
    40  type Application interface {
    41  	Charm() (Charm, bool, error)
    42  	CharmModifiedVersion() int
    43  	SetOperatorStatus(status.StatusInfo) error
    44  	WatchUnits() state.StringsWatcher
    45  	AllUnits() ([]Unit, error)
    46  }
    47  
    48  // Charm provides the subset of charm state required by the
    49  // CAAS operator facade.
    50  type Charm interface {
    51  	URL() *charm.URL
    52  	BundleSha256() string
    53  }
    54  
    55  type stateShim struct {
    56  	*state.State
    57  }
    58  
    59  func (s stateShim) Application(id string) (Application, error) {
    60  	app, err := s.State.Application(id)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	return applicationShim{app}, nil
    65  }
    66  
    67  func (s stateShim) Model() (Model, error) {
    68  	model, err := s.State.Model()
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  	return model.CAASModel()
    73  }
    74  
    75  type applicationShim struct {
    76  	*state.Application
    77  }
    78  
    79  func (a applicationShim) Charm() (Charm, bool, error) {
    80  	return a.Application.Charm()
    81  }
    82  
    83  func (a applicationShim) AllUnits() ([]Unit, error) {
    84  	all, err := a.Application.AllUnits()
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	result := make([]Unit, len(all))
    89  	for i, u := range all {
    90  		result[i] = u
    91  	}
    92  	return result, nil
    93  }
    94  
    95  type Unit interface {
    96  	Tag() names.Tag
    97  }