From eea9bcea38e0c5649f444c829f3a4f9c22aa486c Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Wed, 28 Sep 2022 09:38:03 +0100 Subject: [PATCH] api-client: Refactor to removed hard-coded prefix The existing API client only allows access to "VM" operations, so added a new `simple_api_full_command_with_fds()` that allows access to "VMM" operations too. Also added a `simple_api_full_command()` to avoid having to specify the file descriptors, in a similar manner to `simple_api_command()`. Fixes: #4701. Signed-off-by: James O. D. Hunt --- api_client/src/lib.rs | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/api_client/src/lib.rs b/api_client/src/lib.rs index 8583def4e..ff85aad33 100644 --- a/api_client/src/lib.rs +++ b/api_client/src/lib.rs @@ -141,18 +141,20 @@ fn parse_http_response(socket: &mut dyn Read) -> Result, Error> { } } -pub fn simple_api_command_with_fds( +/// Make an API request using the fully qualified command name. +/// For example, full_command could be "vm.create" or "vmm.ping". +pub fn simple_api_full_command_with_fds( socket: &mut T, method: &str, - c: &str, + full_command: &str, request_body: Option<&str>, request_fds: Vec, ) -> Result<(), Error> { socket .send_with_fds( &[format!( - "{} /api/v1/vm.{} HTTP/1.1\r\nHost: localhost\r\nAccept: */*\r\n", - method, c + "{} /api/v1/{} HTTP/1.1\r\nHost: localhost\r\nAccept: */*\r\n", + method, full_command ) .as_bytes()], &request_fds, @@ -181,6 +183,29 @@ pub fn simple_api_command_with_fds( Ok(()) } +pub fn simple_api_full_command( + socket: &mut T, + method: &str, + full_command: &str, + request_body: Option<&str>, +) -> Result<(), Error> { + simple_api_full_command_with_fds(socket, method, full_command, request_body, Vec::new()) +} + +pub fn simple_api_command_with_fds( + socket: &mut T, + method: &str, + c: &str, + request_body: Option<&str>, + request_fds: Vec, +) -> Result<(), Error> { + // Create the full VM command. For VMM commands, use + // simple_api_full_command(). + let full_command = format!("vm.{}", c); + + simple_api_full_command_with_fds(socket, method, &full_command, request_body, request_fds) +} + pub fn simple_api_command( socket: &mut T, method: &str,