all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH perl-rs 2/5] perl-rs: use api functions from proxmox-apt
Date: Tue,  9 Jul 2024 08:20:23 +0200	[thread overview]
Message-ID: <20240709062026.53271-2-w.bumiller@proxmox.com> (raw)
In-Reply-To: <20240709062026.53271-1-w.bumiller@proxmox.com>

From: Dietmar Maurer <dietmar@proxmox.com>

Signed-off-by: Dietmar Maurer <dietmar@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



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


  reply	other threads:[~2024-07-09  6:20 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-09  6:20 [pve-devel] applied-series: [PATCH perl-rs 1/5] perl-rs: use proxmox-apt-api-types Wolfgang Bumiller
2024-07-09  6:20 ` Wolfgang Bumiller [this message]
2024-07-09  6:20 ` [pve-devel] [PATCH perl-rs 3/5] perl-rs: add further apt api calls Wolfgang Bumiller
2024-07-09  6:20 ` [pve-devel] [PATCH perl-rs 4/5] pve-rs: common: send apt update notification via proxmox-notify Wolfgang Bumiller
2024-07-09  6:20 ` [pve-devel] [PATCH perl-rs 5/5] apt: minor parameter cleanup Wolfgang Bumiller

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=20240709062026.53271-2-w.bumiller@proxmox.com \
    --to=w.bumiller@proxmox.com \
    --cc=pve-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal