diff --git a/moto/mediastore/exceptions.py b/moto/mediastore/exceptions.py index 9ed31cb2..ce7c9bbc 100644 --- a/moto/mediastore/exceptions.py +++ b/moto/mediastore/exceptions.py @@ -1,4 +1,5 @@ from __future__ import unicode_literals + from moto.core.exceptions import JsonRESTError @@ -6,6 +7,15 @@ class MediaStoreClientError(JsonRESTError): code = 400 +class ContainerNotFoundException(MediaStoreClientError): + def __init__(self, msg=None): + self.code = 400 + super(ContainerNotFoundException, self).__init__( + "ContainerNotFoundException", + msg or "The specified container does not exist", + ) + + class ResourceNotFoundException(MediaStoreClientError): def __init__(self, msg=None): self.code = 400 diff --git a/moto/mediastore/models.py b/moto/mediastore/models.py index b26d606b..37f2d64b 100644 --- a/moto/mediastore/models.py +++ b/moto/mediastore/models.py @@ -1,9 +1,16 @@ from __future__ import unicode_literals -from boto3 import Session -from moto.core import BaseBackend, BaseModel + from collections import OrderedDict from datetime import date -from .exceptions import ResourceNotFoundException, PolicyNotFoundException + +from boto3 import Session + +from moto.core import BaseBackend, BaseModel +from .exceptions import ( + ContainerNotFoundException, + ResourceNotFoundException, + PolicyNotFoundException, +) class Container(BaseModel): @@ -16,6 +23,7 @@ class Container(BaseModel): self.lifecycle_policy = None self.policy = None self.metric_policy = None + self.tags = kwargs.get("tags") def to_dict(self, exclude=None): data = { @@ -24,6 +32,7 @@ class Container(BaseModel): "Endpoint": self.endpoint, "Status": self.status, "CreationTime": self.creation_time, + "Tags": self.tags, } if exclude: for key in exclude: @@ -50,10 +59,17 @@ class MediaStoreBackend(BaseBackend): endpoint="/{}".format(name), status="CREATING", creation_time=date.today().strftime("%m/%d/%Y, %H:%M:%S"), + tags=tags, ) self._containers[name] = container return container + def delete_container(self, name): + if name not in self._containers: + raise ContainerNotFoundException() + del self._containers[name] + return {} + def describe_container(self, name): if name not in self._containers: raise ResourceNotFoundException() @@ -66,6 +82,10 @@ class MediaStoreBackend(BaseBackend): response_containers = [c.to_dict() for c in containers] return response_containers, None + def list_tags_for_resource(self, name): + tags = self._containers[name].tags + return tags + def put_lifecycle_policy(self, container_name, lifecycle_policy): if container_name not in self._containers: raise ResourceNotFoundException() diff --git a/moto/mediastore/responses.py b/moto/mediastore/responses.py index 0ff1ec5d..72f0f25a 100644 --- a/moto/mediastore/responses.py +++ b/moto/mediastore/responses.py @@ -1,7 +1,9 @@ from __future__ import unicode_literals + +import json + from moto.core.responses import BaseResponse from .models import mediastore_backends -import json class MediaStoreResponse(BaseResponse): @@ -17,6 +19,11 @@ class MediaStoreResponse(BaseResponse): container = self.mediastore_backend.create_container(name=name, tags=tags) return json.dumps(dict(Container=container.to_dict())) + def delete_container(self): + name = self._get_param("ContainerName") + result = self.mediastore_backend.delete_container(name=name) + return json.dumps(result) + def describe_container(self): name = self._get_param("ContainerName") container = self.mediastore_backend.describe_container(name=name) @@ -30,6 +37,11 @@ class MediaStoreResponse(BaseResponse): ) return json.dumps(dict(dict(Containers=containers), NextToken=next_token)) + def list_tags_for_resource(self): + name = self._get_param("Resource") + tags = self.mediastore_backend.list_tags_for_resource(name) + return json.dumps(dict(Tags=tags)) + def put_lifecycle_policy(self): container_name = self._get_param("ContainerName") lifecycle_policy = self._get_param("LifecyclePolicy") diff --git a/tests/test_mediastore/test_mediastore.py b/tests/test_mediastore/test_mediastore.py index baf16ff1..9bd429d8 100644 --- a/tests/test_mediastore/test_mediastore.py +++ b/tests/test_mediastore/test_mediastore.py @@ -1,11 +1,12 @@ from __future__ import unicode_literals import boto3 -import sure # noqa import pytest -from moto import mock_mediastore +import sure # noqa from botocore.exceptions import ClientError +from moto import mock_mediastore + region = "eu-west-1" @@ -192,3 +193,51 @@ def test_get_metric_policy_raises_error_if_container_does_not_have_metric_policy with pytest.raises(ClientError) as ex: client.get_metric_policy(ContainerName="container-name") ex.value.response["Error"]["Code"].should.equal("PolicyNotFoundException") + + +@mock_mediastore +def test_list_tags_for_resource(): + client = boto3.client("mediastore", region_name=region) + tags = [{"Key": "customer"}] + + create_response = client.create_container( + ContainerName="Awesome container!", Tags=tags + ) + container = create_response["Container"] + response = client.list_tags_for_resource(Resource=container["Name"]) + response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200) + response["Tags"].should.equal(tags) + + +@mock_mediastore +def test_list_tags_for_resource_return_none_if_no_tags(): + client = boto3.client("mediastore", region_name=region) + + create_response = client.create_container(ContainerName="Awesome container!") + container = create_response["Container"] + response = client.list_tags_for_resource(Resource=container["Name"]) + response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200) + response.get("Tags").should.equal(None) + + +@mock_mediastore +def test_delete_container(): + client = boto3.client("mediastore", region_name=region) + container_name = "Awesome container!" + create_response = client.create_container(ContainerName=container_name) + container = create_response["Container"] + response = client.delete_container(ContainerName=container["Name"]) + response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200) + containers = client.list_containers(NextToken="next-token")["Containers"] + container_exists = any(d["Name"] == container_name for d in containers) + container_exists.should.equal(False) + + +@mock_mediastore +def test_delete_container_raise_error_if_container_not_found(): + client = boto3.client("mediastore", region_name=region) + client.create_container(ContainerName="Awesome container!") + + with pytest.raises(ClientError) as ex: + client.delete_container(ContainerName="notAvailable") + ex.value.response["Error"]["Code"].should.equal("ContainerNotFoundException")