Add validations when creating/deleting resources
Change-Id: I4a2b4c0a9013605d465dc4ab2f012d1f8b785287
This commit is contained in:
parent
df17a1bda6
commit
d544f046f1
|
@ -24,9 +24,11 @@ from webob import exc
|
|||
from meteos.api import common
|
||||
from meteos.api.openstack import wsgi
|
||||
from meteos.api.views import datasets as dataset_views
|
||||
from meteos.common import constants
|
||||
from meteos import exception
|
||||
from meteos.i18n import _, _LI
|
||||
from meteos import engine
|
||||
from meteos import utils
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
@ -114,10 +116,15 @@ class DatasetController(wsgi.Controller, wsgi.AdminActionsMixin):
|
|||
try:
|
||||
experiment = self.engine_api.get_experiment(
|
||||
context, dataset['experiment_id'])
|
||||
utils.is_valid_status(experiment.__class__.__name__,
|
||||
experiment.status,
|
||||
constants.STATUS_AVAILABLE)
|
||||
template = self.engine_api.get_template(
|
||||
context, experiment.template_id)
|
||||
except exception.NotFound:
|
||||
raise exc.HTTPNotFound()
|
||||
except exception.InvalidStatus:
|
||||
raise
|
||||
|
||||
display_name = dataset.get('display_name')
|
||||
display_description = dataset.get('display_description')
|
||||
|
|
|
@ -24,9 +24,11 @@ from webob import exc
|
|||
from meteos.api import common
|
||||
from meteos.api.openstack import wsgi
|
||||
from meteos.api.views import experiments as experiment_views
|
||||
from meteos.common import constants
|
||||
from meteos import exception
|
||||
from meteos.i18n import _, _LI
|
||||
from meteos import engine
|
||||
from meteos import utils
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
@ -112,9 +114,15 @@ class ExperimentController(wsgi.Controller, wsgi.AdminActionsMixin):
|
|||
LOG.debug("Create experiment with request: %s", experiment)
|
||||
|
||||
try:
|
||||
self.engine_api.get_template(context, experiment['template_id'])
|
||||
template = self.engine_api.get_template(context,
|
||||
experiment['template_id'])
|
||||
utils.is_valid_status(template.__class__.__name__,
|
||||
template.status,
|
||||
constants.STATUS_AVAILABLE)
|
||||
except exception.NotFound:
|
||||
raise exc.HTTPNotFound()
|
||||
except exception.InvalidStatus:
|
||||
raise
|
||||
|
||||
display_name = experiment.get('display_name')
|
||||
display_description = experiment.get('display_description')
|
||||
|
|
|
@ -24,9 +24,11 @@ from webob import exc
|
|||
from meteos.api import common
|
||||
from meteos.api.openstack import wsgi
|
||||
from meteos.api.views import learnings as learning_views
|
||||
from meteos.common import constants
|
||||
from meteos import exception
|
||||
from meteos.i18n import _, _LI
|
||||
from meteos import engine
|
||||
from meteos import utils
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
@ -119,14 +121,23 @@ class LearningController(wsgi.Controller, wsgi.AdminActionsMixin):
|
|||
|
||||
try:
|
||||
model = self.engine_api.get_model(context, model_id)
|
||||
utils.is_valid_status(model.__class__.__name__,
|
||||
model.status,
|
||||
(constants.STATUS_AVAILABLE,
|
||||
constants.STATUS_ACTIVE))
|
||||
experiment = self.engine_api.get_experiment(
|
||||
context,
|
||||
model.experiment_id)
|
||||
utils.is_valid_status(experiment.__class__.__name__,
|
||||
experiment.status,
|
||||
constants.STATUS_AVAILABLE)
|
||||
template = self.engine_api.get_template(
|
||||
context,
|
||||
experiment.template_id)
|
||||
except exception.NotFound:
|
||||
raise exc.HTTPNotFound()
|
||||
except exception.InvalidStatus:
|
||||
raise
|
||||
|
||||
new_learning = self.engine_api.create_learning(
|
||||
context,
|
||||
|
|
|
@ -24,9 +24,11 @@ from webob import exc
|
|||
from meteos.api import common
|
||||
from meteos.api.openstack import wsgi
|
||||
from meteos.api.views import model_evaluations as model_evaluation_views
|
||||
from meteos.common import constants
|
||||
from meteos import exception
|
||||
from meteos.i18n import _, _LI
|
||||
from meteos import engine
|
||||
from meteos import utils
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
@ -122,14 +124,22 @@ class ModelEvaluationController(wsgi.Controller, wsgi.AdminActionsMixin):
|
|||
|
||||
try:
|
||||
model = self.engine_api.get_model(context, model_id)
|
||||
utils.is_valid_status(model.__class__.__name__,
|
||||
model.status,
|
||||
constants.STATUS_AVAILABLE)
|
||||
experiment = self.engine_api.get_experiment(
|
||||
context,
|
||||
model.experiment_id)
|
||||
utils.is_valid_status(experiment.__class__.__name__,
|
||||
experiment.status,
|
||||
constants.STATUS_AVAILABLE)
|
||||
template = self.engine_api.get_template(
|
||||
context,
|
||||
experiment.template_id)
|
||||
except exception.NotFound:
|
||||
raise exc.HTTPNotFound()
|
||||
except exception.InvalidStatus:
|
||||
raise
|
||||
|
||||
new_model_evaluation = self.engine_api.create_model_evaluation(
|
||||
context,
|
||||
|
|
|
@ -24,9 +24,11 @@ from webob import exc
|
|||
from meteos.api import common
|
||||
from meteos.api.openstack import wsgi
|
||||
from meteos.api.views import models as model_views
|
||||
from meteos.common import constants
|
||||
from meteos import exception
|
||||
from meteos.i18n import _, _LI
|
||||
from meteos import engine
|
||||
from meteos import utils
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
@ -42,6 +44,9 @@ class ModelMixin(object):
|
|||
|
||||
try:
|
||||
model = self.engine_api.get_model(context, id)
|
||||
utils.is_valid_status(model.__class__.__name__,
|
||||
model.status,
|
||||
constants.STATUS_AVAILABLE)
|
||||
experiment = self.engine_api.get_experiment(
|
||||
context, model.experiment_id)
|
||||
template = self.engine_api.get_template(
|
||||
|
@ -67,6 +72,9 @@ class ModelMixin(object):
|
|||
|
||||
try:
|
||||
model = self.engine_api.get_model(context, id)
|
||||
utils.is_valid_status(model.__class__.__name__,
|
||||
model.status,
|
||||
constants.STATUS_ACTIVE)
|
||||
experiment = self.engine_api.get_experiment(
|
||||
context, model.experiment_id)
|
||||
template = self.engine_api.get_template(
|
||||
|
@ -210,10 +218,15 @@ class ModelController(wsgi.Controller, ModelMixin, wsgi.AdminActionsMixin):
|
|||
try:
|
||||
experiment = self.engine_api.get_experiment(
|
||||
context, model['experiment_id'])
|
||||
utils.is_valid_status(experiment.__class__.__name__,
|
||||
experiment.status,
|
||||
constants.STATUS_AVAILABLE)
|
||||
template = self.engine_api.get_template(
|
||||
context, experiment.template_id)
|
||||
except exception.NotFound:
|
||||
raise exc.HTTPNotFound()
|
||||
except exception.InvalidStatus:
|
||||
raise
|
||||
|
||||
display_name = model.get('display_name')
|
||||
display_description = model.get('display_description')
|
||||
|
|
|
@ -31,7 +31,11 @@ from meteos.engine import rpcapi as engine_rpcapi
|
|||
from meteos import exception
|
||||
from meteos.i18n import _, _LE, _LI, _LW
|
||||
from meteos import policy
|
||||
from meteos import utils
|
||||
|
||||
DELETE_VALID_STATUSES = (constants.STATUS_AVAILABLE,
|
||||
constants.STATUS_ERROR,
|
||||
constants.STATUS_INACTIVE)
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
||||
|
@ -111,12 +115,9 @@ class API(base.Base):
|
|||
|
||||
template = self.db.template_get(context, id)
|
||||
|
||||
statuses = (constants.STATUS_AVAILABLE, constants.STATUS_ERROR,
|
||||
constants.STATUS_INACTIVE)
|
||||
if not (force or template['status'] in statuses):
|
||||
msg = _("Learning status must be one of %(statuses)s") % {
|
||||
"statuses": statuses}
|
||||
raise exception.InvalidLearning(reason=msg)
|
||||
utils.is_valid_status(template.__class__.__name__,
|
||||
template['status'],
|
||||
DELETE_VALID_STATUSES)
|
||||
|
||||
self.engine_rpcapi.delete_template(context, id)
|
||||
|
||||
|
@ -189,12 +190,9 @@ class API(base.Base):
|
|||
|
||||
experiment = self.db.experiment_get(context, id)
|
||||
|
||||
statuses = (constants.STATUS_AVAILABLE, constants.STATUS_ERROR,
|
||||
constants.STATUS_INACTIVE)
|
||||
if not (force or experiment['status'] in statuses):
|
||||
msg = _("Learning status must be one of %(statuses)s") % {
|
||||
"statuses": statuses}
|
||||
raise exception.InvalidLearning(reason=msg)
|
||||
utils.is_valid_status(experiment.__class__.__name__,
|
||||
experiment['status'],
|
||||
DELETE_VALID_STATUSES)
|
||||
|
||||
self.engine_rpcapi.delete_experiment(context, id)
|
||||
|
||||
|
@ -304,12 +302,9 @@ class API(base.Base):
|
|||
|
||||
dataset = self.db.dataset_get(context, id)
|
||||
|
||||
statuses = (constants.STATUS_AVAILABLE, constants.STATUS_ERROR,
|
||||
constants.STATUS_INACTIVE)
|
||||
if not (force or dataset['status'] in statuses):
|
||||
msg = _("Learning status must be one of %(statuses)s") % {
|
||||
"statuses": statuses}
|
||||
raise exception.InvalidLearning(reason=msg)
|
||||
utils.is_valid_status(dataset.__class__.__name__,
|
||||
dataset['status'],
|
||||
DELETE_VALID_STATUSES)
|
||||
|
||||
self.engine_rpcapi.delete_dataset(context,
|
||||
dataset['cluster_id'],
|
||||
|
@ -442,12 +437,9 @@ class API(base.Base):
|
|||
|
||||
model = self.db.model_get(context, id)
|
||||
|
||||
statuses = (constants.STATUS_AVAILABLE, constants.STATUS_ERROR,
|
||||
constants.STATUS_INACTIVE)
|
||||
if not (force or model['status'] in statuses):
|
||||
msg = _("Learning status must be one of %(statuses)s") % {
|
||||
"statuses": statuses}
|
||||
raise exception.InvalidLearning(reason=msg)
|
||||
utils.is_valid_status(model.__class__.__name__,
|
||||
model['status'],
|
||||
DELETE_VALID_STATUSES)
|
||||
|
||||
self.engine_rpcapi.delete_model(context,
|
||||
model['cluster_id'],
|
||||
|
@ -589,12 +581,9 @@ class API(base.Base):
|
|||
|
||||
model_evaluation = self.db.model_evaluation_get(context, id)
|
||||
|
||||
statuses = (constants.STATUS_AVAILABLE, constants.STATUS_ERROR,
|
||||
constants.STATUS_INACTIVE)
|
||||
if not (force or model_evaluation['status'] in statuses):
|
||||
msg = _("Model Evaluation status must be one of %(statuses)s") % {
|
||||
"statuses": statuses}
|
||||
raise exception.InvalidLearning(reason=msg)
|
||||
utils.is_valid_status(model_evaluation.__class__.__name__,
|
||||
model_evaluation['status'],
|
||||
DELETE_VALID_STATUSES)
|
||||
|
||||
if model_evaluation.job_id:
|
||||
self.engine_rpcapi\
|
||||
|
@ -691,12 +680,9 @@ class API(base.Base):
|
|||
|
||||
learning = self.db.learning_get(context, id)
|
||||
|
||||
statuses = (constants.STATUS_AVAILABLE, constants.STATUS_ERROR,
|
||||
constants.STATUS_INACTIVE)
|
||||
if not (force or learning['status'] in statuses):
|
||||
msg = _("Learning status must be one of %(statuses)s") % {
|
||||
"statuses": statuses}
|
||||
raise exception.InvalidLearning(reason=msg)
|
||||
utils.is_valid_status(learning.__class__.__name__,
|
||||
learning['status'],
|
||||
DELETE_VALID_STATUSES)
|
||||
|
||||
if learning.job_id:
|
||||
self.engine_rpcapi.delete_learning(context,
|
||||
|
|
|
@ -148,6 +148,10 @@ class InvalidLearning(Invalid):
|
|||
message = _("Invalid learning: %(reason)s.")
|
||||
|
||||
|
||||
class InvalidStatus(Invalid):
|
||||
message = _("Invalid Status: %(reason)s.")
|
||||
|
||||
|
||||
class NotAuthorized(MeteosException):
|
||||
message = _("Not authorized.")
|
||||
code = 403
|
||||
|
|
|
@ -353,3 +353,11 @@ def wait_for_access_update(context, db, learning_instance,
|
|||
raise exception.LearningMigrationFailed(reason=msg)
|
||||
else:
|
||||
time.sleep(tries ** 2)
|
||||
|
||||
|
||||
def is_valid_status(resource_name, status, valid_statuses):
|
||||
if status not in valid_statuses:
|
||||
msg = _("%(resource_name)s status must be %(valid_statuses)s") % {
|
||||
"resource_name": resource_name,
|
||||
"valid_statuses": valid_statuses}
|
||||
raise exception.InvalidStatus(reason=msg)
|
||||
|
|
Loading…
Reference in New Issue