Skip to content

Commit 72daf33

Browse files
committed
Fixes for control flow.
1 parent a7be2cb commit 72daf33

2 files changed

Lines changed: 38 additions & 19 deletions

File tree

python/example_code/controltower/controltower_wrapper.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ def enable_control(self, control_arn, target_identifier):
187187
):
188188
logger.info("Control is already enabled for this target")
189189
return None
190+
elif (err.response["Error"]["Code"] == "ResourceNotFoundException"
191+
and "not registered with AWS Control Tower" in err.response["Error"]["Message"]):
192+
logger.error("Control Tower must be enabled to work with controls.")
193+
return None
190194
logger.error(
191195
"Couldn't enable control. Here's why: %s: %s",
192196
err.response["Error"]["Code"],
@@ -427,9 +431,10 @@ def list_enabled_controls(self, target_identifier):
427431
:return: List of enabled controls.
428432
:raises ClientError: If the listing operation fails.
429433
"""
434+
enabled_controls = []
430435
try:
431436
paginator = self.controltower_client.get_paginator("list_enabled_controls")
432-
enabled_controls = []
437+
433438
for page in paginator.paginate(targetIdentifier=target_identifier):
434439
enabled_controls.extend(page["enabledControls"])
435440
return enabled_controls
@@ -439,6 +444,11 @@ def list_enabled_controls(self, target_identifier):
439444
logger.error(
440445
"Access denied. Please ensure you have the necessary permissions."
441446
)
447+
return enabled_controls
448+
elif (err.response["Error"]["Code"] == "ResourceNotFoundException"
449+
and "not registered with AWS Control Tower" in err.response["Error"]["Message"]):
450+
logger.error("Control Tower must be enabled to work with controls.")
451+
return enabled_controls
442452
else:
443453
logger.error(
444454
"Couldn't list enabled controls. Here's why: %s: %s",

scenarios/basics/controltower/SPECIFICATION.md

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,14 @@ Found existing Sandbox OU: ou-spdo-e3mtcidv
8585
- List available baselines.
8686
- If a landing zone exists:
8787
- List enabled baselines.
88-
- Prompt the user if they would like to enable another baseline from the list.
89-
- Get the operational status of the baseline operation.
90-
- Reset the baseline.
91-
- Disable the baseline.
88+
- Prompt the user if they would like to enable the Control Tower baseline.
89+
- Notify the user if this baseline is already enabled.
90+
- Prompt the user if they would like to reset the baseline.
91+
- Prompt the user if they would like to disable the baseline.
92+
- For all baseline operations, wait for a successful status before moving on.
93+
- Get the operational status of the baseline operation.
94+
- Re-enable the Control Tower baseline if it was initially enabled.
95+
- The control tower baseline must be enabled in order to work with enabling/disabling controls.
9296

9397
Example
9498
```
@@ -140,10 +144,12 @@ and so is included as part of this example.
140144

141145
- List Controls in Control Catalog.
142146
- If a landing zone exists:
143-
- Enable a control.
144-
- Get the operational status of the control.
145147
- List enabled controls.
146-
- Disable the control.
148+
- Prompt the user if they would like to enable the first control.
149+
- Notify the user if this control is already enabled.
150+
- Prompt the user if they would like to disable the control.
151+
- For all control operations, wait for a success status before moving on.
152+
- Get the operational status of the control
147153

148154
Example
149155
```
@@ -203,7 +209,9 @@ The following errors are handled in the Control Tower wrapper class:
203209
| `EnableBaseline` | ValidationException | Handle case where baseline is already enabled and return None. |
204210
| `DisableBaseline` | ConflictException | Notify the user that the baseline could not be disabled, and continue. |
205211
| `ListControls` | AccessDeniedException | Notify the user of insufficient permissions and exit. |
212+
| `ListEnabledControls` | ResourceNotFound | Check for "not registered with AWS Control Tower" and notify the user. |
206213
| `EnableControl` | ValidationException | Handle case where control is already enabled and return None. |
214+
| `EnableControl` | ResourceNotFound | Check for "not registered with AWS Control Tower" and notify the user. |
207215
| `GetControlOperation` | ResourceNotFound | Notify the user that the control operation was not found. |
208216
| `DisableControl` | ResourceNotFound | Notify the user that the control was not found. |
209217
| `ListLandingZones` | AccessDeniedException | Notify the user of insufficient permissions and exit. |
@@ -213,16 +221,17 @@ The following errors are handled in the Control Tower wrapper class:
213221

214222
## Metadata
215223

216-
| action / scenario | metadata file | metadata key |
217-
|---------------------------------|----------------------------|-----------------------------------|
218-
| `ListBaselines` | controltower_metadata.yaml | controltower_Hello |
219-
| `ListBaselines` | controltower_metadata.yaml | controltower_ListBaselines |
220-
| `ListEnabledBaselines` | controltower_metadata.yaml | controltower_ListEnabledBaselines |
221-
| `EnableBaseline` | controltower_metadata.yaml | controltower_EnableBaseline |
222-
| `DisableBaseline` | controltower_metadata.yaml | controltower_DisableBaseline |
223-
| `EnableControl` | controltower_metadata.yaml | controltower_EnableControl |
224-
| `GetControlOperation` | controltower_metadata.yaml | controltower_GetControlOperation |
225-
| `DisableControl` | controltower_metadata.yaml | controltower_DisableControl |
226-
| `ListLandingZones` | controltower_metadata.yaml | controltower_ListLandingZones |
224+
| action / scenario | metadata file | metadata key |
225+
|--------------------------------|----------------------------|-----------------------------------|
226+
| `ListBaselines` | controltower_metadata.yaml | controltower_Hello |
227+
| `ListBaselines` | controltower_metadata.yaml | controltower_ListBaselines |
228+
| `ListEnabledBaselines` | controltower_metadata.yaml | controltower_ListEnabledBaselines |
229+
| `EnableBaseline` | controltower_metadata.yaml | controltower_EnableBaseline |
230+
| `DisableBaseline` | controltower_metadata.yaml | controltower_DisableBaseline |
231+
| `ListEnabledControls` | controltower_metadata.yaml | controltower_ListEnabledControls |
232+
| `EnableControl` | controltower_metadata.yaml | controltower_EnableControl |
233+
| `GetControlOperation` | controltower_metadata.yaml | controltower_GetControlOperation |
234+
| `DisableControl` | controltower_metadata.yaml | controltower_DisableControl |
235+
| `ListLandingZones` | controltower_metadata.yaml | controltower_ListLandingZones |
227236
| `Control Tower Basics Scenario` | controltower_metadata.yaml | controltower_Scenario |
228237

0 commit comments

Comments
 (0)