Elastic Beanstalk - Rename and Add Implementation Coverage

This commit is contained in:
Bert Blommers 2020-03-30 14:08:22 +01:00
parent c32c17a13e
commit 7d524eaec9
8 changed files with 74 additions and 53 deletions

View File

@ -2878,15 +2878,15 @@
- [ ] test_failover - [ ] test_failover
## elasticbeanstalk ## elasticbeanstalk
0% implemented 13% implemented
- [ ] abort_environment_update - [ ] abort_environment_update
- [ ] apply_environment_managed_action - [ ] apply_environment_managed_action
- [ ] check_dns_availability - [ ] check_dns_availability
- [ ] compose_environments - [ ] compose_environments
- [ ] create_application - [X] create_application
- [ ] create_application_version - [ ] create_application_version
- [ ] create_configuration_template - [ ] create_configuration_template
- [ ] create_environment - [X] create_environment
- [ ] create_platform_version - [ ] create_platform_version
- [ ] create_storage_location - [ ] create_storage_location
- [ ] delete_application - [ ] delete_application
@ -2903,13 +2903,13 @@
- [ ] describe_environment_managed_action_history - [ ] describe_environment_managed_action_history
- [ ] describe_environment_managed_actions - [ ] describe_environment_managed_actions
- [ ] describe_environment_resources - [ ] describe_environment_resources
- [ ] describe_environments - [X] describe_environments
- [ ] describe_events - [ ] describe_events
- [ ] describe_instances_health - [ ] describe_instances_health
- [ ] describe_platform_version - [ ] describe_platform_version
- [ ] list_available_solution_stacks - [X] list_available_solution_stacks
- [ ] list_platform_versions - [ ] list_platform_versions
- [ ] list_tags_for_resource - [X] list_tags_for_resource
- [ ] rebuild_environment - [ ] rebuild_environment
- [ ] request_environment_info - [ ] request_environment_info
- [ ] restart_app_server - [ ] restart_app_server
@ -2921,7 +2921,7 @@
- [ ] update_application_version - [ ] update_application_version
- [ ] update_configuration_template - [ ] update_configuration_template
- [ ] update_environment - [ ] update_environment
- [ ] update_tags_for_resource - [X] update_tags_for_resource
- [ ] validate_configuration_settings - [ ] validate_configuration_settings
## elastictranscoder ## elastictranscoder

View File

@ -21,7 +21,7 @@ from .datasync import mock_datasync # noqa
from .dynamodb import mock_dynamodb, mock_dynamodb_deprecated # noqa from .dynamodb import mock_dynamodb, mock_dynamodb_deprecated # noqa
from .dynamodb2 import mock_dynamodb2, mock_dynamodb2_deprecated # noqa from .dynamodb2 import mock_dynamodb2, mock_dynamodb2_deprecated # noqa
from .dynamodbstreams import mock_dynamodbstreams # noqa from .dynamodbstreams import mock_dynamodbstreams # noqa
from .eb import mock_eb # noqa from .elasticbeanstalk import mock_elasticbeanstalk # noqa
from .ec2 import mock_ec2, mock_ec2_deprecated # noqa from .ec2 import mock_ec2, mock_ec2_deprecated # noqa
from .ec2_instance_connect import mock_ec2_instance_connect # noqa from .ec2_instance_connect import mock_ec2_instance_connect # noqa
from .ecr import mock_ecr, mock_ecr_deprecated # noqa from .ecr import mock_ecr, mock_ecr_deprecated # noqa

View File

@ -1,4 +1,4 @@
from .models import eb_backends from .models import eb_backends
from moto.core.models import base_decorator from moto.core.models import base_decorator
mock_eb = base_decorator(eb_backends) mock_elasticbeanstalk = base_decorator(eb_backends)

View File

@ -3,7 +3,7 @@ import weakref
import boto.beanstalk import boto.beanstalk
from moto.core import BaseBackend, BaseModel from moto.core import BaseBackend, BaseModel
from .exceptions import InvalidParameterValueError from .exceptions import InvalidParameterValueError, ResourceNotFoundException
class FakeEnvironment(BaseModel): class FakeEnvironment(BaseModel):
@ -90,6 +90,54 @@ class EBBackend(BaseBackend):
self.applications[application_name] = new_app self.applications[application_name] = new_app
return new_app return new_app
def create_environment(self, app, environment_name, stack_name, tags):
return app.create_environment(
environment_name=environment_name,
solution_stack_name=stack_name,
tags=tags,
)
def describe_environments(self):
envs = []
for app in self.applications.values():
for env in app.environments.values():
envs.append(env)
return envs
def list_available_solution_stacks(self):
# Implemented in response.py
pass
def update_tags_for_resource(self, resource_arn, tags_to_add, tags_to_remove):
try:
res = self._find_environment_by_arn(resource_arn)
except KeyError:
raise ResourceNotFoundException(
"Resource not found for ARN '{}'.".format(resource_arn)
)
for key, value in tags_to_add.items():
res.tags[key] = value
for key in tags_to_remove:
del res.tags[key]
def list_tags_for_resource(self, resource_arn):
try:
res = self._find_environment_by_arn(resource_arn)
except KeyError:
raise ResourceNotFoundException(
"Resource not found for ARN '{}'.".format(resource_arn)
)
return res.tags
def _find_environment_by_arn(self, arn):
for app in self.applications.keys():
for env in self.applications[app].environments.values():
if env.environment_arn == arn:
return env
raise KeyError()
eb_backends = dict( eb_backends = dict(
(region.name, EBBackend(region.name)) for region in boto.beanstalk.regions() (region.name, EBBackend(region.name)) for region in boto.beanstalk.regions()

View File

@ -1,7 +1,7 @@
from moto.core.responses import BaseResponse from moto.core.responses import BaseResponse
from moto.core.utils import tags_from_query_string from moto.core.utils import tags_from_query_string
from .models import eb_backends from .models import eb_backends
from .exceptions import InvalidParameterValueError, ResourceNotFoundException from .exceptions import InvalidParameterValueError
class EBResponse(BaseResponse): class EBResponse(BaseResponse):
@ -34,9 +34,10 @@ class EBResponse(BaseResponse):
) )
tags = tags_from_query_string(self.querystring, prefix="Tags.member") tags = tags_from_query_string(self.querystring, prefix="Tags.member")
env = app.create_environment( env = self.backend.create_environment(
app,
environment_name=self._get_param("EnvironmentName"), environment_name=self._get_param("EnvironmentName"),
solution_stack_name=self._get_param("SolutionStackName"), stack_name=self._get_param("SolutionStackName"),
tags=tags, tags=tags,
) )
@ -44,11 +45,7 @@ class EBResponse(BaseResponse):
return template.render(environment=env, region=self.backend.region,) return template.render(environment=env, region=self.backend.region,)
def describe_environments(self): def describe_environments(self):
envs = [] envs = self.backend.describe_environments()
for app in self.backend.applications.values():
for env in app.environments.values():
envs.append(env)
template = self.response_template(EB_DESCRIBE_ENVIRONMENTS) template = self.response_template(EB_DESCRIBE_ENVIRONMENTS)
return template.render(environments=envs,) return template.render(environments=envs,)
@ -57,43 +54,19 @@ class EBResponse(BaseResponse):
def list_available_solution_stacks(): def list_available_solution_stacks():
return EB_LIST_AVAILABLE_SOLUTION_STACKS return EB_LIST_AVAILABLE_SOLUTION_STACKS
def _find_environment_by_arn(self, arn):
for app in self.backend.applications.keys():
for env in self.backend.applications[app].environments.values():
if env.environment_arn == arn:
return env
raise KeyError()
def update_tags_for_resource(self): def update_tags_for_resource(self):
resource_arn = self._get_param("ResourceArn") resource_arn = self._get_param("ResourceArn")
try:
res = self._find_environment_by_arn(resource_arn)
except KeyError:
raise ResourceNotFoundException(
"Resource not found for ARN '{}'.".format(resource_arn)
)
tags_to_add = tags_from_query_string( tags_to_add = tags_from_query_string(
self.querystring, prefix="TagsToAdd.member" self.querystring, prefix="TagsToAdd.member"
) )
for key, value in tags_to_add.items():
res.tags[key] = value
tags_to_remove = self._get_multi_param("TagsToRemove.member") tags_to_remove = self._get_multi_param("TagsToRemove.member")
for key in tags_to_remove: self.backend.update_tags_for_resource(resource_arn, tags_to_add, tags_to_remove)
del res.tags[key]
return EB_UPDATE_TAGS_FOR_RESOURCE return EB_UPDATE_TAGS_FOR_RESOURCE
def list_tags_for_resource(self): def list_tags_for_resource(self):
resource_arn = self._get_param("ResourceArn") resource_arn = self._get_param("ResourceArn")
try: tags = self.backend.list_tags_for_resource(resource_arn)
res = self._find_environment_by_arn(resource_arn)
except KeyError:
raise ResourceNotFoundException(
"Resource not found for ARN '{}'.".format(resource_arn)
)
tags = res.tags
template = self.response_template(EB_LIST_TAGS_FOR_RESOURCE) template = self.response_template(EB_LIST_TAGS_FOR_RESOURCE)
return template.render(tags=tags, arn=resource_arn,) return template.render(tags=tags, arn=resource_arn,)

View File

@ -2,10 +2,10 @@ import boto3
import sure # noqa import sure # noqa
from botocore.exceptions import ClientError from botocore.exceptions import ClientError
from moto import mock_eb from moto import mock_elasticbeanstalk
@mock_eb @mock_elasticbeanstalk
def test_create_application(): def test_create_application():
# Create Elastic Beanstalk Application # Create Elastic Beanstalk Application
conn = boto3.client("elasticbeanstalk", region_name="us-east-1") conn = boto3.client("elasticbeanstalk", region_name="us-east-1")
@ -13,7 +13,7 @@ def test_create_application():
app["Application"]["ApplicationName"].should.equal("myapp") app["Application"]["ApplicationName"].should.equal("myapp")
@mock_eb @mock_elasticbeanstalk
def test_create_application_dup(): def test_create_application_dup():
conn = boto3.client("elasticbeanstalk", region_name="us-east-1") conn = boto3.client("elasticbeanstalk", region_name="us-east-1")
conn.create_application(ApplicationName="myapp",) conn.create_application(ApplicationName="myapp",)
@ -22,7 +22,7 @@ def test_create_application_dup():
) )
@mock_eb @mock_elasticbeanstalk
def test_describe_applications(): def test_describe_applications():
# Create Elastic Beanstalk Application # Create Elastic Beanstalk Application
conn = boto3.client("elasticbeanstalk", region_name="us-east-1") conn = boto3.client("elasticbeanstalk", region_name="us-east-1")
@ -33,7 +33,7 @@ def test_describe_applications():
apps["Applications"][0]["ApplicationName"].should.equal("myapp") apps["Applications"][0]["ApplicationName"].should.equal("myapp")
@mock_eb @mock_elasticbeanstalk
def test_create_environment(): def test_create_environment():
# Create Elastic Beanstalk Environment # Create Elastic Beanstalk Environment
conn = boto3.client("elasticbeanstalk", region_name="us-east-1") conn = boto3.client("elasticbeanstalk", region_name="us-east-1")
@ -42,7 +42,7 @@ def test_create_environment():
env["EnvironmentName"].should.equal("myenv") env["EnvironmentName"].should.equal("myenv")
@mock_eb @mock_elasticbeanstalk
def test_describe_environments(): def test_describe_environments():
# List Elastic Beanstalk Envs # List Elastic Beanstalk Envs
conn = boto3.client("elasticbeanstalk", region_name="us-east-1") conn = boto3.client("elasticbeanstalk", region_name="us-east-1")
@ -72,7 +72,7 @@ def tags_list_to_dict(tag_list):
return tag_dict return tag_dict
@mock_eb @mock_elasticbeanstalk
def test_create_environment_tags(): def test_create_environment_tags():
conn = boto3.client("elasticbeanstalk", region_name="us-east-1") conn = boto3.client("elasticbeanstalk", region_name="us-east-1")
conn.create_application(ApplicationName="myapp",) conn.create_application(ApplicationName="myapp",)
@ -88,7 +88,7 @@ def test_create_environment_tags():
tags_list_to_dict(tags["ResourceTags"]).should.equal(env_tags) tags_list_to_dict(tags["ResourceTags"]).should.equal(env_tags)
@mock_eb @mock_elasticbeanstalk
def test_update_tags(): def test_update_tags():
conn = boto3.client("elasticbeanstalk", region_name="us-east-1") conn = boto3.client("elasticbeanstalk", region_name="us-east-1")
conn.create_application(ApplicationName="myapp",) conn.create_application(ApplicationName="myapp",)
@ -122,7 +122,7 @@ def test_update_tags():
tags_list_to_dict(tags["ResourceTags"]).should.equal(total_env_tags) tags_list_to_dict(tags["ResourceTags"]).should.equal(total_env_tags)
@mock_eb @mock_elasticbeanstalk
def test_list_available_solution_stacks(): def test_list_available_solution_stacks():
conn = boto3.client("elasticbeanstalk", region_name="us-east-1") conn = boto3.client("elasticbeanstalk", region_name="us-east-1")
stacks = conn.list_available_solution_stacks() stacks = conn.list_available_solution_stacks()