Add Chassis DB model and DB-API.

This patch adds a 'extra', 'updated_at' and 'created_at'
attributes to the DB model of a chassis and DB-API methods
for CRUD operations on chassis.

Implements blueprint: ironic-object-model

Change-Id: I9240f318ad34752f93c1f16607b6d46658881dc4
This commit is contained in:
Roman Prykhodchenko 2013-06-19 01:13:15 +03:00
parent c8f4b6ffeb
commit 43f26c56b4
7 changed files with 187 additions and 0 deletions

View File

@ -249,6 +249,10 @@ class PortNotFound(NotFound):
message = _("Port %(port)s could not be found.")
class ChassisNotFound(NotFound):
message = _("Chassis %(chassis)s could not be found.")
class PowerStateFailure(IronicException):
message = _("Failed to set node power state to %(pstate)s.")

View File

@ -192,3 +192,34 @@ class Connection(object):
:param port: The id or MAC of a port.
"""
@abc.abstractmethod
def create_chassis(self, values):
"""Create a new chassis.
:param values: Dict of values.
"""
@abc.abstractmethod
def get_chassis(self, chassis):
"""Return a chassis representation.
:param chassis: The id or the UUID of a chassis.
:returns: A chassis.
"""
@abc.abstractmethod
def update_chassis(self, chassis, values):
"""Update properties of an chassis.
:param chassis: The id or the uuid of a chassis.
:param values: Dict of values to update.
:returns: A chassis.
"""
@abc.abstractmethod
def destroy_chassis(self, chassis):
"""Destroy a chassis.
:param chassis: The id or the uuid of a chassis.
"""

View File

@ -299,3 +299,40 @@ class Connection(api.Connection):
count = query.delete()
if count != 1:
raise exception.PortNotFound(port=port)
def get_chassis(self, chassis):
query = model_query(models.Chassis)
query = add_identity_filter(query, chassis)
try:
return query.one()
except NoResultFound:
raise exception.ChassisNotFound(chassis=chassis)
def create_chassis(self, values):
chassis = models.Chassis()
chassis.update(values)
chassis.save()
return chassis
def update_chassis(self, chassis, values):
session = get_session()
with session.begin():
query = model_query(models.Chassis, session=session)
query = add_identity_filter(query, chassis)
count = query.update(values)
if count != 1:
raise exception.ChassisNotFound(chassis=chassis)
ref = query.one()
return ref
def destroy_chassis(self, chassis):
session = get_session()
with session.begin():
query = model_query(models.Chassis, session=session)
query = add_identity_filter(query, chassis)
count = query.delete()
if count != 1:
raise exception.ChassisNotFound(chassis=chassis)

View File

@ -0,0 +1,31 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# -*- encoding: utf-8 -*-
#
# 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 sqlalchemy import Table, Column, Text, DateTime, MetaData
def upgrade(migrate_engine):
meta = MetaData()
meta.bind = migrate_engine
chassis = Table('chassis', meta, autoload=True)
chassis.create_column(Column('extra', Text))
chassis.create_column(Column('created_at', DateTime))
chassis.create_column(Column('updated_at', DateTime))
def downgrade(migrate_engine):
raise NotImplementedError('Downgrade from version 007 is unsupported.')

View File

@ -85,6 +85,7 @@ class Chassis(Base):
__tablename__ = 'chassis'
id = Column(Integer, primary_key=True)
uuid = Column(String(36), unique=True)
extra = Column(JSONEncodedDict)
class Node(Base):

View File

@ -0,0 +1,71 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2013 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
# 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.
"""Tests for manipulating Chassis via the DB API"""
from ironic.common import exception
from ironic.db import api as dbapi
from ironic.openstack.common import uuidutils
from ironic.tests.db import base
from ironic.tests.db import utils
class DbChassisTestCase(base.DbTestCase):
def setUp(self):
super(DbChassisTestCase, self).setUp()
self.dbapi = dbapi.get_instance()
self.ch = utils.get_test_chassis()
self.ch = self.dbapi.create_chassis(self.ch)
def test_get_chassis_by_id(self):
chassis = self.dbapi.get_chassis('42')
self.assertEqual(chassis['id'], self.ch['id'])
self.assertEqual(chassis['uuid'], self.ch['uuid'])
def test_get_chassis_by_uuid(self):
chassis = self.dbapi.get_chassis('42')
self.assertEqual(chassis['id'], self.ch['id'])
self.assertEqual(chassis['uuid'], self.ch['uuid'])
def test_get_chassis_that_does_not_exist(self):
self.assertRaises(exception.ChassisNotFound,
self.dbapi.get_chassis, 666)
def test_update_chassis(self):
new_uuid = uuidutils.generate_uuid()
self.ch['uuid'] = new_uuid
res = self.dbapi.update_chassis(self.ch['id'], {'uuid': new_uuid})
self.assertEqual(res['uuid'], new_uuid)
def test_update_chassis_that_does_not_exist(self):
new_uuid = uuidutils.generate_uuid()
self.assertRaises(exception.ChassisNotFound,
self.dbapi.update_chassis, 666, {'uuid': new_uuid})
def test_destroy_chassis(self):
self.dbapi.destroy_chassis(self.ch['id'])
self.assertRaises(exception.ChassisNotFound,
self.dbapi.get_chassis, self.ch['id'])
def test_destroy_chassis_that_does_not_exist(self):
self.assertRaises(exception.ChassisNotFound,
self.dbapi.destroy_chassis, 666)

View File

@ -98,3 +98,15 @@ def get_test_port(**kw):
}
return port
def get_test_chassis(**kw):
chassis = {
'id': kw.get('id', 42),
'uuid': kw.get('uuid', 'e74c40e0-d825-11e2-a28f-0800200c9a66'),
'extra': kw.get('extra', '{}'),
'created_at': kw.get('created_at'),
'updated_at': kw.get('updated_at'),
}
return chassis