Parcourir la source

Merge pull request #10 from aznashwan/val-dest-env

Validate the 'destination_environment' in API before appending the 'storage_mappings' to it.
Nashwan Azhari il y a 7 ans
Parent
commit
a113f1f38f
2 fichiers modifiés avec 32 ajouts et 24 suppressions
  1. 16 12
      coriolis/api/v1/migrations.py
  2. 16 12
      coriolis/api/v1/replicas.py

+ 16 - 12
coriolis/api/v1/migrations.py

@@ -45,7 +45,7 @@ class MigrationController(api_wsgi.Controller):
             req, self._migration_api.get_migrations(
                 context, include_tasks=True))
 
-    def _validate_migration_input(self, migration):
+    def _validate_migration_input(self, context, migration):
         try:
             origin_endpoint_id = migration["origin_endpoint_id"]
             destination_endpoint_id = migration["destination_endpoint_id"]
@@ -56,14 +56,25 @@ class MigrationController(api_wsgi.Controller):
 
             network_map = migration.get("network_map", {})
             api_utils.validate_network_map(network_map)
+            destination_environment['network_map'] = network_map
 
-            storage_mappings = migration.get("storage_mappings", {})
-            api_utils.validate_storage_mappings(storage_mappings)
+            # NOTE(aznashwan): we validate the destination environment for the
+            # import provider before appending the 'storage_mappings' parameter
+            # for plugins with strict property name checks which do not yet
+            # support storage mapping features:
+            is_valid, message = (
+                self._endpoints_api.validate_target_environment(
+                    context, destination_endpoint_id, destination_environment))
+            if not is_valid:
+                raise exc.HTTPBadRequest(
+                    explanation="Invalid destination "
+                                "environment: %s" % message)
 
             # TODO(aznashwan): until the provider plugin interface is updated
             # to have separate 'network_map' and 'storage_mappings' fields,
             # we add them as part of the destination environment:
-            destination_environment['network_map'] = network_map
+            storage_mappings = migration.get("storage_mappings", {})
+            api_utils.validate_storage_mappings(storage_mappings)
             destination_environment['storage_mappings'] = storage_mappings
 
             return (origin_endpoint_id, destination_endpoint_id,
@@ -98,14 +109,7 @@ class MigrationController(api_wsgi.Controller):
              notes,
              skip_os_morphing, network_map,
              storage_mappings) = self._validate_migration_input(
-                migration_body)
-            is_valid, message = (
-                self._endpoints_api.validate_target_environment(
-                    context, destination_endpoint_id, destination_environment))
-            if not is_valid:
-                raise exc.HTTPBadRequest(
-                    explanation="Invalid destination "
-                                "environment: %s" % message)
+                context, migration_body)
 
             migration = self._migration_api.migrate_instances(
                 context, origin_endpoint_id, destination_endpoint_id,

+ 16 - 12
coriolis/api/v1/replicas.py

@@ -45,7 +45,7 @@ class ReplicaController(api_wsgi.Controller):
             req, self._replica_api.get_replicas(
                 context, include_tasks_executions=True))
 
-    def _validate_create_body(self, body):
+    def _validate_create_body(self, context, body):
         try:
             replica = body["replica"]
 
@@ -57,6 +57,20 @@ class ReplicaController(api_wsgi.Controller):
 
             network_map = replica.get("network_map", {})
             api_utils.validate_network_map(network_map)
+            destination_environment['network_map'] = network_map
+
+            # NOTE(aznashwan): we validate the destination environment for the
+            # import provider before appending the 'storage_mappings' parameter
+            # for plugins with strict property name checks which do not yet
+            # support storage mapping features:
+            is_valid, message = (
+                self._endpoints_api.validate_target_environment(
+                    context, destination_endpoint_id,
+                    destination_environment))
+            if not is_valid:
+                raise exc.HTTPBadRequest(
+                    explanation="Invalid destination "
+                                "environment: %s" % message)
 
             storage_mappings = replica.get("storage_mappings", {})
             api_utils.validate_storage_mappings(storage_mappings)
@@ -64,7 +78,6 @@ class ReplicaController(api_wsgi.Controller):
             # TODO(aznashwan): until the provider plugin interface is updated
             # to have separate 'network_map' and 'storage_mappings' fields,
             # we add them as part of the destination environment:
-            destination_environment['network_map'] = network_map
             destination_environment['storage_mappings'] = storage_mappings
 
             return (origin_endpoint_id, destination_endpoint_id,
@@ -81,16 +94,7 @@ class ReplicaController(api_wsgi.Controller):
 
         (origin_endpoint_id, destination_endpoint_id,
          destination_environment, instances, network_map,
-         storage_mappings, notes) = self._validate_create_body(body)
-
-        is_valid, message = (
-            self._endpoints_api.validate_target_environment(
-                context, destination_endpoint_id,
-                destination_environment))
-        if not is_valid:
-            raise exc.HTTPBadRequest(
-                explanation="Invalid destination "
-                            "environment: %s" % message)
+         storage_mappings, notes) = self._validate_create_body(context, body)
 
         return replica_view.single(req, self._replica_api.create(
             context, origin_endpoint_id, destination_endpoint_id,