/
workflow.yaml
188 lines (185 loc) · 6.69 KB
/
workflow.yaml
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
main:
params:
- args
steps:
- set_variables:
assign:
- project: ${args.project_id}
- tag_key: ${args.tag_key}
- tag_value: ${args.tag_value}
- retention: ${args.retention}
- timestamp: ${sys.now() - retention}
- snapshots_untagged: []
- start:
call: sys.log
args:
text: '${
"snapshot-release::main " +
"- project = " + project +
", tag_key = " + tag_key +
", tag_value = " + tag_value +
", retention = " + retention
}'
severity: DEBUG
- list_snapshots:
call: googleapis.compute.v1.snapshots.list
args:
project: ${project}
orderBy: creationTimestamp desc
result: snapshots
- process_snapshots:
switch:
- condition: ${"items" in snapshots}
parallel:
shared: [snapshots_untagged]
for:
in: ${snapshots.items}
value: snapshot
steps:
- filter_creationTimestamp:
call: filter_creationTimestamp
args:
project: ${project}
snapshot: ${snapshot}
timestamp: ${timestamp}
result: snapshot_filtered_creationTimestamp
- filter_tag:
call: filter_tag
args:
project: ${project}
snapshot: ${snapshot_filtered_creationTimestamp}
tag_value: ${tag_value}
result: snapshot_filtered_tag
- untag_snapshot:
call: untag_snapshot
args:
project: ${project}
snapshot: ${snapshot_filtered_tag}
tag_key: ${tag_key}
tag_value: ${tag_value}
result: snapshot_untagged
- set_results:
switch:
- condition: ${snapshot_untagged != null}
steps:
- assign_result:
assign:
- snapshots_untagged: ${list.concat(snapshots_untagged, snapshot_untagged)}
- return_data:
return: ${snapshots_untagged}
filter_creationTimestamp:
params:
- project
- snapshot
- timestamp
steps:
- start:
call: sys.log
args:
text: '${
"snapshot-release::filter_creationTimestamp " +
"- project = " + project +
", snapshot.name = " + snapshot.name +
", timestamp = " + timestamp
}'
severity: DEBUG
- compare_timestamps:
switch:
- condition: ${time.parse(snapshot.creationTimestamp) <= timestamp}
steps:
- write_log:
call: sys.log
args:
text: '${
"snapshot-release::filter_creationTimestamp " +
"- " + snapshot.name + " (" + time.parse(snapshot.creationTimestamp) + " <= " + timestamp + ")"
}'
severity: DEBUG
- return_data:
return: ${snapshot}
filter_tag:
params:
- project
- snapshot
- tag_value
steps:
- set_variables:
assign:
- snapshot_result: ${snapshot}
- check_snapshot:
switch:
- condition: ${snapshot != null}
steps:
- start:
call: sys.log
args:
text: '${
"snapshot-release::filter_tag " +
"- project = " + project +
", snapshot.name = " + snapshot.name +
", tag_value = " + tag_value
}'
severity: DEBUG
- retrieve_tag:
call: googleapis.cloudresourcemanager.v3.tagBindings.list
args:
parent: ${"//compute.googleapis.com/projects/" + project + "/global/snapshots/" + snapshot.id}
result: result
- assert_tag:
switch:
- condition: ${len(result) > 0 and len(result.tagBindings) > 0}
for:
value: binding
in: ${result.tagBindings}
steps:
- check_tag_value:
switch:
# Check against the value is sufficient as the value is bound to the key
- condition: ${binding.tagValue == tag_value}
steps:
- assign_result:
assign:
- snapshot_result: null
- return_data:
return: ${snapshot_result}
untag_snapshot:
params:
- project
- snapshot
- tag_key
- tag_value
steps:
- check_snapshot:
switch:
- condition: ${snapshot != null}
steps:
- start:
call: sys.log
args:
text: '${"snapshot-release::untag_snapshots " +
"- project = " + project +
", snapshot.name = " + snapshot.name +
", tag_key = " + tag_key +
", tag_value = " + tag_value
}'
severity: DEBUG
- create_tag_binding:
try:
call: googleapis.cloudresourcemanager.v3.tagBindings.create
args:
body:
parent: ${"//compute.googleapis.com/projects/" + project + "/global/snapshots/" + snapshot.id}
tagValue: ${tag_value}
# Known issue with Cloud Resource Manager API: https://cloud.google.com/workflows/docs/issues#long-running_operation_exception_when_using_cloud_connector
except:
as: error
steps:
- response_type_error:
switch:
- condition: ${"ResponseTypeError" in error.tags and error.operation.done == true}
next: return_data
- unhandled_exception:
raise: ${error}
- return_data:
return: ${snapshot}
next: end