]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Add VPC resource implementation
authorSteve Baker <sbaker@redhat.com>
Tue, 20 Nov 2012 03:27:20 +0000 (16:27 +1300)
committerSteve Baker <sbaker@redhat.com>
Sun, 2 Dec 2012 22:54:33 +0000 (11:54 +1300)
This implements the AWS::EC2::VPC resource.

Change-Id: Ib50897d692292c42b21cffb7d8f6758f2d1a5f1f

heat/engine/resources/vpc.py [new file with mode: 0644]
heat/tests/test_vpc.py [new file with mode: 0644]

diff --git a/heat/engine/resources/vpc.py b/heat/engine/resources/vpc.py
new file mode 100644 (file)
index 0000000..1e76102
--- /dev/null
@@ -0,0 +1,55 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+#
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+
+from heat.openstack.common import log as logging
+from heat.engine import resource
+
+logger = logging.getLogger(__name__)
+
+
+class VPC(resource.Resource):
+    properties_schema = {'CidrBlock': {'Type': 'String'},
+                         'InstanceTenancy': {'Type': 'String',
+                            'AllowedValues': ['default', 'dedicated'],
+                            'Default': 'default',
+                            'Implemented': False}
+    }
+
+    def __init__(self, name, json_snippet, stack):
+        super(VPC, self).__init__(name, json_snippet, stack)
+
+    def handle_create(self):
+        client = self.quantum()
+        props = {'name': self.name}
+        # Creates a network with an implicit router
+        net = client.create_network({'network': props})['network']
+        router = client.create_router({'router': props})['router']
+        id = '%s:%s' % (net['id'], router['id'])
+        self.resource_id_set(id)
+
+    def handle_delete(self):
+        client = self.quantum()
+        (network_id, router_id) = self.resource_id.split(':')
+        client.delete_router(router_id)
+        client.delete_network(network_id)
+
+    def handle_update(self):
+        return self.UPDATE_REPLACE
+
+
+def resource_mapping():
+    return {
+        'AWS::EC2::VPC': VPC,
+    }
diff --git a/heat/tests/test_vpc.py b/heat/tests/test_vpc.py
new file mode 100644 (file)
index 0000000..4a5a1dc
--- /dev/null
@@ -0,0 +1,129 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+
+
+import sys
+
+import nose
+import unittest
+import mox
+
+from nose.plugins.attrib import attr
+
+from heat.common import context
+from heat.common import template_format
+from heat.engine.resources import vpc
+from heat.engine import parser
+from utils import skip_if
+
+try:
+    from quantumclient.v2_0 import client as quantumclient
+except:
+    skip_test = True
+else:
+    skip_test = False
+
+test_template_vpc = '''
+Resources:
+  the_vpc:
+    Type: AWS::EC2::VPC
+    Properties: {CidrBlock: '10.0.0.0/24'}
+'''
+
+
+class FakeQuantum():
+
+    def create_network(self, name):
+        return {"network": {
+            "status": "ACTIVE",
+            "subnets": [],
+            "name": "name",
+            "admin_state_up": True,
+            "shared": False,
+            "tenant_id": "c1210485b2424d48804aad5d39c61b8f",
+            "id": "aaaa"
+        }}
+
+    def create_router(self, name):
+        return {"router": {
+            "status": "ACTIVE",
+            "name": "name",
+            "admin_state_up": True,
+            "tenant_id": "c1210485b2424d48804aad5d39c61b8f",
+            "id": "bbbb"
+        }}
+
+    def delete_network(self, id):
+        pass
+
+    def delete_router(self, id):
+        pass
+
+
+@attr(tag=['unit', 'resource'])
+@attr(speed='fast')
+class QuantumTest(unittest.TestCase):
+    @skip_if(skip_test, 'unable to import quantumclient')
+    def setUp(self):
+        self.m = mox.Mox()
+        self.m.CreateMock(quantumclient)
+        self.m.StubOutWithMock(vpc.VPC, 'quantum')
+
+    def tearDown(self):
+        self.m.UnsetStubs()
+        print "QuantumTest teardown complete"
+
+    def parse_stack(self, t):
+        ctx = context.RequestContext.from_dict({
+            'tenant': 'test_tenant',
+            'username': 'test_username',
+            'password': 'password',
+            'auth_url': 'http://localhost:5000/v2.0'})
+        stack = parser.Stack(ctx, 'test_stack', parser.Template(t),
+            stack_id=-1, parameters={'external_network': 'abcd1234'})
+
+        return stack
+
+    def create_vpc(self, t, stack, resource_name):
+        resource = vpc.VPC('the_vpc',
+                                      t['Resources'][resource_name],
+                                      stack)
+        self.assertEqual(None, resource.create())
+        self.assertEqual(vpc.VPC.CREATE_COMPLETE, resource.state)
+        return resource
+
+    @skip_if(skip_test, 'unable to import quantumclient')
+    def test_vpc(self):
+        fq = FakeQuantum()
+        vpc.VPC.quantum().MultipleTimes().AndReturn(fq)
+
+        self.m.ReplayAll()
+        t = template_format.parse(test_template_vpc)
+        stack = self.parse_stack(t)
+        resource = self.create_vpc(t, stack, 'the_vpc')
+
+        resource.validate()
+
+        ref_id = resource.FnGetRefId()
+        self.assertEqual('aaaa:bbbb', ref_id)
+
+        self.assertEqual(vpc.VPC.UPDATE_REPLACE, resource.handle_update())
+
+        self.assertEqual(None, resource.delete())
+        self.m.VerifyAll()
+
+    # allows testing of the test directly, shown below
+    if __name__ == '__main__':
+        sys.argv.append(__file__)
+        nose.main()