consul_rs/
agent.rs

1use super::catalog;
2use super::config_entry;
3use super::health;
4use lazy_static::lazy_static;
5use serde_derive::{Deserialize, Serialize};
6use serde_json::Value;
7use std::collections::HashMap;
8
9/// ServiceKind is the kind of service being registered.
10type ServiceKind = String;
11
12lazy_static! {
13    /// SERVICE_KIND_TYPICAL is a typical, classic Consul service. This is
14    /// represented by the absence of a value. This was chosen for ease of
15    /// backwards compatibility: existing services in the catalog would
16    /// default to the typical service.
17    pub static ref SERVICE_KIND_TYPICAL: ServiceKind = {
18        String::new()
19    };
20    /// SERVICE_KIND_CONNECT_PROXY is a proxy for the Connect feature. This
21    /// service proxies another service within Consul and speaks the connect
22    /// protocol.
23    pub static ref SERVICE_KIND_CONNECT_PROXY: ServiceKind = {
24        String::from("connect-proxy")
25    };
26
27    /// SERVICE_KIND_MESH_GATEWAY is a Mesh Gateway for the Connect feature. This
28    /// service will proxy connections based off the SNI header set by other
29    /// connect proxies
30    pub static ref SERVICE_KIND_MESH_GATEWAY: ServiceKind = {
31        String::from("mesh-gateway")
32    };
33
34    /// SERVICE_KIND_TERMINATING_GATEWAY is a Terminating Gateway for the Connect
35    /// feature. This service will proxy connections to services outside the mesh.
36    pub static ref SERVICE_KIND_TERMINATING_GATEWAY: ServiceKind = {
37        String::from("terminating-gateway")
38    };
39}
40
41/// UpstreamDestType is the type of upstream discovery mechanism.
42type UpstreamDestType = String;
43
44lazy_static! {
45    /// UpstreamDestTypeService discovers instances via healthy service lookup.
46    pub static ref UPSTREAM_DEST_TYPE_SERVICE: UpstreamDestType = {
47        String::from("service")
48    };
49
50    /// UpstreamDestTypePreparedQuery discovers instances via prepared query execution.
51    pub static ref UPSTREAM_DEST_TYPE_PREPARED_QUERY: UpstreamDestType = {
52        String::from("prepared_query")
53    };
54}
55
56/// AgentCheck represents a check known to the api
57#[derive(Default, Debug, Clone, Serialize, Deserialize)]
58#[allow(non_snake_case)]
59pub struct AgentCheck {
60    pub Node: Option<String>,
61    pub CheckID: Option<String>,
62    pub Name: Option<String>,
63    pub Status: Option<String>,
64    pub Notes: Option<String>,
65    pub Output: Option<String>,
66    pub ServiceID: Option<String>,
67    pub ServiceName: Option<String>,
68    pub Type: Option<String>,
69    pub Definition: Option<health::HealthCheckDefinition>,
70    pub Namespace: String,
71}
72
73/// Filter
74#[derive(Default, Debug, Clone, Serialize, Deserialize)]
75pub struct Filter {
76    pub filter: String,
77}
78
79/// AgentWeights represent optional weights for a service
80#[derive(Default, Debug, Clone, Serialize, Deserialize)]
81#[allow(non_snake_case)]
82pub struct AgentWeights {
83    pub Passing: Option<usize>,
84    pub Warning: Option<usize>,
85}
86
87/// AgentService represents a service known to the agent
88#[derive(Default, Debug, Clone, Serialize, Deserialize)]
89#[allow(non_snake_case)]
90pub struct AgentService {
91    pub Kind: Option<ServiceKind>,
92    pub ID: Option<String>,
93    pub Service: Option<String>,
94    pub Tags: Option<Vec<String>>,
95    pub Meta: Option<HashMap<String, String>>,
96    pub Port: Option<usize>,
97    pub Address: Option<String>,
98    pub TaggedAddresses: Option<HashMap<String, catalog::ServiceAddress>>,
99    pub Weights: Option<AgentWeights>,
100    pub EnableTagOverride: Option<bool>,
101    pub CreateIndex: Option<u64>,
102    pub ModifyIndex: Option<u64>,
103    pub ContentHash: Option<String>,
104    pub Proxy: Option<AgentServiceConnectProxyConfig>,
105    pub Connect: Option<AgentServiceConnect>,
106    /// NOTE: If we ever set the ContentHash outside of singular service lookup then we may need
107    /// to include the Namespace in the hash. When we do, then we are in for lots of fun with test.
108    /// For now though, ignoring it works well enough.
109    pub Namespace: Option<String>,
110    /// Datacenter is only ever returned and is ignored if presented.
111    pub Datacenter: Option<String>,
112}
113
114/// AgentServiceChecksInfo returns information about a Service and its checks
115#[derive(Default, Debug, Clone, Serialize, Deserialize)]
116#[allow(non_snake_case)]
117pub struct AgentServiceChecksInfo {
118    pub AggregatedStatus: Option<String>,
119    pub Service: Option<AgentService>,
120    pub Checks: Option<health::HealthChecks>,
121}
122
123/// AgentServiceConnect represents the Connect configuration of a service.
124#[derive(Default, Debug, Clone, Serialize, Deserialize)]
125#[allow(non_snake_case)]
126pub struct AgentServiceConnect {
127    pub Native: Option<bool>,
128    pub SidecarService: Box<Option<AgentServiceRegistration>>,
129}
130
131/// AgentServiceConnectProxyConfig is the proxy configuration in a connect-proxy
132/// ServiceDefinition or response.
133#[derive(Default, Debug, Clone, Serialize, Deserialize)]
134#[allow(non_snake_case)]
135pub struct AgentServiceConnectProxyConfig {
136    pub DestinationServiceName: Option<String>,
137    pub DestinationServiceID: Option<String>,
138    pub LocalServiceAddress: Option<String>,
139    pub LocalServicePort: Option<String>,
140    pub Mode: Option<config_entry::ProxyMode>,
141    pub TransparentProxy: Option<String>,
142    pub Config: Option<HashMap<String, Value>>,
143    pub Upstreams: Option<Vec<Upstream>>,
144    pub MeshGateway: Option<config_entry::MeshGatewayConfig>,
145    pub Expose: Option<config_entry::ExposeConfig>,
146}
147
148/// AgentServiceRegistration is used to register a new service
149#[derive(Default, Debug, Clone, Serialize, Deserialize)]
150#[allow(non_snake_case)]
151pub struct AgentServiceRegistration {
152    pub Kind: Option<ServiceKind>,
153    pub ID: Option<String>,
154    pub Name: Option<String>,
155    pub Tags: Option<Vec<String>>,
156    pub Port: Option<usize>,
157    pub Address: Option<String>,
158    pub TaggedAddresses: Option<HashMap<String, catalog::ServiceAddress>>,
159    pub EnableTagOverride: Option<bool>,
160    pub Meta: Option<HashMap<String, String>>,
161    pub Weights: Option<AgentWeights>,
162    pub Check: Option<AgentServiceCheck>,
163    pub Checks: Option<AgentServiceChecks>,
164    pub Proxy: Option<AgentServiceConnectProxyConfig>,
165    pub Connect: Option<AgentServiceConnect>,
166    // pub Namespace: Option<String>,
167}
168
169/// ServiceRegisterOpts is used to pass extra options to the service register.
170#[derive(Default, Debug, Clone, Serialize, Deserialize)]
171#[allow(non_snake_case)]
172pub struct ServiceRegisterOpts {
173    ///Missing healthchecks will be deleted from the agent.
174    ///Using this parameter allows to idempotently register a service and its checks without
175    ///having to manually deregister checks.
176    #[serde(rename = "replace-existing-checks")]
177    pub ReplaceExistingChecks: bool,
178}
179
180/// Upstream is the response structure for a proxy upstream configuration.
181#[derive(Default, Debug, Clone, Serialize, Deserialize)]
182#[allow(non_snake_case)]
183pub struct Upstream {
184    pub DestinationType: Option<UpstreamDestType>,
185    pub DestinationNamespace: Option<String>,
186    pub DestinationName: Option<String>,
187    pub Datacenter: Option<String>,
188    pub LocalBindAddress: Option<String>,
189    pub LocalBindPort: Option<usize>,
190    pub Config: HashMap<String, Value>,
191    pub MeshGateway: Option<config_entry::MeshGatewayConfig>,
192    pub CentrallyConfigured: Option<bool>,
193}
194//
195type AgentServiceChecks = Vec<AgentServiceCheck>;
196
197/// AgentServiceCheck is used to define a node or service level check
198#[derive(Default, Debug, Clone, Serialize, Deserialize)]
199#[allow(non_snake_case)]
200pub struct AgentServiceCheck {
201    pub CheckID: Option<String>,
202    pub Name: Option<String>,
203    pub Args: Option<Vec<String>>,
204    pub DockerContainerID: Option<String>,
205    /// Only supported for Docker.
206    pub Shell: Option<String>,
207    pub Interval: Option<String>,
208    pub Timeout: Option<String>,
209    pub TTL: Option<String>,
210    pub HTTP: Option<String>,
211    pub Header: Option<HashMap<String, String>>,
212    pub Method: Option<String>,
213    pub Body: Option<String>,
214    pub TCP: Option<String>,
215    pub Status: Option<String>,
216    pub Notes: Option<String>,
217    pub TLSServerName: Option<String>,
218    pub TLSSkipVerify: Option<bool>,
219    pub GRPC: Option<String>,
220    pub GRPCUseTLS: Option<bool>,
221    pub AliasNode: Option<String>,
222    pub AliasService: Option<String>,
223    pub SuccessBeforePassing: Option<i64>,
224    pub FailuresBeforeCritical: Option<i64>,
225
226    /// In Consul 0.7 and later, checks that are associated with a service
227    /// may also contain this optional DeregisterCriticalServiceAfter field,
228    /// which is a timeout in the same Go time format as Interval and TTL. If
229    /// a check is in the critical state for more than this configured value,
230    /// then its associated service (and all of its associated checks) will
231    /// automatically be deregistered.
232    pub DeregisterCriticalServiceAfter: Option<String>,
233}
234
235/// Metrics info is used to store different types of metric values from the agent.
236#[derive(Default, Debug, Clone, Serialize, Deserialize)]
237#[allow(non_snake_case)]
238pub struct MetricsInfo {
239    pub Timestamp: Option<String>,
240    pub Gauges: Option<Vec<GaugeValue>>,
241    pub Points: Option<Vec<PointValue>>,
242    pub Counters: Option<Vec<SampledValue>>,
243    pub Samples: Option<Vec<SampledValue>>,
244}
245
246/// GaugeValue stores one value that is updated as time goes on, such as
247/// the amount of memory allocated.
248#[derive(Default, Debug, Clone, Serialize, Deserialize)]
249#[allow(non_snake_case)]
250pub struct GaugeValue {
251    pub Name: Option<String>,
252    pub Value: Option<f32>,
253    pub Labels: Option<HashMap<String, String>>,
254}
255
256/// PointValue holds a series of points for a metric.
257#[derive(Default, Debug, Clone, Serialize, Deserialize)]
258#[allow(non_snake_case)]
259pub struct PointValue {
260    pub Name: Option<String>,
261    pub Points: Option<Vec<f32>>,
262}
263
264/// SampledValue stores info about a metric that is incremented over time,
265/// such as the number of requests to an HTTP endpoint.
266#[derive(Default, Debug, Clone, Serialize, Deserialize)]
267#[allow(non_snake_case)]
268pub struct SampledValue {
269    pub Name: Option<String>,
270    pub Count: Option<i64>,
271    pub Sum: Option<f64>,
272    pub Min: Option<f64>,
273    pub Max: Option<f64>,
274    pub Mean: Option<f64>,
275    pub Stddev: Option<f64>,
276    pub Labels: Option<HashMap<String, String>>,
277}