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

     1  // Copyright 2021 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package controller
     5  
     6  import (
     7  	"github.com/juju/charm/v12"
     8  	"github.com/juju/names/v5"
     9  
    10  	jujucontroller "github.com/juju/juju/controller"
    11  	"github.com/juju/juju/core/permission"
    12  	"github.com/juju/juju/state"
    13  )
    14  
    15  // The interfaces below are used to create mocks for testing.
    16  
    17  type ControllerAccess interface {
    18  	ControllerTag() names.ControllerTag
    19  	AddControllerUser(spec state.UserAccessSpec) (permission.UserAccess, error)
    20  	UserAccess(subject names.UserTag, target names.Tag) (permission.UserAccess, error)
    21  	ControllerInfo() (*state.ControllerInfo, error)
    22  	CreateCloudAccess(cloud string, user names.UserTag, access permission.Access) error
    23  	GetCloudAccess(cloud string, user names.UserTag) (permission.Access, error)
    24  	RemoveCloudAccess(cloud string, user names.UserTag) error
    25  	UserPermission(subject names.UserTag, target names.Tag) (permission.Access, error)
    26  	RemoveUserAccess(subject names.UserTag, target names.Tag) error
    27  	SetUserAccess(subject names.UserTag, target names.Tag, access permission.Access) (permission.UserAccess, error)
    28  }
    29  
    30  type Backend interface {
    31  	ControllerAccess
    32  	Model() (*state.Model, error)
    33  	Application(name string) (Application, error)
    34  	MongoVersion() (string, error)
    35  	ControllerModelUUID() string
    36  	AllModelUUIDs() ([]string, error)
    37  	AllBlocksForController() ([]state.Block, error)
    38  	RemoveAllBlocksForController() error
    39  	ModelExists(uuid string) (bool, error)
    40  	ControllerConfig() (jujucontroller.Config, error)
    41  	UpdateControllerConfig(updateAttrs map[string]interface{}, removeAttrs []string) error
    42  }
    43  
    44  type Application interface {
    45  	Name() string
    46  	Relations() ([]Relation, error)
    47  	CharmConfig(branchName string) (charm.Settings, error)
    48  }
    49  
    50  type Relation interface {
    51  	Endpoint(applicationname string) (state.Endpoint, error)
    52  	RelatedEndpoints(applicationname string) ([]state.Endpoint, error)
    53  	ApplicationSettings(appName string) (map[string]interface{}, error)
    54  	ModelUUID() string
    55  }
    56  
    57  type stateShim struct {
    58  	*state.State
    59  }
    60  
    61  func (s stateShim) Application(name string) (Application, error) {
    62  	app, err := s.State.Application(name)
    63  	return applicationShim{app}, err
    64  }
    65  
    66  type applicationShim struct {
    67  	*state.Application
    68  }
    69  
    70  func (a applicationShim) Relations() ([]Relation, error) {
    71  	rels, err := a.Application.Relations()
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  	result := make([]Relation, len(rels))
    76  	for i, r := range rels {
    77  		result[i] = r
    78  	}
    79  	return result, nil
    80  }