headless_lms_chatbot/
azure_datasources.rs

1use serde_json::json;
2
3use crate::prelude::*;
4
5pub const API_VERSION: &str = "2024-07-01";
6
7pub async fn does_azure_datasource_exist(
8    datasource_name: &str,
9    app_config: &ApplicationConfiguration,
10) -> anyhow::Result<bool> {
11    let azure_config = app_config.azure_configuration.as_ref().ok_or_else(|| {
12        anyhow::anyhow!("Azure configuration is missing from the application configuration")
13    })?;
14
15    let search_config = azure_config.search_config.as_ref().ok_or_else(|| {
16        anyhow::anyhow!("Azure search configuration is missing from the Azure configuration")
17    })?;
18    let mut url = search_config.search_endpoint.clone();
19    url.set_path(&format!("datasources('{}')", datasource_name));
20    url.set_query(Some(&format!("api-version={}", API_VERSION)));
21
22    let response = REQWEST_CLIENT
23        .get(url)
24        .header("Content-Type", "application/json")
25        .header("api-key", search_config.search_api_key.clone())
26        .send()
27        .await?;
28
29    if response.status().is_success() {
30        Ok(true)
31    } else if response.status() == 404 {
32        Ok(false)
33    } else {
34        let status = response.status();
35        let error_text = response.text().await?;
36        Err(anyhow::anyhow!(
37            "Error checking if index exists. Status: {}. Error: {}",
38            status,
39            error_text
40        ))
41    }
42}
43
44pub async fn create_azure_datasource(
45    datasource_name: &str,
46    container_name: &str,
47    app_config: &ApplicationConfiguration,
48) -> anyhow::Result<()> {
49    // Retrieve Azure configurations from the application configuration
50    let azure_config = app_config.azure_configuration.as_ref().ok_or_else(|| {
51        anyhow::anyhow!("Azure configuration is missing from the application configuration")
52    })?;
53
54    let search_config = azure_config.search_config.as_ref().ok_or_else(|| {
55        anyhow::anyhow!("Azure search configuration is missing from the Azure configuration")
56    })?;
57
58    let blob_storage_config = azure_config.blob_storage_config.as_ref().ok_or_else(|| {
59        anyhow::anyhow!("Blob storage configuration is missing from the Azure configuration")
60    })?;
61
62    let connection_string = blob_storage_config.connection_string()?;
63
64    let mut url = search_config.search_endpoint.clone();
65    url.set_path(&format!("datasources/{}", datasource_name));
66    url.set_query(Some(&format!("api-version={}", API_VERSION)));
67
68    let datasource_definition = json!({
69        "name": datasource_name,
70        "type": "azureblob",
71        "container": {
72            "name": container_name,
73        },
74        "credentials": {
75            "connectionString": connection_string,
76        },
77    });
78
79    let response = REQWEST_CLIENT
80        .put(url)
81        .header("Content-Type", "application/json")
82        .header("api-key", search_config.search_api_key.clone())
83        .json(&datasource_definition)
84        .send()
85        .await?;
86
87    if response.status().is_success() {
88        Ok(())
89    } else {
90        let status = response.status();
91        let error_text = response.text().await?;
92        Err(anyhow::anyhow!(
93            "Error creating datasource. Status: {}. Error: {}",
94            status,
95            error_text
96        ))
97    }
98}