forked from purpleidea/mgmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.go
183 lines (163 loc) · 5.21 KB
/
copy.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Mgmt
// Copyright (C) 2013-2024+ James Shubin and the project contributors
// Written by James Shubin <[email protected]> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Additional permission under GNU GPL version 3 section 7
//
// If you modify this program, or any covered work, by linking or combining it
// with embedded mcl code and modules (and that the embedded mcl code and
// modules which link with this program, contain a copy of their source code in
// the authoritative form) containing parts covered by the terms of any other
// license, the licensors of this program grant you additional permission to
// convey the resulting work. Furthermore, the licensors of this program grant
// the original author, James Shubin, additional permission to update this
// additional permission if he deems it necessary to achieve the goals of this
// additional permission.
package engine
import (
"fmt"
"github.com/purpleidea/mgmt/util/errwrap"
)
// ResCopy copies a resource. This is the main entry point for copying a
// resource since it does all the common engine-level copying as well.
func ResCopy(r CopyableRes) (CopyableRes, error) {
res := r.Copy()
res.SetKind(r.Kind())
res.SetName(r.Name())
if x, ok := r.(MetaRes); ok {
dst, ok := res.(MetaRes)
if !ok {
// programming error
panic("meta interfaces are illogical")
}
dst.SetMetaParams(x.MetaParams().Copy()) // copy b/c we have it
}
if x, ok := r.(RefreshableRes); ok {
dst, ok := res.(RefreshableRes)
if !ok {
// programming error
panic("refresh interfaces are illogical")
}
dst.SetRefresh(x.Refresh()) // no need to copy atm
}
// copy meta params for resources with auto edges
if x, ok := r.(EdgeableRes); ok {
dst, ok := res.(EdgeableRes)
if !ok {
// programming error
panic("autoedge interfaces are illogical")
}
dst.SetAutoEdgeMeta(x.AutoEdgeMeta()) // no need to copy atm
}
// copy meta params for resources with auto grouping
if x, ok := r.(GroupableRes); ok {
dst, ok := res.(GroupableRes)
if !ok {
// programming error
panic("autogroup interfaces are illogical")
}
dst.SetAutoGroupMeta(x.AutoGroupMeta()) // no need to copy atm
grouped := []GroupableRes{}
for _, g := range x.GetGroup() {
g0, ok := g.(CopyableRes)
if !ok {
return nil, fmt.Errorf("resource wasn't copyable")
}
g1, err := ResCopy(g0)
if err != nil {
return nil, err
}
g2, ok := g1.(GroupableRes)
if !ok {
return nil, fmt.Errorf("resource wasn't groupable")
}
g2.SetParent(dst) // store who my parent is
grouped = append(grouped, g2)
}
dst.SetGroup(grouped)
}
if x, ok := r.(RecvableRes); ok {
dst, ok := res.(RecvableRes)
if !ok {
// programming error
panic("recv interfaces are illogical")
}
dst.SetRecv(x.Recv()) // no need to copy atm
}
if x, ok := r.(SendableRes); ok {
dst, ok := res.(SendableRes)
if !ok {
// programming error
panic("send interfaces are illogical")
}
if err := dst.Send(x.Sent()); err != nil { // no need to copy atm
return nil, errwrap.Wrapf(err, "can't copy send")
}
}
// copy meta params for resources with reversible traits
if x, ok := r.(ReversibleRes); ok {
dst, ok := res.(ReversibleRes)
if !ok {
// programming error
panic("reversible interfaces are illogical")
}
dst.SetReversibleMeta(x.ReversibleMeta()) // no need to copy atm
}
return res, nil
}
// ResMerge merges a set of resources that are compatible with each other. This
// is the main entry point for the merging. They must each successfully be able
// to run AdaptCmp without error.
func ResMerge(r ...CompatibleRes) (CompatibleRes, error) {
if len(r) == 0 {
return nil, fmt.Errorf("zero resources given")
}
if len(r) == 1 {
return r[0], nil
}
if len(r) > 2 {
r0 := r[0]
r1, err := ResMerge(r[1:]...)
if err != nil {
return nil, err
}
return ResMerge(r0, r1)
}
// now we have r[0] and r[1] to merge here...
r0 := r[0]
r1 := r[1]
if err := AdaptCmp(r0, r1); err != nil {
return nil, err
}
res, err := r0.Merge(r1) // resource method of this interface
if err != nil {
return nil, err
}
// meta should have come over in the copy
if x, ok := res.(RefreshableRes); ok {
x0, ok0 := r0.(RefreshableRes)
x1, ok1 := r1.(RefreshableRes)
if !ok0 || !ok1 {
// programming error
panic("refresh interfaces are illogical")
}
x.SetRefresh(x0.Refresh() || x1.Refresh()) // true if either is!
}
// the other traits and metaparams can't be merged easily... so we don't
// merge them, and if they were present and differed, and weren't copied
// in the ResCopy method, then we should have errored above in AdaptCmp!
return res, nil
}