]> git.proxmox.com Git - proxmox-apt.git/blob - src/repositories/standard.rs
bump version to 0.3.0-1
[proxmox-apt.git] / src / repositories / standard.rs
1 use std::convert::TryFrom;
2 use std::fmt::Display;
3
4 use anyhow::{bail, Error};
5 use serde::{Deserialize, Serialize};
6
7 use crate::repositories::repository::{
8 APTRepository, APTRepositoryFileType, APTRepositoryPackageType,
9 };
10
11 use proxmox::api::api;
12
13 #[api(
14 properties: {
15 handle: {
16 description: "Handle referencing a standard repository.",
17 type: String,
18 },
19 },
20 )]
21 #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
22 #[serde(rename_all = "kebab-case")]
23 /// Reference to a standard repository and configuration status.
24 pub struct APTStandardRepository {
25 /// Handle referencing a standard repository.
26 pub handle: APTRepositoryHandle,
27
28 /// Configuration status of the associated repository.
29 #[serde(skip_serializing_if = "Option::is_none")]
30 pub status: Option<bool>,
31
32 /// Full name of the repository.
33 pub name: String,
34 }
35
36 #[api]
37 #[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq)]
38 #[serde(rename_all = "kebab-case")]
39 /// Handles for Proxmox repositories.
40 pub enum APTRepositoryHandle {
41 /// The enterprise repository for production use.
42 Enterprise,
43 /// The repository that can be used without subscription.
44 NoSubscription,
45 /// The test repository.
46 Test,
47 /// Ceph Pacific repository.
48 CephPacific,
49 /// Ceph Pacific test repository.
50 CephPacificTest,
51 /// Ceph Octoput repository.
52 CephOctopus,
53 /// Ceph Octoput test repository.
54 CephOctopusTest,
55 }
56
57 impl TryFrom<&str> for APTRepositoryHandle {
58 type Error = Error;
59
60 fn try_from(string: &str) -> Result<Self, Error> {
61 match string {
62 "enterprise" => Ok(APTRepositoryHandle::Enterprise),
63 "no-subscription" => Ok(APTRepositoryHandle::NoSubscription),
64 "test" => Ok(APTRepositoryHandle::Test),
65 "ceph-pacific" => Ok(APTRepositoryHandle::CephPacific),
66 "ceph-pacific-test" => Ok(APTRepositoryHandle::CephPacificTest),
67 "ceph-octopus" => Ok(APTRepositoryHandle::CephOctopus),
68 "ceph-octopus-test" => Ok(APTRepositoryHandle::CephOctopusTest),
69 _ => bail!("unknown repository handle '{}'", string),
70 }
71 }
72 }
73
74 impl Display for APTRepositoryHandle {
75 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76 match self {
77 APTRepositoryHandle::Enterprise => write!(f, "enterprise"),
78 APTRepositoryHandle::NoSubscription => write!(f, "no-subscription"),
79 APTRepositoryHandle::Test => write!(f, "test"),
80 APTRepositoryHandle::CephPacific => write!(f, "ceph-pacific"),
81 APTRepositoryHandle::CephPacificTest => write!(f, "ceph-pacific-test"),
82 APTRepositoryHandle::CephOctopus => write!(f, "ceph-octopus"),
83 APTRepositoryHandle::CephOctopusTest => write!(f, "ceph-octopus-test"),
84 }
85 }
86 }
87
88 impl APTRepositoryHandle {
89 /// Get the full name of the repository.
90 pub fn name(self) -> String {
91 match self {
92 APTRepositoryHandle::Enterprise => "Enterprise Repository",
93 APTRepositoryHandle::NoSubscription => "No-Subscription Repository",
94 APTRepositoryHandle::Test => "Test Repository",
95 APTRepositoryHandle::CephPacific => "Ceph Pacific Repository",
96 APTRepositoryHandle::CephPacificTest => "Ceph Pacific Test Repository",
97 APTRepositoryHandle::CephOctopus => "Ceph Octopus Repository",
98 APTRepositoryHandle::CephOctopusTest => "Ceph Octopus Test Repository",
99 }
100 .to_string()
101 }
102
103 /// Get the standard file path for the repository referenced by the handle.
104 pub fn path(self, product: &str) -> String {
105 match self {
106 APTRepositoryHandle::Enterprise => {
107 format!("/etc/apt/sources.list.d/{}-enterprise.list", product)
108 }
109 APTRepositoryHandle::NoSubscription => "/etc/apt/sources.list".to_string(),
110 APTRepositoryHandle::Test => "/etc/apt/sources.list".to_string(),
111 APTRepositoryHandle::CephPacific => "/etc/apt/sources.list.d/ceph.list".to_string(),
112 APTRepositoryHandle::CephPacificTest => "/etc/apt/sources.list.d/ceph.list".to_string(),
113 APTRepositoryHandle::CephOctopus => "/etc/apt/sources.list.d/ceph.list".to_string(),
114 APTRepositoryHandle::CephOctopusTest => "/etc/apt/sources.list.d/ceph.list".to_string(),
115 }
116 }
117
118 /// Get package type, URI and the component associated with the handle.
119 pub fn info(self, product: &str) -> (APTRepositoryPackageType, String, String) {
120 match self {
121 APTRepositoryHandle::Enterprise => (
122 APTRepositoryPackageType::Deb,
123 format!("https://enterprise.proxmox.com/debian/{}", product),
124 format!("{}-enterprise", product),
125 ),
126 APTRepositoryHandle::NoSubscription => (
127 APTRepositoryPackageType::Deb,
128 format!("http://download.proxmox.com/debian/{}", product),
129 format!("{}-no-subscription", product),
130 ),
131 APTRepositoryHandle::Test => (
132 APTRepositoryPackageType::Deb,
133 format!("http://download.proxmox.com/debian/{}", product),
134 format!("{}test", product),
135 ),
136 APTRepositoryHandle::CephPacific => (
137 APTRepositoryPackageType::Deb,
138 "http://download.proxmox.com/debian/ceph-pacific".to_string(),
139 "main".to_string(),
140 ),
141 APTRepositoryHandle::CephPacificTest => (
142 APTRepositoryPackageType::Deb,
143 "http://download.proxmox.com/debian/ceph-pacific".to_string(),
144 "test".to_string(),
145 ),
146 APTRepositoryHandle::CephOctopus => (
147 APTRepositoryPackageType::Deb,
148 "http://download.proxmox.com/debian/ceph-octopus".to_string(),
149 "main".to_string(),
150 ),
151 APTRepositoryHandle::CephOctopusTest => (
152 APTRepositoryPackageType::Deb,
153 "http://download.proxmox.com/debian/ceph-octopus".to_string(),
154 "test".to_string(),
155 ),
156 }
157 }
158
159 /// Get the standard repository referenced by the handle.
160 ///
161 /// An URI in the result is not '/'-terminated (under the assumption that no valid
162 /// product name is).
163 pub fn to_repository(self, product: &str, suite: &str) -> APTRepository {
164 let (package_type, uri, component) = self.info(product);
165
166 APTRepository {
167 types: vec![package_type],
168 uris: vec![uri],
169 suites: vec![suite.to_string()],
170 components: vec![component],
171 options: vec![],
172 comment: String::new(),
173 file_type: APTRepositoryFileType::List,
174 enabled: true,
175 }
176 }
177 }