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 {
55 authority: &input.authority,
56 mutation_attempt: input.mutation_attempt,
57 context: &input.context,
58 target: &input.target,
59 }
60 .compare(&ResponseIdentity {
61 authority: &request.authority().digest(),
62 mutation_attempt: request.mutation_attempt(),
63 context: request.plan().context(),
64 target: request.payload().target(),
65 })
66 .map_err(|mismatch| match mismatch {
67 ResponseIdentityMismatch::Authority => IcSnapshotUploadAssociationError::AuthorityMismatch,
68 ResponseIdentityMismatch::Attempt => IcSnapshotUploadAssociationError::AttemptMismatch,
69 ResponseIdentityMismatch::Context => IcSnapshotUploadAssociationError::ContextMismatch,
70 ResponseIdentityMismatch::Target => IcSnapshotUploadAssociationError::TargetMismatch,
71 })?;
72 let reply = IcSnapshotUploadReply::decode(request.payload(), &input.reply)?;
73 Ok(IcSnapshotUploadAcknowledgementView {
74 acknowledgement,
75 reply,
76 })
77}
78
79#[derive(Debug, Error)]
81pub enum IcSnapshotUploadAssociationError {
82 #[error(transparent)]
84 Reservation(#[from] IcSnapshotUploadAttemptError),
85 #[error("snapshot upload acknowledgement authority mismatch")]
87 AuthorityMismatch,
88 #[error("snapshot upload acknowledgement attempt mismatch")]
90 AttemptMismatch,
91 #[error("snapshot upload acknowledgement context mismatch")]
93 ContextMismatch,
94 #[error("snapshot upload acknowledgement target mismatch")]
96 TargetMismatch,
97 #[error(transparent)]
99 Reply(#[from] IcSnapshotUploadError),
100}