quelch 0.10.1

Ingest data from Jira, Confluence, and more directly into Azure AI Search
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/// Compute a plan by diffing local rigg files against live Azure AI Search resources.
///
/// # Overview
///
/// `run` reads every YAML file under `rigg_dir/{subdir}/*.yaml`, fetches the
/// equivalent resources from Azure via the [`RiggApiAdapter`] trait, and
/// produces a [`PlanReport`] classifying each resource as **create**,
/// **update**, **delete**, or **unchanged**.
///
/// # Field-level diff
///
/// For update entries, [`ResourceDiff`] contains [`FieldChange`] values with
/// dotted JSON-pointer paths (e.g. `fields.0.searchable`) so callers can
/// render a human-readable diff.
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use serde_json::Value as JsonValue;

use rigg_core::resources::ResourceKind;

/// Errors that can occur during plan computation.
#[derive(Debug, thiserror::Error)]
pub enum PlanError {
    #[error("io: {0}")]
    Io(#[from] std::io::Error),
    #[error("rigg: {0}")]
    Rigg(String),
    #[error("yaml: {0}")]
    Yaml(#[from] serde_yaml::Error),
}

/// A reference to a single Azure AI Search resource.
#[derive(Debug, Clone)]
pub struct ResourceRef {
    /// The type of this resource.
    pub kind: ResourceKind,
    /// The resource name (used as the Azure resource identifier).
    pub name: String,
}

/// A field-level change within a resource that differs between local and live.
#[derive(Debug, Clone)]
pub struct FieldChange {
    /// Dotted path to the changed field (e.g. `fields.0.searchable`).
    pub path: String,
    /// Value in the live (Azure) state.
    pub from: serde_json::Value,
    /// Value in the local (disk) state.
    pub to: serde_json::Value,
}

/// All field-level changes for a single resource that needs updating.
#[derive(Debug)]
pub struct ResourceDiff {
    /// Individual field paths that changed, with before/after values.
    pub field_changes: Vec<FieldChange>,
}

/// The output of a plan run: resources to create, update, delete, or leave alone.
#[derive(Debug, Default)]
pub struct PlanReport {
    /// Resources present locally but not in Azure → will be created.
    pub creates: Vec<ResourceRef>,
    /// Resources present in both but with differing content → will be updated.
    pub updates: Vec<(ResourceRef, ResourceDiff)>,
    /// Resources present in Azure but not locally → will be deleted.
    pub deletes: Vec<ResourceRef>,
    /// Resources identical in both places → no action needed.
    pub unchanged: Vec<ResourceRef>,
}

/// Adapter trait abstracting rigg-client operations for testability.
///
/// Production code wires [`RiggClientAdapter`]; tests inject [`MockRiggApi`].
#[trait_variant::make(Send)]
pub trait RiggApiAdapter: Sync {
    /// List all resources of the given kind, returning raw JSON objects.
    async fn list_resources(
        &self,
        kind: ResourceKind,
    ) -> Result<Vec<serde_json::Value>, anyhow::Error>;

    /// Create or replace a resource.
    async fn upsert_resource(
        &self,
        kind: ResourceKind,
        name: &str,
        body: &serde_json::Value,
    ) -> Result<(), anyhow::Error>;

    /// Delete a resource by name.
    async fn delete_resource(&self, kind: ResourceKind, name: &str) -> Result<(), anyhow::Error>;
}

/// Production adapter that wraps `rigg_client::AzureSearchClient`.
pub struct RiggClientAdapter {
    client: rigg_client::AzureSearchClient,
}

impl RiggClientAdapter {
    /// Create an adapter connected to the given endpoint with the default
    /// Azure CLI / environment-variable auth provider.
    pub fn new(base_url: String, preview_api_version: String) -> Result<Self, anyhow::Error> {
        let auth =
            rigg_client::auth::get_auth_provider().map_err(|e| anyhow::anyhow!("auth: {e}"))?;
        let client = rigg_client::AzureSearchClient::with_auth(base_url, preview_api_version, auth)
            .map_err(|e| anyhow::anyhow!("client: {e}"))?;
        Ok(Self { client })
    }
}

impl RiggApiAdapter for RiggClientAdapter {
    async fn list_resources(
        &self,
        kind: ResourceKind,
    ) -> Result<Vec<serde_json::Value>, anyhow::Error> {
        self.client
            .list(kind)
            .await
            .map_err(|e| anyhow::anyhow!("{e}"))
    }

    async fn upsert_resource(
        &self,
        kind: ResourceKind,
        name: &str,
        body: &serde_json::Value,
    ) -> Result<(), anyhow::Error> {
        self.client
            .create_or_update(kind, name, body)
            .await
            .map(|_| ())
            .map_err(|e| anyhow::anyhow!("{e}"))
    }

    async fn delete_resource(&self, kind: ResourceKind, name: &str) -> Result<(), anyhow::Error> {
        self.client
            .delete(kind, name)
            .await
            .map_err(|e| anyhow::anyhow!("{e}"))
    }
}

// ---------------------------------------------------------------------------
// Resource kind → local subdirectory mapping
// ---------------------------------------------------------------------------

/// Maps a [`ResourceKind`] to the flat subdirectory name quelch uses on disk.
///
/// This matches the layout that [`crate::azure::rigg::write`] writes to, which
/// differs from rigg-core's categorised `directory_name()` structure.
/// Exposed for use by the `push` and `pull` modules.
pub fn subdir_for_kind(kind: ResourceKind) -> &'static str {
    subdir_for(kind)
}

fn subdir_for(kind: ResourceKind) -> &'static str {
    match kind {
        ResourceKind::Index => "indexes",
        ResourceKind::DataSource => "datasources",
        ResourceKind::Skillset => "skillsets",
        ResourceKind::Indexer => "indexers",
        ResourceKind::KnowledgeSource => "knowledge_sources",
        ResourceKind::KnowledgeBase => "knowledge_bases",
        // Not generated by quelch, but handled gracefully.
        ResourceKind::SynonymMap => "synonym_maps",
        ResourceKind::Alias => "aliases",
        ResourceKind::Agent => "agents",
    }
}

/// The resource kinds quelch manages (matches `write.rs` groups).
///
/// Exposed so `push` and `pull` can iterate over the same set.
pub const MANAGED_KINDS: &[ResourceKind] = &[
    ResourceKind::DataSource,
    ResourceKind::Skillset,
    ResourceKind::Index,
    ResourceKind::Indexer,
    ResourceKind::KnowledgeSource,
    ResourceKind::KnowledgeBase,
];

// ---------------------------------------------------------------------------
// Plan entry point
// ---------------------------------------------------------------------------

/// Run a plan: diff local rigg files against live Azure resources.
///
/// `rigg_dir` must be the root of the quelch rigg output directory (the
/// directory containing `indexes/`, `datasources/`, etc. subdirs).
///
/// `api` is the rigg adapter — use [`RiggClientAdapter`] in production or a
/// mock in tests.
pub async fn run<A: RiggApiAdapter>(rigg_dir: &Path, api: &A) -> Result<PlanReport, PlanError> {
    let local = read_local(rigg_dir)?;
    let live = fetch_live(api).await?;
    Ok(compute_diff(local, live))
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

type ResourceMap = HashMap<(ResourceKind, String), serde_json::Value>;

/// Read all local YAML files under `rigg_dir` into a map keyed by (kind, name).
fn read_local(rigg_dir: &Path) -> Result<ResourceMap, PlanError> {
    let mut map = ResourceMap::new();

    for kind in MANAGED_KINDS {
        let subdir = rigg_dir.join(subdir_for(*kind));
        if !subdir.exists() {
            continue;
        }

        for entry in std::fs::read_dir(&subdir)? {
            let entry = entry?;
            let path: PathBuf = entry.path();

            if path.extension().and_then(|e| e.to_str()) != Some("yaml") {
                continue;
            }

            let yaml_text = std::fs::read_to_string(&path)?;
            // Parse YAML → JSON value so we can do uniform comparison.
            let yaml_val: serde_yaml::Value = serde_yaml::from_str(&yaml_text)?;
            let json_val = yaml_to_json(yaml_val);

            // Extract the resource name.
            let name = extract_name(&json_val, &path);
            map.insert((*kind, name), json_val);
        }
    }

    Ok(map)
}

/// Fetch all live resources from Azure into the same map shape.
async fn fetch_live<A: RiggApiAdapter>(api: &A) -> Result<ResourceMap, PlanError> {
    let mut map = ResourceMap::new();

    for kind in MANAGED_KINDS {
        let items = api
            .list_resources(*kind)
            .await
            .map_err(|e| PlanError::Rigg(e.to_string()))?;

        for item in items {
            let name = item
                .get("name")
                .and_then(|v| v.as_str())
                .unwrap_or_default()
                .to_string();
            if !name.is_empty() {
                map.insert((*kind, name), item);
            }
        }
    }

    Ok(map)
}

/// Compute the plan diff between local and live states.
fn compute_diff(local: ResourceMap, live: ResourceMap) -> PlanReport {
    let mut report = PlanReport::default();

    // Everything in local.
    for ((kind, name), local_val) in &local {
        let rref = ResourceRef {
            kind: *kind,
            name: name.clone(),
        };

        match live.get(&(*kind, name.clone())) {
            None => {
                // Local only → create.
                report.creates.push(rref);
            }
            Some(live_val) => {
                // Both → diff.
                let changes = diff_values(local_val, live_val, "");
                if changes.is_empty() {
                    report.unchanged.push(rref);
                } else {
                    report.updates.push((
                        rref,
                        ResourceDiff {
                            field_changes: changes,
                        },
                    ));
                }
            }
        }
    }

    // Everything in live but not in local → delete.
    for (kind, name) in live.keys() {
        if !local.contains_key(&(*kind, name.clone())) {
            report.deletes.push(ResourceRef {
                kind: *kind,
                name: name.clone(),
            });
        }
    }

    report
}

/// Recursively diff two JSON values, producing [`FieldChange`] entries for
/// every leaf that differs.
fn diff_values(local: &JsonValue, live: &JsonValue, path: &str) -> Vec<FieldChange> {
    let mut changes = Vec::new();
    diff_values_inner(local, live, path, &mut changes);
    changes
}

fn diff_values_inner(
    local: &JsonValue,
    live: &JsonValue,
    path: &str,
    changes: &mut Vec<FieldChange>,
) {
    match (local, live) {
        (JsonValue::Object(loc_map), JsonValue::Object(live_map)) => {
            // Keys in local.
            for (k, loc_v) in loc_map {
                let child_path = if path.is_empty() {
                    k.clone()
                } else {
                    format!("{path}.{k}")
                };
                match live_map.get(k) {
                    None => {
                        // Field absent in live — treat as a change.
                        changes.push(FieldChange {
                            path: child_path,
                            from: JsonValue::Null,
                            to: loc_v.clone(),
                        });
                    }
                    Some(live_v) => {
                        diff_values_inner(loc_v, live_v, &child_path, changes);
                    }
                }
            }
            // Keys in live but not in local.
            for (k, live_v) in live_map {
                if !loc_map.contains_key(k) {
                    let child_path = if path.is_empty() {
                        k.clone()
                    } else {
                        format!("{path}.{k}")
                    };
                    changes.push(FieldChange {
                        path: child_path,
                        from: live_v.clone(),
                        to: JsonValue::Null,
                    });
                }
            }
        }
        (JsonValue::Array(loc_arr), JsonValue::Array(live_arr)) => {
            let max_len = loc_arr.len().max(live_arr.len());
            for i in 0..max_len {
                let child_path = if path.is_empty() {
                    i.to_string()
                } else {
                    format!("{path}.{i}")
                };
                match (loc_arr.get(i), live_arr.get(i)) {
                    (Some(l), Some(r)) => diff_values_inner(l, r, &child_path, changes),
                    (Some(l), None) => changes.push(FieldChange {
                        path: child_path,
                        from: JsonValue::Null,
                        to: l.clone(),
                    }),
                    (None, Some(r)) => changes.push(FieldChange {
                        path: child_path,
                        from: r.clone(),
                        to: JsonValue::Null,
                    }),
                    (None, None) => {}
                }
            }
        }
        // Leaf comparison.
        _ => {
            if local != live {
                changes.push(FieldChange {
                    path: path.to_string(),
                    from: live.clone(),
                    to: local.clone(),
                });
            }
        }
    }
}

/// Extract the resource `name` from a JSON object, falling back to the
/// file stem if the `name` key is absent.
fn extract_name(val: &JsonValue, path: &Path) -> String {
    if let Some(s) = val.get("name").and_then(|v| v.as_str()) {
        return s.to_string();
    }
    // Fall back to file stem.
    path.file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("unknown")
        .to_string()
}

/// Convert `serde_yaml::Value` to `serde_json::Value` for uniform comparison.
fn yaml_to_json(v: serde_yaml::Value) -> serde_json::Value {
    // Round-trip through JSON serialisation — straightforward and correct
    // for the YAML subset that Azure resource files use.
    let json_str = serde_json::to_string(&v).unwrap_or_default();
    serde_json::from_str(&json_str).unwrap_or(serde_json::Value::Null)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
pub mod tests {
    use super::*;
    use std::collections::HashMap;
    use std::sync::{Arc, Mutex};

    // -----------------------------------------------------------------------
    // Mock adapter
    // -----------------------------------------------------------------------

    /// A simple mock rigg API for unit tests.
    #[derive(Default)]
    pub struct MockRiggApi {
        /// Pre-loaded live resources per kind.
        live: HashMap<ResourceKind, Vec<serde_json::Value>>,
        /// Recorded upsert calls: (kind, name).
        pub upserted: Arc<Mutex<Vec<(ResourceKind, String)>>>,
        /// Recorded delete calls: (kind, name).
        pub deleted: Arc<Mutex<Vec<(ResourceKind, String)>>>,
    }

    impl MockRiggApi {
        pub fn with_live(mut self, kind: ResourceKind, items: Vec<serde_json::Value>) -> Self {
            self.live.insert(kind, items);
            self
        }
    }

    impl RiggApiAdapter for MockRiggApi {
        async fn list_resources(
            &self,
            kind: ResourceKind,
        ) -> Result<Vec<serde_json::Value>, anyhow::Error> {
            Ok(self.live.get(&kind).cloned().unwrap_or_default())
        }

        async fn upsert_resource(
            &self,
            kind: ResourceKind,
            name: &str,
            _body: &serde_json::Value,
        ) -> Result<(), anyhow::Error> {
            self.upserted.lock().unwrap().push((kind, name.to_string()));
            Ok(())
        }

        async fn delete_resource(
            &self,
            kind: ResourceKind,
            name: &str,
        ) -> Result<(), anyhow::Error> {
            self.deleted.lock().unwrap().push((kind, name.to_string()));
            Ok(())
        }
    }

    // -----------------------------------------------------------------------
    // Helpers
    // -----------------------------------------------------------------------

    fn write_index(dir: &Path, name: &str, content: &str) {
        let sub = dir.join("indexes");
        std::fs::create_dir_all(&sub).unwrap();
        std::fs::write(sub.join(format!("{name}.yaml")), content).unwrap();
    }

    fn minimal_index_json(name: &str) -> serde_json::Value {
        serde_json::json!({ "name": name, "fields": [] })
    }

    fn minimal_index_yaml(name: &str) -> String {
        format!("name: {name}\nfields: []\n")
    }

    // -----------------------------------------------------------------------
    // plan tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn plan_creates_when_local_only() {
        let dir = tempfile::tempdir().unwrap();
        write_index(
            dir.path(),
            "jira-issues",
            &minimal_index_yaml("jira-issues"),
        );

        let api = MockRiggApi::default(); // live is empty
        let report = run(dir.path(), &api).await.unwrap();

        assert_eq!(report.creates.len(), 1, "expected one create");
        assert_eq!(report.creates[0].name, "jira-issues");
        assert!(matches!(report.creates[0].kind, ResourceKind::Index));
        assert!(report.updates.is_empty());
        assert!(report.deletes.is_empty());
        assert!(report.unchanged.is_empty());
    }

    #[tokio::test]
    async fn plan_deletes_when_live_only() {
        let dir = tempfile::tempdir().unwrap();
        // No local files.

        let api = MockRiggApi::default()
            .with_live(ResourceKind::Index, vec![minimal_index_json("jira-issues")]);
        let report = run(dir.path(), &api).await.unwrap();

        assert_eq!(report.deletes.len(), 1, "expected one delete");
        assert_eq!(report.deletes[0].name, "jira-issues");
        assert!(report.creates.is_empty());
        assert!(report.updates.is_empty());
        assert!(report.unchanged.is_empty());
    }

    #[tokio::test]
    async fn plan_unchanged_when_identical() {
        let dir = tempfile::tempdir().unwrap();
        write_index(
            dir.path(),
            "jira-issues",
            &minimal_index_yaml("jira-issues"),
        );

        let api = MockRiggApi::default()
            .with_live(ResourceKind::Index, vec![minimal_index_json("jira-issues")]);
        let report = run(dir.path(), &api).await.unwrap();

        assert_eq!(report.unchanged.len(), 1, "expected one unchanged");
        assert!(report.creates.is_empty());
        assert!(report.updates.is_empty());
        assert!(report.deletes.is_empty());
    }

    #[tokio::test]
    async fn plan_updates_when_field_changes() {
        let dir = tempfile::tempdir().unwrap();
        // Local has searchable: true on a field; live has false.
        let local_yaml = "name: jira-issues\nfields:\n  - name: title\n    searchable: true\n";
        write_index(dir.path(), "jira-issues", local_yaml);

        let live_json = serde_json::json!({
            "name": "jira-issues",
            "fields": [{ "name": "title", "searchable": false }]
        });
        let api = MockRiggApi::default().with_live(ResourceKind::Index, vec![live_json]);
        let report = run(dir.path(), &api).await.unwrap();

        assert_eq!(report.updates.len(), 1, "expected one update");
        let (rref, diff) = &report.updates[0];
        assert_eq!(rref.name, "jira-issues");
        // At least one field change mentioning "searchable".
        let mentions_searchable = diff
            .field_changes
            .iter()
            .any(|fc| fc.path.contains("searchable"));
        assert!(
            mentions_searchable,
            "expected a FieldChange for 'searchable', got: {:?}",
            diff.field_changes
                .iter()
                .map(|f| &f.path)
                .collect::<Vec<_>>()
        );
        assert!(report.creates.is_empty());
        assert!(report.deletes.is_empty());
        assert!(report.unchanged.is_empty());
    }

    #[test]
    fn diff_values_leaf_change() {
        let a = serde_json::json!({"x": 1});
        let b = serde_json::json!({"x": 2});
        let changes = diff_values(&a, &b, "");
        assert_eq!(changes.len(), 1);
        assert_eq!(changes[0].path, "x");
        assert_eq!(changes[0].from, serde_json::json!(2));
        assert_eq!(changes[0].to, serde_json::json!(1));
    }

    #[test]
    fn diff_values_nested() {
        let a = serde_json::json!({"a": {"b": true}});
        let b = serde_json::json!({"a": {"b": false}});
        let changes = diff_values(&a, &b, "");
        assert_eq!(changes.len(), 1);
        assert_eq!(changes[0].path, "a.b");
    }

    #[test]
    fn diff_values_identical_empty() {
        let a = serde_json::json!({"name": "x"});
        let changes = diff_values(&a, &a, "");
        assert!(changes.is_empty());
    }

    #[test]
    fn subdir_for_round_trips_all_managed_kinds() {
        for kind in MANAGED_KINDS {
            // subdir_for should not panic for any managed kind.
            let s = subdir_for(*kind);
            assert!(!s.is_empty());
        }
    }
}