Skip to content

Commit

Permalink
python impl
Browse files Browse the repository at this point in the history
Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
  • Loading branch information
rissson committed Dec 1, 2024
1 parent 9adb548 commit 5d5de15
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 8 deletions.
19 changes: 15 additions & 4 deletions python-kadmin-rs/python/kadmin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
from kadmin._lib import KAdmin, Policy, Principal, Params, DbArgs, __version__
from kadmin._lib import (
DbArgs,
KAdmin,
Params,
Policy,
Principal,
TlData,
TlDataEntry,
__version__,
)

__all__ = (
"__version__",
"DbArgs",
"KAdmin",
"Params",
"Policy",
"Principal",
"Params",
"DbArgs",
"TlData",
"TlDataEntry",
"__version__",
)
10 changes: 10 additions & 0 deletions python-kadmin-rs/python/kadmin/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Policy:
attributes: int
max_life: datetime.timedelta | None
max_renewable_life: datetime.timedelta | None
tl_data: TlData

def modify(self, **kwargs) -> Policy: ...
def delete(self) -> None: ...
Expand All @@ -71,3 +72,12 @@ class Params: ...

@final
class DbArgs: ...

@final
class TlDataEntry:
data_type: int
contents: list[int]

@final
class TlData:
entries: list[TlDataEntry]
19 changes: 15 additions & 4 deletions python-kadmin-rs/python/kadmin_local/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
from kadmin_local._lib import KAdmin, Policy, Principal, Params, DbArgs, __version__
from kadmin_local._lib import (
DbArgs,
KAdmin,
Params,
Policy,
Principal,
TlData,
TlDataEntry,
__version__,
)

__all__ = (
"__version__",
"DbArgs",
"KAdmin",
"Params",
"Policy",
"Principal",
"Params",
"DbArgs",
"TlData",
"TlDataEntry",
"__version__",
)
10 changes: 10 additions & 0 deletions python-kadmin-rs/python/kadmin_local/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Policy:
attributes: int
max_life: datetime.timedelta | None
max_renewable_life: datetime.timedelta | None
tl_data: TlData

def modify(self, **kwargs) -> Policy: ...
def delete(self) -> None: ...
Expand All @@ -50,3 +51,12 @@ class Params: ...

@final
class DbArgs: ...

@final
class TlDataEntry:
data_type: int
contents: list[int]

@final
class TlData:
entries: list[TlDataEntry]
91 changes: 91 additions & 0 deletions python-kadmin-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub mod pykadmin {
policy::Policy as KPolicy,
principal::Principal as KPrincipal,
sync::{KAdmin as KKAdmin, KAdminBuilder},
tl_data::{TlData as KTlData, TlDataEntry as KTlDataEntry},
};
use pyo3::{
prelude::*,
Expand Down Expand Up @@ -161,6 +162,67 @@ pub mod pykadmin {
}
}

/// A single TL-data entry
#[pyclass]
#[derive(Clone, Debug, Default)]
pub struct TlDataEntry {

Check failure on line 168 in python-kadmin-rs/src/lib.rs

View workflow job for this annotation

GitHub Actions / lint (clippy)

exported structs should not be exhaustive
/// TL-data type
///
/// :type: int
#[pyo3(get, set)]
pub data_type: i16,
/// Entry contents
///
/// :type: list[int]
#[pyo3(get, set)]
pub contents: Vec<u8>,
}

impl From<KTlDataEntry> for TlDataEntry {
fn from(item: KTlDataEntry) -> Self {
Self {
data_type: item.data_type,
contents: item.contents,
}
}
}

impl Into<KTlDataEntry> for TlDataEntry {

Check failure on line 190 in python-kadmin-rs/src/lib.rs

View workflow job for this annotation

GitHub Actions / lint (clippy)

an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true
fn into(self) -> KTlDataEntry {
KTlDataEntry {
data_type: self.data_type,
contents: self.contents,
}
}
}

/// TL-data entries
#[pyclass]
#[derive(Clone, Debug, Default)]
pub struct TlData {

Check failure on line 202 in python-kadmin-rs/src/lib.rs

View workflow job for this annotation

GitHub Actions / lint (clippy)

exported structs should not be exhaustive
/// TL-data entries
///
/// :type: list[TlDataEntry]
#[pyo3(get, set)]
pub entries: Vec<TlDataEntry>,
}

impl From<KTlData> for TlData {
fn from(item: KTlData) -> Self {
Self {
entries: item.entries.into_iter().map(|e| e.into()).collect(),
}
}
}

impl Into<KTlData> for TlData {

Check failure on line 218 in python-kadmin-rs/src/lib.rs

View workflow job for this annotation

GitHub Actions / lint (clippy)

an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true
fn into(self) -> KTlData {
KTlData {
entries: self.entries.into_iter().map(|e| e.into()).collect(),
}
}
}

/// Interface to kadm5
///
/// This class has no constructor. Instead, use the `with_` methods
Expand Down Expand Up @@ -296,6 +358,9 @@ pub mod pykadmin {
if let Some(max_renewable_life) = kwargs.get_item("max_renewable_life")? {
builder = builder.max_renewable_life(max_renewable_life.extract()?);
}
if let Some(tl_data) = kwargs.get_item("tl_data")? {
builder = builder.tl_data(tl_data.extract::<TlData>()?.into());
}
}
Ok(Policy {
inner: builder
Expand Down Expand Up @@ -554,6 +619,9 @@ pub mod pykadmin {
if let Some(max_renewable_life) = kwargs.get_item("max_renewable_life")? {
modifier = modifier.max_renewable_life(max_renewable_life.extract()?);
}
if let Some(tl_data) = kwargs.get_item("tl_data")? {
modifier = modifier.tl_data(tl_data.extract::<TlData>()?.into());
}
Ok(Self {
inner: modifier
.modify(self.kadmin.deref())
Expand Down Expand Up @@ -859,6 +927,29 @@ pub mod pykadmin {
let _ = std::mem::replace(&mut self.inner, policy);
Ok(())
}

/// Policy TL-data
///
/// :getter: Get the TL-data
/// :setter: Set the TL-data. Completely overrides existing TL-data, make sure to re-use
/// the old ones
/// :type: TlData
#[getter]
pub fn get_tl_data(&self) -> TlData {
(*self.inner.tl_data()).clone().into()
}

/// Ignored
#[setter]
pub fn set_tl_data(&mut self, tl_data: TlData) -> Result<()> {
let policy = self
.inner
.modifier()
.tl_data(tl_data.into())
.modify(self.kadmin.deref())?;
let _ = std::mem::replace(&mut self.inner, policy);
Ok(())
}
}

/// python-kadmin-rs exceptions
Expand Down

0 comments on commit 5d5de15

Please sign in to comment.