public inbox for pdm-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Lukas Wagner <l.wagner@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [PATCH datacenter-manager v3 10/21] context: establish PdmApplication object
Date: Thu, 27 Aug 2026 13:42:33 +0200	[thread overview]
Message-ID: <20260827114244.424784-11-l.wagner@proxmox.com> (raw)
In-Reply-To: <20260827114244.424784-1-l.wagner@proxmox.com>

PdmApplication bundles the client factory, remote config, subscription
key config, and product config behind a single cloneable handle. This
replaces the growing set of independent global statics with one object
that can be assembled differently for production, the fake-remote
feature, and integration tests.

It is reachable through a new context::pdm_application() accessor, built
by the same context::init() call that already seeds the existing
remote/subscription config globals. Later commits in this series inject
it through the API handlers via the router's shared state instead of
this accessor.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 server/src/context/mod.rs | 102 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 98 insertions(+), 4 deletions(-)

diff --git a/server/src/context/mod.rs b/server/src/context/mod.rs
index 77c84f98..3e89bceb 100644
--- a/server/src/context/mod.rs
+++ b/server/src/context/mod.rs
@@ -2,9 +2,12 @@
 //!
 //! Make sure to call `init` *once* when starting up the API server.
 
-use std::sync::Arc;
+use std::sync::{Arc, OnceLock};
 
 use anyhow::Error;
+
+use proxmox_product_config::{ProductConfig, ProductConfigParams};
+
 use pdm_config::{remotes::RemoteConfig, subscriptions::SubscriptionKeyConfig};
 
 use crate::connection::{self, ClientFactory};
@@ -15,16 +18,36 @@ mod faked_remotes;
 #[cfg(not(remote_config = "faked"))]
 mod default;
 
+static APP: OnceLock<PdmApplication> = OnceLock::new();
+
 /// Dependency-inject concrete implementations needed at runtime.
-pub fn init() -> Result<(), Error> {
+pub fn init() -> Result<PdmApplication, Error> {
     let factory = context_factory()?;
 
-    pdm_config::subscriptions::init(factory.make_subscription_key_config()?);
+    let app = factory.make_pdm_application()?;
+    APP.set(app.clone())
+        .map_err(|_| anyhow::format_err!("context::init was already called"))?;
+
+    // NOTE: This is technically a second, independent instance of the remote config.
+    // Long-term, we'd like to get rid of the global instance handle in pdm_config::remotes
+    // anyway, and the implementation is stateless, so having this second
+    // instance is not an issue *currently*.
     pdm_config::remotes::init(factory.make_remote_config()?);
+    pdm_config::subscriptions::init(factory.make_subscription_key_config()?);
+
     // FIXME: Rather let connection use an Application context object from here
     connection::init(factory.make_client_factory()?);
 
-    Ok(())
+    Ok(app)
+}
+
+/// Retrieve a handle to [`PdmApplication`] for this server.
+///
+/// Prefer to retrieve this via the API handler using [`proxmox_router::State`].
+pub fn pdm_application() -> PdmApplication {
+    APP.get()
+        .expect("context::init was not called to set up the application context object")
+        .clone()
 }
 
 pub trait ContextFactory {
@@ -43,6 +66,30 @@ pub trait ContextFactory {
             pdm_config::subscriptions::DefaultSubscriptionKeyConfig,
         ))
     }
+
+    fn make_product_config(&self) -> Result<ProductConfig, Error> {
+        let product_config = ProductConfig::new(ProductConfigParams {
+            api_user: pdm_config::api_user()?,
+            priv_user: pdm_config::priv_user()?,
+            config_dir: pdm_buildcfg::configdir!("/").into(),
+            state_dir: pdm_buildcfg::statedir!("/").into(),
+            run_dir: pdm_buildcfg::rundir!("/").into(),
+            cache_dir: pdm_buildcfg::PDM_CACHE_DIR.into(),
+        });
+
+        Ok(product_config)
+    }
+
+    fn make_pdm_application(&self) -> Result<PdmApplication, Error> {
+        Ok(PdmApplication {
+            inner: Arc::new(PdmApplicationInner {
+                client_factory: self.make_client_factory()?,
+                remote_config: self.make_remote_config()?,
+                subscription_key_config: self.make_subscription_key_config()?,
+                product_config: self.make_product_config()?,
+            }),
+        })
+    }
 }
 
 fn context_factory() -> Result<impl ContextFactory, Error> {
@@ -55,3 +102,50 @@ fn context_factory() -> Result<impl ContextFactory, Error> {
         Ok(default::DefaultContextFactory)
     }
 }
+
+/// Application context handle.
+///
+/// This type gives access to dependency-injected implementations and general product
+/// configuration.
+///
+/// This implements [`Clone`] and can be cheaply copied (it contains a single `Arc`).
+#[derive(Clone)]
+pub struct PdmApplication {
+    inner: Arc<PdmApplicationInner>,
+}
+
+impl PdmApplication {
+    /// Get a handle to the [`ClientFactory`] as an [`Arc`].
+    ///
+    /// Prefer to use [`Self::client_factory`] if possible.
+    pub fn client_factory_shared(&self) -> Arc<dyn ClientFactory + Send + Sync> {
+        Arc::clone(&self.inner.client_factory)
+    }
+
+    /// Get a reference to the [`ClientFactory`].
+    pub fn client_factory(&self) -> &(dyn ClientFactory + Send + Sync) {
+        self.inner.client_factory.as_ref()
+    }
+
+    /// Get a reference to the [`RemoteConfig`].
+    pub fn remote_config(&self) -> &(dyn RemoteConfig + Send + Sync) {
+        self.inner.remote_config.as_ref()
+    }
+
+    /// Get a reference to the [`SubscriptionKeyConfig`].
+    pub fn subscription_key_config(&self) -> &(dyn SubscriptionKeyConfig + Send + Sync) {
+        self.inner.subscription_key_config.as_ref()
+    }
+
+    /// Get a reference to the [`ProductConfig`].
+    pub fn product_config(&self) -> &ProductConfig {
+        &self.inner.product_config
+    }
+}
+
+struct PdmApplicationInner {
+    client_factory: Arc<dyn ClientFactory + Send + Sync>,
+    remote_config: Box<dyn RemoteConfig + Send + Sync>,
+    subscription_key_config: Box<dyn SubscriptionKeyConfig + Send + Sync>,
+    product_config: ProductConfig,
+}
-- 
2.47.3





  parent reply	other threads:[~2026-08-27 11:43 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-27 11:42 [PATCH datacenter-manager/proxmox v3 00/21] inject application context via API macro for easier integration testing Lukas Wagner
2026-08-27 11:42 ` [PATCH proxmox v3 01/21] router: introduce shared state Lukas Wagner
2026-08-27 11:42 ` [PATCH proxmox v3 02/21] rest-server: allow to inject " Lukas Wagner
2026-08-27 11:42 ` [PATCH proxmox v3 03/21] api-macro: support shared state extraction type Lukas Wagner
2026-08-27 11:42 ` [PATCH proxmox v3 04/21] product-config: add ProductConfig type Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 05/21] context: promote context to a dir-style module Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 06/21] pdm-config: remotes: rename trait methods to read/write/lock Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 07/21] pdm-config: subscriptions: " Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 08/21] remote iterator: pass remote config reader explicitly Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 09/21] context: introduce a ContextFactory to build application context Lukas Wagner
2026-08-27 11:42 ` Lukas Wagner [this message]
2026-08-27 11:42 ` [PATCH datacenter-manager v3 11/21] context: register PdmApplication in router Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 12/21] connection: use client factory from PdmApplication handle Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 13/21] parallel fetcher: pass arguments to closure in a single type Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 14/21] server: migrate existing ParallelFetcher users to use PdmApplication Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 15/21] tests: add helpers for building API-handler-level integration tests Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 16/21] tests: add example tests for SDN API routes Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 17/21] api-cache: add wrapper type Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 18/21] context: provide api-cache on the app object Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 19/21] api: subscriptions: use PdmApplication instead of globals Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 20/21] pdm-config: subscriptions: drop unused accessor functions Lukas Wagner
2026-08-27 11:42 ` [PATCH datacenter-manager v3 21/21] tests: add example tests for remote subscription management Lukas Wagner

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=20260827114244.424784-11-l.wagner@proxmox.com \
    --to=l.wagner@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