gcp-lite-rs 0.1.1

Lightweight HTTP client for Google Cloud Platform APIs
Documentation
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Mock HTTP client for testing without hitting real GCP APIs.

use crate::{GcpError, Result};
use serde_json::Value;
use std::sync::{Arc, Mutex};

/// Mock HTTP client for testing
pub struct MockClient {
    expectations: Arc<Mutex<Vec<Expectation>>>,
    call_history: Arc<Mutex<Vec<Call>>>,
}

struct Expectation {
    method: String,
    path_matcher: PathMatcher,
    responses: Vec<Response>,
    response_index: usize,
    times: ExpectedTimes,
    called: usize,
}

enum PathMatcher {
    #[allow(dead_code)]
    Exact(String),
    StartsWith(String),
    #[allow(dead_code)]
    Regex(regex::Regex),
}

#[derive(Clone)]
enum Response {
    Json(Value),
    FixturePath(String),
    Error(GcpError),
}

enum ExpectedTimes {
    Once,
    Times(usize),
    AtLeast(usize),
    #[allow(dead_code)]
    Any,
}

#[derive(Debug)]
#[allow(dead_code)]
struct Call {
    method: String,
    path: String,
    body: Option<Value>,
}

impl Default for MockClient {
    fn default() -> Self {
        Self::new()
    }
}

impl MockClient {
    /// Create a new mock client
    pub fn new() -> Self {
        Self {
            expectations: Arc::new(Mutex::new(Vec::new())),
            call_history: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Expect a GET request
    pub fn expect_get(&mut self, path: &str) -> ExpectationBuilder<'_> {
        ExpectationBuilder::new(self, "GET", path)
    }

    /// Expect a POST request
    pub fn expect_post(&mut self, path: &str) -> ExpectationBuilder<'_> {
        ExpectationBuilder::new(self, "POST", path)
    }

    /// Expect a DELETE request
    pub fn expect_delete(&mut self, path: &str) -> ExpectationBuilder<'_> {
        ExpectationBuilder::new(self, "DELETE", path)
    }

    /// Expect a PUT request
    pub fn expect_put(&mut self, path: &str) -> ExpectationBuilder<'_> {
        ExpectationBuilder::new(self, "PUT", path)
    }

    /// Expect a PATCH request
    pub fn expect_patch(&mut self, path: &str) -> ExpectationBuilder<'_> {
        ExpectationBuilder::new(self, "PATCH", path)
    }

    pub(crate) async fn execute(
        &self,
        method: &str,
        url: &str,
        body: Option<&Value>,
    ) -> Result<Vec<u8>> {
        let path = if url.starts_with("http") {
            // Find the first / after the protocol (e.g. after https://)
            if let Some(pos) = url.find("://") {
                if let Some(slash_pos) = url[pos + 3..].find('/') {
                    &url[pos + 3 + slash_pos..]
                } else {
                    "/"
                }
            } else {
                url
            }
        } else {
            url
        };

        // Record call
        self.call_history.lock().unwrap().push(Call {
            method: method.to_string(),
            path: path.to_string(),
            body: body.cloned(),
        });

        // Find matching expectation
        let mut expectations = self.expectations.lock().unwrap();

        for expectation in expectations.iter_mut() {
            if expectation.method == method && expectation.matches_path(path) {
                expectation.called += 1;

                // Get the response for this call (cycle through responses for sequential calls)
                let response = if expectation.responses.len() == 1 {
                    &expectation.responses[0]
                } else {
                    let idx = expectation
                        .response_index
                        .min(expectation.responses.len() - 1);
                    expectation.response_index += 1;
                    &expectation.responses[idx]
                };

                return match response {
                    Response::Json(value) => Ok(serde_json::to_vec(value).unwrap()),
                    Response::FixturePath(path) => {
                        let fixture = std::fs::read_to_string(path)
                            .unwrap_or_else(|_| panic!("Failed to read fixture file: {}", path));
                        Ok(fixture.into_bytes())
                    }
                    Response::Error(err) => Err(err.clone()),
                };
            }
        }

        panic!(
            "Unexpected call: {} {}\nCall history:\n{:#?}",
            method,
            path,
            self.call_history.lock().unwrap()
        );
    }
}

impl Drop for MockClient {
    fn drop(&mut self) {
        // Verify all expectations were met
        let expectations = self.expectations.lock().unwrap();
        for exp in expectations.iter() {
            match exp.times {
                ExpectedTimes::Once if exp.called != 1 => {
                    panic!(
                        "Expected {} {} to be called once, called {} times",
                        exp.method,
                        exp.path_display(),
                        exp.called
                    );
                }
                ExpectedTimes::Times(n) if exp.called != n => {
                    panic!(
                        "Expected {} {} to be called {} times, called {} times",
                        exp.method,
                        exp.path_display(),
                        n,
                        exp.called
                    );
                }
                ExpectedTimes::AtLeast(n) if exp.called < n => {
                    panic!(
                        "Expected {} {} to be called at least {} times, called {} times",
                        exp.method,
                        exp.path_display(),
                        n,
                        exp.called
                    );
                }
                _ => {}
            }
        }
    }
}

/// Builder for setting up expectations
pub struct ExpectationBuilder<'a> {
    client: &'a mut MockClient,
    method: String,
    path: String,
    responses: Vec<Response>,
    times: ExpectedTimes,
}

impl<'a> ExpectationBuilder<'a> {
    fn new(client: &'a mut MockClient, method: &str, path: &str) -> Self {
        Self {
            client,
            method: method.to_string(),
            path: path.to_string(),
            responses: Vec::new(),
            times: ExpectedTimes::Once,
        }
    }

    /// Set the response to return JSON
    pub fn returning_json(mut self, value: Value) -> Self {
        self.responses.push(Response::Json(value));
        self
    }

    /// Set multiple sequential responses (for polling scenarios)
    pub fn returning_json_sequence(mut self, values: Vec<Value>) -> Self {
        for value in values {
            self.responses.push(Response::Json(value));
        }
        self
    }

    /// Set the response to load from a fixture file
    pub fn returning_fixture(mut self, path: &str) -> Self {
        self.responses.push(Response::FixturePath(path.to_string()));
        self
    }

    /// Set the response to return an error
    pub fn returning_error(mut self, error: GcpError) -> Self {
        self.responses.push(Response::Error(error));
        self
    }

    /// Set the number of times this expectation should be called
    pub fn times(mut self, n: usize) -> Self {
        self.times = ExpectedTimes::Times(n);
        self
    }

    /// Set the minimum number of times this expectation should be called
    pub fn at_least(mut self, n: usize) -> Self {
        self.times = ExpectedTimes::AtLeast(n);
        self
    }

    /// Use exact path matching (including query parameters)
    pub fn with_exact_path(self) -> Self {
        // This will be handled in Drop by checking if we should use Exact matcher
        self
    }
}

impl<'a> Drop for ExpectationBuilder<'a> {
    fn drop(&mut self) {
        if self.responses.is_empty() {
            panic!(
                "No response set for expectation: {} {}",
                self.method, self.path
            );
        }

        let expectation = Expectation {
            method: self.method.clone(),
            path_matcher: PathMatcher::StartsWith(self.path.clone()),
            responses: std::mem::take(&mut self.responses),
            response_index: 0,
            times: std::mem::replace(&mut self.times, ExpectedTimes::Once),
            called: 0,
        };

        self.client.expectations.lock().unwrap().push(expectation);
    }
}

impl Expectation {
    fn matches_path(&self, path: &str) -> bool {
        match &self.path_matcher {
            PathMatcher::Exact(p) => {
                // For exact matching, also handle query params
                // Split both paths at '?' and compare base paths and query params
                let (expected_base, expected_query) = split_path_query(p);
                let (actual_base, actual_query) = split_path_query(path);

                if expected_base != actual_base {
                    return false;
                }

                // If expected has no query params, match any query params
                // If expected has query params, they must match
                if expected_query.is_empty() {
                    true
                } else {
                    expected_query == actual_query
                }
            }
            PathMatcher::StartsWith(p) => path.starts_with(p),
            PathMatcher::Regex(r) => r.is_match(path),
        }
    }

    fn path_display(&self) -> String {
        match &self.path_matcher {
            PathMatcher::Exact(p) => p.clone(),
            PathMatcher::StartsWith(p) => format!("{}*", p),
            PathMatcher::Regex(r) => format!("/{}/", r.as_str()),
        }
    }
}

fn split_path_query(path: &str) -> (&str, &str) {
    if let Some(pos) = path.find('?') {
        (&path[..pos], &path[pos + 1..])
    } else {
        (path, "")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::GcpHttpClient;

    #[tokio::test]
    async fn mock_service_usage_api() {
        let mut mock = MockClient::new();

        mock.expect_get("/v1/projects/test/services/compute.googleapis.com")
            .returning_json(serde_json::json!({
                "name": "projects/test/services/compute.googleapis.com",
                "state": "ENABLED"
            }))
            .times(1);

        let client = GcpHttpClient::from_mock(mock);

        let result = client
            .service_usage()
            .is_service_enabled("test", "compute.googleapis.com")
            .await;

        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[tokio::test]
    async fn mock_full_arbiter_workflow() {
        // Simulate a complete arbiter-gcp workflow:
        // 1. Check API enablement
        // 2. List assets
        // 3. Get recommendations
        // 4. Execute remediation

        let mut mock = MockClient::new();

        // Service Usage check
        mock.expect_get("/v1/projects/test/services/compute.googleapis.com")
            .returning_json(serde_json::json!({
                "name": "projects/test/services/compute.googleapis.com",
                "state": "ENABLED"
            }))
            .times(1);

        // Asset listing
        mock.expect_get("/v1/assets")
            .returning_json(serde_json::json!({
                "assets": [
                    {
                        "name": "//compute.googleapis.com/projects/test/zones/us-central1-a/disks/unused-disk",
                        "assetType": "compute.googleapis.com/Disk"
                    }
                ]
            }))
            .times(1);

        // Remediation - delete disk
        mock.expect_delete("/compute/v1/projects/test/zones/us-central1-a/disks/unused-disk")
            .returning_json(serde_json::json!({
                "name": "op-123",
                "status": "PENDING",
                "selfLink": "https://compute.googleapis.com/compute/v1/projects/test/zones/us-central1-a/operations/op-123"
            }))
            .times(1);

        // Operation polling uses selfLink, so it includes the full path with /compute/v1
        mock.expect_get("/compute/v1/projects/test/zones/us-central1-a/operations/op-123")
            .returning_json(serde_json::json!({
                "name": "op-123",
                "status": "DONE"
            }))
            .at_least(1);

        let client = GcpHttpClient::from_mock(mock);

        // 1. Check API
        let enabled = client
            .service_usage()
            .is_service_enabled("test", "compute.googleapis.com")
            .await
            .unwrap();
        assert!(enabled);

        // 2. List assets (simplified)
        let response = client
            .get("https://cloudasset.googleapis.com/v1/assets")
            .await
            .unwrap();
        let _assets: serde_json::Value = serde_json::from_slice(&response).unwrap();

        // 3. Execute remediation
        client
            .compute()
            .delete_disk("test", "us-central1-a", "unused-disk")
            .await
            .unwrap();

        println!("✓ Complete workflow succeeded");
    }
}