github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/migrations/remoteentities.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  // MigrationRemoteEntity represents a state.RemoteEntity
    12  // Point of use interface to enable better encapsulation.
    13  type MigrationRemoteEntity interface {
    14  	ID() string
    15  	Token() string
    16  	Macaroon() string
    17  }
    18  
    19  // RemoteEntitiesSource defines an inplace usage for reading all the remote
    20  // entities.
    21  type RemoteEntitiesSource interface {
    22  	AllRemoteEntities() ([]MigrationRemoteEntity, error)
    23  }
    24  
    25  // RemoteEntitiesModel defines an inplace usage for adding a remote entity
    26  // to a model.
    27  type RemoteEntitiesModel interface {
    28  	AddRemoteEntity(description.RemoteEntityArgs) description.RemoteEntity
    29  }
    30  
    31  // ExportRemoteEntities describes a way to execute a migration for exporting
    32  // remote entities.
    33  type ExportRemoteEntities struct{}
    34  
    35  // Execute the migration of the remote entities 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 excellent place to use them.
    39  func (ExportRemoteEntities) Execute(src RemoteEntitiesSource, dst RemoteEntitiesModel) error {
    40  	entities, err := src.AllRemoteEntities()
    41  	if err != nil {
    42  		return errors.Trace(err)
    43  	}
    44  	for _, entity := range entities {
    45  		// Despite remote entities having a member for macaroon,
    46  		// they are not exported.
    47  		dst.AddRemoteEntity(description.RemoteEntityArgs{
    48  			ID:    entity.ID(),
    49  			Token: entity.Token(),
    50  		})
    51  	}
    52  	return nil
    53  }