Skip to content

Commit

Permalink
ResourceGroupsTaggingAPI: Fix Workspaces access in unsupported region (
Browse files Browse the repository at this point in the history
  • Loading branch information
viren-nadkarni committed Apr 6, 2024
1 parent 5c1d1d0 commit 3f121df
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
21 changes: 15 additions & 6 deletions moto/resourcegroupstaggingapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ def dynamodb_backend(self) -> DynamoDBBackend:
return dynamodb_backends[self.account_id][self.region_name]

@property
def workspaces_backend(self) -> WorkSpacesBackend:
return workspaces_backends[self.account_id][self.region_name]
def workspaces_backend(self) -> Optional[WorkSpacesBackend]:
# Workspaces service has limited region availability
if self.region_name in workspaces_backends[self.account_id].regions:
return workspaces_backends[self.account_id][self.region_name]
return None

def _get_resources_generator(
self,
Expand Down Expand Up @@ -537,7 +540,9 @@ def format_tag_keys(
}

# Workspaces
if not resource_type_filters or "workspaces" in resource_type_filters:
if self.workspaces_backend and (
not resource_type_filters or "workspaces" in resource_type_filters
):
for ws in self.workspaces_backend.workspaces.values():
tags = format_tag_keys(ws.tags, ["Key", "Value"])
if not tags or not tag_filter(
Expand All @@ -551,7 +556,9 @@ def format_tag_keys(
}

# Workspace Directories
if not resource_type_filters or "workspaces-directory" in resource_type_filters:
if self.workspaces_backend and (
not resource_type_filters or "workspaces-directory" in resource_type_filters
):
for wd in self.workspaces_backend.workspace_directories.values():
tags = format_tag_keys(wd.tags, ["Key", "Value"])
if not tags or not tag_filter(
Expand All @@ -565,7 +572,9 @@ def format_tag_keys(
}

# Workspace Images
if not resource_type_filters or "workspaces-image" in resource_type_filters:
if self.workspaces_backend and (
not resource_type_filters or "workspaces-image" in resource_type_filters
):
for wi in self.workspaces_backend.workspace_images.values():
tags = format_tag_keys(wi.tags, ["Key", "Value"])
if not tags or not tag_filter(
Expand Down Expand Up @@ -932,7 +941,7 @@ def tag_resources(
)
elif arn.startswith("arn:aws:workspaces:"):
resource_id = arn.split("/")[-1]
self.workspaces_backend.create_tags(
self.workspaces_backend.create_tags( # type: ignore[union-attr]
resource_id, TaggingService.convert_dict_to_tags_input(tags)
)
elif arn.startswith("arn:aws:logs:"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@mock_aws
def test_glue_jobs():
glue = boto3.client("glue", region_name="us-west-2")
glue = boto3.client("glue", region_name="us-west-1")
tag_key = str(uuid4())[0:6]
tag_val = str(uuid4())[0:6]
job_name = glue.create_job(
Expand All @@ -17,9 +17,9 @@ def test_glue_jobs():
Command={"Name": "test_command"},
Tags={tag_key: tag_val},
)["Name"]
job_arn = f"arn:aws:glue:us-west-2:{DEFAULT_ACCOUNT_ID}:job/{job_name}"
job_arn = f"arn:aws:glue:us-west-1:{DEFAULT_ACCOUNT_ID}:job/{job_name}"

rtapi = boto3.client("resourcegroupstaggingapi", region_name="us-west-2")
rtapi = boto3.client("resourcegroupstaggingapi", region_name="us-west-1")
resources = rtapi.get_resources(ResourceTypeFilters=["glue"])[
"ResourceTagMappingList"
]
Expand Down

0 comments on commit 3f121df

Please sign in to comment.