1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use minicbor::{Decode, Encode};
use serde::{Deserialize, Serialize};

use crate::cloud::Controller;
use ockam_core::api::{Error, Reply, Request, Status};
use ockam_core::{self, async_trait, Result};
use ockam_node::Context;

const TARGET: &str = "ockam_api::cloud::subscription";
const API_SERVICE: &str = "subscriptions";

#[derive(Encode, Decode, Debug)]
#[cfg_attr(test, derive(Clone))]
#[rustfmt::skip]
#[cbor(map)]
pub struct ActivateSubscription {
    #[n(1)] pub space_id: Option<String>,
    #[n(2)] pub subscription_data: String,
    #[n(3)] pub space_name: Option<String>,
    #[n(4)] pub owner_emails: Option<Vec<String>>,
}

impl ActivateSubscription {
    /// Activates a subscription for an existing space
    pub fn existing<S: Into<String>>(space_id: S, subscription_data: S) -> Self {
        Self {
            space_id: Some(space_id.into()),
            subscription_data: subscription_data.into(),
            space_name: None,
            owner_emails: None,
        }
    }

    /// Activates a subscription for a space that will be newly created with the given space name
    #[allow(unused)]
    pub fn create<S: Into<String>, T: AsRef<str>>(
        space_name: S,
        owner_emails: &[T],
        subscription_data: S,
    ) -> Self {
        Self {
            space_id: None,
            subscription_data: subscription_data.into(),
            space_name: Some(space_name.into()),
            owner_emails: Some(owner_emails.iter().map(|x| x.as_ref().into()).collect()),
        }
    }
}

#[derive(Encode, Decode, Serialize, Deserialize, Debug)]
#[cfg_attr(test, derive(Clone))]
#[cbor(map)]
pub struct Subscription {
    #[n(1)]
    pub id: String,
    #[n(2)]
    marketplace: String,
    #[n(3)]
    pub status: String,
    #[n(4)]
    pub entitlements: String,
    #[n(5)]
    pub metadata: String,
    #[n(6)]
    pub contact_info: String,
    #[n(7)]
    pub space_id: Option<String>,
}

#[async_trait]
pub trait Subscriptions {
    async fn activate_subscription(
        &self,
        ctx: &Context,
        space_id: String,
        subscription_data: String,
    ) -> Result<Reply<Subscription>>;

    async fn unsubscribe(
        &self,
        ctx: &Context,
        subscription_id: String,
    ) -> Result<Reply<Subscription>>;

    async fn update_subscription_contact_info(
        &self,
        ctx: &Context,
        subscription_id: String,
        contact_info: String,
    ) -> Result<Reply<Subscription>>;

    async fn update_subscription_space(
        &self,
        ctx: &Context,
        subscription_id: String,
        new_space_id: String,
    ) -> Result<Reply<Subscription>>;

    async fn get_subscriptions(&self, ctx: &Context) -> Result<Reply<Vec<Subscription>>>;

    async fn get_subscription(
        &self,
        ctx: &Context,
        subscription_id: String,
    ) -> Result<Reply<Subscription>>;

    async fn get_subscription_by_space_id(
        &self,
        ctx: &Context,
        space_id: String,
    ) -> Result<Reply<Subscription>>;
}

#[async_trait]
impl Subscriptions for Controller {
    async fn activate_subscription(
        &self,
        ctx: &Context,
        space_id: String,
        subscription_data: String,
    ) -> Result<Reply<Subscription>> {
        let req_body = ActivateSubscription::existing(space_id, subscription_data);
        trace!(target: TARGET, space_id = ?req_body.space_id, space_name = ?req_body.space_name, "activating subscription");
        let req = Request::post("/v0/activate").body(req_body);
        self.secure_client.ask(ctx, API_SERVICE, req).await
    }

    async fn unsubscribe(
        &self,
        ctx: &Context,
        subscription_id: String,
    ) -> Result<Reply<Subscription>> {
        trace!(target: TARGET, subscription = %subscription_id, "unsubscribing");
        let req = Request::put(format!("/v0/{subscription_id}/unsubscribe"));
        self.secure_client.ask(ctx, API_SERVICE, req).await
    }

    async fn update_subscription_contact_info(
        &self,
        ctx: &Context,
        subscription_id: String,
        contact_info: String,
    ) -> Result<Reply<Subscription>> {
        trace!(target: TARGET, subscription = %subscription_id, "updating subscription contact info");
        let req = Request::put(format!("/v0/{subscription_id}/contact_info")).body(contact_info);
        self.secure_client.ask(ctx, API_SERVICE, req).await
    }

    async fn update_subscription_space(
        &self,
        ctx: &Context,
        subscription_id: String,
        new_space_id: String,
    ) -> Result<Reply<Subscription>> {
        trace!(target: TARGET, subscription = %subscription_id, new_space_id = %new_space_id, "updating subscription space");
        let req = Request::put(format!("/v0/{subscription_id}/space_id")).body(new_space_id);
        self.secure_client.ask(ctx, API_SERVICE, req).await
    }

    async fn get_subscriptions(&self, ctx: &Context) -> Result<Reply<Vec<Subscription>>> {
        trace!(target: TARGET, "listing subscriptions");
        let req = Request::get("/v0/");
        self.secure_client.ask(ctx, API_SERVICE, req).await
    }

    async fn get_subscription(
        &self,
        ctx: &Context,
        subscription_id: String,
    ) -> Result<Reply<Subscription>> {
        trace!(target: TARGET, subscription = %subscription_id, "getting subscription");
        let req = Request::get(format!("/v0/{subscription_id}"));
        self.secure_client.ask(ctx, API_SERVICE, req).await
    }

    async fn get_subscription_by_space_id(
        &self,
        ctx: &Context,
        space_id: String,
    ) -> Result<Reply<Subscription>> {
        let subscriptions: Vec<Subscription> = self.get_subscriptions(ctx).await?.success()?;
        let subscription = subscriptions
            .into_iter()
            .find(|s| s.space_id == Some(space_id.clone()));
        match subscription {
            Some(subscription) => Ok(Reply::Successful(subscription)),
            None => Ok(Reply::Failed(
                Error::new_without_path(),
                Some(Status::NotFound),
            )),
        }
    }
}

#[cfg(test)]
pub mod tests {
    use quickcheck::{quickcheck, Arbitrary, Gen, TestResult};

    use crate::schema::tests::validate_with_schema;

    use super::*;

    quickcheck! {
        fn subcription(s: Subscription) -> TestResult {
            validate_with_schema("subscription", s)
        }

        fn activate_subcription(s: ActivateSubscription) -> TestResult {
            validate_with_schema("activate_subscription", s)
        }
    }

    impl Arbitrary for Subscription {
        fn arbitrary(g: &mut Gen) -> Self {
            Subscription {
                id: String::arbitrary(g),
                marketplace: String::arbitrary(g),
                status: String::arbitrary(g),
                entitlements: String::arbitrary(g),
                metadata: String::arbitrary(g),
                contact_info: String::arbitrary(g),
                space_id: bool::arbitrary(g).then(|| String::arbitrary(g)),
            }
        }
    }

    impl Arbitrary for ActivateSubscription {
        fn arbitrary(g: &mut Gen) -> Self {
            ActivateSubscription::create(
                String::arbitrary(g),
                &[String::arbitrary(g), String::arbitrary(g)],
                String::arbitrary(g),
            )
        }
    }
}