Skip to content

Commit 56fd496

Browse files
thomassagaborigloi
authored andcommitted
CP-24903: new error network_incompatible_purposes
Define this type of error, and raise it as and where appropriate. Signed-off-by: Thomas Sanders <[email protected]>
1 parent 2ec0d2a commit 56fd496

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

ocaml/idl/datamodel.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,8 @@ let _ =
566566
~doc:"You tried to create a PIF, but the network you tried to attach it to is already attached to some other PIF, and so the creation failed." ();
567567
error Api_errors.cannot_destroy_system_network [ "network" ]
568568
~doc:"You tried to destroy a system network: these cannot be destroyed." ();
569+
error Api_errors.network_incompatible_purposes ["new_purpose"; "conflicting_purpose"]
570+
~doc:"You tried to add a purpose to a network but the new purpose is not compatible with an existing purpose of the network or other networks." ();
569571
error Api_errors.pif_is_physical ["PIF"]
570572
~doc:"You tried to destroy a PIF, but it represents an aspect of the physical host configuration, and so cannot be destroyed. The parameter echoes the PIF handle you gave." ();
571573
error Api_errors.pif_is_vlan ["PIF"]

ocaml/xapi-consts/api_errors.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ let operation_not_allowed = "OPERATION_NOT_ALLOWED"
9999
let operation_blocked = "OPERATION_BLOCKED"
100100
let network_already_connected = "NETWORK_ALREADY_CONNECTED"
101101
let network_unmanaged = "NETWORK_UNMANAGED"
102+
let network_incompatible_purposes = "NETWORK_INCOMPATIBLE_PURPOSES"
103+
102104
let cannot_destroy_system_network = "CANNOT_DESTROY_SYSTEM_NETWORK"
103105
let pif_is_physical = "PIF_IS_PHYSICAL"
104106
let pif_is_vlan = "PIF_IS_VLAN"

ocaml/xapi/xapi_network.ml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,27 +332,33 @@ let with_networks_attached_for_vm ~__context ?host ~vm f =
332332

333333
let assert_can_add_purpose ~__context ~network ~current newval =
334334
let sop (*string-of-porpoise*) = Record_util.network_purpose_to_string in
335+
let reject str =
336+
raise Api_errors.(Server_error (network_incompatible_purposes, [ (sop newval); str ]))
337+
in
335338
if (List.mem `himn current) then (
336-
info "Existing network purpose %s is not compatible with any other purpose." (sop `himn)
339+
info "Existing network purpose %s is not compatible with any other purpose." (sop `himn);
340+
reject (sop `himn)
337341
);
338342
if (List.mem `unmanaged current) then (
339-
info "Existing network purpose %s is not compatible with any other purpose." (sop `unmanaged)
343+
info "Existing network purpose %s is not compatible with any other purpose." (sop `unmanaged);
344+
reject (sop `unmanaged)
340345
);
341-
342346
let assert_no_net_has_bad_porpoise bads =
343347
(* Sadly we can't use Db.Network.get_refs_where because the expression
344348
* type doesn't allow searching for a value inside a list. *)
345349
Db.Network.get_all ~__context |>
346350
List.iter (fun nwk ->
347351
Db.Network.get_purposes ~__context ~self:nwk |>
348352
List.iter (fun suspect -> if (List.mem suspect bads) then
349-
info "Cannot set new network purpose %s when there is a network with purpose %s" (sop newval) (sop suspect)
353+
info "Cannot set new network purpose %s when there is a network with purpose %s" (sop newval) (sop suspect);
354+
reject (sop suspect)
350355
)
351356
)
352357
in
353358
match newval with
354359
| `himn | `unmanaged -> if current <> [] then (
355-
info "New network purpose %s is not compatible with any other purpose" (sop porpoise)
360+
info "New network purpose %s is not compatible with any other purpose" (sop porpoise);
361+
reject "any"
356362
)
357363
| `management -> if (List.mem `guest current) then (
358364
info "Combining network purposes %s and %s is inadvisable for security reasons, but not prevented." (sop `management) (sop `guest)

0 commit comments

Comments
 (0)