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

     1  // Copyright 2022 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package highavailability
     5  
     6  import (
     7  	"reflect"
     8  
     9  	"github.com/juju/errors"
    10  
    11  	apiservererrors "github.com/juju/juju/apiserver/errors"
    12  	"github.com/juju/juju/apiserver/facade"
    13  	"github.com/juju/juju/state"
    14  )
    15  
    16  // Register is called to expose a package of facades onto a given registry.
    17  func Register(registry facade.FacadeRegistry) {
    18  	registry.MustRegister("HighAvailability", 2, func(ctx facade.Context) (facade.Facade, error) {
    19  		return newHighAvailabilityAPI(ctx)
    20  	}, reflect.TypeOf((*HighAvailabilityAPI)(nil)))
    21  }
    22  
    23  // newHighAvailabilityAPI creates a new server-side highavailability API end point.
    24  func newHighAvailabilityAPI(ctx facade.Context) (*HighAvailabilityAPI, error) {
    25  	// Only clients can access the high availability facade.
    26  	authorizer := ctx.Auth()
    27  	if !authorizer.AuthClient() {
    28  		return nil, apiservererrors.ErrPerm
    29  	}
    30  
    31  	st := ctx.State()
    32  	model, err := st.Model()
    33  	if err != nil {
    34  		return nil, errors.Trace(err)
    35  	}
    36  	if model.Type() == state.ModelTypeCAAS {
    37  		return nil, errors.NotSupportedf("high availability on kubernetes controllers")
    38  	}
    39  
    40  	return &HighAvailabilityAPI{
    41  		state:      st,
    42  		resources:  ctx.Resources(),
    43  		authorizer: authorizer,
    44  	}, nil
    45  }