Add vault operations.

This commit is contained in:
Steve Pulec 2015-06-03 22:55:07 -04:00
parent b296294086
commit 7156df1a63
9 changed files with 208 additions and 0 deletions

View File

@ -13,6 +13,7 @@ from .dynamodb2 import mock_dynamodb2 # flake8: noqa
from .ec2 import mock_ec2 # flake8: noqa
from .elb import mock_elb # flake8: noqa
from .emr import mock_emr # flake8: noqa
from .glacier import mock_glacier # flake8: noqa
from .iam import mock_iam # flake8: noqa
from .kinesis import mock_kinesis # flake8: noqa
from .rds import mock_rds # flake8: noqa

View File

@ -6,6 +6,7 @@ from moto.dynamodb2 import dynamodb_backend2
from moto.ec2 import ec2_backend
from moto.elb import elb_backend
from moto.emr import emr_backend
from moto.glacier import glacier_backend
from moto.kinesis import kinesis_backend
from moto.rds import rds_backend
from moto.redshift import redshift_backend
@ -24,6 +25,7 @@ BACKENDS = {
'ec2': ec2_backend,
'elb': elb_backend,
'emr': emr_backend,
'glacier': glacier_backend,
'kinesis': kinesis_backend,
'redshift': redshift_backend,
'rds': rds_backend,

12
moto/glacier/__init__.py Normal file
View File

@ -0,0 +1,12 @@
from __future__ import unicode_literals
from .models import glacier_backends
from ..core.models import MockAWS
glacier_backend = glacier_backends['us-east-1']
def mock_glacier(func=None):
if func:
return MockAWS(glacier_backends)(func)
else:
return MockAWS(glacier_backends)

52
moto/glacier/models.py Normal file
View File

@ -0,0 +1,52 @@
from __future__ import unicode_literals
import boto.glacier
from moto.core import BaseBackend
class Vault(object):
def __init__(self, vault_name, region):
self.vault_name = vault_name
self.region = region
@property
def arn(self):
return "arn:aws:glacier:{}:012345678901:vaults/{}".format(self.region, self.vault_name)
def to_dict(self):
return {
"CreationDate": "2013-03-20T17:03:43.221Z",
"LastInventoryDate": "2013-03-20T17:03:43.221Z",
"NumberOfArchives": None,
"SizeInBytes": None,
"VaultARN": self.arn,
"VaultName": self.vault_name,
}
class GlacierBackend(BaseBackend):
def __init__(self, region_name):
self.vaults = {}
self.region_name = region_name
def reset(self):
region_name = self.region_name
self.__dict__ = {}
self.__init__(region_name)
def get_vault(self, vault_name):
return self.vaults[vault_name]
def create_vault(self, vault_name):
self.vaults[vault_name] = Vault(vault_name, self.region_name)
def list_vaules(self):
return self.vaults.values()
def delete_vault(self, vault_name):
self.vaults.pop(vault_name)
glacier_backends = {}
for region in boto.glacier.regions():
glacier_backends[region.name] = GlacierBackend(region)

64
moto/glacier/responses.py Normal file
View File

@ -0,0 +1,64 @@
from __future__ import unicode_literals
import json
from six.moves.urllib.parse import urlparse, parse_qs
from moto.core.responses import _TemplateEnvironmentMixin
from .models import glacier_backends
from .utils import region_from_glacier_url, vault_from_glacier_url
class GlacierResponse(_TemplateEnvironmentMixin):
def __init__(self, backend):
self.backend = backend
@classmethod
def all_vault_response(clazz, request, full_url, headers):
region_name = region_from_glacier_url(full_url)
response_instance = GlacierResponse(glacier_backends[region_name])
return response_instance._all_vault_response(request, full_url, headers)
def _all_vault_response(self, request, full_url, headers):
vaults = self.backend.list_vaules()
response = json.dumps({
"Marker": None,
"VaultList": [
vault.to_dict() for vault in vaults
]
})
headers['content-type'] = 'application/json'
return 200, headers, response
@classmethod
def vault_response(clazz, request, full_url, headers):
region_name = region_from_glacier_url(full_url)
response_instance = GlacierResponse(glacier_backends[region_name])
return response_instance._vault_response(request, full_url, headers)
def _vault_response(self, request, full_url, headers):
method = request.method
parsed_url = urlparse(full_url)
querystring = parse_qs(parsed_url.query, keep_blank_values=True)
vault_name = vault_from_glacier_url(full_url)
if method == 'GET':
return self._vault_response_get(vault_name, querystring, headers)
elif method == 'PUT':
return self._vault_response_put(vault_name, querystring, headers)
elif method == 'DELETE':
return self._vault_response_delete(vault_name, querystring, headers)
def _vault_response_get(self, vault_name, querystring, headers):
vault = self.backend.get_vault(vault_name)
headers['content-type'] = 'application/json'
return 200, headers, json.dumps(vault.to_dict())
def _vault_response_put(self, vault_name, querystring, headers):
self.backend.create_vault(vault_name)
return 201, headers, ""
def _vault_response_delete(self, vault_name, querystring, headers):
self.backend.delete_vault(vault_name)
return 204, headers, ""

11
moto/glacier/urls.py Normal file
View File

@ -0,0 +1,11 @@
from __future__ import unicode_literals
from .responses import GlacierResponse
url_bases = [
"https?://glacier.(.+).amazonaws.com",
]
url_paths = {
'{0}/(?P<account_number>.+)/vaults$': GlacierResponse.all_vault_response,
'{0}/(?P<account_number>.+)/vaults/(?P<vault_name>.+)$': GlacierResponse.vault_response,
}

14
moto/glacier/utils.py Normal file
View File

@ -0,0 +1,14 @@
from six.moves.urllib.parse import urlparse
def region_from_glacier_url(url):
domain = urlparse(url).netloc
if '.' in domain:
return domain.split(".")[1]
else:
return 'us-east-1'
def vault_from_glacier_url(full_url):
return full_url.split("/")[-1]

View File

@ -0,0 +1,21 @@
from __future__ import unicode_literals
import json
import sure # noqa
import moto.server as server
from moto import mock_glacier
'''
Test the different server responses
'''
@mock_glacier
def test_list_vaults():
backend = server.create_backend_app("glacier")
test_client = backend.test_client()
res = test_client.get('/1234bcd/vaults')
json.loads(res.data).should.equal({u'Marker': None, u'VaultList': []})

View File

@ -0,0 +1,31 @@
from __future__ import unicode_literals
import boto.glacier
import sure # noqa
from moto import mock_glacier
@mock_glacier
def test_create_vault():
conn = boto.glacier.connect_to_region("us-west-2")
conn.create_vault("my_vault")
vaults = conn.list_vaults()
vaults.should.have.length_of(1)
vaults[0].name.should.equal("my_vault")
@mock_glacier
def test_delete_vault():
conn = boto.glacier.connect_to_region("us-west-2")
conn.create_vault("my_vault")
vaults = conn.list_vaults()
vaults.should.have.length_of(1)
conn.delete_vault("my_vault")
vaults = conn.list_vaults()
vaults.should.have.length_of(0)