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

     1  // Copyright 2019 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package modelgeneration
     5  
     6  import (
     7  	"github.com/juju/charm/v12"
     8  	"github.com/juju/errors"
     9  
    10  	"github.com/juju/juju/core/cache"
    11  	"github.com/juju/juju/state"
    12  )
    13  
    14  type modelShim struct {
    15  	*state.Model
    16  }
    17  
    18  // Branch wraps the state model branch method,
    19  // returning the locally defined Generation interface.
    20  func (g *modelShim) Branch(name string) (Generation, error) {
    21  	m, err := g.Model.Branch(name)
    22  	return m, errors.Trace(err)
    23  }
    24  
    25  // Branches wraps the state model branches method,
    26  // returning a collection of the Generation interface.
    27  func (g *modelShim) Branches() ([]Generation, error) {
    28  	branches, err := g.Model.Branches()
    29  	if err != nil {
    30  		return nil, errors.Trace(err)
    31  	}
    32  
    33  	res := make([]Generation, len(branches))
    34  	for i, b := range branches {
    35  		res[i] = b
    36  	}
    37  	return res, nil
    38  }
    39  
    40  // Generation wraps the state model Generation method,
    41  // returning the locally defined Generation interface.
    42  func (g *modelShim) Generation(id int) (Generation, error) {
    43  	m, err := g.Model.Generation(id)
    44  	return m, errors.Trace(err)
    45  }
    46  
    47  // Branches wraps the state model Generations method,
    48  // returning a collection of the Generation interface.
    49  func (g *modelShim) Generations() ([]Generation, error) {
    50  	branches, err := g.Model.Generations()
    51  	if err != nil {
    52  		return nil, errors.Trace(err)
    53  	}
    54  
    55  	res := make([]Generation, len(branches))
    56  	for i, b := range branches {
    57  		res[i] = b
    58  	}
    59  	return res, nil
    60  }
    61  
    62  type applicationShim struct {
    63  	*state.Application
    64  }
    65  
    66  // DefaultCharmConfig returns the default configuration
    67  // for this application's charm.
    68  func (a *applicationShim) DefaultCharmConfig() (charm.Settings, error) {
    69  	ch, _, err := a.Charm()
    70  	if err != nil {
    71  		return nil, errors.Trace(err)
    72  	}
    73  	return ch.Config().DefaultSettings(), nil
    74  }
    75  
    76  type stateShim struct {
    77  	*state.State
    78  }
    79  
    80  func (st *stateShim) Model() (Model, error) {
    81  	model, err := st.State.Model()
    82  	if err != nil {
    83  		return nil, errors.Trace(err)
    84  	}
    85  	return &modelShim{Model: model}, nil
    86  }
    87  
    88  func (st *stateShim) Application(name string) (Application, error) {
    89  	app, err := st.State.Application(name)
    90  	if err != nil {
    91  		return nil, errors.Trace(err)
    92  	}
    93  	return &applicationShim{Application: app}, nil
    94  }
    95  
    96  type modelCacheShim struct {
    97  	*cache.Model
    98  }