From 5753438add1b7cafe1a70bc256789679bd1e7b7e Mon Sep 17 00:00:00 2001 From: Alvaro Lopez Garcia Date: Tue, 3 Mar 2015 13:58:49 +0100 Subject: [PATCH] Add OpenStack Resource Template --- .coveragerc | 4 ++-- ooi/openstack/templates.py | 42 +++++++++++++++++++++++++++++++++++++ ooi/tests/test_openstack.py | 27 ++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/.coveragerc b/.coveragerc index c438e25..2a35d4e 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1,7 +1,7 @@ [run] branch = True source = ooi -omit = ooi/tests/*,ooi/openstack/* +omit = ooi/tests/* [report] -ignore-errors = True \ No newline at end of file +ignore-errors = True diff --git a/ooi/openstack/templates.py b/ooi/openstack/templates.py index 4e74c43..bc887ab 100644 --- a/ooi/openstack/templates.py +++ b/ooi/openstack/templates.py @@ -14,6 +14,7 @@ # License for the specific language governing permissions and limitations # under the License. +from ooi.occi.core import attribute from ooi.occi.core import mixin from ooi.occi.infrastructure import templates from ooi.openstack import helpers @@ -26,3 +27,44 @@ class OpenStackOSTemplate(mixin.Mixin): uuid, name, related=[templates.os_tpl]) + + +class OpenStackResourceTemplate(mixin.Mixin): + + def __init__(self, name, cores, memory, disk, ephemeral=0, swap=0): + attrs = [ + attribute.InmutableAttribute("occi.compute.cores", cores), + attribute.InmutableAttribute("occi.compute.memory", memory), + attribute.InmutableAttribute("occi.compute.disk", disk), + attribute.InmutableAttribute("occi.compute.ephemeral", ephemeral), + attribute.InmutableAttribute("occi.compute.swap", swap) + ] + + attrs = attribute.AttributeCollection({a.name: a for a in attrs}) + + super(OpenStackResourceTemplate, self).__init__( + helpers.build_scheme("template/resource"), + name, + "Flavor: %s" % name, + related=[templates.resource_tpl], + attributes=attrs) + + @property + def cores(self): + return self.attributes["occi.compute.cores"].value + + @property + def memory(self): + return self.attributes["occi.compute.memory"].value + + @property + def disk(self): + return self.attributes["occi.compute.disk"].value + + @property + def ephemeral(self): + return self.attributes["occi.compute.ephemeral"].value + + @property + def swap(self): + return self.attributes["occi.compute.swap"].value diff --git a/ooi/tests/test_openstack.py b/ooi/tests/test_openstack.py index 01e66f0..cfb4da9 100644 --- a/ooi/tests/test_openstack.py +++ b/ooi/tests/test_openstack.py @@ -33,3 +33,30 @@ class TestOpenStackOSTemplate(base.TestCase): self.assertEqual(title, tpl.title) self.assertTrue(tpl.scheme.startswith(helpers._PREFIX)) self.assertIn(occi_templates.os_tpl, tpl.related) + + +class TestOpenStackResourceTemplate(base.TestCase): + def test_resource_template(self): + name = "m1.humongous" + cores = 10 + memory = 30 + disk = 40 + swap = 20 + ephemeral = 50 + + tpl = templates.OpenStackResourceTemplate(name, + cores, + memory, + disk, + swap=swap, + ephemeral=ephemeral) + + self.assertEqual(name, tpl.term) + self.assertEqual("Flavor: %s" % name, tpl.title) + self.assertTrue(tpl.scheme.startswith(helpers._PREFIX)) + self.assertIn(occi_templates.resource_tpl, tpl.related) + self.assertEqual(cores, tpl.cores) + self.assertEqual(memory, tpl.memory) + self.assertEqual(disk, tpl.disk) + self.assertEqual(swap, tpl.swap) + self.assertEqual(ephemeral, tpl.ephemeral)