feat(metering): the entry plan bills no target as extra and leaves demo targets out
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Agent integration (pull_request) Successful in 5m15s
CI / Check (pull_request) Successful in 23m9s
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Agent integration (pull_request) Successful in 5m15s
CI / Check (pull_request) Successful in 23m9s
Sharang's answers to the entry plan's open questions: - A plan with `extra_targets: 0` bills every active target as included, also those already beyond its included ones when a workspace moves to it: no extra-target line, no counted change scans on them. Only new targets are refused. A swap on such a plan is refused as unlimited. - The demo seeder marks its targets (`demo: true`); the cap neither counts nor refuses them. Demo targets seeded before this count as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q9inRcu428A2iNjDHPEAT9
This commit is contained in:
1 parent
7ef6387d39
commit
53da41ae94
7 files changed
+126
-12
No files matched your search
@@ -173,8 +173,9 @@ impl ComplianceAgent {
|
||||
}
|
||||
|
||||
/// Store which of the tenant's targets are the plan's included ones and
|
||||
/// which are extra (t72), from the plan's `included_targets`. Without a
|
||||
/// registry every target is included. Best-effort: when the plan cannot
|
||||
/// which are extra (t72), from the plan's `included_targets`; a plan that
|
||||
/// allows no extra target includes them all. Without a registry every
|
||||
/// target is included. Best-effort: when the plan cannot
|
||||
/// be read or the targets not updated, the stored split stays.
|
||||
///
|
||||
/// # Returns
|
||||
@@ -187,7 +188,7 @@ impl ComplianceAgent {
|
||||
) -> Vec<BillingChange> {
|
||||
match self.plan(tenant_id).await {
|
||||
Some(plan) => {
|
||||
self.assign_billing(tenant_id, db, plan.included_targets)
|
||||
self.assign_billing(tenant_id, db, plan.billed_included())
|
||||
.await
|
||||
}
|
||||
None => Vec::new(),
|
||||
@@ -360,7 +361,7 @@ impl ComplianceAgent {
|
||||
// counts, and the plan's limits whether a counted one may run (t72).
|
||||
let plan = self.plan(tenant_id).await;
|
||||
if let Some(plan) = &plan {
|
||||
self.assign_billing(tenant_id, &db, plan.included_targets)
|
||||
self.assign_billing(tenant_id, &db, plan.billed_included())
|
||||
.await;
|
||||
}
|
||||
let orchestrator =
|
||||
|
||||
@@ -1759,7 +1759,7 @@ pub async fn swap_target_billing(
|
||||
&db,
|
||||
incoming,
|
||||
outgoing,
|
||||
plan.included_targets.is_some(),
|
||||
plan.billed_included().is_some(),
|
||||
chrono::Utc::now(),
|
||||
)
|
||||
.await
|
||||
|
||||
@@ -166,6 +166,7 @@ async fn seed_one(
|
||||
let mut target =
|
||||
compliance_core::models::OnboardedTarget::new(full_name.to_string(), demo.target_type);
|
||||
target.description = demo.description.clone();
|
||||
target.demo = true;
|
||||
for a in demo.artifacts.iter().filter(|a| !a.is_upload()) {
|
||||
match a.resolved_source_ref() {
|
||||
Some(source_ref) => target.artifacts.push(targets::reference_artifact(
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
//!
|
||||
//! **Included and extra targets.** The oldest active targets are included,
|
||||
//! up to the plan's `included_targets`; every other active target is extra.
|
||||
//! A plan that allows no extra target (the entry plan) includes them all.
|
||||
//! The split is stored on each target ([`OnboardedTarget::billing`]) and
|
||||
//! brought up to date before each scan and when a target is created, so
|
||||
//! archiving an included target moves the oldest extra one up.
|
||||
@@ -192,6 +193,16 @@ impl Plan {
|
||||
pub fn target_cap(&self) -> Option<u32> {
|
||||
Some(self.included_targets?.saturating_add(self.extra_targets?))
|
||||
}
|
||||
|
||||
/// How many targets the billing split includes; `None` includes every
|
||||
/// active target. A plan that allows no extra target bills none as
|
||||
/// extra, not even those already active beyond its included ones.
|
||||
pub fn billed_included(&self) -> Option<u32> {
|
||||
match self.extra_targets {
|
||||
Some(0) => None,
|
||||
_ => self.included_targets,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The tenant's counted Prüfungen and booked Prüftage per month.
|
||||
@@ -472,6 +483,19 @@ mod tests {
|
||||
assert_eq!(plan(Some(u32::MAX), Some(1)).target_cap(), Some(u32::MAX));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_plan_allowing_no_extra_target_bills_none_as_extra() {
|
||||
let plan = |included_targets, extra_targets| Plan {
|
||||
included_targets,
|
||||
extra_targets,
|
||||
limits: PlanLimits::default(),
|
||||
};
|
||||
assert_eq!(plan(Some(2), Some(0)).billed_included(), None, "entry");
|
||||
assert_eq!(plan(Some(5), None).billed_included(), Some(5));
|
||||
assert_eq!(plan(Some(5), Some(2)).billed_included(), Some(5));
|
||||
assert_eq!(plan(None, None).billed_included(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_swap_holds_until_the_berlin_month_ends() {
|
||||
let at = |y, m, d, h| {
|
||||
|
||||
@@ -24,7 +24,8 @@ use crate::error::AgentError;
|
||||
/// # Errors
|
||||
///
|
||||
/// [`AgentError::TargetLimit`] when the plan has no room for another active
|
||||
/// target (the entry plan's `extra_targets: 0`); a database error.
|
||||
/// target (the entry plan's `extra_targets: 0`); demo targets are outside
|
||||
/// that count. A database error.
|
||||
pub async fn create_target(
|
||||
agent: &ComplianceAgent,
|
||||
db: &Database,
|
||||
@@ -34,10 +35,11 @@ pub async fn create_target(
|
||||
// The plan is read once for the cap and the billing. Without an answer
|
||||
// from the registry the target is added, as a scan then runs unmetered.
|
||||
let plan = agent.plan(tenant_id).await;
|
||||
if let Some(cap) = plan.and_then(|p| p.target_cap()) {
|
||||
// Demo targets are outside the cap: neither refused nor counted.
|
||||
if let Some(cap) = plan.and_then(|p| p.target_cap()).filter(|_| !target.demo) {
|
||||
let active = db
|
||||
.onboarded_targets()
|
||||
.count_documents(doc! { "archived_at": null })
|
||||
.count_documents(doc! { "archived_at": null, "demo": { "$ne": true } })
|
||||
.await?;
|
||||
if active >= u64::from(cap) {
|
||||
return Err(AgentError::TargetLimit(cap));
|
||||
@@ -48,7 +50,7 @@ pub async fn create_target(
|
||||
// A target beyond the plan's included ones is extra from the start (t72).
|
||||
if let Some(plan) = plan {
|
||||
if let Some((_, billing)) = agent
|
||||
.assign_billing(tenant_id, db, plan.included_targets)
|
||||
.assign_billing(tenant_id, db, plan.billed_included())
|
||||
.await
|
||||
.into_iter()
|
||||
.find(|(id, _)| Some(*id) == target.id)
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
//! The entry plan's target cap against a fake tenant registry: with
|
||||
//! `extra_targets: 0` a new target is refused (409 `target_limit`) once the
|
||||
//! included ones are all active, archiving one frees its place, and a plan
|
||||
//! without the key keeps adding extra targets as before. Needs a live Mongo
|
||||
//! for the agent itself; skips cleanly without one.
|
||||
//! included ones are all active, archiving one frees its place, targets
|
||||
//! already beyond it are never billed as extra, demo targets are outside the
|
||||
//! cap, and a plan without the key keeps adding extra targets as before.
|
||||
//! Needs a live Mongo for the agent itself; skips cleanly without one.
|
||||
|
||||
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
||||
|
||||
@@ -22,6 +23,7 @@ use tower::ServiceExt;
|
||||
use compliance_agent::agent::ComplianceAgent;
|
||||
use compliance_agent::api::handlers::onboarding::create_target;
|
||||
use compliance_agent::database::DatabasePool;
|
||||
use compliance_core::models::{OnboardedTarget, TargetBilling, TargetType};
|
||||
use compliance_core::{OrgRole, TenantContext, TenantStatus};
|
||||
|
||||
use common::dev_config;
|
||||
@@ -169,3 +171,82 @@ async fn a_plan_without_the_key_adds_extra_targets_as_before() {
|
||||
assert_eq!(status, StatusCode::OK);
|
||||
assert_eq!(body["data"]["billing"], "extra");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn targets_already_beyond_the_entry_plan_are_never_billed_extra() {
|
||||
let registry = fake_registry().await;
|
||||
let Some(agent) = agent(®istry).await else {
|
||||
return;
|
||||
};
|
||||
// Arrange: three active targets from before the plan, one billed extra.
|
||||
let db = agent.db_pool.for_tenant_id("t-entry").await.unwrap();
|
||||
for (name, billing) in [
|
||||
("Anlage 1", TargetBilling::Included),
|
||||
("Anlage 2", TargetBilling::Included),
|
||||
("Anlage 3", TargetBilling::Extra),
|
||||
] {
|
||||
let mut target = OnboardedTarget::new(name.into(), TargetType::WebApp);
|
||||
target.billing = billing;
|
||||
db.onboarded_targets().insert_one(&target).await.unwrap();
|
||||
}
|
||||
|
||||
// Act
|
||||
agent.refresh_billing("t-entry", &db).await;
|
||||
|
||||
// Assert: all three stay, none is extra, and a fourth is refused.
|
||||
let extra = mongodb::bson::to_bson(&TargetBilling::Extra).unwrap();
|
||||
assert_eq!(
|
||||
db.onboarded_targets()
|
||||
.count_documents(doc! { "billing": extra })
|
||||
.await
|
||||
.unwrap(),
|
||||
0
|
||||
);
|
||||
assert_eq!(
|
||||
db.onboarded_targets()
|
||||
.count_documents(doc! {})
|
||||
.await
|
||||
.unwrap(),
|
||||
3
|
||||
);
|
||||
assert_eq!(
|
||||
add(&agent, "t-entry", "Anlage 4").await.0,
|
||||
StatusCode::CONFLICT
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn demo_targets_are_neither_counted_nor_refused() {
|
||||
let registry = fake_registry().await;
|
||||
let Some(agent) = agent(®istry).await else {
|
||||
return;
|
||||
};
|
||||
let db = agent.db_pool.for_tenant_id("t-entry").await.unwrap();
|
||||
let demo = |name: &str| {
|
||||
let mut target = OnboardedTarget::new(name.into(), TargetType::WebApp);
|
||||
target.demo = true;
|
||||
target
|
||||
};
|
||||
|
||||
// A demo target leaves both included places free.
|
||||
compliance_agent::targets::create_target(&agent, &db, "t-entry", demo("Demo A"))
|
||||
.await
|
||||
.expect("demo target added");
|
||||
assert_eq!(
|
||||
add(&agent, "t-entry", "Steuerung A").await.0,
|
||||
StatusCode::OK
|
||||
);
|
||||
assert_eq!(
|
||||
add(&agent, "t-entry", "Steuerung B").await.0,
|
||||
StatusCode::OK
|
||||
);
|
||||
assert_eq!(
|
||||
add(&agent, "t-entry", "Steuerung C").await.0,
|
||||
StatusCode::CONFLICT
|
||||
);
|
||||
|
||||
// With the places taken, the seeder still adds its targets.
|
||||
compliance_agent::targets::create_target(&agent, &db, "t-entry", demo("Demo B"))
|
||||
.await
|
||||
.expect("demo target added at the cap");
|
||||
}
|
||||
@@ -708,6 +708,10 @@ pub struct OnboardedTarget {
|
||||
/// order number and firmware against Siemens' CSAF advisories.
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub devices: Vec<super::PlcDevice>,
|
||||
/// Whether the demo seeder added the target: a plan's cap on active
|
||||
/// targets neither counts nor refuses it. Not settable through the API.
|
||||
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||
pub demo: bool,
|
||||
/// Creation timestamp.
|
||||
#[serde(
|
||||
default = "chrono::Utc::now",
|
||||
@@ -771,6 +775,7 @@ impl OnboardedTarget {
|
||||
included_until: None,
|
||||
supplier: None,
|
||||
devices: Vec::new(),
|
||||
demo: false,
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user