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

     1  // Copyright 2019 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package subnets
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/names/v5"
     9  
    10  	"github.com/juju/juju/apiserver/common/networkingcommon"
    11  	"github.com/juju/juju/core/network"
    12  	"github.com/juju/juju/state"
    13  	"github.com/juju/juju/state/stateenvirons"
    14  )
    15  
    16  func NewStateShim(st *state.State) (*stateShim, error) {
    17  	m, err := st.Model()
    18  	if err != nil {
    19  		return nil, errors.Trace(err)
    20  	}
    21  	return &stateShim{EnvironConfigGetter: stateenvirons.EnvironConfigGetter{Model: m},
    22  		State: st, modelTag: m.ModelTag()}, nil
    23  }
    24  
    25  // stateShim forwards and adapts state.State methods to Backing
    26  // method.
    27  type stateShim struct {
    28  	stateenvirons.EnvironConfigGetter
    29  	*state.State
    30  	modelTag names.ModelTag
    31  }
    32  
    33  func (s *stateShim) ModelTag() names.ModelTag {
    34  	return s.modelTag
    35  }
    36  
    37  func (s *stateShim) AllSubnets() ([]networkingcommon.BackingSubnet, error) {
    38  	results, err := s.State.AllSubnets()
    39  	if err != nil {
    40  		return nil, errors.Trace(err)
    41  	}
    42  	subnets := make([]networkingcommon.BackingSubnet, len(results))
    43  	for i, result := range results {
    44  		subnets[i] = result
    45  	}
    46  	return subnets, nil
    47  }
    48  
    49  func (s *stateShim) SubnetByCIDR(cidr string) (networkingcommon.BackingSubnet, error) {
    50  	result, err := s.State.SubnetByCIDR(cidr)
    51  	if err != nil {
    52  		return nil, errors.Trace(err)
    53  	}
    54  	return result, nil
    55  }
    56  
    57  // SubnetsByCIDR wraps each result of a call to state.SubnetsByCIDR
    58  // in a subnet shim and returns the result.
    59  func (s *stateShim) SubnetsByCIDR(cidr string) ([]networkingcommon.BackingSubnet, error) {
    60  	subnets, err := s.State.SubnetsByCIDR(cidr)
    61  	if err != nil {
    62  		return nil, errors.Trace(err)
    63  	}
    64  	if len(subnets) == 0 {
    65  		return nil, nil
    66  	}
    67  
    68  	result := make([]networkingcommon.BackingSubnet, len(subnets))
    69  	for i, subnet := range subnets {
    70  		result[i] = subnet
    71  	}
    72  	return result, nil
    73  }
    74  
    75  func (s *stateShim) AvailabilityZones() (network.AvailabilityZones, error) {
    76  	// TODO (hml) 2019-09-13
    77  	// now available... include.
    78  	// AvailabilityZones() is defined in the common.ZonedEnviron interface
    79  	return nil, nil
    80  }
    81  
    82  func (s *stateShim) SetAvailabilityZones(_ network.AvailabilityZones) error {
    83  	return nil
    84  }
    85  
    86  func (s *stateShim) AllSpaces() ([]networkingcommon.BackingSpace, error) {
    87  	results, err := s.State.AllSpaces()
    88  	if err != nil {
    89  		return nil, errors.Trace(err)
    90  	}
    91  	spaces := make([]networkingcommon.BackingSpace, len(results))
    92  	for i, result := range results {
    93  		spaces[i] = result
    94  	}
    95  	return spaces, nil
    96  }