use super::network::{NetworkPolicySpec, NetworkProfileSpec, NetworkSegmentSpec, NetworkSpec};
use crate::Sensitive;
use crate::management::models::PermissionEffect;
#[derive(Debug, Clone, Default)]
pub struct ManagementManifest {
pub resources: Vec<ResourceSpec>,
pub permissions: Vec<PermissionSpec>,
pub roles: Vec<RoleSpec>,
pub groups: Vec<GroupSpec>,
pub users: Vec<UserSpec>,
pub service_accounts: Vec<ServiceAccountSpec>,
pub network: NetworkSpec,
}
impl ManagementManifest {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_resource(mut self, resource: ResourceSpec) -> Self {
self.resources.push(resource);
self
}
#[must_use]
pub fn with_permission(mut self, permission: PermissionSpec) -> Self {
self.permissions.push(permission);
self
}
#[must_use]
pub fn with_role(mut self, role: RoleSpec) -> Self {
self.roles.push(role);
self
}
#[must_use]
pub fn with_group(mut self, group: GroupSpec) -> Self {
self.groups.push(group);
self
}
#[must_use]
pub fn with_user(mut self, user: UserSpec) -> Self {
self.users.push(user);
self
}
#[must_use]
pub fn with_service_account(mut self, account: ServiceAccountSpec) -> Self {
self.service_accounts.push(account);
self
}
#[must_use]
pub fn with_network_policy(mut self, policy: NetworkPolicySpec) -> Self {
self.network.policy = Some(policy);
self
}
#[must_use]
pub fn with_network_segment(mut self, segment: NetworkSegmentSpec) -> Self {
self.network.segments.push(segment);
self
}
#[must_use]
pub fn with_network_profile(mut self, profile: NetworkProfileSpec) -> Self {
self.network.profiles.push(profile);
self
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ResourceSpec {
pub key: String,
pub name: String,
pub resource_type: String,
pub parent: Option<String>,
pub scopes: Vec<ScopeSpec>,
pub metadata: Option<serde_json::Value>,
}
impl ResourceSpec {
pub fn new(
key: impl Into<String>,
name: impl Into<String>,
resource_type: impl Into<String>,
) -> Self {
Self {
key: key.into(),
name: name.into(),
resource_type: resource_type.into(),
parent: None,
scopes: Vec::new(),
metadata: None,
}
}
#[must_use]
pub fn with_metadata(mut self, metadata: serde_json::Value) -> Self {
self.metadata = Some(metadata);
self
}
#[must_use]
pub fn under(mut self, parent: impl Into<String>) -> Self {
self.parent = Some(parent.into());
self
}
#[must_use]
pub fn with_scope(mut self, scope: ScopeSpec) -> Self {
self.scopes.push(scope);
self
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ScopeSpec {
pub key: String,
pub name: String,
pub description: String,
}
impl ScopeSpec {
pub fn new(
key: impl Into<String>,
name: impl Into<String>,
description: impl Into<String>,
) -> Self {
Self {
key: key.into(),
name: name.into(),
description: description.into(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct PermissionSpec {
pub key: String,
pub action: String,
pub description: String,
}
impl PermissionSpec {
pub fn new(
key: impl Into<String>,
action: impl Into<String>,
description: impl Into<String>,
) -> Self {
Self {
key: key.into(),
action: action.into(),
description: description.into(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct RoleSpec {
pub key: String,
pub name: String,
pub description: String,
pub is_global: bool,
pub grants: Vec<GrantSpec>,
}
impl RoleSpec {
pub fn new(
key: impl Into<String>,
name: impl Into<String>,
description: impl Into<String>,
) -> Self {
Self {
key: key.into(),
name: name.into(),
description: description.into(),
is_global: false,
grants: Vec::new(),
}
}
#[must_use]
pub fn global(mut self) -> Self {
self.is_global = true;
self
}
#[must_use]
pub fn granting(mut self, grant: GrantSpec) -> Self {
self.grants.push(grant);
self
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct GrantSpec {
pub permission: String,
pub effect: Option<PermissionEffect>,
pub scopes: Vec<String>,
}
impl GrantSpec {
pub fn allow(permission: impl Into<String>) -> Self {
Self {
permission: permission.into(),
effect: Some(PermissionEffect::Allow),
scopes: Vec::new(),
}
}
pub fn deny(permission: impl Into<String>) -> Self {
Self {
permission: permission.into(),
effect: Some(PermissionEffect::Deny),
scopes: Vec::new(),
}
}
#[must_use]
pub fn scoped_to(mut self, scopes: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.scopes = scopes.into_iter().map(Into::into).collect();
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum RoleBinding {
Role(String),
Scoped {
role: String,
resource: String,
inherit: bool,
},
}
impl RoleBinding {
pub fn at(role: impl Into<String>, resource: impl Into<String>) -> Self {
RoleBinding::Scoped {
role: role.into(),
resource: resource.into(),
inherit: true,
}
}
pub fn at_only(role: impl Into<String>, resource: impl Into<String>) -> Self {
RoleBinding::Scoped {
role: role.into(),
resource: resource.into(),
inherit: false,
}
}
#[must_use]
pub fn role(&self) -> &str {
match self {
RoleBinding::Role(role) | RoleBinding::Scoped { role, .. } => role,
}
}
#[must_use]
pub fn resource(&self) -> Option<&str> {
match self {
RoleBinding::Role(_) => None,
RoleBinding::Scoped { resource, .. } => Some(resource),
}
}
#[must_use]
pub fn inherit(&self) -> bool {
match self {
RoleBinding::Role(_) => true,
RoleBinding::Scoped { inherit, .. } => *inherit,
}
}
}
impl PartialEq<str> for RoleBinding {
fn eq(&self, other: &str) -> bool {
matches!(self, RoleBinding::Role(role) if role == other)
}
}
impl PartialEq<&str> for RoleBinding {
fn eq(&self, other: &&str) -> bool {
self == *other
}
}
impl From<&str> for RoleBinding {
fn from(role: &str) -> Self {
RoleBinding::Role(role.to_string())
}
}
impl From<String> for RoleBinding {
fn from(role: String) -> Self {
RoleBinding::Role(role)
}
}
impl From<&String> for RoleBinding {
fn from(role: &String) -> Self {
RoleBinding::Role(role.clone())
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct GroupSpec {
pub key: String,
pub name: String,
pub description: String,
pub roles: Vec<RoleBinding>,
}
impl GroupSpec {
pub fn new(
key: impl Into<String>,
name: impl Into<String>,
description: impl Into<String>,
) -> Self {
Self {
key: key.into(),
name: name.into(),
description: description.into(),
roles: Vec::new(),
}
}
#[must_use]
pub fn with_roles(mut self, roles: impl IntoIterator<Item = impl Into<RoleBinding>>) -> Self {
self.roles = roles.into_iter().map(Into::into).collect();
self
}
}
#[derive(Debug, Clone)]
pub struct UserSpec {
pub key: String,
pub username: String,
pub email: String,
pub initial_password: Option<Sensitive<String>>,
pub roles: Vec<RoleBinding>,
pub groups: Vec<String>,
}
impl UserSpec {
pub fn new(
key: impl Into<String>,
username: impl Into<String>,
email: impl Into<String>,
) -> Self {
Self {
key: key.into(),
username: username.into(),
email: email.into(),
initial_password: None,
roles: Vec::new(),
groups: Vec::new(),
}
}
#[must_use]
pub fn with_initial_password(mut self, password: Sensitive<String>) -> Self {
self.initial_password = Some(password);
self
}
#[must_use]
pub fn with_roles(mut self, roles: impl IntoIterator<Item = impl Into<RoleBinding>>) -> Self {
self.roles = roles.into_iter().map(Into::into).collect();
self
}
#[must_use]
pub fn in_groups(mut self, groups: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.groups = groups.into_iter().map(Into::into).collect();
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ServiceAccountSpec {
pub key: String,
pub name: String,
pub description: Option<String>,
pub roles: Vec<RoleBinding>,
}
impl ServiceAccountSpec {
pub fn new(key: impl Into<String>, name: impl Into<String>) -> Self {
Self {
key: key.into(),
name: name.into(),
description: None,
roles: Vec::new(),
}
}
#[must_use]
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
#[must_use]
pub fn with_roles(mut self, roles: impl IntoIterator<Item = impl Into<RoleBinding>>) -> Self {
self.roles = roles.into_iter().map(Into::into).collect();
self
}
}