github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/apiserver/facades/client/applicationoffers/state.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package applicationoffers
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/names/v5"
     9  
    10  	commoncrossmodel "github.com/juju/juju/apiserver/common/crossmodel"
    11  	"github.com/juju/juju/core/crossmodel"
    12  	"github.com/juju/juju/core/network"
    13  	"github.com/juju/juju/core/permission"
    14  	"github.com/juju/juju/environs/context"
    15  	"github.com/juju/juju/state"
    16  )
    17  
    18  // StatePool provides the subset of a state pool.
    19  type StatePool interface {
    20  	// Get returns a State for a given model from the pool.
    21  	Get(modelUUID string) (Backend, func(), error)
    22  
    23  	// Get returns a Model from the pool.
    24  	GetModel(modelUUID string) (Model, func(), error)
    25  }
    26  
    27  var GetStatePool = func(sp *state.StatePool) StatePool {
    28  	return &statePoolShim{sp}
    29  
    30  }
    31  
    32  type statePoolShim struct {
    33  	*state.StatePool
    34  }
    35  
    36  func (pool statePoolShim) Get(modelUUID string) (Backend, func(), error) {
    37  	st, err := pool.StatePool.Get(modelUUID)
    38  	if err != nil {
    39  		return nil, nil, errors.Trace(err)
    40  	}
    41  	return &stateShim{
    42  		st:      st.State,
    43  		Backend: commoncrossmodel.GetBackend(st.State),
    44  	}, func() { st.Release() }, nil
    45  }
    46  
    47  func (pool statePoolShim) GetModel(modelUUID string) (Model, func(), error) {
    48  	m, ph, err := pool.StatePool.GetModel(modelUUID)
    49  	if err != nil {
    50  		return nil, nil, errors.Trace(err)
    51  	}
    52  	return &modelShim{m}, func() { ph.Release() }, nil
    53  }
    54  
    55  // Backend provides selected methods off the state.State struct.
    56  type Backend interface {
    57  	commoncrossmodel.Backend
    58  	Charm(string) (commoncrossmodel.Charm, error)
    59  	ApplicationOffer(name string) (*crossmodel.ApplicationOffer, error)
    60  	Model() (Model, error)
    61  	OfferConnections(string) ([]OfferConnection, error)
    62  	SpaceByName(string) (Space, error)
    63  	User(names.UserTag) (User, error)
    64  
    65  	CreateOfferAccess(offer names.ApplicationOfferTag, user names.UserTag, access permission.Access) error
    66  	UpdateOfferAccess(offer names.ApplicationOfferTag, user names.UserTag, access permission.Access) error
    67  	RemoveOfferAccess(offer names.ApplicationOfferTag, user names.UserTag) error
    68  	GetOfferUsers(offerUUID string) (map[string]permission.Access, error)
    69  
    70  	// GetModelCallContext gets everything that is needed to make cloud calls on behalf of the state current model.
    71  	GetModelCallContext() context.ProviderCallContext
    72  
    73  	AllSpaceInfos() (network.SpaceInfos, error)
    74  }
    75  
    76  var GetStateAccess = func(st *state.State) Backend {
    77  	return &stateShim{
    78  		st:      st,
    79  		Backend: commoncrossmodel.GetBackend(st),
    80  	}
    81  }
    82  
    83  type stateShim struct {
    84  	commoncrossmodel.Backend
    85  	st *state.State
    86  }
    87  
    88  func (s stateShim) UserPermission(subject names.UserTag, target names.Tag) (permission.Access, error) {
    89  	return s.st.UserPermission(subject, target)
    90  }
    91  
    92  func (s stateShim) CreateOfferAccess(offer names.ApplicationOfferTag, user names.UserTag, access permission.Access) error {
    93  	return s.st.CreateOfferAccess(offer, user, access)
    94  }
    95  
    96  func (s stateShim) UpdateOfferAccess(offer names.ApplicationOfferTag, user names.UserTag, access permission.Access) error {
    97  	return s.st.UpdateOfferAccess(offer, user, access)
    98  }
    99  
   100  func (s stateShim) RemoveOfferAccess(offer names.ApplicationOfferTag, user names.UserTag) error {
   101  	return s.st.RemoveOfferAccess(offer, user)
   102  }
   103  
   104  func (s stateShim) GetOfferUsers(offerUUID string) (map[string]permission.Access, error) {
   105  	return s.st.GetOfferUsers(offerUUID)
   106  }
   107  
   108  func (s *stateShim) SpaceByName(name string) (Space, error) {
   109  	return s.st.SpaceByName(name)
   110  }
   111  
   112  func (s *stateShim) Model() (Model, error) {
   113  	m, err := s.st.Model()
   114  	return &modelShim{m}, err
   115  }
   116  
   117  func (s *stateShim) GetModelCallContext() context.ProviderCallContext {
   118  	return context.CallContext(s.st)
   119  }
   120  
   121  func (s *stateShim) AllSpaceInfos() (network.SpaceInfos, error) {
   122  	return s.st.AllSpaceInfos()
   123  }
   124  
   125  type stateCharmShim struct {
   126  	*state.Charm
   127  }
   128  
   129  func (s stateShim) Charm(curl string) (commoncrossmodel.Charm, error) {
   130  	ch, err := s.st.Charm(curl)
   131  	if err != nil {
   132  		return nil, err
   133  	}
   134  	return stateCharmShim{ch}, nil
   135  }
   136  
   137  func (s *stateShim) ApplicationOffer(name string) (*crossmodel.ApplicationOffer, error) {
   138  	offers := state.NewApplicationOffers(s.st)
   139  	return offers.ApplicationOffer(name)
   140  }
   141  
   142  var GetApplicationOffers = func(backend interface{}) crossmodel.ApplicationOffers {
   143  	switch st := backend.(type) {
   144  	case *state.State:
   145  		return state.NewApplicationOffers(st)
   146  	case *stateShim:
   147  		return state.NewApplicationOffers(st.st)
   148  	}
   149  	return nil
   150  }
   151  
   152  type Space interface {
   153  	Name() string
   154  	NetworkSpace() (network.SpaceInfo, error)
   155  	ProviderId() network.Id
   156  }
   157  
   158  type Model interface {
   159  	UUID() string
   160  	ModelTag() names.ModelTag
   161  	Name() string
   162  	Type() state.ModelType
   163  	Owner() names.UserTag
   164  }
   165  
   166  type modelShim struct {
   167  	*state.Model
   168  }
   169  
   170  func (s *stateShim) OfferConnections(offerUUID string) ([]OfferConnection, error) {
   171  	conns, err := s.st.OfferConnections(offerUUID)
   172  	if err != nil {
   173  		return nil, err
   174  	}
   175  	result := make([]OfferConnection, len(conns))
   176  	for i, oc := range conns {
   177  		result[i] = offerConnectionShim{oc}
   178  	}
   179  	return result, nil
   180  }
   181  
   182  type OfferConnection interface {
   183  	SourceModelUUID() string
   184  	UserName() string
   185  	RelationKey() string
   186  	RelationId() int
   187  }
   188  
   189  type offerConnectionShim struct {
   190  	*state.OfferConnection
   191  }
   192  
   193  func (s *stateShim) User(tag names.UserTag) (User, error) {
   194  	return s.st.User(tag)
   195  }
   196  
   197  type User interface {
   198  	DisplayName() string
   199  }