MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / save_settings_record

Function save_settings_record

crates/openshell-server/src/grpc/policy.rs:3768–3812  ·  view source on GitHub ↗
(
    store: &Store,
    object_type: &str,
    name: &str,
    settings: &StoredSettings,
)

Source from the content-addressed store, hash-verified

3766}
3767
3768async fn save_settings_record(
3769 store: &Store,
3770 object_type: &str,
3771 name: &str,
3772 settings: &StoredSettings,
3773) -> Result<(), Status> {
3774 use crate::persistence::WriteCondition;
3775
3776 let payload = serde_json::to_vec(settings)
3777 .map_err(|e| Status::internal(format!("encode settings payload failed: {e}")))?;
3778
3779 let (id, condition) = if settings.resource_version == 0 {
3780 // Create new settings (resource_version 0 means never persisted)
3781 (uuid::Uuid::new_v4().to_string(), WriteCondition::MustCreate)
3782 } else {
3783 // Update existing with CAS on the version from when it was loaded
3784 // Fetch the record to get the stable ID
3785 let existing = store
3786 .get_by_name(object_type, name)
3787 .await
3788 .map_err(|e| Status::internal(format!("fetch settings for CAS failed: {e}")))?
3789 .ok_or_else(|| Status::not_found("settings disappeared since load"))?;
3790
3791 (
3792 existing.id,
3793 WriteCondition::MatchResourceVersion(settings.resource_version),
3794 )
3795 };
3796
3797 // Single-attempt CAS write
3798 store
3799 .put_if(object_type, &id, name, &payload, None, condition)
3800 .await
3801 .map_err(|e| match e {
3802 crate::persistence::PersistenceError::Conflict { .. } => {
3803 Status::aborted("settings were modified concurrently; please retry")
3804 }
3805 crate::persistence::PersistenceError::UniqueViolation { .. } => {
3806 Status::aborted("settings were created concurrently; please retry")
3807 }
3808 other => super::persistence_error_to_status(other, "persist settings"),
3809 })?;
3810
3811 Ok(())
3812}
3813
3814fn decode_policy_from_global_settings(
3815 global: &StoredSettings,

Callers 2

save_global_settingsFunction · 0.85
save_sandbox_settingsFunction · 0.85

Calls 3

get_by_nameMethod · 0.45
put_ifMethod · 0.45

Tested by

no test coverage detected