Skip to content

Commit

Permalink
Refactor API layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Dany9966 committed Nov 19, 2024
1 parent f199fe2 commit 41b9db9
Show file tree
Hide file tree
Showing 64 changed files with 1,095 additions and 1,364 deletions.
18 changes: 9 additions & 9 deletions coriolis/api/v1/deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ def detail(self, req):
def _validate_deployment_input(self, context, body):
deployment = body["deployment"]

replica_id = deployment.get("replica_id", "")
transfer_id = deployment.get("transfer_id", "")

if not replica_id:
if not transfer_id:
raise exc.HTTPBadRequest(
explanation="Missing 'replica_id' field from deployment "
explanation="Missing 'transfer_id' field from deployment "
"body. A deployment can be created strictly "
"based on an existing Replica.")
"based on an existing Transfer.")

clone_disks = deployment.get("clone_disks", True)
force = deployment.get("force", False)
Expand All @@ -84,23 +84,23 @@ def _validate_deployment_input(self, context, body):
user_scripts, deployment.get("instances", []))

return (
replica_id, force, clone_disks, skip_os_morphing,
transfer_id, force, clone_disks, skip_os_morphing,
instance_osmorphing_minion_pool_mappings,
user_scripts)

def create(self, req, body):
context = req.environ['coriolis.context']
context.can(deployment_policies.get_deployments_policy_label("create"))

(replica_id, force, clone_disks, skip_os_morphing,
(transfer_id, force, clone_disks, skip_os_morphing,
instance_osmorphing_minion_pool_mappings,
user_scripts) = self._validate_deployment_input(
context, body)

# NOTE: destination environment for replica should have been
# NOTE: destination environment for transfer should have been
# validated upon its creation.
deployment = self._deployment_api.deploy_replica_instances(
context, replica_id, instance_osmorphing_minion_pool_mappings,
deployment = self._deployment_api.deploy_transfer_instances(
context, transfer_id, instance_osmorphing_minion_pool_mappings,
clone_disks, force, skip_os_morphing,
user_scripts=user_scripts)

Expand Down
74 changes: 0 additions & 74 deletions coriolis/api/v1/replica_tasks_executions.py

This file was deleted.

58 changes: 30 additions & 28 deletions coriolis/api/v1/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
from coriolis.api.v1 import provider_schemas
from coriolis.api.v1 import providers
from coriolis.api.v1 import regions
from coriolis.api.v1 import replica_actions
from coriolis.api.v1 import replica_schedules
from coriolis.api.v1 import replica_tasks_execution_actions
from coriolis.api.v1 import replica_tasks_executions
from coriolis.api.v1 import replicas
from coriolis.api.v1 import services
from coriolis.api.v1 import transfer_actions
from coriolis.api.v1 import transfer_schedules
from coriolis.api.v1 import transfer_tasks_execution_actions
from coriolis.api.v1 import transfer_tasks_executions
from coriolis.api.v1 import transfers

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -154,44 +154,46 @@ def _setup_routes(self, mapper, ext_mgr):
action='action',
conditions={'method': 'POST'})

self.resources['replicas'] = replicas.create_resource()
mapper.resource('replica', 'replicas',
controller=self.resources['replicas'],
self.resources['transfers'] = transfers.create_resource()
mapper.resource('transfer', 'transfers',
controller=self.resources['transfers'],
collection={'detail': 'GET'},
member={'action': 'POST'})

replica_actions_resource = replica_actions.create_resource()
self.resources['replica_actions'] = replica_actions_resource
migration_path = '/{project_id}/replicas/{id}'
mapper.connect('replica_actions',
transfer_actions_resource = transfer_actions.create_resource()
self.resources['transfer_actions'] = transfer_actions_resource
migration_path = '/{project_id}/transfers/{id}'
mapper.connect('transfer_actions',
migration_path + '/actions',
controller=self.resources['replica_actions'],
controller=self.resources['transfer_actions'],
action='action',
conditions={'method': 'POST'})

self.resources['replica_tasks_executions'] = \
replica_tasks_executions.create_resource()
mapper.resource('execution', 'replicas/{replica_id}/executions',
controller=self.resources['replica_tasks_executions'],
self.resources['transfer_tasks_executions'] = \
transfer_tasks_executions.create_resource()
mapper.resource('execution', 'transfers/{transfer_id}/executions',
controller=self.resources['transfer_tasks_executions'],
collection={'detail': 'GET'},
member={'action': 'POST'})

replica_tasks_execution_actions_resource = \
replica_tasks_execution_actions.create_resource()
self.resources['replica_tasks_execution_actions'] = \
replica_tasks_execution_actions_resource
migration_path = '/{project_id}/replicas/{replica_id}/executions/{id}'
mapper.connect('replica_tasks_execution_actions',
transfer_tasks_execution_actions_resource = \
transfer_tasks_execution_actions.create_resource()
self.resources['transfer_tasks_execution_actions'] = \
transfer_tasks_execution_actions_resource
migration_path = ('/{project_id}/transfers/{transfer_id}/'
'executions/{id}')
mapper.connect('transfer_tasks_execution_actions',
migration_path + '/actions',
controller=self.resources[
'replica_tasks_execution_actions'],
'transfer_tasks_execution_actions'],
action='action',
conditions={'method': 'POST'})

sched = replica_schedules.create_resource()
self.resources['replica_schedules'] = sched
mapper.resource('replica_schedule', 'replicas/{replica_id}/schedules',
controller=self.resources['replica_schedules'],
sched = transfer_schedules.create_resource()
self.resources['transfer_schedules'] = sched
mapper.resource('transfer_schedule',
'transfers/{transfer_id}/schedules',
controller=self.resources['transfer_schedules'],
collection={'index': 'GET'},
member={'action': 'POST'})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
# Copyright 2016 Cloudbase Solutions Srl
# All Rights Reserved.

from coriolis.api.v1.views import replica_tasks_execution_view
from coriolis.api.v1.views import transfer_tasks_execution_view
from coriolis.api import wsgi as api_wsgi
from coriolis import exception
from coriolis.policies import replicas as replica_policies
from coriolis.replicas import api
from coriolis.policies import transfers as transfer_policies
from coriolis.transfers import api

from webob import exc


class ReplicaActionsController(api_wsgi.Controller):
class TransferActionsController(api_wsgi.Controller):
def __init__(self):
self._replica_api = api.API()
super(ReplicaActionsController, self).__init__()
self._transfer_api = api.API()
super(TransferActionsController, self).__init__()

@api_wsgi.action('delete-disks')
def _delete_disks(self, req, id, body):
context = req.environ['coriolis.context']
context.can(
replica_policies.get_replicas_policy_label("delete_disks"))
transfer_policies.get_transfers_policy_label("delete_disks"))
try:
return replica_tasks_execution_view.single(
self._replica_api.delete_disks(context, id))
return transfer_tasks_execution_view.single(
self._transfer_api.delete_disks(context, id))
except exception.NotFound as ex:
raise exc.HTTPNotFound(explanation=ex.msg)
except exception.InvalidParameterValue as ex:
raise exc.HTTPNotFound(explanation=ex.msg)


def create_resource():
return api_wsgi.Resource(ReplicaActionsController())
return api_wsgi.Resource(TransferActionsController())
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Copyright 2017 Cloudbase Solutions Srl
# All Rights Reserved.

from coriolis.api.v1.views import replica_schedule_view
from coriolis.api.v1.views import transfer_schedule_view
from coriolis.api import wsgi as api_wsgi
from coriolis import exception
from coriolis.policies import replica_schedules as schedules_policies
from coriolis.policies import transfer_schedules as schedules_policies
from coriolis import schemas
from coriolis.transfer_cron import api

Expand All @@ -18,31 +18,31 @@
LOG = logging.getLogger(__name__)


class ReplicaScheduleController(api_wsgi.Controller):
class TransferScheduleController(api_wsgi.Controller):
def __init__(self):
self._schedule_api = api.API()
super(ReplicaScheduleController, self).__init__()
super(TransferScheduleController, self).__init__()

def show(self, req, replica_id, id):
def show(self, req, transfer_id, id):
context = req.environ["coriolis.context"]
context.can(
schedules_policies.get_replica_schedules_policy_label("show"))
schedule = self._schedule_api.get_schedule(context, replica_id, id)
schedules_policies.get_transfer_schedules_policy_label("show"))
schedule = self._schedule_api.get_schedule(context, transfer_id, id)
if not schedule:
raise exc.HTTPNotFound()

return replica_schedule_view.single(schedule)
return transfer_schedule_view.single(schedule)

def index(self, req, replica_id):
def index(self, req, transfer_id):
context = req.environ["coriolis.context"]
context.can(
schedules_policies.get_replica_schedules_policy_label("list"))
schedules_policies.get_transfer_schedules_policy_label("list"))

show_expired = strutils.bool_from_string(
req.GET.get("show_expired", True), strict=True)
return replica_schedule_view.collection(
return transfer_schedule_view.collection(
self._schedule_api.get_schedules(
context, replica_id, expired=show_expired))
context, transfer_id, expired=show_expired))

def _validate_schedule(self, schedule):
schema = schemas.SCHEDULE_API_BODY_SCHEMA["properties"]["schedule"]
Expand Down Expand Up @@ -100,45 +100,45 @@ def _validate_update_body(self, update_body):
body["expiration_date"] = exp
return body

def create(self, req, replica_id, body):
def create(self, req, transfer_id, body):
context = req.environ["coriolis.context"]
context.can(
schedules_policies.get_replica_schedules_policy_label("create"))
schedules_policies.get_transfer_schedules_policy_label("create"))

LOG.debug("Got request: %r %r %r" % (req, replica_id, body))
LOG.debug("Got request: %r %r %r" % (req, transfer_id, body))
try:
schedule, enabled, exp_date, shutdown = self._validate_create_body(
body)
except Exception as err:
raise exception.InvalidInput(err)

return replica_schedule_view.single(self._schedule_api.create(
context, replica_id, schedule, enabled, exp_date, shutdown))
return transfer_schedule_view.single(self._schedule_api.create(
context, transfer_id, schedule, enabled, exp_date, shutdown))

def update(self, req, replica_id, id, body):
def update(self, req, transfer_id, id, body):
context = req.environ["coriolis.context"]
context.can(
schedules_policies.get_replica_schedules_policy_label("update"))
schedules_policies.get_transfer_schedules_policy_label("update"))

LOG.debug("Got request: %r %r %r %r" % (
req, replica_id, id, body))
req, transfer_id, id, body))

try:
update_values = self._validate_update_body(body)
except Exception as err:
raise exception.InvalidInput(err)

return replica_schedule_view.single(self._schedule_api.update(
context, replica_id, id, update_values))
return transfer_schedule_view.single(self._schedule_api.update(
context, transfer_id, id, update_values))

def delete(self, req, replica_id, id):
def delete(self, req, transfer_id, id):
context = req.environ["coriolis.context"]
context.can(
schedules_policies.get_replica_schedules_policy_label("delete"))
schedules_policies.get_transfer_schedules_policy_label("delete"))

self._schedule_api.delete(context, replica_id, id)
self._schedule_api.delete(context, transfer_id, id)
raise exc.HTTPNoContent()


def create_resource():
return api_wsgi.Resource(ReplicaScheduleController())
return api_wsgi.Resource(TransferScheduleController())
Loading

0 comments on commit 41b9db9

Please sign in to comment.