blazar/blazar/states.py
Masahito Muroi 68d769bf71 Migrate climate namespace to blazar
Although the Climate project was renamed to Blazar in 2014, the code has
still been using the climate namespace.

This patch moves all code to the blazar directory and allows users and
operators to use the 'blazar' command namespace. The 'climate' namespace
remains O release to allow users of Blazar time to move their own
code to the blazar namespace, but will be removed in P release
development cycle.

Change-Id: Icbe6fab1051aae4ac819982a1fa4f323cb0bf2d0
Partial-Bug: #1662734
2017-03-02 09:36:14 +00:00

100 lines
3.1 KiB
Python

# Copyright (c) 2014 Red Hat.
#
# 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.
"""Actions and states for Blazar objects."""
import abc
from oslo_log import log as logging
import six
from blazar.db import api as db_api
from blazar.db import exceptions as db_exc
from blazar.manager import exceptions as mgr_exc
LOG = logging.getLogger(__name__)
@six.add_metaclass(abc.ABCMeta)
class ObjectState(object):
ACTIONS = (CREATE, DELETE, UPDATE
) = ('CREATE', 'DELETE', 'UPDATE')
STATUSES = (IN_PROGRESS, FAILED, COMPLETE
) = ('IN_PROGRESS', 'FAILED', 'COMPLETE')
id = None
action = None
status = None
status_reason = None
def __init__(self, id, autosave=True,
action=None, status=None, status_reason=None):
self.id = id
self.autosave = autosave
if action is not None and status is not None:
self.update(action, status, status_reason)
def current(self):
return {'action': self.action,
'status': self.status,
'status_reason': self.status_reason}
def update(self, action, status, status_reason=None):
if action not in self.ACTIONS or status not in self.STATUSES:
raise mgr_exc.InvalidStateUpdate(id=self.id, action=action,
status=status)
self.action = action
self.status = status
self.status_reason = status_reason
if self.autosave is True:
self.save()
@abc.abstractmethod
def save(self):
pass
class LeaseState(ObjectState):
ACTIONS = (CREATE, DELETE, UPDATE, START, STOP
) = ('CREATE', 'DELETE', 'UPDATE', 'START', 'STOP')
def __init__(self, id, autosave=True,
action=None, status=None, status_reason=None):
if action is None or status is None:
# NOTE(sbauza): The lease can be not yet in DB, so lease_get can
# return None
lease = db_api.lease_get(id) or {}
action = lease.get('action', action)
status = lease.get('status', status)
status_reason = lease.get('status_reason', status_reason)
super(LeaseState, self).__init__(id, autosave,
action, status, status_reason)
def save(self):
try:
db_api.lease_update(self.id, self.current())
except db_exc.BlazarDBException:
# Lease can be not yet in DB, we must first write it
raise mgr_exc.InvalidState(id=self.id, state=self.current())
return self.current()
# NOTE(sbauza): For convenient purpose
lease = LeaseState