github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/peergrouper/shim.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package peergrouper
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/mgo/v3"
     9  	"github.com/juju/replicaset/v3"
    10  
    11  	"github.com/juju/juju/core/status"
    12  	"github.com/juju/juju/state"
    13  )
    14  
    15  // This file holds code that translates from State
    16  // to the interface expected internally by the worker.
    17  
    18  type StateShim struct {
    19  	*state.State
    20  }
    21  
    22  func (s StateShim) ControllerNode(id string) (ControllerNode, error) {
    23  	return s.State.ControllerNode(id)
    24  }
    25  
    26  func (s StateShim) ControllerHost(id string) (ControllerHost, error) {
    27  	// For IAAS models, controllers are machines.
    28  	// For CAAS models, access to the controller is via a k8s service.
    29  	model, err := s.State.Model()
    30  	if err != nil {
    31  		return nil, errors.Trace(err)
    32  	}
    33  	if model.Type() == state.ModelTypeIAAS {
    34  		return s.State.Machine(id)
    35  	}
    36  
    37  	cloudService, err := s.State.CloudService(model.ControllerUUID())
    38  	if err != nil {
    39  		return nil, errors.Trace(err)
    40  	}
    41  	return &cloudServiceShim{cloudService}, nil
    42  }
    43  
    44  func (s StateShim) RemoveControllerReference(c ControllerNode) error {
    45  	return s.State.RemoveControllerReference(c)
    46  }
    47  
    48  func (s StateShim) Space(name string) (Space, error) {
    49  	return s.State.SpaceByName(name)
    50  }
    51  
    52  // cloudServiceShim stubs out functionality not yet
    53  // supported by the k8s service abstraction.
    54  // We don't yet support HA on k8s.
    55  type cloudServiceShim struct {
    56  	*state.CloudService
    57  }
    58  
    59  func (*cloudServiceShim) Life() state.Life {
    60  	// We don't manage the life of a cloud service entity.
    61  	return state.Alive
    62  }
    63  
    64  func (*cloudServiceShim) SetStatus(status.StatusInfo) error {
    65  	// We don't record the status of a cloud service entity.
    66  	return nil
    67  }
    68  
    69  // MongoSessionShim wraps a *mgo.Session to conform to the
    70  // MongoSession interface.
    71  type MongoSessionShim struct {
    72  	*mgo.Session
    73  }
    74  
    75  func (s MongoSessionShim) CurrentStatus() (*replicaset.Status, error) {
    76  	return replicaset.CurrentStatus(s.Session)
    77  }
    78  
    79  func (s MongoSessionShim) CurrentMembers() ([]replicaset.Member, error) {
    80  	return replicaset.CurrentMembers(s.Session)
    81  }
    82  
    83  func (s MongoSessionShim) Set(members []replicaset.Member) error {
    84  	return replicaset.Set(s.Session, members)
    85  }
    86  
    87  func (s MongoSessionShim) StepDownPrimary() error {
    88  	return replicaset.StepDownPrimary(s.Session)
    89  }
    90  
    91  func (s MongoSessionShim) Refresh() {
    92  	s.Session.Refresh()
    93  }