[pve-devel] [PATCH perl-rs 2/5] perl-rs: use api functions from proxmox-apt

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Jul 9 08:20:23 CEST 2024


From: Dietmar Maurer <dietmar at proxmox.com>

Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 common/src/apt/repositories.rs | 117 +++------------------------------
 pmg-rs/src/apt/repositories.rs |  11 +++-
 pve-rs/src/apt/repositories.rs |  13 ++--
 3 files changed, 26 insertions(+), 115 deletions(-)

diff --git a/common/src/apt/repositories.rs b/common/src/apt/repositories.rs
index 3b05449..61824d7 100644
--- a/common/src/apt/repositories.rs
+++ b/common/src/apt/repositories.rs
@@ -1,39 +1,17 @@
 #[perlmod::package(name = "Proxmox::RS::APT::Repositories")]
 pub mod export {
 
-    use anyhow::{bail, Error};
-    use serde::{Deserialize, Serialize};
+    use anyhow::Error;
 
-    use proxmox_apt::repositories::{APTRepositoryFileImpl, APTRepositoryImpl};
-    use proxmox_apt_api_types::{APTRepositoriesResult, APTRepositoryFile, APTRepositoryHandle};
+    use proxmox_apt_api_types::{
+        APTChangeRepositoryOptions, APTRepositoriesResult, APTRepositoryHandle,
+    };
     use proxmox_config_digest::ConfigDigest;
 
-    #[derive(Deserialize, Serialize)]
-    #[serde(rename_all = "kebab-case")]
-    /// For changing an existing repository.
-    pub struct ChangeProperties {
-        /// Whether the repository should be enabled or not.
-        pub enabled: Option<bool>,
-    }
-
     /// Get information about configured repositories and standard repositories for `product`.
     #[export]
     pub fn repositories(product: &str) -> Result<APTRepositoriesResult, Error> {
-        let (files, errors, digest) = proxmox_apt::repositories::repositories()?;
-
-        let suite = proxmox_apt::repositories::get_current_release_codename()?;
-
-        let infos = proxmox_apt::repositories::check_repositories(&files, suite);
-        let standard_repos =
-            proxmox_apt::repositories::standard_repositories(&files, product, suite);
-
-        Ok(APTRepositoriesResult {
-            files,
-            errors,
-            digest,
-            infos,
-            standard_repos,
-        })
+        proxmox_apt::list_repositories(product)
     }
 
     /// Add the repository identified by the `handle` and `product`.
@@ -42,63 +20,11 @@ pub mod export {
     /// The `digest` parameter asserts that the configuration has not been modified.
     #[export]
     pub fn add_repository(
-        handle: &str,
+        handle: APTRepositoryHandle,
         product: &str,
         digest: Option<ConfigDigest>,
     ) -> Result<(), Error> {
-        let (mut files, errors, current_digest) = proxmox_apt::repositories::repositories()?;
-
-        let handle: APTRepositoryHandle = handle.parse()?;
-        let suite = proxmox_apt::repositories::get_current_release_codename()?;
-
-        current_digest.detect_modification(digest.as_ref())?;
-
-        // check if it's already configured first
-        for file in files.iter_mut() {
-            for repo in file.repositories.iter_mut() {
-                if repo.is_referenced_repository(handle, product, &suite.to_string()) {
-                    if repo.enabled {
-                        return Ok(());
-                    }
-
-                    repo.set_enabled(true);
-                    file.write()?;
-
-                    return Ok(());
-                }
-            }
-        }
-
-        let (repo, path) =
-            proxmox_apt::repositories::get_standard_repository(handle, product, suite);
-
-        if let Some(error) = errors.iter().find(|error| error.path == path) {
-            bail!(
-                "unable to parse existing file {} - {}",
-                error.path,
-                error.error,
-            );
-        }
-
-        if let Some(file) = files
-            .iter_mut()
-            .find(|file| file.path.as_ref() == Some(&path))
-        {
-            file.repositories.push(repo);
-
-            file.write()?;
-        } else {
-            let mut file = match APTRepositoryFile::new(&path)? {
-                Some(file) => file,
-                None => bail!("invalid path - {}", path),
-            };
-
-            file.repositories.push(repo);
-
-            file.write()?;
-        }
-
-        Ok(())
+        proxmox_apt::add_repository_handle(product, handle, digest)
     }
 
     /// Change the properties of the specified repository.
@@ -108,34 +34,9 @@ pub mod export {
     pub fn change_repository(
         path: &str,
         index: usize,
-        options: ChangeProperties,
+        options: APTChangeRepositoryOptions,
         digest: Option<ConfigDigest>,
     ) -> Result<(), Error> {
-        let (mut files, errors, current_digest) = proxmox_apt::repositories::repositories()?;
-
-        current_digest.detect_modification(digest.as_ref())?;
-
-        if let Some(error) = errors.iter().find(|error| error.path == path) {
-            bail!("unable to parse file {} - {}", error.path, error.error);
-        }
-
-        if let Some(file) = files
-            .iter_mut()
-            .find(|file| file.path.as_ref() == Some(&path.to_string()))
-        {
-            if let Some(repo) = file.repositories.get_mut(index) {
-                if let Some(enabled) = options.enabled {
-                    repo.set_enabled(enabled);
-                }
-
-                file.write()?;
-            } else {
-                bail!("invalid index - {}", index);
-            }
-        } else {
-            bail!("invalid path - {}", path);
-        }
-
-        Ok(())
+        proxmox_apt::change_repository(path, index, &options, digest)
     }
 }
diff --git a/pmg-rs/src/apt/repositories.rs b/pmg-rs/src/apt/repositories.rs
index 3680d5c..d8e89c2 100644
--- a/pmg-rs/src/apt/repositories.rs
+++ b/pmg-rs/src/apt/repositories.rs
@@ -1,7 +1,9 @@
 #[perlmod::package(name = "PMG::RS::APT::Repositories")]
 mod export {
     use anyhow::Error;
-    use proxmox_apt_api_types::APTRepositoriesResult;
+    use proxmox_apt_api_types::{
+        APTChangeRepositoryOptions, APTRepositoriesResult, APTRepositoryHandle,
+    };
     use proxmox_config_digest::ConfigDigest;
 
     use crate::common::apt::repositories::export as common;
@@ -17,7 +19,10 @@ mod export {
     ///
     /// The `digest` parameter asserts that the configuration has not been modified.
     #[export]
-    pub fn add_repository(handle: &str, digest: Option<ConfigDigest>) -> Result<(), Error> {
+    pub fn add_repository(
+        handle: APTRepositoryHandle,
+        digest: Option<ConfigDigest>,
+    ) -> Result<(), Error> {
         common::add_repository(handle, "pmg", digest)
     }
 
@@ -28,7 +33,7 @@ mod export {
     pub fn change_repository(
         path: &str,
         index: usize,
-        options: common::ChangeProperties,
+        options: APTChangeRepositoryOptions,
         digest: Option<ConfigDigest>,
     ) -> Result<(), Error> {
         common::change_repository(path, index, options, digest)
diff --git a/pve-rs/src/apt/repositories.rs b/pve-rs/src/apt/repositories.rs
index c1867a1..7fbf97c 100644
--- a/pve-rs/src/apt/repositories.rs
+++ b/pve-rs/src/apt/repositories.rs
@@ -2,7 +2,9 @@
 mod export {
     use anyhow::Error;
 
-    use proxmox_apt_api_types::APTRepositoriesResult;
+    use proxmox_apt_api_types::{
+        APTChangeRepositoryOptions, APTRepositoriesResult, APTRepositoryHandle,
+    };
     use proxmox_config_digest::ConfigDigest;
 
     use crate::common::apt::repositories::export as common;
@@ -10,7 +12,7 @@ mod export {
     /// Get information about configured and standard repositories.
     #[export]
     pub fn repositories() -> Result<APTRepositoriesResult, Error> {
-        common::repositories("pve")
+        proxmox_apt::list_repositories("pve")
     }
 
     /// Add the repository identified by the `handle`.
@@ -18,7 +20,10 @@ mod export {
     ///
     /// The `digest` parameter asserts that the configuration has not been modified.
     #[export]
-    pub fn add_repository(handle: &str, digest: Option<ConfigDigest>) -> Result<(), Error> {
+    pub fn add_repository(
+        handle: APTRepositoryHandle,
+        digest: Option<ConfigDigest>,
+    ) -> Result<(), Error> {
         common::add_repository(handle, "pve", digest)
     }
 
@@ -29,7 +34,7 @@ mod export {
     pub fn change_repository(
         path: &str,
         index: usize,
-        options: common::ChangeProperties,
+        options: APTChangeRepositoryOptions,
         digest: Option<ConfigDigest>,
     ) -> Result<(), Error> {
         common::change_repository(path, index, options, digest)
-- 
2.39.2





More information about the pve-devel mailing list