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