-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
operations.go
196 lines (163 loc) 路 6.1 KB
/
operations.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
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2016-2022, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package edit
import (
"fmt"
"github.com/pulumi/pulumi/pkg/v3/resource/deploy"
"github.com/pulumi/pulumi/pkg/v3/resource/deploy/providers"
"github.com/pulumi/pulumi/pkg/v3/resource/graph"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)
// OperationFunc is the type of functions that edit resources within a snapshot. The edits are made in-place to the
// given snapshot and pertain to the specific passed-in resource.
type OperationFunc func(*deploy.Snapshot, *resource.State) error
// DeleteResource deletes a given resource from the snapshot, if it is possible to do so.
//
// If targetDependents is true, dependents will also be deleted. Otherwise an error
// instance of `ResourceHasDependenciesError` will be returned.
//
// If non-nil, onProtected will be called on all protected resources planed for deletion.
//
// If a resource is marked protected after onProtected is called, an error instance of
// `ResourceHasDependenciesError` will be returned.
func DeleteResource(
snapshot *deploy.Snapshot, condemnedRes *resource.State,
onProtected func(*resource.State) error, targetDependents bool,
) error {
contract.Require(snapshot != nil, "snapshot")
contract.Require(condemnedRes != nil, "state")
handleProtected := func(res *resource.State) error {
if !res.Protect {
return nil
}
var err error
if onProtected != nil {
err = onProtected(res)
}
if err == nil && res.Protect {
err = ResourceProtectedError{res}
}
return err
}
if err := handleProtected(condemnedRes); err != nil {
return err
}
dg := graph.NewDependencyGraph(snapshot.Resources)
deleteSet := map[resource.URN]bool{
condemnedRes.URN: true,
}
if dependencies := dg.DependingOn(condemnedRes, nil, true); len(dependencies) != 0 {
if !targetDependents {
return ResourceHasDependenciesError{Condemned: condemnedRes, Dependencies: dependencies}
}
for _, dep := range dependencies {
err := handleProtected(dep)
if err != nil {
return err
}
deleteSet[dep.URN] = true
}
}
// If there are no resources that depend on condemnedRes, iterate through the snapshot and keep everything that's
// not condemnedRes.
var newSnapshot []*resource.State
var children []*resource.State
for _, res := range snapshot.Resources {
// While iterating, keep track of the set of resources that are parented to our
// condemned resource. This acts as a check on DependingOn, preventing a bug from
// introducing state corruption.
if res.Parent == condemnedRes.URN && !deleteSet[res.URN] {
children = append(children, res)
}
if !deleteSet[res.URN] {
newSnapshot = append(newSnapshot, res)
}
}
// If there exists a resource that is the child of condemnedRes, we can't delete it.
contract.Assertf(len(children) == 0, "unexpected children in resource dependency list")
// Otherwise, we're good to go. Writing the new resource list into the snapshot persists the mutations that we have
// made above.
snapshot.Resources = newSnapshot
return nil
}
// UnprotectResource unprotects a resource.
func UnprotectResource(_ *deploy.Snapshot, res *resource.State) error {
res.Protect = false
return nil
}
// LocateResource returns all resources in the given snapshot that have the given URN.
func LocateResource(snap *deploy.Snapshot, urn resource.URN) []*resource.State {
// If there is no snapshot then return no resources
if snap == nil {
return nil
}
var resources []*resource.State
for _, res := range snap.Resources {
if res.URN == urn {
resources = append(resources, res)
}
}
return resources
}
// RenameStack changes the `stackName` component of every URN in a snapshot. In addition, it rewrites the name of
// the root Stack resource itself. May optionally change the project/package name as well.
func RenameStack(snap *deploy.Snapshot, newName tokens.Name, newProject tokens.PackageName) error {
contract.Require(snap != nil, "snap")
rewriteUrn := func(u resource.URN) resource.URN {
project := u.Project()
if newProject != "" {
project = newProject
}
// The pulumi:pulumi:Stack resource's name component is of the form `<project>-<stack>` so we want
// to rename the name portion as well.
if u.QualifiedType() == "pulumi:pulumi:Stack" {
return resource.NewURN(newName.Q(), project, "", u.QualifiedType(), tokens.QName(project)+"-"+newName.Q())
}
return resource.NewURN(newName.Q(), project, "", u.QualifiedType(), u.Name())
}
rewriteState := func(res *resource.State) {
contract.Assert(res != nil)
res.URN = rewriteUrn(res.URN)
if res.Parent != "" {
res.Parent = rewriteUrn(res.Parent)
}
for depIdx, dep := range res.Dependencies {
res.Dependencies[depIdx] = rewriteUrn(dep)
}
for _, propDeps := range res.PropertyDependencies {
for depIdx, dep := range propDeps {
propDeps[depIdx] = rewriteUrn(dep)
}
}
if res.Provider != "" {
providerRef, err := providers.ParseReference(res.Provider)
contract.AssertNoErrorf(err, "failed to parse provider reference from validated checkpoint")
providerRef, err = providers.NewReference(rewriteUrn(providerRef.URN()), providerRef.ID())
contract.AssertNoErrorf(err, "failed to generate provider reference from valid reference")
res.Provider = providerRef.String()
}
}
if err := snap.VerifyIntegrity(); err != nil {
return fmt.Errorf("checkpoint is invalid: %w", err)
}
for _, res := range snap.Resources {
rewriteState(res)
}
for _, ops := range snap.PendingOperations {
rewriteState(ops.Resource)
}
return nil
}