eureka_client/rest/
structures.rs

1use serde_json;
2use std::collections::HashMap;
3use std::env;
4use std::fmt::{Display, Error as FmtError, Formatter};
5
6#[derive(Debug, Clone, Serialize)]
7pub struct Register<'a> {
8    pub instance: &'a Instance,
9}
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12#[serde(rename_all = "camelCase")]
13pub struct Instance {
14    /// This doubles as the instance ID, because why not, Eureka?
15    pub host_name: String,
16    pub app: String,
17    pub ip_addr: String,
18    pub vip_address: String,
19    pub secure_vip_address: String,
20    pub status: StatusType,
21    pub port: Option<PortData>,
22    pub secure_port: PortData,
23    pub home_page_url: String,
24    pub status_page_url: String,
25    pub health_check_url: String,
26    pub data_center_info: DataCenterInfo,
27    pub lease_info: Option<LeaseInfo>,
28    /// optional app specific metadata
29    pub metadata: Option<HashMap<String, String>>,
30}
31
32impl Default for Instance {
33    fn default() -> Self {
34        Instance {
35            host_name: "localhost".to_string(),
36            app: env::var("CARGO_PKG_NAME").unwrap_or_default(),
37            ip_addr: "127.0.0.1".to_string(),
38            vip_address: env::var("CARGO_PKG_NAME").unwrap_or_default(),
39            secure_vip_address: env::var("CARGO_PKG_NAME").unwrap_or_default(),
40            status: StatusType::Starting,
41            port: None,
42            secure_port: PortData::new(443, false),
43            home_page_url: String::new(),
44            status_page_url: String::new(),
45            health_check_url: String::new(),
46            data_center_info: DataCenterInfo::default(),
47            lease_info: None,
48            metadata: None,
49        }
50    }
51}
52
53#[derive(Debug, Clone, Serialize, Deserialize)]
54pub struct PortData {
55    #[serde(rename = "$")]
56    value: u16,
57    #[serde(rename = "@enabled")]
58    enabled: String,
59}
60
61impl PortData {
62    pub fn new(port: u16, enabled: bool) -> Self {
63        PortData {
64            value: port,
65            enabled: enabled.to_string(),
66        }
67    }
68
69    pub fn value(&self) -> Option<u16> {
70        if self.enabled == "true" {
71            Some(self.value)
72        } else {
73            None
74        }
75    }
76}
77
78#[derive(Debug, Clone, Deserialize)]
79pub struct AllApplications {
80    pub applications: Applications,
81}
82
83#[derive(Debug, Clone, Deserialize)]
84pub struct Applications {
85    pub application: Vec<Application>,
86}
87
88#[derive(Debug, Clone, Deserialize)]
89pub struct ApplicationWrapper {
90    pub application: Application,
91    pub name: String,
92}
93
94#[derive(Debug, Clone, Deserialize)]
95pub struct Application {
96    pub instance: Vec<Instance>,
97}
98
99#[derive(Debug, Clone, Deserialize)]
100pub struct InstanceWrapper {
101    pub instance: Instance,
102}
103
104#[derive(Debug, Clone, Serialize, Deserialize)]
105pub struct DataCenterInfo {
106    #[serde(rename = "@class")]
107    class: String,
108    pub name: DcNameType,
109    /// metadata is only allowed if name is Amazon, and then is required
110    #[serde(skip_serializing_if = "Option::is_none")]
111    pub metadata: Option<AmazonMetadataType>,
112}
113
114impl Default for DataCenterInfo {
115    fn default() -> Self {
116        DataCenterInfo {
117            class: "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo".into(),
118            name: DcNameType::MyOwn,
119            metadata: None,
120        }
121    }
122}
123
124#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
125#[serde(rename_all = "camelCase")]
126pub struct LeaseInfo {
127    /// (optional) if you want to change the length of lease - default if 90 secs
128    pub eviction_duration_in_secs: Option<usize>,
129}
130
131#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
132pub enum DcNameType {
133    MyOwn,
134    Amazon,
135}
136
137impl Display for DcNameType {
138    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
139        write!(f, "{:?}", self)
140    }
141}
142
143#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
144#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
145pub enum StatusType {
146    Up,
147    Down,
148    Starting,
149    OutOfService,
150    Unknown,
151}
152
153impl Display for StatusType {
154    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
155        write!(
156            f,
157            "{}",
158            serde_json::to_value(self).unwrap().as_str().unwrap()
159        )
160    }
161}
162
163#[derive(Debug, Clone, Serialize, Deserialize)]
164#[serde(rename_all = "kebab-case")]
165pub struct AmazonMetadataType {
166    pub ami_launch_index: String,
167    pub local_hostname: String,
168    pub availability_zone: String,
169    pub instance_id: String,
170    pub public_ipv4: String,
171    pub public_hostname: String,
172    pub ami_manifest_path: String,
173    pub local_ipv4: String,
174    pub hostname: String,
175    pub ami_id: String,
176    pub instance_type: String,
177}