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
use std::collections::HashMap;
use std::sync::Arc;

use async_stream::try_stream;
use futures::stream::Stream;
use serde::Deserialize;

use crate::common::{Blocking, QueryMetadata, QueryOptions};
use crate::errors::Error;
use crate::health::HealthCheck;
use crate::http_client::HttpClient;

#[derive(Deserialize, Debug)]
pub struct Weights {
    #[serde(rename = "Passing")]
    pub passing: u64,
    #[serde(rename = "Warning")]
    pub warning: u64,
}

#[derive(Deserialize, Debug)]
pub struct CatalogNode {
    #[serde(rename = "ID")]
    pub id: String,
    #[serde(rename = "Node")]
    pub node: String,
    #[serde(rename = "Address")]
    pub address: String,
    #[serde(rename = "Datacenter")]
    pub datacenter: String,
    #[serde(rename = "TaggedAddresses")]
    pub tagged_addresses: HashMap<String, String>,
    #[serde(rename = "Meta")]
    pub meta: HashMap<String, String>,
    #[serde(rename = "CreateIndex")]
    pub create_index: u64,
    #[serde(rename = "ModifyIndex")]
    pub modify_index: u64,
}

#[derive(Deserialize, Debug)]
pub struct ServiceAddress {
    #[serde(rename = "Address")]
    pub address: String,
    #[serde(rename = "Port")]
    pub port: u16,
}

#[derive(Deserialize, Debug)]
pub struct CatalogServiceNode {
    #[serde(rename = "ID")]
    pub id: String,
    #[serde(rename = "Node")]
    pub node: String,
    #[serde(rename = "Address")]
    pub address: String,
    #[serde(rename = "Datacenter")]
    pub datacenter: String,
    #[serde(rename = "TaggedAddresses")]
    pub tagged_addresses: HashMap<String, String>,
    #[serde(rename = "NodeMeta")]
    pub node_meta: HashMap<String, String>,
    #[serde(rename = "ServiceID")]
    pub service_id: String,
    #[serde(rename = "ServiceName")]
    pub service_name: String,
    #[serde(rename = "ServiceAddress")]
    pub service_address: String,
    #[serde(rename = "ServiceTaggedAddresses")]
    pub service_tagged_addresses: Option<HashMap<String, ServiceAddress>>,
    #[serde(rename = "ServiceTags")]
    pub service_tags: Vec<String>,
    #[serde(rename = "ServiceMeta")]
    pub service_meta: HashMap<String, String>,
    #[serde(rename = "ServicePort")]
    pub service_port: u16,
    #[serde(rename = "ServiceWeights")]
    pub service_weights: Option<Weights>,
    #[serde(rename = "ServiceEnableTagOverride")]
    pub service_enable_tag_override: bool,
    // TODO: eventually add support for this
    // #[serde(rename = "ServiceProxy")]
    // pub service_proxy: AgentServiceConnectProxyConfig,
    // #[serde(rename = "ServiceConnect")]
    // pub service_connect: AgentServiceConnect,
    #[serde(rename = "CreateIndex")]
    pub create_index: u64,
    #[serde(rename = "Checks")]
    pub checks: Option<Vec<HealthCheck>>,
    #[serde(rename = "ModifyIndex")]
    pub modify_index: u64,
    #[serde(rename = "Namespace")]
    pub namespace: Option<String>,
}

/// Catalog operations.
///
/// This type can be used to interact with the "Catalog" portion of the Consul API.
#[derive(Clone, Debug)]
pub struct Catalog {
    http_client: Arc<HttpClient>,
}

impl Catalog {
    /// Creates a new [`Catalog`].
    pub(crate) fn new(http_client: Arc<HttpClient>) -> Catalog {
        Catalog { http_client }
    }

    /// Gets the nodes running the specified service.
    pub async fn get_service_nodes(
        &self,
        service: &str,
        options: Option<QueryOptions>,
    ) -> Result<(Vec<CatalogServiceNode>, QueryMetadata), Error> {
        let request = self.http_client.build_request(
            "get",
            &["v1", "catalog", "service", service],
            options.as_ref(),
            (),
        )?;
        let response = self
            .http_client
            .run_request(request, options.as_ref())
            .await?;
        let (parsed, meta) = self.http_client.parse_query_response(response).await?;
        Ok((parsed, meta))
    }

    /// Gets a stream of changes in nodes running the specified service.
    ///
    /// Each item in the response stream represents all nodes running in the service after a change
    /// to the service has occurred.  The stream will terminate if any error is hit during the
    /// background requests made to Consul.
    pub fn watch_service_nodes(
        &self,
        service: &str,
        options: Option<QueryOptions>,
    ) -> impl Stream<Item = Result<(Vec<CatalogServiceNode>, QueryMetadata), Error>> {
        let service = service.to_string();
        let http_client = self.http_client.clone();
        let mut options = options.or_else(|| Some(QueryOptions::default()));

        let mut blocking: Option<Blocking> = None;

        try_stream! {
            loop {
                // Override the blocking settings before every request.
                let options = options.as_mut().map(|opts| { opts.blocking = blocking.take(); &*opts });

                let request = http_client.build_request("GET", &["v1", "catalog", "service", &service], options, ())?;
                let response = http_client.run_request(request, options).await?;
                let (parsed, meta) = http_client.parse_query_response(response).await?;

                // Override our blocking configuration based on the metadata from this response.
                blocking = meta.as_blocking();

                yield (parsed, meta);
            }
        }
    }
}