public inbox for pdm-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [pdm-devel] [PATCH datacenter-manager 3/9] server: connection: add new function that allows for explicit endpoint
Date: Mon, 13 Jan 2025 16:45:44 +0100	[thread overview]
Message-ID: <20250113154550.3462139-6-d.csapak@proxmox.com> (raw)
In-Reply-To: <20250113154550.3462139-1-d.csapak@proxmox.com>

sometimes it's necessary to connect to a specific endpoint that is
configured, instead of letting it automatically choose, so add the
function for that.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 server/src/connection.rs | 61 ++++++++++++++++++++++++++++++++--------
 1 file changed, 49 insertions(+), 12 deletions(-)

diff --git a/server/src/connection.rs b/server/src/connection.rs
index 8c97b4c..0adeba2 100644
--- a/server/src/connection.rs
+++ b/server/src/connection.rs
@@ -26,11 +26,21 @@ struct ConnectInfo {
 
 /// Returns a [`proxmox_client::Client`] and a token prefix for the specified
 /// [`pdm_api_types::Remote`]
-fn prepare_connect_client(remote: &Remote) -> Result<ConnectInfo, Error> {
+fn prepare_connect_client(
+    remote: &Remote,
+    target_endpoint: Option<&str>,
+) -> Result<ConnectInfo, Error> {
     let node = remote
         .nodes
-        .first()
-        .ok_or_else(|| format_err!("no nodes configured for remote"))?;
+        .iter()
+        .find(|endpoint| match target_endpoint {
+            Some(target) => target == endpoint.hostname,
+            None => true,
+        })
+        .ok_or_else(|| match target_endpoint {
+            Some(endpoint) => format_err!("{endpoint} not configured for remote"),
+            None => format_err!("no nodes configured for remote"),
+        })?;
     let mut options = TlsOptions::default();
 
     if let Some(fp) = &node.fingerprint {
@@ -66,12 +76,12 @@ fn prepare_connect_client(remote: &Remote) -> Result<ConnectInfo, Error> {
 ///
 /// It does not actually opens a connection there, but prepares the client with the correct
 /// authentication information and settings for the [`RemoteType`]
-fn connect(remote: &Remote) -> Result<Client, anyhow::Error> {
+fn connect(remote: &Remote, target_endpoint: Option<&str>) -> Result<Client, anyhow::Error> {
     let ConnectInfo {
         client,
         perl_compat,
         prefix,
-    } = prepare_connect_client(remote)?;
+    } = prepare_connect_client(remote, target_endpoint)?;
     client.set_authentication(proxmox_client::Token {
         userid: remote.authid.to_string(),
         prefix,
@@ -91,11 +101,14 @@ fn connect(remote: &Remote) -> Result<Client, anyhow::Error> {
 /// This is intended for API calls that accept a user in addition to tokens.
 ///
 /// Note: currently does not support two factor authentication.
-async fn connect_or_login(remote: &Remote) -> Result<Client, anyhow::Error> {
+async fn connect_or_login(
+    remote: &Remote,
+    target_endpoint: Option<&str>,
+) -> Result<Client, anyhow::Error> {
     if remote.authid.is_token() {
-        connect(remote)
+        connect(remote, target_endpoint)
     } else {
-        let info = prepare_connect_client(remote)?;
+        let info = prepare_connect_client(remote, target_endpoint)?;
         let client = info.client;
         match client
             .login(proxmox_login::Login::new(
@@ -131,6 +144,13 @@ pub trait ClientFactory {
     /// Create a new API client for PBS remotes
     fn make_pbs_client(&self, remote: &Remote) -> Result<Box<PbsClient>, Error>;
 
+    /// Create a new API client for PVE remotes, but with a specific endpoint.
+    fn make_pve_client_with_endpoint(
+        &self,
+        remote: &Remote,
+        target_endpoint: Option<&str>,
+    ) -> Result<Box<dyn PveClient + Send + Sync>, Error>;
+
     /// Create a new API client for PVE remotes.
     ///
     /// In case the remote has a user configured (instead of an API token), it will connect and get
@@ -163,25 +183,34 @@ pub struct DefaultClientFactory;
 #[async_trait::async_trait]
 impl ClientFactory for DefaultClientFactory {
     fn make_pve_client(&self, remote: &Remote) -> Result<Box<dyn PveClient + Send + Sync>, Error> {
-        let client = crate::connection::connect(remote)?;
+        let client = crate::connection::connect(remote, None)?;
         Ok(Box::new(PveClientImpl(client)))
     }
 
     fn make_pbs_client(&self, remote: &Remote) -> Result<Box<PbsClient>, Error> {
-        let client = crate::connection::connect(remote)?;
+        let client = crate::connection::connect(remote, None)?;
         Ok(Box::new(PbsClient(client)))
     }
 
+    fn make_pve_client_with_endpoint(
+        &self,
+        remote: &Remote,
+        target_endpoint: Option<&str>,
+    ) -> Result<Box<dyn PveClient + Send + Sync>, Error> {
+        let client = crate::connection::connect(remote, target_endpoint)?;
+        Ok(Box::new(PveClientImpl(client)))
+    }
+
     async fn make_pve_client_and_login(
         &self,
         remote: &Remote,
     ) -> Result<Box<dyn PveClient + Send + Sync>, Error> {
-        let client = connect_or_login(remote).await?;
+        let client = connect_or_login(remote, None).await?;
         Ok(Box::new(PveClientImpl(client)))
     }
 
     async fn make_pbs_client_and_login(&self, remote: &Remote) -> Result<Box<PbsClient>, Error> {
-        let client = connect_or_login(remote).await?;
+        let client = connect_or_login(remote, None).await?;
         Ok(Box::new(PbsClient(client)))
     }
 }
@@ -201,6 +230,14 @@ pub fn make_pve_client(remote: &Remote) -> Result<Box<dyn PveClient + Send + Syn
     instance().make_pve_client(remote)
 }
 
+/// Create a new API client for PVE remotes, but for a specific endpoint
+pub fn make_pve_client_with_endpoint(
+    remote: &Remote,
+    target_endpoint: Option<&str>,
+) -> Result<Box<dyn PveClient + Send + Sync>, Error> {
+    instance().make_pve_client_with_endpoint(remote, target_endpoint)
+}
+
 /// Create a new API client for PBS remotes
 pub fn make_pbs_client(remote: &Remote) -> Result<Box<PbsClient>, Error> {
     instance().make_pbs_client(remote)
-- 
2.39.5



_______________________________________________
pdm-devel mailing list
pdm-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel


  parent reply	other threads:[~2025-01-13 15:46 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-13 15:45 [pdm-devel] [PATCH proxmox-api-types/datacenter-manager] remote migration: make target endpoint selectable Dominik Csapak
2025-01-13 15:45 ` [pdm-devel] [PATCH proxmox-api-types 1/2] add more network interface methods Dominik Csapak
2025-01-13 15:45 ` [pdm-devel] [PATCH proxmox-api-types 2/2] add cluster status api call Dominik Csapak
2025-01-13 15:45 ` [pdm-devel] [PATCH datacenter-manager 1/9] server: factor qemu/lxc code into own modules Dominik Csapak
2025-01-13 15:45 ` [pdm-devel] [PATCH datacenter-manager 2/9] server: api: fix remote upid tracking for qemu remote migration Dominik Csapak
2025-01-13 15:45 ` Dominik Csapak [this message]
2025-01-13 15:45 ` [pdm-devel] [PATCH datacenter-manager 4/9] server: api: add target-endpoint parameter to remote migrate api calls Dominik Csapak
2025-01-13 15:45 ` [pdm-devel] [PATCH datacenter-manager 5/9] server: api: pve: add remote cluster-status api call Dominik Csapak
2025-01-13 15:45 ` [pdm-devel] [PATCH datacenter-manager 6/9] pdm-client: add cluster status method Dominik Csapak
2025-01-13 15:45 ` [pdm-devel] [PATCH datacenter-manager 7/9] pdm-client: add target-endpoint parameter to remote migration methods Dominik Csapak
2025-01-13 15:45 ` [pdm-devel] [PATCH datacenter-manager 8/9] ui: widget: add remote endpoint selector Dominik Csapak
2025-01-13 15:45 ` [pdm-devel] [PATCH datacenter-manager 9/9] ui: migrate: make target endpoint selectable for remote migration Dominik Csapak
2025-01-14  9:35 ` [pdm-devel] [PATCH proxmox-api-types/datacenter-manager] remote migration: make target endpoint selectable Dietmar Maurer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250113154550.3462139-6-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pdm-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal