Skip to content

Commit 340a00c

Browse files
committed
added role/From impl for trino roles plus parsing test
1 parent a99d59a commit 340a00c

3 files changed

Lines changed: 23 additions & 24 deletions

File tree

rust/operator-binary/src/controller/build/resource/pdb.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cmp::max, str::FromStr};
1+
use std::cmp::max;
22

33
use stackable_operator::{
44
commons::pdb::PdbConfig,
@@ -26,8 +26,7 @@ pub fn build_pdb(
2626
TrinoRole::Coordinator => max_unavailable_coordinators(),
2727
TrinoRole::Worker => max_unavailable_workers(worker_count(cluster)),
2828
});
29-
let role_name =
30-
RoleName::from_str(&role.to_string()).expect("a TrinoRole is a valid RFC 1123 role name");
29+
let role_name: RoleName = role.into();
3130
let pdb = pod_disruption_budget_builder_with_role(
3231
cluster,
3332
&product_name(),

rust/operator-binary/src/controller/mod.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ impl ValidatedCluster {
249249
) -> ResourceNames {
250250
ResourceNames {
251251
cluster_name: self.name.clone(),
252-
role_name: Self::role_name(role),
252+
role_name: role.into(),
253253
role_group_name: role_group_name.clone(),
254254
}
255255
}
@@ -288,10 +288,6 @@ impl ValidatedCluster {
288288
}
289289

290290
/// A [`TrinoRole`] as a type-safe [`RoleName`].
291-
fn role_name(role: &TrinoRole) -> RoleName {
292-
RoleName::from_str(&role.to_string()).expect("a TrinoRole is a valid RFC 1123 role name")
293-
}
294-
295291
fn recommended_labels_with(
296292
&self,
297293
version: &ProductVersion,
@@ -311,7 +307,7 @@ impl ValidatedCluster {
311307

312308
/// Recommended labels for a role-group resource (using the resolved product version).
313309
pub fn recommended_labels(&self, role: &TrinoRole, role_group_name: &RoleGroupName) -> Labels {
314-
self.recommended_labels_for(&Self::role_name(role), role_group_name)
310+
self.recommended_labels_for(&role.into(), role_group_name)
315311
}
316312

317313
/// Recommended labels for a resource that is not tied to a concrete [`TrinoRole`] (e.g. the
@@ -331,21 +327,12 @@ impl ValidatedCluster {
331327
role: &TrinoRole,
332328
role_group_name: &RoleGroupName,
333329
) -> Labels {
334-
self.recommended_labels_with(
335-
&UNVERSIONED_PRODUCT_VERSION,
336-
&Self::role_name(role),
337-
role_group_name,
338-
)
330+
self.recommended_labels_with(&UNVERSIONED_PRODUCT_VERSION, &role.into(), role_group_name)
339331
}
340332

341333
/// Selector labels matching the pods of a role group.
342334
pub fn role_group_selector(&self, role: &TrinoRole, role_group_name: &RoleGroupName) -> Labels {
343-
role_group_selector(
344-
self,
345-
&product_name(),
346-
&Self::role_name(role),
347-
role_group_name,
348-
)
335+
role_group_selector(self, &product_name(), &role.into(), role_group_name)
349336
}
350337
}
351338

@@ -468,17 +455,18 @@ pub(crate) fn validated_cluster() -> ValidatedCluster {
468455

469456
#[cfg(test)]
470457
mod tests {
458+
use stackable_operator::v2::types::operator::RoleName;
471459
use strum::IntoEnumIterator;
472460

473-
use super::ValidatedCluster;
474461
use crate::crd::TrinoRole;
475462

476-
/// Locks the invariant behind the `expect` in [`ValidatedCluster::role_name`]: every
477-
/// `TrinoRole` variant (present and future) must serialise to a valid `RoleName`.
463+
/// Locks the invariant behind the `expect` in the `From<TrinoRole> for RoleName` impls:
464+
/// every `TrinoRole` variant (present and future) must serialise to a valid `RoleName`.
478465
#[test]
479466
fn every_trino_role_serialises_to_a_valid_role_name() {
480467
for role in TrinoRole::iter() {
481-
ValidatedCluster::role_name(&role);
468+
let _: RoleName = (&role).into();
469+
let _: RoleName = role.into();
482470
}
483471
}
484472
}

rust/operator-binary/src/crd/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,18 @@ pub enum TrinoRole {
420420
Worker,
421421
}
422422

423+
impl From<TrinoRole> for RoleName {
424+
fn from(value: TrinoRole) -> Self {
425+
RoleName::from_str(&value.to_string()).expect("a TrinoRole is a valid role name")
426+
}
427+
}
428+
429+
impl From<&TrinoRole> for RoleName {
430+
fn from(value: &TrinoRole) -> Self {
431+
RoleName::from_str(&value.to_string()).expect("a TrinoRole is a valid role name")
432+
}
433+
}
434+
423435
impl TrinoRole {
424436
pub fn listener_class_name(&self, trino: &v1alpha1::TrinoCluster) -> Option<ListenerClassName> {
425437
match self {

0 commit comments

Comments
 (0)