github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/migrations/relationnetworks.go (about)

     1  // Copyright 2019 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package migrations
     5  
     6  import (
     7  	"github.com/juju/description/v5"
     8  	"github.com/juju/errors"
     9  )
    10  
    11  // MigrationRelationNetworks represents a state.RelationNetwork
    12  // Point of use interface to enable better encapsulation.
    13  type MigrationRelationNetworks interface {
    14  	Id() string
    15  	RelationKey() string
    16  	CIDRS() []string
    17  }
    18  
    19  // RelationNetworksSource defines an inplace usage for reading all the relation
    20  // networks.
    21  type RelationNetworksSource interface {
    22  	AllRelationNetworks() ([]MigrationRelationNetworks, error)
    23  }
    24  
    25  // RelationNetworksModel defines an inplace usage for adding a relation networks
    26  // to a model.
    27  type RelationNetworksModel interface {
    28  	AddRelationNetwork(description.RelationNetworkArgs) description.RelationNetwork
    29  }
    30  
    31  // ExportRelationNetworks describes a way to execute a migration for
    32  // exporting relation networks.
    33  type ExportRelationNetworks struct{}
    34  
    35  // Execute the migration of the relation networks using typed interfaces, to
    36  // ensure we don't loose any type safety.
    37  // This doesn't conform to an interface because go doesn't have generics, but
    38  // when this does arrive this would be an execellent place to use them.
    39  func (ExportRelationNetworks) Execute(src RelationNetworksSource, dst RelationNetworksModel) error {
    40  	entities, err := src.AllRelationNetworks()
    41  	if err != nil {
    42  		return errors.Trace(err)
    43  	}
    44  	for _, entity := range entities {
    45  		dst.AddRelationNetwork(description.RelationNetworkArgs{
    46  			ID:          entity.Id(),
    47  			RelationKey: entity.RelationKey(),
    48  			CIDRS:       entity.CIDRS(),
    49  		})
    50  	}
    51  	return nil
    52  }