|
2 | 2 | // Licensed under the MIT license.
|
3 | 3 | // SPDX-License-Identifier: MIT
|
4 | 4 |
|
5 |
| -use config::{Config, File, FileFormat}; |
6 |
| -use log::debug; |
| 5 | +use config::{Config, ConfigError, File, FileFormat}; |
| 6 | +use core_protobuf_data_access::chariott::service_discovery::core::v1::{ |
| 7 | + service_registry_client::ServiceRegistryClient, DiscoverRequest, |
| 8 | +}; |
| 9 | +use log::{debug, info}; |
| 10 | +use serde_derive::Deserialize; |
7 | 11 | use std::future::Future;
|
| 12 | +use strum_macros::Display; |
8 | 13 | use tokio::time::{sleep, Duration};
|
| 14 | +use tonic::{Request, Status}; |
| 15 | + |
| 16 | +/// An identifier used when discovering a service through Chariott. |
| 17 | +#[derive(Debug, Deserialize)] |
| 18 | +pub struct ServiceIdentifier { |
| 19 | + /// The namespace of the service. |
| 20 | + pub namespace: String, |
| 21 | + /// The name of the service. |
| 22 | + pub name: String, |
| 23 | + /// The version of the service. |
| 24 | + pub version: String, |
| 25 | +} |
| 26 | + |
| 27 | +/// An enum representing where to discover a service's URI. |
| 28 | +#[derive(Display, Debug, Deserialize)] |
| 29 | +pub enum ServiceUriSource { |
| 30 | + /// Use the local configuration settings to find the service's URI. |
| 31 | + Local { service_uri: String }, |
| 32 | + /// Use Chariott to discover the service's URI. |
| 33 | + Chariott { chariott_uri: String, service_identifier: ServiceIdentifier }, |
| 34 | +} |
9 | 35 |
|
10 | 36 | /// Load the settings.
|
11 | 37 | ///
|
12 | 38 | /// # Arguments
|
13 | 39 | /// * `config_filename` - Name of the config file to load settings from.
|
14 |
| -pub fn load_settings<T>(config_filename: &str) -> T |
| 40 | +pub fn load_settings<T>(config_filename: &str) -> Result<T, ConfigError> |
15 | 41 | where
|
16 | 42 | T: for<'de> serde::Deserialize<'de>,
|
17 | 43 | {
|
18 | 44 | let config =
|
19 |
| - Config::builder().add_source(File::new(config_filename, FileFormat::Yaml)).build().unwrap(); |
20 |
| - |
21 |
| - let settings: T = config.try_deserialize().unwrap(); |
| 45 | + Config::builder().add_source(File::new(config_filename, FileFormat::Yaml)).build()?; |
22 | 46 |
|
23 |
| - settings |
| 47 | + config.try_deserialize() |
24 | 48 | }
|
25 | 49 |
|
26 | 50 | /// Retry a function that returns an error.
|
|
64 | 88 | last_error
|
65 | 89 | }
|
66 | 90 |
|
| 91 | +/// Use Chariott to discover a service. |
| 92 | +/// |
| 93 | +/// # Arguments |
| 94 | +/// * `chariott_uri` - Chariott's URI. |
| 95 | +/// * `namespace` - The service's namespace. |
| 96 | +/// * `name` - The service's name. |
| 97 | +/// * `version` - The service's version. |
| 98 | +/// # `expected_communication_kind` - The service's expected communication kind. |
| 99 | +/// # `expected_communication_reference` - The service's expected communication reference. |
| 100 | +pub async fn discover_service_using_chariott( |
| 101 | + chariott_uri: &str, |
| 102 | + namespace: &str, |
| 103 | + name: &str, |
| 104 | + version: &str, |
| 105 | + expected_communication_kind: &str, |
| 106 | + expected_communication_reference: &str, |
| 107 | +) -> Result<String, Status> { |
| 108 | + let mut client = ServiceRegistryClient::connect(chariott_uri.to_string()) |
| 109 | + .await |
| 110 | + .map_err(|e| Status::internal(e.to_string()))?; |
| 111 | + |
| 112 | + let request = Request::new(DiscoverRequest { |
| 113 | + namespace: namespace.to_string(), |
| 114 | + name: name.to_string(), |
| 115 | + version: version.to_string(), |
| 116 | + }); |
| 117 | + |
| 118 | + let response = client.discover(request).await?; |
| 119 | + |
| 120 | + let service = response.into_inner().service.ok_or_else(|| Status::not_found("Did not find a service in Chariott with namespace '{namespace}', name '{name}' and version {version}"))?; |
| 121 | + |
| 122 | + if service.communication_kind != expected_communication_kind |
| 123 | + && service.communication_reference != expected_communication_reference |
| 124 | + { |
| 125 | + Err(Status::not_found( |
| 126 | + "Did not find a service in Chariott with namespace '{namespace}', name '{name}' and version {version} that has communication kind '{communication_kind} and communication_reference '{communication_reference}''", |
| 127 | + )) |
| 128 | + } else { |
| 129 | + Ok(service.uri) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +/// Get a service's URI from settings or from Chariott. |
| 134 | +/// |
| 135 | +/// # Arguments |
| 136 | +/// * `service_uri_source` - Enum providing information on how to get the service URI. |
| 137 | +/// # `expected_communication_kind` - The service's expected communication kind. |
| 138 | +/// # `expected_communication_reference` - The service's expected communication reference. |
| 139 | +pub async fn get_service_uri( |
| 140 | + service_uri_source: ServiceUriSource, |
| 141 | + expected_communication_kind: &str, |
| 142 | + expected_communication_reference: &str, |
| 143 | +) -> Result<String, Status> { |
| 144 | + let result = match service_uri_source { |
| 145 | + ServiceUriSource::Local { service_uri } => { |
| 146 | + info!("URI set in settings."); |
| 147 | + service_uri |
| 148 | + } |
| 149 | + ServiceUriSource::Chariott { chariott_uri, service_identifier } => { |
| 150 | + info!("Retrieving URI from Chariott."); |
| 151 | + |
| 152 | + execute_with_retry( |
| 153 | + 30, |
| 154 | + Duration::from_secs(1), |
| 155 | + || { |
| 156 | + discover_service_using_chariott( |
| 157 | + &chariott_uri, |
| 158 | + &service_identifier.namespace, |
| 159 | + &service_identifier.name, |
| 160 | + &service_identifier.version, |
| 161 | + expected_communication_kind, |
| 162 | + expected_communication_reference, |
| 163 | + ) |
| 164 | + }, |
| 165 | + Some(format!( |
| 166 | + "Attempting to discover service '{}' with chariott.", |
| 167 | + service_identifier.name |
| 168 | + )), |
| 169 | + ) |
| 170 | + .await? |
| 171 | + } |
| 172 | + }; |
| 173 | + |
| 174 | + Ok(result) |
| 175 | +} |
| 176 | + |
67 | 177 | #[cfg(test)]
|
68 | 178 | mod tests {
|
69 | 179 | use super::*;
|
|
0 commit comments