|
| 1 | +// Copyright (c) 2023 - 2026 Restate Software, Inc., Restate GmbH. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// Use of this software is governed by the Business Source License |
| 5 | +// included in the LICENSE file. |
| 6 | +// |
| 7 | +// As of the Change Date specified in that file, in accordance with |
| 8 | +// the Business Source License, use of this software will be governed |
| 9 | +// by the Apache License, Version 2.0. |
| 10 | + |
| 11 | +use serde::{Deserialize, Serialize}; |
| 12 | +use serde_with::serde_as; |
| 13 | + |
| 14 | +use restate_limiter::{PersistedRule, Precondition, RulePattern, UserLimits}; |
| 15 | +use restate_types::Version; |
| 16 | +use restate_util_string::ReString; |
| 17 | + |
| 18 | +/// One entry in the body of `PUT /limits/rules`. |
| 19 | +/// |
| 20 | +/// Each entry carries a fully-specified rule body plus an optional |
| 21 | +/// [`Precondition`]. Omitting the `precondition` field defaults to |
| 22 | +/// `Precondition::None` (unconditional upsert). |
| 23 | +#[serde_as] |
| 24 | +#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))] |
| 25 | +#[derive(Debug, Deserialize)] |
| 26 | +pub struct UpsertRuleRequest { |
| 27 | + /// The pattern that selects which scope/limit-key combinations the |
| 28 | + /// rule applies to. Examples: `"*"`, `"scope1/*"`, `"scope1/foo/bar"`. |
| 29 | + #[cfg_attr(feature = "schema", schema(value_type = String))] |
| 30 | + #[serde_as(as = "serde_with::DisplayFromStr")] |
| 31 | + pub pattern: RulePattern<ReString>, |
| 32 | + #[serde(default)] |
| 33 | + pub limits: UserLimits, |
| 34 | + /// Free-form description shown in the rule book; not consulted at |
| 35 | + /// runtime. |
| 36 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 37 | + pub reason: Option<String>, |
| 38 | + /// Soft-tombstone toggle. `true` parks the rule (the runtime treats |
| 39 | + /// it as absent) without removing it. |
| 40 | + #[serde(default)] |
| 41 | + pub disabled: bool, |
| 42 | + /// Optimistic-concurrency guard. `{ "type": "matches", "version": v }` |
| 43 | + /// requires the rule's current version to be `v`; |
| 44 | + /// `{ "type": "does_not_exist" }` requires the rule to be absent |
| 45 | + /// (strict insert); `{ "type": "none" }` (or omitted) is |
| 46 | + /// unconditional. |
| 47 | + #[serde(default)] |
| 48 | + pub precondition: Precondition, |
| 49 | +} |
| 50 | + |
| 51 | +/// One entry in the body of `POST /limits/rules/bulk-delete`. |
| 52 | +#[serde_as] |
| 53 | +#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))] |
| 54 | +#[derive(Debug, Deserialize)] |
| 55 | +pub struct DeleteRuleRequest { |
| 56 | + #[cfg_attr(feature = "schema", schema(value_type = String))] |
| 57 | + #[serde_as(as = "serde_with::DisplayFromStr")] |
| 58 | + pub pattern: RulePattern<ReString>, |
| 59 | + /// Optimistic-concurrency match. Absent → unconditional delete |
| 60 | + /// (idempotent: deleting an already-absent rule succeeds as a |
| 61 | + /// no-op). Present → reject unless the rule's current version is |
| 62 | + /// the supplied value. |
| 63 | + #[cfg_attr(feature = "schema", schema(value_type = Option<u32>))] |
| 64 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 65 | + pub expected_version: Option<Version>, |
| 66 | +} |
| 67 | + |
| 68 | +#[serde_as] |
| 69 | +#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))] |
| 70 | +#[derive(Debug, Serialize)] |
| 71 | +pub struct RuleResponse { |
| 72 | + #[cfg_attr(feature = "schema", schema(value_type = String))] |
| 73 | + #[serde_as(as = "serde_with::DisplayFromStr")] |
| 74 | + pub pattern: RulePattern<ReString>, |
| 75 | + pub limits: UserLimits, |
| 76 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 77 | + pub reason: Option<String>, |
| 78 | + pub disabled: bool, |
| 79 | + /// Per-rule version: bumped on runtime-relevant changes. |
| 80 | + #[cfg_attr(feature = "schema", schema(value_type = u32))] |
| 81 | + pub version: Version, |
| 82 | + /// Seconds since UNIX epoch. |
| 83 | + pub last_modified_seconds_since_epoch: u64, |
| 84 | +} |
| 85 | + |
| 86 | +impl From<(RulePattern<ReString>, &PersistedRule)> for RuleResponse { |
| 87 | + fn from((pattern, rule): (RulePattern<ReString>, &PersistedRule)) -> Self { |
| 88 | + RuleResponse { |
| 89 | + pattern, |
| 90 | + limits: rule.limits.clone(), |
| 91 | + reason: rule.reason.clone(), |
| 92 | + disabled: rule.disabled, |
| 93 | + version: rule.version, |
| 94 | + last_modified_seconds_since_epoch: rule.last_modified.as_unix_seconds(), |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments