diff --git a/moto/rds2/models.py b/moto/rds2/models.py index a3f06203b..915386c00 100644 --- a/moto/rds2/models.py +++ b/moto/rds2/models.py @@ -60,8 +60,8 @@ class Database(object): if self.db_subnet_group_name: self.db_subnet_group = rds2_backends[self.region].describe_subnet_groups(self.db_subnet_group_name)[0] else: - self.db_subnet_group = [] - self.db_security_groups = kwargs.get('security_groups', ['a']) + self.db_subnet_group = None + self.security_groups = kwargs.get('security_groups', []) self.vpc_security_group_ids = kwargs.get('vpc_security_group_ids', []) self.preferred_maintenance_window = kwargs.get('preferred_maintenance_window', 'wed:06:38-wed:07:08') self.db_parameter_group_name = kwargs.get('db_parameter_group_name', None) @@ -174,37 +174,14 @@ class Database(object): "DBParameterGroupName": "{{ database.db_parameter_group_name }}" } },{%- endif %} - "DBSecurityGroups": [{ - {% for security_group in database.db_security_groups -%}{%- if loop.index != 1 -%},{%- endif -%} - "DBSecurityGroup": { + "DBSecurityGroups": [ + {% for security_group in database.security_groups -%}{%- if loop.index != 1 -%},{%- endif -%} + {"DBSecurityGroup": { "Status": "active", "DBSecurityGroupName": "{{ security_group }}" - }{% endfor %} - }],{%- if database.db_subnet_group -%} - "DBSubnetGroup": { - "DBSubnetGroupDescription": "nabil-db-subnet-group", - "DBSubnetGroupName": "nabil-db-subnet-group", - "SubnetGroupStatus": "Complete", - "Subnets": [ - { - "SubnetAvailabilityZone": { - "Name": "us-west-2c", - "ProvisionedIopsCapable": false - }, - "SubnetIdentifier": "subnet-c0ea0099", - "SubnetStatus": "Active" - }, - { - "SubnetAvailabilityZone": { - "Name": "us-west-2a", - "ProvisionedIopsCapable": false - }, - "SubnetIdentifier": "subnet-ff885d88", - "SubnetStatus": "Active" - } - ], - "VpcId": "vpc-8e6ab6eb" - },{%- endif %} + }}{% endfor %} + ], + {%- if database.db_subnet_group -%}{{ database.db_subnet_group.to_json() }},{%- endif %} "Engine": "{{ database.engine }}", "EngineVersion": "{{ database.engine_version }}", "LatestRestorableTime": null, @@ -386,8 +363,7 @@ class SubnetGroup(object): return template.render(subnet_group=self) def to_json(self): - template = Template("""{ - "DBSubnetGroup": { + template = Template(""""DBSubnetGroup": { "VpcId": "{{ subnet_group.vpc_id }}", "SubnetGroupStatus": "{{ subnet_group.status }}", "DBSubnetGroupDescription": "{{ subnet_group.description }}", @@ -404,8 +380,7 @@ class SubnetGroup(object): }{%- if not loop.last -%},{%- endif -%}{% endfor %} ] } - } - }""") + }""") return template.render(subnet_group=self) @classmethod diff --git a/moto/rds2/responses.py b/moto/rds2/responses.py index d262c1a5f..c4753e603 100644 --- a/moto/rds2/responses.py +++ b/moto/rds2/responses.py @@ -394,7 +394,7 @@ AUTHORIZE_SECURITY_GROUP_TEMPLATE = """{ CREATE_SUBNET_GROUP_TEMPLATE = """{ "CreateDBSubnetGroupResponse": { "CreateDBSubnetGroupResult": - {{ subnet_group.to_json() }}, + { {{ subnet_group.to_json() }} }, "ResponseMetadata": { "RequestId": "3a401b3f-bb9e-11d3-f4c6-37db295f7674" } } }""" @@ -404,7 +404,7 @@ DESCRIBE_SUBNET_GROUPS_TEMPLATE = """{ "DescribeDBSubnetGroupsResult": { "DBSubnetGroups": [ {% for subnet_group in subnet_groups %} - {{ subnet_group.to_json() }}{%- if not loop.last -%},{%- endif -%} + { {{ subnet_group.to_json() }} }{%- if not loop.last -%},{%- endif -%} {% endfor %} ], "Marker": null @@ -414,11 +414,7 @@ DESCRIBE_SUBNET_GROUPS_TEMPLATE = """{ }""" -DELETE_SUBNET_GROUP_TEMPLATE = """ - - 6295e5ab-bbf3-11d3-f4c6-37db295f7674 - -""" +DELETE_SUBNET_GROUP_TEMPLATE = """{"DeleteDBSubnetGroupResponse": {"ResponseMetadata": {"RequestId": "13785dd5-a7fc-11e4-bb9c-7f371d0859b0"}}}""" CREATE_OPTION_GROUP_TEMPLATE = """{ "CreateOptionGroupResponse": { diff --git a/tests/test_rds2/test_rds2.py b/tests/test_rds2/test_rds2.py index 1a191e492..a6d930ea0 100644 --- a/tests/test_rds2/test_rds2.py +++ b/tests/test_rds2/test_rds2.py @@ -4,7 +4,7 @@ import boto.rds2 import boto.vpc from boto.exception import BotoServerError import sure # noqa - +import json from moto import mock_ec2, mock_rds2 from tests.helpers import disable_on_py3 @@ -435,35 +435,63 @@ def test_security_group_authorize(): result['DescribeDBSecurityGroupsResponse']['DescribeDBSecurityGroupsResult']['DBSecurityGroups'][0]['IPRanges'].should.equal(['10.3.2.45/32', '10.3.2.46/32']) -#@disable_on_py3() -#@mock_rds2 -#def test_add_security_group_to_database(): -# conn = boto.rds2.connect_to_region("us-west-2") -# -# database = conn.create_dbinstance("db-master-1", 10, 'db.m1.small', 'root', 'hunter2') -# security_group = conn.create_dbsecurity_group('db_sg', 'DB Security Group') -# database.modify(security_groups=[security_group]) -# -# database = conn.get_all_dbinstances()[0] -# list(database.security_groups).should.have.length_of(1) -# -# database.security_groups[0].name.should.equal("db_sg") -# -# -#@mock_ec2 -#@mock_rds2 -#def test_add_database_subnet_group(): -# vpc_conn = boto.vpc.connect_to_region("us-west-2") -# vpc = vpc_conn.create_vpc("10.0.0.0/16") -# subnet1 = vpc_conn.create_subnet(vpc.id, "10.1.0.0/24") -# subnet2 = vpc_conn.create_subnet(vpc.id, "10.2.0.0/24") -# -# subnet_ids = [subnet1.id, subnet2.id] -# conn = boto.rds2.connect_to_region("us-west-2") -# subnet_group = conn.create_db_subnet_group("db_subnet", "my db subnet", subnet_ids) -# subnet_group.name.should.equal('db_subnet') -# subnet_group.description.should.equal("my db subnet") -# list(subnet_group.subnet_ids).should.equal(subnet_ids) +@disable_on_py3() +@mock_rds2 +def test_add_security_group_to_database(): + conn = boto.rds2.connect_to_region("us-west-2") + + conn.create_db_instance(db_instance_identifier='db-master-1', + allocated_storage=10, + engine='postgres', + db_instance_class='db.m1.small', + master_username='root', + master_user_password='hunter2') + result = conn.describe_db_instances() + result['DescribeDBInstancesResponse']['DescribeDBInstancesResult']['DBInstances'][0]['DBSecurityGroups'].should.equal([]) + conn.create_db_security_group('db_sg', 'DB Security Group') + conn.modify_db_instance(db_instance_identifier='db-master-1', + db_security_groups=['db_sg']) + result = conn.describe_db_instances() + result['DescribeDBInstancesResponse']['DescribeDBInstancesResult']['DBInstances'][0]['DBSecurityGroups'][0]['DBSecurityGroup']['DBSecurityGroupName'].should.equal('db_sg') + + +@mock_ec2 +@mock_rds2 +def test_create_database_subnet_group(): + vpc_conn = boto.vpc.connect_to_region("us-west-2") + vpc = vpc_conn.create_vpc("10.0.0.0/16") + subnet1 = vpc_conn.create_subnet(vpc.id, "10.1.0.0/24") + subnet2 = vpc_conn.create_subnet(vpc.id, "10.2.0.0/24") + + subnet_ids = [subnet1.id, subnet2.id] + conn = boto.rds2.connect_to_region("us-west-2") + result = conn.create_db_subnet_group("db_subnet", "my db subnet", subnet_ids) + result['CreateDBSubnetGroupResponse']['CreateDBSubnetGroupResult']['DBSubnetGroup']['DBSubnetGroupName'].should.equal("db_subnet") + result['CreateDBSubnetGroupResponse']['CreateDBSubnetGroupResult']['DBSubnetGroup']['DBSubnetGroupDescription'].should.equal("my db subnet") + subnets = result['CreateDBSubnetGroupResponse']['CreateDBSubnetGroupResult']['DBSubnetGroup']['Subnets'] + subnet_group_ids = [subnets['Subnet'][0]['SubnetIdentifier'], subnets['Subnet'][1]['SubnetIdentifier']] + list(subnet_group_ids).should.equal(subnet_ids) + + +@disable_on_py3() +@mock_ec2 +@mock_rds2 +def test_create_database_in_subnet_group(): + vpc_conn = boto.vpc.connect_to_region("us-west-2") + vpc = vpc_conn.create_vpc("10.0.0.0/16") + subnet = vpc_conn.create_subnet(vpc.id, "10.1.0.0/24") + + conn = boto.rds2.connect_to_region("us-west-2") + conn.create_db_subnet_group("db_subnet1", "my db subnet", [subnet.id]) + conn.create_db_instance(db_instance_identifier='db-master-1', + allocated_storage=10, + engine='postgres', + db_instance_class='db.m1.small', + master_username='root', + master_user_password='hunter2', + db_subnet_group_name='db_subnet1') + result = conn.describe_db_instances("db-master-1") + result['DescribeDBInstancesResponse']['DescribeDBInstancesResult']['DBInstances'][0]['DBSubnetGroup']['DBSubnetGroupName'].should.equal("db_subnet1") @mock_ec2 @@ -493,41 +521,29 @@ def test_describe_database_subnet_group(): conn.describe_db_subnet_groups.when.called_with("not-a-subnet").should.throw(BotoServerError) -#@mock_ec2 -#@mock_rds2 -#def test_delete_database_subnet_group(): -# vpc_conn = boto.vpc.connect_to_region("us-west-2") -# vpc = vpc_conn.create_vpc("10.0.0.0/16") -# subnet = vpc_conn.create_subnet(vpc.id, "10.1.0.0/24") -# -# conn = boto.rds2.connect_to_region("us-west-2") -# conn.create_db_subnet_group("db_subnet1", "my db subnet", [subnet.id]) -# list(conn.get_all_db_subnet_groups()).should.have.length_of(1) -# -# conn.delete_db_subnet_group("db_subnet1") -# list(conn.get_all_db_subnet_groups()).should.have.length_of(0) -# -# conn.delete_db_subnet_group.when.called_with("db_subnet1").should.throw(BotoServerError) -# -# -#@disable_on_py3() -#@mock_ec2 -#@mock_rds2 -#def test_create_database_in_subnet_group(): -# vpc_conn = boto.vpc.connect_to_region("us-west-2") -# vpc = vpc_conn.create_vpc("10.0.0.0/16") -# subnet = vpc_conn.create_subnet(vpc.id, "10.1.0.0/24") -# -# conn = boto.rds2.connect_to_region("us-west-2") -# conn.create_db_subnet_group("db_subnet1", "my db subnet", [subnet.id]) -# -# database = conn.create_dbinstance("db-master-1", 10, 'db.m1.small', -# 'root', 'hunter2', db_subnet_group_name="db_subnet1") -# -# database = conn.get_all_dbinstances("db-master-1")[0] -# database.subnet_group.name.should.equal("db_subnet1") -# -# +@mock_ec2 +@mock_rds2 +def test_delete_database_subnet_group(): + vpc_conn = boto.vpc.connect_to_region("us-west-2") + vpc = vpc_conn.create_vpc("10.0.0.0/16") + subnet = vpc_conn.create_subnet(vpc.id, "10.1.0.0/24") + + conn = boto.rds2.connect_to_region("us-west-2") + result = conn.describe_db_subnet_groups() + result['DescribeDBSubnetGroupsResponse']['DescribeDBSubnetGroupsResult']['DBSubnetGroups'].should.have.length_of(0) + + conn.create_db_subnet_group("db_subnet1", "my db subnet", [subnet.id]) + result = conn.describe_db_subnet_groups() + result['DescribeDBSubnetGroupsResponse']['DescribeDBSubnetGroupsResult']['DBSubnetGroups'].should.have.length_of(1) + + conn.delete_db_subnet_group("db_subnet1") + result = conn.describe_db_subnet_groups() + result['DescribeDBSubnetGroupsResponse']['DescribeDBSubnetGroupsResult']['DBSubnetGroups'].should.have.length_of(0) + + conn.delete_db_subnet_group.when.called_with("db_subnet1").should.throw(BotoServerError) + + + #@disable_on_py3() #@mock_rds2 #def test_create_database_replica():