From 503d46d36a0edef77f71628f952373aebc9370b5 Mon Sep 17 00:00:00 2001 From: Mike Fuller Date: Mon, 19 Jan 2015 20:29:32 +1100 Subject: [PATCH] Added decribe_option_groups and delete_option_group support --- moto/rds2/models.py | 35 +++++++++++++++++++++++++++++++++++ moto/rds2/responses.py | 32 ++++++++++++++++++++++++++++++++ tests/test_rds2/test_rds2.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) diff --git a/moto/rds2/models.py b/moto/rds2/models.py index f6edb240b..ef2aa4c02 100644 --- a/moto/rds2/models.py +++ b/moto/rds2/models.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals import copy import boto.rds2 +import json from jinja2 import Template from moto.cloudformation.exceptions import UnformattedGetAttTemplateException @@ -462,6 +463,40 @@ class RDS2Backend(BaseBackend): self.option_groups[option_group_id] = option_group return option_group + def delete_option_group(self, option_group_name): + if option_group_name in self.option_groups: + return self.option_groups.pop(option_group_name) + else: + raise RDSClientError('OptionGroupNotFoundFault', 'Specified OptionGroupName: {} not found.'.format(option_group_name)) + + def describe_option_groups(self, option_group_kwargs): + option_group_list = [] + + if option_group_kwargs['marker']: + marker = option_group_kwargs['marker'] + else: + marker = 0 + if option_group_kwargs['max_records']: + max_records = option_group_kwargs['max_records'] + else: + max_records = 100 + + for option_group_name, option_group in self.option_groups.items(): + if option_group_kwargs['name'] and option_group.name != option_group_kwargs['name']: + continue + elif option_group_kwargs['engine_name'] and \ + option_group.engine_name != option_group_kwargs['engine_name']: + continue + elif option_group_kwargs['major_engine_version'] and \ + option_group.major_engine_version != option_group_kwargs['major_engine_version']: + continue + else: + option_group_list.append(option_group) + if not len(option_group_list): + raise RDSClientError('OptionGroupNotFoundFault', + 'Specified OptionGroupName: {} not found.'.format(option_group_kwargs['name'])) + return option_group_list[marker:max_records+marker] + class OptionGroup(object): def __init__(self, name, engine_name, major_engine_version, description=None): diff --git a/moto/rds2/responses.py b/moto/rds2/responses.py index 7b2ece202..eaf3cb87a 100644 --- a/moto/rds2/responses.py +++ b/moto/rds2/responses.py @@ -164,6 +164,21 @@ class RDS2Response(BaseResponse): template = self.response_template(CREATE_OPTION_GROUP_TEMPLATE) return template.render(option_group=option_group) + def delete_option_group(self): + kwargs = self._get_option_group_kwargs() + option_group = self.backend.delete_option_group(kwargs['name']) + template = self.response_template(DELETE_OPTION_GROUP_TEMPLATE) + return template.render(option_group=option_group) + + def describe_option_groups(self): + kwargs = self._get_option_group_kwargs() + kwargs['max_records'] = self._get_param('MaxRecords') + kwargs['marker'] = self._get_param('Marker') + option_groups = self.backend.describe_option_groups(kwargs) + template = self.response_template(DESCRIBE_OPTION_GROUP_TEMPLATE) + return template.render(option_groups=option_groups) + + CREATE_DATABASE_TEMPLATE = """{ "CreateDBInstanceResponse": { "CreateDBInstanceResult": { @@ -287,3 +302,20 @@ CREATE_OPTION_GROUP_TEMPLATE = """{ } } }""" + +DELETE_OPTION_GROUP_TEMPLATE = \ + """{"DeleteOptionGroupResponse": {"ResponseMetadata": {"RequestId": "e2590367-9fa2-11e4-99cf-55e92d41c60e"}}}""" + +DESCRIBE_OPTION_GROUP_TEMPLATE = \ + """{"DescribeOptionGroupsResponse": { + "DescribeOptionGroupsResult": { + "Marker": null, + "OptionGroupsList": [ + {%- for option_group in option_groups -%} + {%- if loop.index != 1 -%},{%- endif -%} + {{ option_group.to_json() }} + {%- endfor -%} + ]}, + "ResponseMetadata": {"RequestId": "4caf445d-9fbc-11e4-87ea-a31c60ed2e36"} + }}""" + diff --git a/tests/test_rds2/test_rds2.py b/tests/test_rds2/test_rds2.py index d75f294f3..384c112dd 100644 --- a/tests/test_rds2/test_rds2.py +++ b/tests/test_rds2/test_rds2.py @@ -91,6 +91,36 @@ def test_create_option_group_empty_description(): conn.create_option_group.when.called_with('test', 'postgres', '9.3', '').should.throw(BotoServerError) +@mock_rds2 +def test_describe_option_group(): + conn = boto.rds2.connect_to_region("us-west-2") + conn.create_option_group('test', 'postgres', '9.3', 'test option group') + option_groups = conn.describe_option_groups('test') + option_groups['DescribeOptionGroupsResponse']['DescribeOptionGroupsResult']['OptionGroupsList'][0]['OptionGroupName'].should.equal('test') + +@mock_rds2 +def test_describe_non_existant_option_group(): + conn = boto.rds2.connect_to_region("us-west-2") + conn.describe_option_groups.when.called_with("not-a-option-group").should.throw(BotoServerError) + + +@mock_rds2 +def test_delete_option_group(): + conn = boto.rds2.connect_to_region("us-west-2") + conn.create_option_group('test', 'postgres', '9.3', 'test option group') + option_groups = conn.describe_option_groups('test') + option_groups['DescribeOptionGroupsResponse']['DescribeOptionGroupsResult']['OptionGroupsList'][0]['OptionGroupName'].should.equal('test') + conn.delete_option_group('test') + conn.describe_option_groups.when.called_with('test').should.throw(BotoServerError) + + +@mock_rds2 +def test_delete_non_existant_option_group(): + conn = boto.rds2.connect_to_region("us-west-2") + conn.delete_option_group.when.called_with('non-existant').should.throw(BotoServerError) + + + #@disable_on_py3() #@mock_rds2 #def test_delete_database():