-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplan.ts
More file actions
130 lines (109 loc) · 3.6 KB
/
plan.ts
File metadata and controls
130 lines (109 loc) · 3.6 KB
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
import { ParameterOperation, PlanResponseData, ResourceConfig, ResourceOperation } from '@codifycli/schemas';
import { getId } from '../utils/index.js';
import { Project } from './project.js';
export class Plan {
raw: PlanResponseData[];
resources: ResourcePlan[];
project: Project;
constructor(resourcePlans: ResourcePlan[], project: Project) {
this.raw = resourcePlans.map((r) => r.raw);
this.resources = resourcePlans
this.project = project
}
// This sorting is necessary with parallel plans because the order may be jumbled in the process
sortByEvalOrder(evalOrder: null | string[]) {
if (!evalOrder) {
return;
}
this.raw.sort((a, b) => {
const aId = getId(a.resourceType, a.resourceName);
const bId = getId(b.resourceType, b.resourceName);
const indexA = evalOrder.indexOf(aId);
const indexB = evalOrder.indexOf(bId);
return indexA > indexB ? 1 : -1;
});
this.resources.sort((a, b) => {
const aId = getId(a.resourceType, a.resourceName);
const bId = getId(b.resourceType, b.resourceName);
const indexA = evalOrder.indexOf(aId);
const indexB = evalOrder.indexOf(bId);
return indexA > indexB ? 1 : -1;
})
}
getResourcePlan(id: string): ResourcePlan | null {
return this.resources.find((r) => r.id === id) ?? null;
}
filterNoopResources(): Plan {
return new Plan(this.resources.filter((r) => r.operation !== ResourceOperation.NOOP), this.project)
}
// If every operation is no-op then a plan is considered empty
isEmpty() {
return this.raw.every((r) => r.operation === ResourceOperation.NOOP);
}
computeTransitiveDependents(failedId: string): Set<string> {
const reverseDeps = new Map<string, Set<string>>();
for (const r of this.project.resourceConfigs) {
for (const depId of r.dependencyIds) {
if (!reverseDeps.has(depId)) reverseDeps.set(depId, new Set());
reverseDeps.get(depId)!.add(r.id);
}
}
const toSkip = new Set<string>();
const queue = [failedId];
while (queue.length > 0) {
const current = queue.shift()!;
const dependents = reverseDeps.get(current) ?? new Set();
for (const dep of dependents) {
if (!toSkip.has(dep)) {
toSkip.add(dep);
queue.push(dep);
}
}
}
return toSkip;
}
*[Symbol.iterator](): Iterator<ResourcePlan> {
for (const resource of this.resources) {
yield resource;
}
}
}
export class ResourcePlan {
raw: PlanResponseData;
planId: string;
operation: ResourceOperation;
resourceName?: string;
resourceType: string;
isStateful: boolean;
parameters: Array<{
name: string;
newValue: null | unknown;
operation: ParameterOperation;
previousValue: null | unknown;
isSensitive?: boolean;
}>
constructor(json: PlanResponseData) {
this.raw = json;
this.planId = json.planId;
this.isStateful = json.isStateful
this.operation = json.operation;
this.resourceName = json.resourceName;
this.resourceType = json.resourceType;
this.parameters = json.parameters;
}
get id(): string {
return (this.resourceName) ? `${this.resourceType}.${this.resourceName}` : this.resourceType;
}
get desiredConfig(): ResourceConfig {
return this.raw.parameters.reduce((obj, parameter) => {
obj[parameter.name] = parameter.newValue;
return obj;
}, {} as ResourceConfig);
}
get currentConfig(): ResourceConfig {
return this.raw.parameters.reduce((obj, parameter) => {
obj[parameter.name] = parameter.previousValue;
return obj;
}, {} as ResourceConfig);
}
}