Skip to content

Commit ba45cee

Browse files
authored
src/params.rs: Add data_network_ip (#22)
For ease of finding the data network IP address.
1 parent 00c280f commit ba45cee

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [0.3.0] unreleased
8+
### Added
9+
10+
- Add `RunParameters::data_network_ip` for ease of finding the IP within the data network assigned to the instance. See [PR 22].
11+
812
### Change
913
- Change `RunParameters::test_instance_params` from `String` to `HashMap` which contains key-value pairs from parsing the parameter string. See [PR 19].
1014

1115
[PR 19]: https://github.com/testground/sdk-rust/pull/19
16+
[PR 22]: https://github.com/testground/sdk-rust/pull/22
1217

1318
## [0.2.0]
1419
### Added

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ version = "0.3.0"
1313
[dependencies]
1414
clap = { version = "3", default-features = false, features = ["std", "derive", "env"] }
1515
futures = { version = "0.3", default-features = false, features = [] }
16+
if-addrs = "0.7.0"
1617
influxdb = { version = "0.5", default-features = false, features = ["reqwest", "serde", "serde_json", "derive"] }
1718
ipnetwork = { version = "0.19.0", default-features = false, features = ["serde"] }
1819
log = "0.4"

src/params.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clap::Parser;
22
use std::collections::HashMap;
3+
use std::net::{IpAddr, Ipv4Addr};
34

45
use std::path::PathBuf;
56

@@ -61,6 +62,26 @@ pub struct RunParameters {
6162
// HOME: /
6263
}
6364

65+
impl RunParameters {
66+
/// Examines the local network interfaces, and tries to find our assigned IP
67+
/// within the data network.
68+
///
69+
/// If running in a sidecar-less environment, the loopback address is
70+
/// returned.
71+
pub fn data_network_ip(&self) -> std::io::Result<Option<IpAddr>> {
72+
if !self.test_sidecar {
73+
// This must be a local:exec runner and we currently don't support
74+
// traffic shaping on it for now, just return the loopback address.
75+
return Ok(Some(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))));
76+
}
77+
78+
Ok(if_addrs::get_if_addrs()?
79+
.into_iter()
80+
.map(|i| i.addr.ip())
81+
.find(|ip| self.test_subnet.contains(*ip)))
82+
}
83+
}
84+
6485
fn parse_key_val(s: &str) -> Result<HashMap<String, String>, String> {
6586
let mut hashmap = HashMap::new();
6687

0 commit comments

Comments
 (0)