slbotbm commented on code in PR #4616:
URL: https://github.com/apache/iggy/pull/4616#discussion_r4397334905
##########
foreign/python/src/client.rs:
##########
@@ -315,6 +318,59 @@ impl IggyClient {
})
}
+ /// Capture a diagnostic snapshot of the server as a compressed ZIP
archive.
+ ///
+ /// Requires an authenticated session whose user holds the `manage_servers`
+ /// global permission.
+ ///
+ /// Args:
+ /// snapshot_compression: Archive compression method as a
+ /// `SnapshotCompression` enum.
+ /// snapshot_types: Data to include in the snapshot as a list of
+ /// `SystemSnapshotType` enums. `SystemSnapshotType.All` cannot be
+ /// combined with any other snapshot type.
+ ///
+ /// Returns:
+ /// An awaitable that resolves to the ZIP archive `bytes`.
+ ///
+ /// Raises:
+ /// TypeError: If `snapshot_compression` is not a `SnapshotCompression`
+ /// or `snapshot_types` contains elements other than
`SystemSnapshotType`.
+ /// ValueError: If `snapshot_types` is empty.
+ /// RuntimeError: If the client is not connected, the session is not
+ /// authenticated, the user lacks the permission,
`SystemSnapshotType.All` is
+ /// combined with another snapshot type, the 30-second binary
+ /// operation deadline expires, or the request fails.
+
#[gen_stub(override_return_type(type_repr="collections.abc.Awaitable[bytes]",
imports=("collections.abc")))]
+ fn snapshot<'a>(
+ &self,
+ py: Python<'a>,
+ snapshot_compression: PySnapshotCompression,
+ snapshot_types: Vec<PySystemSnapshotType>,
+ ) -> PyResult<Bound<'a, PyAny>> {
+ if snapshot_types.is_empty() {
+ return Err(PyValueError::new_err("snapshot_types must not be
empty"));
+ }
+
+ let mut parsed_types = Vec::with_capacity(snapshot_types.len());
+ for snapshot_type in snapshot_types {
+ let rust_type: SystemSnapshotType = snapshot_type.into();
+ if !parsed_types.contains(&rust_type) {
+ parsed_types.push(rust_type);
+ }
+ }
+
+ let inner = self.inner.clone();
+ let compression: SnapshotCompression = snapshot_compression.into();
Review Comment:
`SnapshotCompression::from(snapshot_compression)`
##########
foreign/python/tests/test_snapshot.py:
##########
@@ -0,0 +1,95 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import io
+import zipfile
+
+import pytest
+
+from apache_iggy import (
+ IggyClient,
+ SnapshotCompression,
+ SystemSnapshotType,
+)
+
+
+class TestSnapshot:
+ """Test server snapshot retrieval."""
+
+ @pytest.mark.asyncio
+ async def test_snapshot_returns_zip_bytes(self, iggy_client: IggyClient):
+ """Capturing a snapshot returns a non-empty ZIP archive as bytes."""
+ data = await iggy_client.snapshot(
+ SnapshotCompression.Deflated, [SystemSnapshotType.All]
+ )
+
+ assert isinstance(data, bytes)
+ assert len(data) > 0
+ assert zipfile.is_zipfile(io.BytesIO(data))
+
+ @pytest.mark.unit
+ @pytest.mark.parametrize("invalid_compression", ["deflated", "", 123,
None])
+ def test_snapshot_rejects_invalid_snapshot_compression(self,
invalid_compression):
+ """Passing non-enum snapshot_compression raises TypeError before
sending."""
+ client = IggyClient("127.0.0.1:1")
+ with pytest.raises(TypeError):
+ # pyrefly: ignore # bad-argument-type
+ client.snapshot(invalid_compression, [SystemSnapshotType.Test])
+
+ @pytest.mark.unit
+ def test_snapshot_rejects_invalid_snapshot_type(self):
+ """An unknown or non-enum snapshot type raises TypeError before
sending."""
+ client = IggyClient("127.0.0.1:1")
+ with pytest.raises(TypeError):
+ # pyrefly: ignore # bad-argument-type
+ client.snapshot(SnapshotCompression.Deflated, ["test"])
+
+ @pytest.mark.unit
+ def test_snapshot_rejects_empty_snapshot_types(self):
+ """An empty snapshot_types list raises ValueError before sending."""
+ client = IggyClient("127.0.0.1:1")
+ with pytest.raises(ValueError):
+ client.snapshot(SnapshotCompression.Deflated, [])
+
+ @pytest.mark.asyncio
+ async def test_snapshot_rejects_all_combined_with_other_type(
+ self, iggy_client: IggyClient
+ ):
+ """Combining 'all' with another snapshot type is rejected."""
+ with pytest.raises(RuntimeError, match="(?i)invalid command"):
+ await iggy_client.snapshot(
+ SnapshotCompression.Deflated,
+ [SystemSnapshotType.All, SystemSnapshotType.Test],
+ )
+
+ @pytest.mark.asyncio
+ async def test_snapshot_deduplicates_snapshot_types(self, iggy_client:
IggyClient):
+ """Duplicate snapshot types are deduplicated before encoding."""
+ data = await iggy_client.snapshot(
+ SnapshotCompression.Deflated, [SystemSnapshotType.Test] * 255
+ )
+
+ assert isinstance(data, bytes)
+ assert len(data) > 0
+ assert zipfile.is_zipfile(io.BytesIO(data))
+
+ @pytest.mark.unit
+ def test_snapshot_enums(self):
+ """Snapshot compression and type enums expose expected variants."""
+ assert SnapshotCompression.Deflated == SnapshotCompression.Deflated
+ assert SnapshotCompression.Deflated != SnapshotCompression.Stored
+ assert SystemSnapshotType.All == SystemSnapshotType.All
Review Comment:
This test is not required
##########
foreign/python/src/snapshot.rs:
##########
@@ -0,0 +1,174 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use iggy::prelude::{
+ SnapshotCompression as RustSnapshotCompression, SystemSnapshotType as
RustSystemSnapshotType,
+};
+use pyo3::prelude::*;
+use pyo3_stub_gen::derive::gen_stub_pyclass_enum;
+
+/// Compression method for server snapshots.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+#[gen_stub_pyclass_enum]
+#[pyclass(eq, from_py_object)]
+pub enum SnapshotCompression {
+ /// Store the file as is without compression.
+ Stored,
+ /// Compress the file using Deflate.
+ Deflated,
+ /// Compress the file using BZIP2.
+ Bzip2,
+ /// Compress the file using ZStandard.
+ Zstd,
+ /// Compress the file using LZMA.
+ Lzma,
+ /// Compress the file using XZ.
+ Xz,
+}
+
+impl From<SnapshotCompression> for RustSnapshotCompression {
+ fn from(compression: SnapshotCompression) -> Self {
+ match compression {
+ SnapshotCompression::Stored => RustSnapshotCompression::Stored,
+ SnapshotCompression::Deflated => RustSnapshotCompression::Deflated,
+ SnapshotCompression::Bzip2 => RustSnapshotCompression::Bzip2,
+ SnapshotCompression::Zstd => RustSnapshotCompression::Zstd,
+ SnapshotCompression::Lzma => RustSnapshotCompression::Lzma,
+ SnapshotCompression::Xz => RustSnapshotCompression::Xz,
+ }
+ }
+}
+
+impl From<RustSnapshotCompression> for SnapshotCompression {
+ fn from(compression: RustSnapshotCompression) -> Self {
+ match compression {
+ RustSnapshotCompression::Stored => SnapshotCompression::Stored,
+ RustSnapshotCompression::Deflated => SnapshotCompression::Deflated,
+ RustSnapshotCompression::Bzip2 => SnapshotCompression::Bzip2,
+ RustSnapshotCompression::Zstd => SnapshotCompression::Zstd,
+ RustSnapshotCompression::Lzma => SnapshotCompression::Lzma,
+ RustSnapshotCompression::Xz => SnapshotCompression::Xz,
+ }
+ }
+}
+
+/// Type of system data to include in a snapshot.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+#[gen_stub_pyclass_enum]
+#[pyclass(eq, from_py_object)]
+pub enum SystemSnapshotType {
+ /// Overview of the filesystem.
+ FilesystemOverview,
+ /// List of currently running processes.
+ ProcessList,
+ /// Resource usage statistics of the system.
+ ResourceUsage,
+ /// Test snapshot type for development purposes.
+ Test,
+ /// Server logs.
+ ServerLogs,
+ /// Server configuration.
+ ServerConfig,
+ /// Everything.
+ All,
+}
+
+impl From<SystemSnapshotType> for RustSystemSnapshotType {
+ fn from(snapshot_type: SystemSnapshotType) -> Self {
+ match snapshot_type {
+ SystemSnapshotType::FilesystemOverview =>
RustSystemSnapshotType::FilesystemOverview,
+ SystemSnapshotType::ProcessList =>
RustSystemSnapshotType::ProcessList,
+ SystemSnapshotType::ResourceUsage =>
RustSystemSnapshotType::ResourceUsage,
+ SystemSnapshotType::Test => RustSystemSnapshotType::Test,
+ SystemSnapshotType::ServerLogs =>
RustSystemSnapshotType::ServerLogs,
+ SystemSnapshotType::ServerConfig =>
RustSystemSnapshotType::ServerConfig,
+ SystemSnapshotType::All => RustSystemSnapshotType::All,
+ }
+ }
+}
+
+impl From<RustSystemSnapshotType> for SystemSnapshotType {
+ fn from(snapshot_type: RustSystemSnapshotType) -> Self {
+ match snapshot_type {
+ RustSystemSnapshotType::FilesystemOverview =>
SystemSnapshotType::FilesystemOverview,
+ RustSystemSnapshotType::ProcessList =>
SystemSnapshotType::ProcessList,
+ RustSystemSnapshotType::ResourceUsage =>
SystemSnapshotType::ResourceUsage,
+ RustSystemSnapshotType::Test => SystemSnapshotType::Test,
+ RustSystemSnapshotType::ServerLogs =>
SystemSnapshotType::ServerLogs,
+ RustSystemSnapshotType::ServerConfig =>
SystemSnapshotType::ServerConfig,
+ RustSystemSnapshotType::All => SystemSnapshotType::All,
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
Review Comment:
these tests are not required
##########
foreign/python/src/client.rs:
##########
@@ -315,6 +318,59 @@ impl IggyClient {
})
}
+ /// Capture a diagnostic snapshot of the server as a compressed ZIP
archive.
+ ///
+ /// Requires an authenticated session whose user holds the `manage_servers`
+ /// global permission.
+ ///
+ /// Args:
+ /// snapshot_compression: Archive compression method as a
+ /// `SnapshotCompression` enum.
+ /// snapshot_types: Data to include in the snapshot as a list of
+ /// `SystemSnapshotType` enums. `SystemSnapshotType.All` cannot be
+ /// combined with any other snapshot type.
+ ///
+ /// Returns:
+ /// An awaitable that resolves to the ZIP archive `bytes`.
+ ///
+ /// Raises:
+ /// TypeError: If `snapshot_compression` is not a `SnapshotCompression`
+ /// or `snapshot_types` contains elements other than
`SystemSnapshotType`.
+ /// ValueError: If `snapshot_types` is empty.
+ /// RuntimeError: If the client is not connected, the session is not
+ /// authenticated, the user lacks the permission,
`SystemSnapshotType.All` is
+ /// combined with another snapshot type, the 30-second binary
+ /// operation deadline expires, or the request fails.
+
#[gen_stub(override_return_type(type_repr="collections.abc.Awaitable[bytes]",
imports=("collections.abc")))]
+ fn snapshot<'a>(
+ &self,
+ py: Python<'a>,
+ snapshot_compression: PySnapshotCompression,
+ snapshot_types: Vec<PySystemSnapshotType>,
+ ) -> PyResult<Bound<'a, PyAny>> {
+ if snapshot_types.is_empty() {
+ return Err(PyValueError::new_err("snapshot_types must not be
empty"));
+ }
+
+ let mut parsed_types = Vec::with_capacity(snapshot_types.len());
+ for snapshot_type in snapshot_types {
+ let rust_type: SystemSnapshotType = snapshot_type.into();
Review Comment:
Let's make this `SystemSnapshotType::from(snapshot_type)`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]