ResourceGroup - Add Support for getGroupConfiguration (#3919)
* ResourceGroup - Add Support for getGroupConfiguration * Add tests * Add tests * Fix tests * linting * fix tests * fix tests * fix tests * fixed linting * fix tests
This commit is contained in:
parent
f9e0595e12
commit
eaac32b130
@ -12,7 +12,9 @@ from .exceptions import BadRequestException
|
||||
|
||||
|
||||
class FakeResourceGroup(BaseModel):
|
||||
def __init__(self, name, resource_query, description=None, tags=None):
|
||||
def __init__(
|
||||
self, name, resource_query, description=None, tags=None, configuration=None
|
||||
):
|
||||
self.errors = []
|
||||
description = description or ""
|
||||
tags = tags or {}
|
||||
@ -28,6 +30,7 @@ class FakeResourceGroup(BaseModel):
|
||||
self.arn = "arn:aws:resource-groups:us-west-1:{AccountId}:{name}".format(
|
||||
name=name, AccountId=ACCOUNT_ID
|
||||
)
|
||||
self.configuration = configuration
|
||||
|
||||
@staticmethod
|
||||
def _format_error(key, value, constraint):
|
||||
@ -297,10 +300,16 @@ class ResourceGroupsBackend(BaseBackend):
|
||||
if tag.lower().startswith("aws:"):
|
||||
raise BadRequestException("Tag keys must not start with 'aws:'")
|
||||
|
||||
def create_group(self, name, resource_query, description=None, tags=None):
|
||||
def create_group(
|
||||
self, name, resource_query, description=None, tags=None, configuration=None
|
||||
):
|
||||
tags = tags or {}
|
||||
group = FakeResourceGroup(
|
||||
name=name, resource_query=resource_query, description=description, tags=tags
|
||||
name=name,
|
||||
resource_query=resource_query,
|
||||
description=description,
|
||||
tags=tags,
|
||||
configuration=configuration,
|
||||
)
|
||||
if name in self.groups:
|
||||
raise BadRequestException("Cannot create group: group already exists")
|
||||
@ -350,6 +359,15 @@ class ResourceGroupsBackend(BaseBackend):
|
||||
self.groups.by_name[group_name].resource_query = resource_query
|
||||
return self.groups.by_name[group_name]
|
||||
|
||||
def get_group_configuration(self, group_name):
|
||||
group = self.groups.by_name.get(group_name)
|
||||
configuration = group.configuration
|
||||
return configuration
|
||||
|
||||
def put_group_configuration(self, group_name, configuration):
|
||||
self.groups.by_name[group_name].configuration = configuration
|
||||
return self.groups.by_name[group_name]
|
||||
|
||||
|
||||
resourcegroups_backends = {}
|
||||
for region in Session().get_available_regions("resource-groups"):
|
||||
|
@ -22,8 +22,13 @@ class ResourceGroupsResponse(BaseResponse):
|
||||
description = self._get_param("Description")
|
||||
resource_query = self._get_param("ResourceQuery")
|
||||
tags = self._get_param("Tags")
|
||||
configuration = self._get_param("Configuration")
|
||||
group = self.resourcegroups_backend.create_group(
|
||||
name=name, description=description, resource_query=resource_query, tags=tags
|
||||
name=name,
|
||||
description=description,
|
||||
resource_query=resource_query,
|
||||
tags=tags,
|
||||
configuration=configuration,
|
||||
)
|
||||
return json.dumps(
|
||||
{
|
||||
@ -34,6 +39,7 @@ class ResourceGroupsResponse(BaseResponse):
|
||||
},
|
||||
"ResourceQuery": group.resource_query,
|
||||
"Tags": group.tags,
|
||||
"GroupConfiguration": {"Configuration": group.configuration},
|
||||
}
|
||||
)
|
||||
|
||||
@ -165,3 +171,18 @@ class ResourceGroupsResponse(BaseResponse):
|
||||
return json.dumps(
|
||||
{"GroupQuery": {"GroupName": group.name, "ResourceQuery": resource_query}}
|
||||
)
|
||||
|
||||
def get_group_configuration(self):
|
||||
group_name = self._get_param("Group")
|
||||
configuration = self.resourcegroups_backend.get_group_configuration(
|
||||
group_name=group_name
|
||||
)
|
||||
return json.dumps({"GroupConfiguration": {"Configuration": configuration}})
|
||||
|
||||
def put_group_configuration(self):
|
||||
group_name = self._get_param("Group")
|
||||
configuration = self._get_param("Configuration")
|
||||
self.resourcegroups_backend.put_group_configuration(
|
||||
group_name=group_name, configuration=configuration
|
||||
)
|
||||
return json.dumps({"GroupConfiguration": {"Configuration": configuration}})
|
||||
|
@ -6,6 +6,8 @@ url_bases = ["https?://resource-groups(-fips)?.(.+).amazonaws.com"]
|
||||
url_paths = {
|
||||
"{0}/delete-group$": ResourceGroupsResponse.dispatch,
|
||||
"{0}/get-group$": ResourceGroupsResponse.dispatch,
|
||||
"{0}/get-group-configuration$": ResourceGroupsResponse.dispatch,
|
||||
"{0}/put-group-configuration$": ResourceGroupsResponse.dispatch,
|
||||
"{0}/get-group-query$": ResourceGroupsResponse.dispatch,
|
||||
"{0}/groups$": ResourceGroupsResponse.dispatch,
|
||||
"{0}/groups/(?P<resource_group_name>[^/]+)$": ResourceGroupsResponse.dispatch,
|
||||
|
@ -147,6 +147,75 @@ def test_update_group():
|
||||
response["Group"]["Description"].should.contain("description_2")
|
||||
|
||||
|
||||
@mock_resourcegroups
|
||||
def test_get_group_configuration():
|
||||
resource_groups = boto3.client("resource-groups", region_name="us-east-1")
|
||||
|
||||
group = test_get_group()
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
|
||||
@mock_resourcegroups
|
||||
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"},
|
||||
)
|
||||
|
||||
response["Group"]["Name"].should.contain("test_resource_group_new")
|
||||
|
||||
assert response["GroupConfiguration"]["Configuration"] == configuration
|
||||
response["Tags"]["resource_group_tag_key"].should.contain(
|
||||
"resource_group_tag_value"
|
||||
)
|
||||
|
||||
|
||||
@mock_resourcegroups
|
||||
def test_update_group_query():
|
||||
resource_groups = boto3.client("resource-groups", region_name="us-east-1")
|
||||
|
Loading…
Reference in New Issue
Block a user