github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/migrations/offerconnections.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 // MigrationOfferConnection is an in-place representation of the 12 // state.OfferConnection 13 type MigrationOfferConnection interface { 14 OfferUUID() string 15 RelationId() int 16 RelationKey() string 17 UserName() string 18 SourceModelUUID() string 19 } 20 21 // AllOfferConnectionSource defines an in-place usage for reading all the 22 // offer connections. 23 type AllOfferConnectionSource interface { 24 AllOfferConnections() ([]MigrationOfferConnection, error) 25 } 26 27 // OfferConnectionSource composes all the interfaces to create a offer 28 // connection. 29 type OfferConnectionSource interface { 30 AllOfferConnectionSource 31 } 32 33 // OfferConnectionModel defines an in-place usage for adding a offer connection 34 // to a model. 35 type OfferConnectionModel interface { 36 AddOfferConnection(description.OfferConnectionArgs) description.OfferConnection 37 } 38 39 // ExportOfferConnections describes a way to execute a migration for exporting 40 // offer connections. 41 type ExportOfferConnections struct{} 42 43 // Execute the migration of the offer connections using typed interfaces, to 44 // ensure we don't loose any type safety. 45 // This doesn't conform to an interface because go doesn't have generics, but 46 // when this does arrive this would be an excellent place to use them. 47 func (m ExportOfferConnections) Execute(src OfferConnectionSource, dst OfferConnectionModel) error { 48 offerConnections, err := src.AllOfferConnections() 49 if err != nil { 50 return errors.Trace(err) 51 } 52 53 for _, offerConnection := range offerConnections { 54 m.addOfferConnection(dst, offerConnection) 55 } 56 return nil 57 } 58 59 func (m ExportOfferConnections) addOfferConnection(dst OfferConnectionModel, offer MigrationOfferConnection) { 60 _ = dst.AddOfferConnection(description.OfferConnectionArgs{ 61 OfferUUID: offer.OfferUUID(), 62 RelationID: offer.RelationId(), 63 RelationKey: offer.RelationKey(), 64 SourceModelUUID: offer.SourceModelUUID(), 65 UserName: offer.UserName(), 66 }) 67 }