From c8b35a23f05b79b07efe762c1bafdf61e7959972 Mon Sep 17 00:00:00 2001 From: Peter Razumovsky Date: Thu, 13 Nov 2014 15:00:12 +0300 Subject: [PATCH] Fix [H302] errors in heat/db Change-Id: I9afc8f1ec2a9ff1351cde28298470b8bcd807e7a --- heat/db/sqlalchemy/api.py | 19 +++++----- .../versions/033_software_config.py | 11 +++--- .../versions/034_raw_template_files.py | 4 +- .../versions/037_migrate_hot_template.py | 4 +- .../038_software_config_json_config.py | 9 ++--- .../migrate_repo/versions/044_snapshots.py | 4 +- heat/db/sqlalchemy/models.py | 38 +++++++++---------- heat/db/sqlalchemy/types.py | 15 +++++--- heat/tests/test_sqlalchemy_api.py | 14 ++----- 9 files changed, 55 insertions(+), 63 deletions(-) diff --git a/heat/db/sqlalchemy/api.py b/heat/db/sqlalchemy/api.py index ff9f11c34d..1d961de278 100644 --- a/heat/db/sqlalchemy/api.py +++ b/heat/db/sqlalchemy/api.py @@ -12,8 +12,7 @@ # under the License. '''Implementation of SQLAlchemy backend.''' -from datetime import datetime -from datetime import timedelta +import datetime import six import sys @@ -23,7 +22,7 @@ from oslo.db.sqlalchemy import utils import osprofiler.sqlalchemy import sqlalchemy from sqlalchemy import orm -from sqlalchemy.orm.session import Session +from sqlalchemy.orm import session as orm_session from heat.common import crypt from heat.common import exception @@ -429,7 +428,7 @@ def stack_delete(context, stack_id): '%(id)s %(msg)s') % { 'id': stack_id, 'msg': 'that does not exist'}) - session = Session.object_session(s) + session = orm_session.Session.object_session(s) for r in s.resources: session.delete(r) @@ -508,7 +507,7 @@ def user_creds_delete(context, user_creds_id): raise exception.NotFound( _('Attempt to delete user creds with id ' '%(id)s that does not exist') % {'id': user_creds_id}) - session = Session.object_session(creds) + session = orm_session.Session.object_session(creds) session.delete(creds) session.flush() @@ -674,7 +673,7 @@ def watch_rule_delete(context, watch_id): '%(id)s %(msg)s') % { 'id': watch_id, 'msg': 'that does not exist'}) - session = Session.object_session(wr) + session = orm_session.Session.object_session(wr) for d in wr.watch_data: session.delete(d) @@ -716,7 +715,7 @@ def software_config_get(context, config_id): def software_config_delete(context, config_id): config = software_config_get(context, config_id) - session = Session.object_session(config) + session = orm_session.Session.object_session(config) session.delete(config) session.flush() @@ -763,7 +762,7 @@ def software_deployment_update(context, deployment_id, values): def software_deployment_delete(context, deployment_id): deployment = software_deployment_get(context, deployment_id) - session = Session.object_session(deployment) + session = orm_session.Session.object_session(deployment) session.delete(deployment) session.flush() @@ -796,7 +795,7 @@ def snapshot_update(context, snapshot_id, values): def snapshot_delete(context, snapshot_id): snapshot = snapshot_get(context, snapshot_id) - session = Session.object_session(snapshot) + session = orm_session.Session.object_session(snapshot) session.delete(snapshot) session.flush() @@ -825,7 +824,7 @@ def purge_deleted(age, granularity='days'): elif granularity == 'minutes': age = age * 60 - time_line = datetime.now() - timedelta(seconds=age) + time_line = datetime.datetime.now() - datetime.timedelta(seconds=age) engine = get_engine() meta = sqlalchemy.MetaData() meta.bind = engine diff --git a/heat/db/sqlalchemy/migrate_repo/versions/033_software_config.py b/heat/db/sqlalchemy/migrate_repo/versions/033_software_config.py index 2e733799ec..9bfed20512 100644 --- a/heat/db/sqlalchemy/migrate_repo/versions/033_software_config.py +++ b/heat/db/sqlalchemy/migrate_repo/versions/033_software_config.py @@ -13,8 +13,7 @@ import sqlalchemy -from heat.db.sqlalchemy.types import Json -from heat.db.sqlalchemy.types import LongText +from heat.db.sqlalchemy import types def upgrade(migrate_engine): @@ -31,8 +30,8 @@ def upgrade(migrate_engine): sqlalchemy.Column('name', sqlalchemy.String(255), nullable=True), sqlalchemy.Column('group', sqlalchemy.String(255)), - sqlalchemy.Column('config', LongText), - sqlalchemy.Column('io', Json), + sqlalchemy.Column('config', types.LongText), + sqlalchemy.Column('io', types.Json), sqlalchemy.Column('tenant', sqlalchemy.String(64), nullable=False, index=True), @@ -56,8 +55,8 @@ def upgrade(migrate_engine): sqlalchemy.String(36), sqlalchemy.ForeignKey('software_config.id'), nullable=False), - sqlalchemy.Column('input_values', Json), - sqlalchemy.Column('output_values', Json), + sqlalchemy.Column('input_values', types.Json), + sqlalchemy.Column('output_values', types.Json), sqlalchemy.Column('signal_id', sqlalchemy.String(1024)), sqlalchemy.Column('action', sqlalchemy.String(255)), sqlalchemy.Column('status', sqlalchemy.String(255)), diff --git a/heat/db/sqlalchemy/migrate_repo/versions/034_raw_template_files.py b/heat/db/sqlalchemy/migrate_repo/versions/034_raw_template_files.py index 816c8e6897..b6b3d36900 100644 --- a/heat/db/sqlalchemy/migrate_repo/versions/034_raw_template_files.py +++ b/heat/db/sqlalchemy/migrate_repo/versions/034_raw_template_files.py @@ -13,7 +13,7 @@ import sqlalchemy -from heat.db.sqlalchemy.types import Json +from heat.db.sqlalchemy import types def upgrade(migrate_engine): @@ -21,7 +21,7 @@ def upgrade(migrate_engine): meta.bind = migrate_engine raw_template = sqlalchemy.Table('raw_template', meta, autoload=True) - files = sqlalchemy.Column('files', Json, default={}) + files = sqlalchemy.Column('files', types.Json, default={}) files.create(raw_template) diff --git a/heat/db/sqlalchemy/migrate_repo/versions/037_migrate_hot_template.py b/heat/db/sqlalchemy/migrate_repo/versions/037_migrate_hot_template.py index d2cb00321e..e023379557 100644 --- a/heat/db/sqlalchemy/migrate_repo/versions/037_migrate_hot_template.py +++ b/heat/db/sqlalchemy/migrate_repo/versions/037_migrate_hot_template.py @@ -19,7 +19,7 @@ import sqlalchemy from sqlalchemy.orm import sessionmaker from heat.common.i18n import _ -from heat.engine.hot.parameters import HOTParamSchema +from heat.engine.hot import parameters def upgrade(migrate_engine): @@ -49,7 +49,7 @@ def upgrade(migrate_engine): del schema['Type'] changed = True - if (schema.get('type') not in HOTParamSchema.TYPES + if (schema.get('type') not in parameters.HOTParamSchema.TYPES and schema['type'].istitle()): schema['type'] = schema['type'].lower() changed = True diff --git a/heat/db/sqlalchemy/migrate_repo/versions/038_software_config_json_config.py b/heat/db/sqlalchemy/migrate_repo/versions/038_software_config_json_config.py index d2ed199e72..4d19837fe6 100644 --- a/heat/db/sqlalchemy/migrate_repo/versions/038_software_config_json_config.py +++ b/heat/db/sqlalchemy/migrate_repo/versions/038_software_config_json_config.py @@ -13,21 +13,20 @@ import sqlalchemy -from heat.db.sqlalchemy.types import Json -from heat.db.sqlalchemy.types import LongText +from heat.db.sqlalchemy import types def upgrade(migrate_engine): meta = sqlalchemy.MetaData(bind=migrate_engine) software_config = sqlalchemy.Table('software_config', meta, autoload=True) - software_config.c.config.alter(type=Json) + software_config.c.config.alter(type=types.Json) software_config.c.io.drop() def downgrade(migrate_engine): meta = sqlalchemy.MetaData(bind=migrate_engine) software_config = sqlalchemy.Table('software_config', meta, autoload=True) - software_config.c.config.alter(type=LongText) + software_config.c.config.alter(type=types.LongText) - io = sqlalchemy.Column('io', Json) + io = sqlalchemy.Column('io', types.Json) io.create(software_config) diff --git a/heat/db/sqlalchemy/migrate_repo/versions/044_snapshots.py b/heat/db/sqlalchemy/migrate_repo/versions/044_snapshots.py index 7156917706..8f71435262 100644 --- a/heat/db/sqlalchemy/migrate_repo/versions/044_snapshots.py +++ b/heat/db/sqlalchemy/migrate_repo/versions/044_snapshots.py @@ -13,7 +13,7 @@ import sqlalchemy -from heat.db.sqlalchemy.types import Json +from heat.db.sqlalchemy import types def upgrade(migrate_engine): @@ -34,7 +34,7 @@ def upgrade(migrate_engine): sqlalchemy.Column('updated_at', sqlalchemy.DateTime), sqlalchemy.Column('status', sqlalchemy.String(255)), sqlalchemy.Column('status_reason', sqlalchemy.String(255)), - sqlalchemy.Column('data', Json), + sqlalchemy.Column('data', types.Json), sqlalchemy.Column('tenant', sqlalchemy.String(64), nullable=False, index=True), diff --git a/heat/db/sqlalchemy/models.py b/heat/db/sqlalchemy/models.py index e747128b59..65e00c79d5 100644 --- a/heat/db/sqlalchemy/models.py +++ b/heat/db/sqlalchemy/models.py @@ -20,14 +20,14 @@ from oslo.db.sqlalchemy import models from oslo.utils import timeutils import six import sqlalchemy -from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.ext import declarative from sqlalchemy.orm import backref from sqlalchemy.orm import relationship -from sqlalchemy.orm.session import Session +from sqlalchemy.orm import session as orm_session -from heat.db.sqlalchemy.types import Json +from heat.db.sqlalchemy import types -BASE = declarative_base() +BASE = declarative.declarative_base() def get_session(): @@ -42,7 +42,7 @@ class HeatBase(models.ModelBase, models.TimestampMixin): def expire(self, session=None, attrs=None): """Expire this object ().""" if not session: - session = Session.object_session(self) + session = orm_session.Session.object_session(self) if not session: session = get_session() session.expire(self, attrs) @@ -50,7 +50,7 @@ class HeatBase(models.ModelBase, models.TimestampMixin): def refresh(self, session=None, attrs=None): """Refresh this object.""" if not session: - session = Session.object_session(self) + session = orm_session.Session.object_session(self) if not session: session = get_session() session.refresh(self, attrs) @@ -58,7 +58,7 @@ class HeatBase(models.ModelBase, models.TimestampMixin): def delete(self, session=None): """Delete this object.""" if not session: - session = Session.object_session(self) + session = orm_session.Session.object_session(self) if not session: session = get_session() session.begin() @@ -67,7 +67,7 @@ class HeatBase(models.ModelBase, models.TimestampMixin): def update_and_save(self, values, session=None): if not session: - session = Session.object_session(self) + session = orm_session.Session.object_session(self) if not session: session = get_session() session.begin() @@ -105,8 +105,8 @@ class RawTemplate(BASE, HeatBase): __tablename__ = 'raw_template' id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True) - template = sqlalchemy.Column(Json) - files = sqlalchemy.Column(Json) + template = sqlalchemy.Column(types.Json) + files = sqlalchemy.Column(types.Json) class Stack(BASE, HeatBase, SoftDelete, StateAware): @@ -124,7 +124,7 @@ class Stack(BASE, HeatBase, SoftDelete, StateAware): raw_template = relationship(RawTemplate, backref=backref('stack')) username = sqlalchemy.Column(sqlalchemy.String(256)) tenant = sqlalchemy.Column(sqlalchemy.String(256)) - parameters = sqlalchemy.Column('parameters', Json) + parameters = sqlalchemy.Column('parameters', types.Json) user_creds_id = sqlalchemy.Column( sqlalchemy.Integer, sqlalchemy.ForeignKey('user_creds.id')) @@ -235,7 +235,7 @@ class Resource(BASE, HeatBase, StateAware): name = sqlalchemy.Column('name', sqlalchemy.String(255), nullable=True) nova_instance = sqlalchemy.Column('nova_instance', sqlalchemy.String(255)) # odd name as "metadata" is reserved - rsrc_metadata = sqlalchemy.Column('rsrc_metadata', Json) + rsrc_metadata = sqlalchemy.Column('rsrc_metadata', types.Json) stack_id = sqlalchemy.Column(sqlalchemy.String(36), sqlalchemy.ForeignKey('stack.id'), @@ -249,7 +249,7 @@ class Resource(BASE, HeatBase, StateAware): # time the create/update call was issued, not the time the DB entry is # created/modified. (bug #1193269) updated_at = sqlalchemy.Column(sqlalchemy.DateTime) - properties_data = sqlalchemy.Column('properties_data', Json) + properties_data = sqlalchemy.Column('properties_data', types.Json) class WatchRule(BASE, HeatBase): @@ -259,7 +259,7 @@ class WatchRule(BASE, HeatBase): id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True) name = sqlalchemy.Column('name', sqlalchemy.String(255), nullable=True) - rule = sqlalchemy.Column('rule', Json) + rule = sqlalchemy.Column('rule', types.Json) state = sqlalchemy.Column('state', sqlalchemy.String(255)) last_evaluated = sqlalchemy.Column(sqlalchemy.DateTime, default=timeutils.utcnow) @@ -276,7 +276,7 @@ class WatchData(BASE, HeatBase): __tablename__ = 'watch_data' id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True) - data = sqlalchemy.Column('data', Json) + data = sqlalchemy.Column('data', types.Json) watch_rule_id = sqlalchemy.Column( sqlalchemy.Integer, @@ -298,7 +298,7 @@ class SoftwareConfig(BASE, HeatBase): name = sqlalchemy.Column('name', sqlalchemy.String(255), nullable=True) group = sqlalchemy.Column('group', sqlalchemy.String(255)) - config = sqlalchemy.Column('config', Json) + config = sqlalchemy.Column('config', types.Json) tenant = sqlalchemy.Column( 'tenant', sqlalchemy.String(64), nullable=False, index=True) @@ -323,8 +323,8 @@ class SoftwareDeployment(BASE, HeatBase, StateAware): config = relationship(SoftwareConfig, backref=backref('deployments')) server_id = sqlalchemy.Column('server_id', sqlalchemy.String(36), nullable=False, index=True) - input_values = sqlalchemy.Column('input_values', Json) - output_values = sqlalchemy.Column('output_values', Json) + input_values = sqlalchemy.Column('input_values', types.Json) + output_values = sqlalchemy.Column('output_values', types.Json) tenant = sqlalchemy.Column( 'tenant', sqlalchemy.String(64), nullable=False, index=True) stack_user_project_id = sqlalchemy.Column(sqlalchemy.String(64), @@ -341,7 +341,7 @@ class Snapshot(BASE, HeatBase): sqlalchemy.ForeignKey('stack.id'), nullable=False) name = sqlalchemy.Column('name', sqlalchemy.String(255), nullable=True) - data = sqlalchemy.Column('data', Json) + data = sqlalchemy.Column('data', types.Json) tenant = sqlalchemy.Column( 'tenant', sqlalchemy.String(64), nullable=False, index=True) status = sqlalchemy.Column('status', sqlalchemy.String(255)) diff --git a/heat/db/sqlalchemy/types.py b/heat/db/sqlalchemy/types.py index 5ea154774f..53a2085283 100644 --- a/heat/db/sqlalchemy/types.py +++ b/heat/db/sqlalchemy/types.py @@ -11,13 +11,16 @@ # License for the specific language governing permissions and limitations # under the License. -from json import dumps -from json import loads +import json from sqlalchemy.dialects import mysql from sqlalchemy import types +dumps = json.dumps +loads = json.loads + + class LongText(types.TypeDecorator): impl = types.Text @@ -43,11 +46,11 @@ def associate_with(sqltype): # TODO(leizhang) When we removed sqlalchemy 0.7 dependence # we can import MutableDict directly and remove ./mutable.py try: - from sqlalchemy.ext.mutable import MutableDict as sa_MutableDict - sa_MutableDict.associate_with(Json) + from sqlalchemy.ext import mutable + mutable.MutableDict.associate_with(Json) except ImportError: - from heat.db.sqlalchemy.mutable import MutableDict - MutableDict.associate_with(Json) + from heat.db.sqlalchemy import mutable + mutable.MutableDict.associate_with(Json) associate_with(LongText) associate_with(Json) diff --git a/heat/tests/test_sqlalchemy_api.py b/heat/tests/test_sqlalchemy_api.py index b9b10a5c24..48644cee79 100644 --- a/heat/tests/test_sqlalchemy_api.py +++ b/heat/tests/test_sqlalchemy_api.py @@ -11,14 +11,12 @@ # License for the specific language governing permissions and limitations # under the License. -from datetime import datetime -from datetime import timedelta +import datetime from json import dumps from json import loads import six import uuid -import fixtures import mock import mox from oslo.utils import timeutils @@ -1498,20 +1496,14 @@ class DBAPIStackTest(common.HeatTestCase): db_api.stack_count_all(self.ctx, tenant_safe=False)) def test_purge_deleted(self): - now = datetime.now() - delta = timedelta(seconds=3600 * 7) + now = datetime.datetime.now() + delta = datetime.timedelta(seconds=3600 * 7) deleted = [now - delta * i for i in range(1, 6)] templates = [create_raw_template(self.ctx) for i in range(5)] creds = [create_user_creds(self.ctx) for i in range(5)] stacks = [create_stack(self.ctx, templates[i], creds[i], deleted_at=deleted[i]) for i in range(5)] - class MyDatetime(object): - def now(self): - return now - self.useFixture(fixtures.MonkeyPatch('heat.db.sqlalchemy.api.datetime', - MyDatetime())) - db_api.purge_deleted(age=1, granularity='days') self._deleted_stack_existance(utils.dummy_context(), stacks, (0, 1, 2), (3, 4))