ic_backup/policy/ic_snapshot_upload/
mod.rs1#[cfg(test)]
4mod tests;
5
6use super::response_identity::{ResponseIdentity, ResponseIdentityMismatch};
7use crate::model::{
8 attempt_journal::AttemptJournalRecord,
9 ic_mutation::IcMutationAcknowledgement,
10 ic_snapshot_upload::{
11 IcSnapshotUploadAttempt, IcSnapshotUploadAttemptError, IcSnapshotUploadError,
12 IcSnapshotUploadReply,
13 },
14};
15use thiserror::Error;
16
17#[derive(Debug)]
19pub struct IcSnapshotUploadAcknowledgementView<'request, 'source> {
20 acknowledgement: &'request IcMutationAcknowledgement,
21 reply: IcSnapshotUploadReply<'request, 'source>,
22}
23impl<'request, 'source> IcSnapshotUploadAcknowledgementView<'request, 'source> {
24 #[must_use]
26 pub const fn acknowledgement(&self) -> &'request IcMutationAcknowledgement {
27 self.acknowledgement
28 }
29 #[must_use]
31 pub const fn reply(&self) -> &IcSnapshotUploadReply<'request, 'source> {
32 &self.reply
33 }
34}
35
36pub fn validate_acknowledgement<'request, 'source>(
47 request: &IcSnapshotUploadAttempt<'request, 'source>,
48 journal: &AttemptJournalRecord,
49 acknowledgement: &'request IcMutationAcknowledgement,
50) -> Result<IcSnapshotUploadAcknowledgementView<'request, 'source>, IcSnapshotUploadAssociationError>
51{
52 request.validate_journal(journal)?;
53 let input = acknowledgement.input();
54 ResponseIdentity::from(input)
55 .compare(&ResponseIdentity {
56 authority: &request.authority().digest(),
57 mutation_attempt: request.mutation_attempt(),
58 context: request.plan().context(),
59 target: request.payload().target(),
60 })
61 .map_err(|mismatch| match mismatch {
62 ResponseIdentityMismatch::Authority => {
63 IcSnapshotUploadAssociationError::AuthorityMismatch
64 }
65 ResponseIdentityMismatch::Attempt => IcSnapshotUploadAssociationError::AttemptMismatch,
66 ResponseIdentityMismatch::Context => IcSnapshotUploadAssociationError::ContextMismatch,
67 ResponseIdentityMismatch::Target => IcSnapshotUploadAssociationError::TargetMismatch,
68 })?;
69 let reply = IcSnapshotUploadReply::decode(request.payload(), &input.reply)?;
70 Ok(IcSnapshotUploadAcknowledgementView {
71 acknowledgement,
72 reply,
73 })
74}
75
76#[derive(Debug, Error)]
78pub enum IcSnapshotUploadAssociationError {
79 #[error(transparent)]
81 Reservation(#[from] IcSnapshotUploadAttemptError),
82 #[error("snapshot upload acknowledgement authority mismatch")]
84 AuthorityMismatch,
85 #[error("snapshot upload acknowledgement attempt mismatch")]
87 AttemptMismatch,
88 #[error("snapshot upload acknowledgement context mismatch")]
90 ContextMismatch,
91 #[error("snapshot upload acknowledgement target mismatch")]
93 TargetMismatch,
94 #[error(transparent)]
96 Reply(#[from] IcSnapshotUploadError),
97}