2019-05-25 10:10:34 +00:00
|
|
|
import json
|
2023-08-18 07:48:01 +00:00
|
|
|
|
|
|
|
import boto3
|
2019-05-25 10:10:34 +00:00
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
from moto import mock_aws
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
|
2022-11-29 19:22:09 +00:00
|
|
|
def create_group(client):
|
|
|
|
return client.create_group(
|
2019-05-25 10:10:34 +00:00
|
|
|
Name="test_resource_group",
|
|
|
|
Description="description",
|
|
|
|
ResourceQuery={
|
|
|
|
"Type": "TAG_FILTERS_1_0",
|
|
|
|
"Query": json.dumps(
|
|
|
|
{
|
|
|
|
"ResourceTypeFilters": ["AWS::AllSupported"],
|
|
|
|
"TagFilters": [
|
|
|
|
{"Key": "resources_tag_key", "Values": ["resources_tag_value"]}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
),
|
|
|
|
},
|
|
|
|
Tags={"resource_group_tag_key": "resource_group_tag_value"},
|
|
|
|
)
|
2022-11-29 19:22:09 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-11-29 19:22:09 +00:00
|
|
|
def test_create_group():
|
|
|
|
resource_groups = boto3.client("resource-groups", region_name="us-east-1")
|
|
|
|
|
|
|
|
response = create_group(client=resource_groups)
|
2023-08-18 07:48:01 +00:00
|
|
|
assert "test_resource_group" in response["Group"]["Name"]
|
|
|
|
assert "TAG_FILTERS_1_0" in response["ResourceQuery"]["Type"]
|
|
|
|
assert "resource_group_tag_value" in response["Tags"]["resource_group_tag_key"]
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-05-25 10:10:34 +00:00
|
|
|
def test_delete_group():
|
|
|
|
resource_groups = boto3.client("resource-groups", region_name="us-east-1")
|
|
|
|
|
2022-11-29 19:22:09 +00:00
|
|
|
create_group(client=resource_groups)
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
response = resource_groups.delete_group(GroupName="test_resource_group")
|
2023-08-18 07:48:01 +00:00
|
|
|
assert "test_resource_group" in response["Group"]["Name"]
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
response = resource_groups.list_groups()
|
2023-08-18 07:48:01 +00:00
|
|
|
assert len(response["GroupIdentifiers"]) == 0
|
|
|
|
assert len(response["Groups"]) == 0
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-05-25 10:10:34 +00:00
|
|
|
def test_get_group():
|
|
|
|
resource_groups = boto3.client("resource-groups", region_name="us-east-1")
|
|
|
|
|
2022-11-29 19:22:09 +00:00
|
|
|
create_group(client=resource_groups)
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
response = resource_groups.get_group(GroupName="test_resource_group")
|
2023-08-18 07:48:01 +00:00
|
|
|
assert "description" in response["Group"]["Description"]
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-05-25 10:10:34 +00:00
|
|
|
def test_get_group_query():
|
|
|
|
resource_groups = boto3.client("resource-groups", region_name="us-east-1")
|
|
|
|
|
2022-11-29 19:22:09 +00:00
|
|
|
group_arn = create_group(resource_groups)["Group"]["GroupArn"]
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
response = resource_groups.get_group_query(GroupName="test_resource_group")
|
2023-08-18 07:48:01 +00:00
|
|
|
assert "TAG_FILTERS_1_0" in response["GroupQuery"]["ResourceQuery"]["Type"]
|
2019-05-25 10:10:34 +00:00
|
|
|
|
2022-11-29 19:22:09 +00:00
|
|
|
response_get = resource_groups.get_group_query(Group=group_arn)
|
2023-08-18 07:48:01 +00:00
|
|
|
assert "TAG_FILTERS_1_0" in response_get["GroupQuery"]["ResourceQuery"]["Type"]
|
2021-05-18 19:58:21 +00:00
|
|
|
|
2019-05-25 10:10:34 +00:00
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-05-25 10:10:34 +00:00
|
|
|
def test_get_tags():
|
|
|
|
resource_groups = boto3.client("resource-groups", region_name="us-east-1")
|
|
|
|
|
2022-11-29 19:22:09 +00:00
|
|
|
group_arn = create_group(resource_groups)["Group"]["GroupArn"]
|
2019-05-25 10:10:34 +00:00
|
|
|
|
2022-11-29 19:22:09 +00:00
|
|
|
response = resource_groups.get_tags(Arn=group_arn)
|
2023-08-18 07:48:01 +00:00
|
|
|
assert len(response["Tags"]) == 1
|
|
|
|
assert "resource_group_tag_value" in response["Tags"]["resource_group_tag_key"]
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-05-25 10:10:34 +00:00
|
|
|
def test_list_groups():
|
|
|
|
resource_groups = boto3.client("resource-groups", region_name="us-east-1")
|
|
|
|
|
2022-11-29 19:22:09 +00:00
|
|
|
create_group(resource_groups)
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
response = resource_groups.list_groups()
|
2023-08-18 07:48:01 +00:00
|
|
|
assert len(response["GroupIdentifiers"]) == 1
|
|
|
|
assert len(response["Groups"]) == 1
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-05-25 10:10:34 +00:00
|
|
|
def test_tag():
|
|
|
|
resource_groups = boto3.client("resource-groups", region_name="us-east-1")
|
|
|
|
|
2022-11-29 19:22:09 +00:00
|
|
|
group_arn = create_group(resource_groups)["Group"]["GroupArn"]
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
response = resource_groups.tag(
|
2022-11-29 19:22:09 +00:00
|
|
|
Arn=group_arn,
|
2019-05-25 10:10:34 +00:00
|
|
|
Tags={"resource_group_tag_key_2": "resource_group_tag_value_2"},
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2023-08-18 07:48:01 +00:00
|
|
|
assert "resource_group_tag_value_2" in response["Tags"]["resource_group_tag_key_2"]
|
2019-05-25 10:10:34 +00:00
|
|
|
|
2022-11-29 19:22:09 +00:00
|
|
|
response = resource_groups.get_tags(Arn=group_arn)
|
2023-08-18 07:48:01 +00:00
|
|
|
assert len(response["Tags"]) == 2
|
|
|
|
assert "resource_group_tag_value_2" in response["Tags"]["resource_group_tag_key_2"]
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-05-25 10:10:34 +00:00
|
|
|
def test_untag():
|
|
|
|
resource_groups = boto3.client("resource-groups", region_name="us-east-1")
|
|
|
|
|
2022-11-29 19:22:09 +00:00
|
|
|
group_arn = create_group(resource_groups)["Group"]["GroupArn"]
|
2019-05-25 10:10:34 +00:00
|
|
|
|
2022-11-29 19:22:09 +00:00
|
|
|
response = resource_groups.untag(Arn=group_arn, Keys=["resource_group_tag_key"])
|
2023-08-18 07:48:01 +00:00
|
|
|
assert "resource_group_tag_key" in response["Keys"]
|
2019-05-25 10:10:34 +00:00
|
|
|
|
2022-11-29 19:22:09 +00:00
|
|
|
response = resource_groups.get_tags(Arn=group_arn)
|
2023-08-18 07:48:01 +00:00
|
|
|
assert len(response["Tags"]) == 0
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-05-25 10:10:34 +00:00
|
|
|
def test_update_group():
|
|
|
|
resource_groups = boto3.client("resource-groups", region_name="us-east-1")
|
|
|
|
|
2022-11-29 19:22:09 +00:00
|
|
|
create_group(client=resource_groups)
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
response = resource_groups.update_group(
|
|
|
|
GroupName="test_resource_group", Description="description_2"
|
|
|
|
)
|
2023-08-18 07:48:01 +00:00
|
|
|
assert "description_2" in response["Group"]["Description"]
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
response = resource_groups.get_group(GroupName="test_resource_group")
|
2023-08-18 07:48:01 +00:00
|
|
|
assert "description_2" in response["Group"]["Description"]
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-05-14 06:40:17 +00:00
|
|
|
def test_get_group_configuration():
|
|
|
|
resource_groups = boto3.client("resource-groups", region_name="us-east-1")
|
|
|
|
|
2022-11-29 19:22:09 +00:00
|
|
|
group = create_group(client=resource_groups)
|
2021-05-14 06:40:17 +00:00
|
|
|
|
|
|
|
configuration = [
|
|
|
|
{
|
|
|
|
"Type": "AWS::ResourceGroups::Generic",
|
|
|
|
"Parameters": [
|
|
|
|
{"Name": "allowed-resource-types", "Values": ["AWS::EC2::Host"]},
|
|
|
|
{"Name": "deletion-protection", "Values": ["UNLESS_EMPTY"]},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
resource_groups.put_group_configuration(
|
|
|
|
Group=group["Group"]["Name"], Configuration=configuration
|
|
|
|
)
|
|
|
|
|
|
|
|
configuration_resp = resource_groups.get_group_configuration(
|
|
|
|
Group=group["Group"]["Name"]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert (
|
|
|
|
configuration_resp.get("GroupConfiguration").get("Configuration")
|
|
|
|
== configuration
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-05-14 06:40:17 +00:00
|
|
|
def test_create_group_with_configuration():
|
|
|
|
resource_groups = boto3.client("resource-groups", region_name="us-east-1")
|
|
|
|
|
|
|
|
configuration = [
|
|
|
|
{
|
|
|
|
"Type": "AWS::ResourceGroups::Generic",
|
|
|
|
"Parameters": [
|
|
|
|
{"Name": "allowed-resource-types", "Values": ["AWS::EC2::Host"]},
|
|
|
|
{"Name": "deletion-protection", "Values": ["UNLESS_EMPTY"]},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
]
|
|
|
|
response = resource_groups.create_group(
|
|
|
|
Name="test_resource_group_new",
|
|
|
|
Description="description",
|
|
|
|
ResourceQuery={
|
|
|
|
"Type": "TAG_FILTERS_1_0",
|
|
|
|
"Query": json.dumps(
|
|
|
|
{
|
|
|
|
"ResourceTypeFilters": ["AWS::AllSupported"],
|
|
|
|
"TagFilters": [
|
|
|
|
{"Key": "resources_tag_key", "Values": ["resources_tag_value"]}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
),
|
|
|
|
},
|
|
|
|
Configuration=configuration,
|
|
|
|
Tags={"resource_group_tag_key": "resource_group_tag_value"},
|
|
|
|
)
|
|
|
|
|
2023-08-18 07:48:01 +00:00
|
|
|
assert "test_resource_group_new" in response["Group"]["Name"]
|
2021-05-14 06:40:17 +00:00
|
|
|
|
|
|
|
assert response["GroupConfiguration"]["Configuration"] == configuration
|
2023-08-18 07:48:01 +00:00
|
|
|
assert "resource_group_tag_value" in response["Tags"]["resource_group_tag_key"]
|
2021-05-14 06:40:17 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-05-25 10:10:34 +00:00
|
|
|
def test_update_group_query():
|
|
|
|
resource_groups = boto3.client("resource-groups", region_name="us-east-1")
|
|
|
|
|
2022-11-29 19:22:09 +00:00
|
|
|
group_arn = create_group(resource_groups)["Group"]["GroupArn"]
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
response = resource_groups.update_group_query(
|
|
|
|
GroupName="test_resource_group",
|
|
|
|
ResourceQuery={
|
|
|
|
"Type": "CLOUDFORMATION_STACK_1_0",
|
|
|
|
"Query": json.dumps(
|
|
|
|
{
|
|
|
|
"ResourceTypeFilters": ["AWS::AllSupported"],
|
|
|
|
"StackIdentifier": (
|
|
|
|
"arn:aws:cloudformation:eu-west-1:012345678912:stack/"
|
|
|
|
"test_stack/c223eca0-e744-11e8-8910-500c41f59083"
|
|
|
|
),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)
|
2023-08-18 07:48:01 +00:00
|
|
|
assert "CLOUDFORMATION_STACK_1_0" in response["GroupQuery"]["ResourceQuery"]["Type"]
|
2019-05-25 10:10:34 +00:00
|
|
|
|
|
|
|
response = resource_groups.get_group_query(GroupName="test_resource_group")
|
2023-08-18 07:48:01 +00:00
|
|
|
assert "CLOUDFORMATION_STACK_1_0" in response["GroupQuery"]["ResourceQuery"]["Type"]
|
2021-05-18 19:58:21 +00:00
|
|
|
|
|
|
|
response = resource_groups.update_group_query(
|
2022-11-29 19:22:09 +00:00
|
|
|
Group=group_arn,
|
2021-05-18 19:58:21 +00:00
|
|
|
ResourceQuery={
|
|
|
|
"Type": "TAG_FILTERS_1_0",
|
|
|
|
"Query": json.dumps(
|
|
|
|
{
|
|
|
|
"ResourceTypeFilters": ["AWS::AllSupported"],
|
|
|
|
"TagFilters": [
|
|
|
|
{"Key": "resources_tag_key", "Values": ["resources_tag_value"]}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2023-08-18 07:48:01 +00:00
|
|
|
assert "TAG_FILTERS_1_0" in response["GroupQuery"]["ResourceQuery"]["Type"]
|
2021-05-18 19:58:21 +00:00
|
|
|
|
2022-11-29 19:22:09 +00:00
|
|
|
response = resource_groups.get_group_query(Group=group_arn)
|
2023-08-18 07:48:01 +00:00
|
|
|
assert "TAG_FILTERS_1_0" in response["GroupQuery"]["ResourceQuery"]["Type"]
|