Skip to content

Commit e213078

Browse files
committed
wip
1 parent 8943b5c commit e213078

File tree

1 file changed

+58
-33
lines changed

1 file changed

+58
-33
lines changed

codebundles/aws-c7n-tag-compliance/runbook.robot

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ Process Resources
114114
${dirs}= RW.CLI.Run Cli
115115
... cmd=find ${OUTPUT_DIR}/aws-c7n-tag-compliance/${region} -mindepth 1 -maxdepth 1 -type d | jq -R -s 'split("\n") | map(select(length > 0))';
116116

117+
# Add region header to report
118+
RW.Core.Add Pre To Report === Region: ${region} ===
119+
117120
TRY
118121
${dir_list}= Evaluate json.loads(r'''${dirs.stdout}''') json
119122
Log ${dirs.stdout}
@@ -122,6 +125,10 @@ Process Resources
122125
RETURN
123126
END
124127

128+
# Initialize region-specific issue tracking
129+
${region_issues}= Create Dictionary
130+
${region_resources}= Create List
131+
125132
IF len(@{dir_list}) > 0
126133
FOR ${dir} IN @{dir_list}
127134
${report_data}= RW.CLI.Run Cli
@@ -138,52 +145,70 @@ Process Resources
138145
END
139146

140147
IF len(@{resource_list}) > 0
141-
${pretty_resource_list}= Evaluate pprint.pformat(${resource_list}) modules=pprint
142-
RW.Core.Add Pre To Report ${pretty_resource_list}
143148
${resource_type}= Set Variable ${meta_list["policy"]["resource"]}
144149
${resource_type_title}= Set Variable ${resource_type.title()}
145-
${resource_id}= Set Variable ${EMPTY}
150+
151+
# Load resource ID mappings
152+
TRY
153+
${resource_id_mappings_json}= RW.CLI.Run Cli cmd=cat ${CURDIR}/resource_id_mappings.json
154+
${resource_id_mapping}= Evaluate json.loads(r'''${resource_id_mappings_json.stdout}''') json
155+
EXCEPT
156+
Log Failed to load resource_id_mappings.json, using default mapping WARN
157+
${resource_id_mapping}= Create Dictionary
158+
END
159+
160+
# Collect all resources with missing tags
146161
FOR ${item} IN @{resource_list}
147-
${pretty_item}= Evaluate pprint.pformat(${item}) modules=pprint
148162
${matched_filters}= Set Variable ${item["c7n:MatchedFilters"]}
149163
${cleaned_tags}= Create List
150164
FOR ${tag} IN @{matched_filters}
151165
${cleaned_tag}= Replace String ${tag} tag: ${EMPTY}
152166
Append To List ${cleaned_tags} ${cleaned_tag}
153167
END
154168
${missing_tags}= Evaluate ", ".join($cleaned_tags)
155-
# Load resource ID mappings from external JSON file
156-
TRY
157-
${resource_id_mappings_json}= RW.CLI.Run Cli cmd=cat ${CURDIR}/resource_id_mappings.json
158-
${resource_id_mapping}= Evaluate json.loads(r'''${resource_id_mappings_json.stdout}''') json
159-
EXCEPT
160-
Log Failed to load resource_id_mappings.json, using default mapping WARN
161-
${resource_id_mapping}= Create Dictionary
162-
END
163-
164169
${resource_id}= Set Variable ${resource_id_mapping.get('${resource_type}')}
165-
IF len("${resource_id}") > 0
166-
RW.Core.Add Issue
167-
... severity=4
168-
... expected=AWS `${resource_type_title}` `${item['${resource_id}']}` in AWS Region `${region}` in AWS account `${AWS_ACCOUNT_ID}` should have the following Tags `${missing_tags}`.
169-
... actual=AWS `${resource_type_title}` `${item['${resource_id}']}` in AWS Region `${region}` in AWS account `${AWS_ACCOUNT_ID}` missing tags `${missing_tags}`
170-
... title=Missing tags `${missing_tags}` on `${resource_type_title}` `${item['${resource_id}']} detected in AWS Account `${AWS_ACCOUNT_ID}`
171-
... reproduce_hint=${c7n_output.cmd}
172-
... details=${pretty_item}
173-
... next_steps=Add missing tags `${missing_tags}` to AWS `${resource_type_title}` in AWS region `${region}` and AWS account `${AWS_ACCOUNT_ID}`.
174-
ELSE
175-
RW.Core.Add Issue
176-
... severity=4
177-
... expected=AWS `${resource_type_title}` in AWS Region `${region}` in AWS account `${AWS_ACCOUNT_ID}` should have the following Tags `${AWS_TAGS}`.
178-
... actual=AWS `${resource_type_title}` in AWS Region `${region}` in AWS account `${AWS_ACCOUNT_ID}` missing tags `${AWS_TAGS}`
179-
... title=Missing tags `${AWS_TAGS}` on `${resource_type_title}` detected in AWS Account `${AWS_ACCOUNT_ID}`
180-
... reproduce_hint=${c7n_output.cmd}
181-
... details=${pretty_item}
182-
... next_steps=Escalate to the service owner to review AWS ${resource_type_title} in AWS region `${region}` and AWS account `${AWS_ACCOUNT_ID}` for missing tags: `${AWS_TAGS}`.\nAdd missing tags `${AWS_TAGS}` to AWS `${RESOURCE_TYPE}` in AWS region `${region}` and AWS account `${AWS_ACCOUNT_ID}`.
183-
END
170+
171+
# Add resource to region-specific list
172+
${resource_details}= Create Dictionary
173+
... type=${resource_type_title}
174+
... id=${item['${resource_id}'] if len("${resource_id}") > 0 else "N/A"}
175+
... missing_tags=${missing_tags}
176+
Append To List ${region_resources} ${resource_details}
184177
END
185178
END
186179
END
180+
181+
# If we found resources with issues in this region
182+
IF len(@{region_resources}) > 0
183+
# Create markdown table of resources
184+
${table_header}= Set Variable | Resource Type | Resource ID | Missing Tags |\n|--------------|-------------|--------------|
185+
${table_rows}= Create List
186+
FOR ${resource} IN @{region_resources}
187+
${row}= Set Variable | ${resource['type']} | ${resource['id']} | ${resource['missing_tags']} |
188+
Append To List ${table_rows} ${row}
189+
END
190+
${table}= Set Variable ${table_header}
191+
FOR ${row} IN @{table_rows}
192+
${table}= Set Variable ${table}\n${row}
193+
END
194+
195+
# Add table to report
196+
RW.Core.Add Pre To Report ${table}
197+
198+
# Get the count of resources with issues
199+
${resource_count}= Get Length ${region_resources}
200+
201+
# Create single issue per region
202+
RW.Core.Add Issue
203+
... severity=4
204+
... expected=All resources in AWS Region `${region}` should have the following Tags `${AWS_TAGS}`.
205+
... actual=Found ${resource_count} resources in AWS Region `${region}` missing required tags.
206+
... title=Missing tags `${AWS_TAGS}` detected in AWS Region `${region}`
207+
... reproduce_hint=${c7n_output.cmd}
208+
... details=The following resources are missing required tags:\n\n${table}
209+
... next_steps=Apply missing tags `${AWS_TAGS}` to resources in AWS region `${region}` and AWS account `${AWS_ACCOUNT_ID}`.
210+
END
187211
ELSE
188-
Log No directories found to process. WARN
212+
Log No resources found in region ${region}. INFO
213+
RW.Core.Add Pre To Report No resources found in region ${region}.
189214
END

0 commit comments

Comments
 (0)