Skip to content

Commit

Permalink
adds nats cluster uri as a command line arg
Browse files Browse the repository at this point in the history
Signed-off-by: Gina Peers <gpeers@chef.io>
  • Loading branch information
Gina Peers committed Apr 17, 2019
1 parent 74f255a commit f3a4af4
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 26 deletions.
4 changes: 1 addition & 3 deletions components/common/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ impl AutomateAuthToken {
/// of a token.
#[allow(clippy::needless_pass_by_value)] // Signature required by CLAP
pub fn validate(value: String) -> result::Result<(), String> {
value.parse::<Self>()
.map(|_| ())
.map_err(|_| "This should be impossible".to_string())
value.parse::<Self>().map(|_| ()).map_err(|e| e.to_string())
}

/// Create an instance of `AutomateAuthToken` from validated
Expand Down
119 changes: 100 additions & 19 deletions components/hab/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1201,23 +1201,40 @@ fn maybe_add_event_stream_options(mut app: App<'static, 'static>,
.required(true)
.takes_value(true)
.validator(non_empty));
app = app.arg(Arg::with_name(AutomateAuthToken::ARG_NAME).help("An authentication token for \
streaming events to an \
Automate server.")
.long("event-stream-token")
.required(true)
.takes_value(true)
.validator(AutomateAuthToken::validate)
.env(AutomateAuthToken::ENVVAR));
app =
app.arg(Arg::with_name(EventStreamMetadata::ARG_NAME).help("An arbitrary key-value pair \
to add to each event \
generated by this Supervisor")
.long("event-meta")
.takes_value(true)
.multiple(true)
.validator(EventStreamMetadata::validate)
.required(false));
app = app.arg(Arg::with_name("EVENT_STREAM_URL").help("The event stream connection \
string (host:port) used by this \
Supervisor to send events to a \
messaging server.")
.long("event-stream-url")
.required(true)
.takes_value(true)
.validator(non_empty));
app = app.arg(
Arg::with_name(AutomateAuthToken::ARG_NAME)
.help(
"An authentication token for \
streaming events to an \
messaging server.",
)
.long("event-stream-token")
.required(true)
.takes_value(true)
.validator(AutomateAuthToken::validate)
.env(AutomateAuthToken::ENVVAR),
);
app = app.arg(
Arg::with_name(EventStreamMetadata::ARG_NAME)
.help(
"An arbitrary key-value pair \
to add to each event \
generated by this Supervisor",
)
.long("event-meta")
.takes_value(true)
.multiple(true)
.validator(EventStreamMetadata::validate)
.required(false),
);
}

app
Expand Down Expand Up @@ -1430,7 +1447,7 @@ mod tests {
}

#[test]
fn run_requries_app_and_env_and_token() {
fn run_requires_app_and_env_and_token_and_url() {
let matches = sub_sup_run(event_stream_enabled()).get_matches_from_safe(vec!["run"]);
assert!(matches.is_err());
assert_eq!(matches.unwrap_err().kind,
Expand All @@ -1444,12 +1461,14 @@ mod tests {
"MY_ENV",
"--event-stream-token",
"MY_TOKEN",
"--event-stream-url",
"127.0.0.1:4222",
]);
assert!(matches.is_ok());
}

#[test]
fn app_and_env_and_token_options_require_event_stream_feature() {
fn app_and_env_and_token_and_url_options_require_event_stream_feature() {
let matches = sub_sup_run(no_feature_flags()).get_matches_from_safe(vec![
"run",
"--event-stream-application",
Expand All @@ -1458,6 +1477,8 @@ mod tests {
"MY_ENV",
"--event-stream-token",
"MY_TOKEN",
"--event-stream-url",
"127.0.0.1:4222",
]);
assert!(matches.is_err());
let error = matches.unwrap_err();
Expand All @@ -1475,6 +1496,8 @@ mod tests {
"MY_ENV",
"--event-stream-token",
"MY_TOKEN",
"--event-stream-url",
"127.0.0.1:4222",
]);
assert!(matches.is_err());
let error = matches.unwrap_err();
Expand All @@ -1493,6 +1516,8 @@ mod tests {
"MY_ENV",
"--event-stream-token",
"MY_TOKEN",
"--event-stream-url",
"127.0.0.1:4222",
]);
assert!(matches.is_err());
let error = matches.unwrap_err();
Expand All @@ -1508,6 +1533,8 @@ mod tests {
"--event-stream-environment",
"--event-stream-token",
"MY_TOKEN",
"--event-stream-url",
"127.0.0.1:4222",
]);
assert!(matches.is_err());
let error = matches.unwrap_err();
Expand All @@ -1526,6 +1553,8 @@ mod tests {
"",
"--event-stream-token",
"MY_TOKEN",
"--event-stream-url",
"127.0.0.1:4222",
]);
assert!(matches.is_err());
let error = matches.unwrap_err();
Expand All @@ -1544,6 +1573,8 @@ mod tests {
"MY_TOKEN",
"--event-stream-environment",
"MY_ENV",
"--event-stream-url",
"127.0.0.1:4222",
]);
assert!(matches.is_err());
let error = matches.unwrap_err();
Expand All @@ -1567,6 +1598,8 @@ mod tests {
"MY_ENV",
"--event-stream-token",
"MY_TOKEN",
"--event-stream-url",
"127.0.0.1:4222",
]);
assert!(matches.is_ok());
let matches = matches.unwrap();
Expand All @@ -1587,6 +1620,8 @@ mod tests {
"MY_ENV",
"--event-stream-token",
"MY_TOKEN",
"--event-stream-url",
"127.0.0.1:4222",
]);
assert!(matches.is_err());
assert_eq!(matches.unwrap_err().kind, clap::ErrorKind::EmptyValue);
Expand All @@ -1604,6 +1639,8 @@ mod tests {
"MY_ENV",
"--event-stream-token",
"MY_TOKEN",
"--event-stream-url",
"127.0.0.1:4222",
]);
assert!(matches.is_err());
assert_eq!(matches.unwrap_err().kind, clap::ErrorKind::ValueValidation);
Expand All @@ -1621,6 +1658,8 @@ mod tests {
"MY_ENV",
"--event-stream-token",
"MY_TOKEN",
"--event-stream-url",
"127.0.0.1:4222",
]);
assert!(matches.is_err());
assert_eq!(matches.unwrap_err().kind, clap::ErrorKind::ValueValidation);
Expand All @@ -1638,6 +1677,8 @@ mod tests {
"MY_ENV",
"--event-stream-token",
"MY_TOKEN",
"--event-stream-url",
"127.0.0.1:4222",
]);
assert!(matches.is_err());
assert_eq!(matches.unwrap_err().kind, clap::ErrorKind::ValueValidation);
Expand All @@ -1651,6 +1692,8 @@ mod tests {
"MY_APP",
"--event-stream-environment",
"MY_ENV",
"--event-stream-url",
"127.0.0.1:4222",
"--event-stream-token",
]);
assert!(matches.is_err());
Expand All @@ -1670,6 +1713,44 @@ mod tests {
"MY_ENV",
"--event-stream-token",
"",
"--event-stream-url",
"127.0.0.1:4222",
]);
assert!(matches.is_err());
let error = matches.unwrap_err();
assert_eq!(error.kind, clap::ErrorKind::ValueValidation);
}

#[test]
fn url_option_must_take_a_value() {
let matches = sub_sup_run(event_stream_enabled()).get_matches_from_safe(vec![
"run",
"--event-stream-application",
"MY_APP",
"--event-stream-environment",
"MY_ENV",
"--event-stream-token",
"MY_TOKEN",
"--event-stream-url",
]);
assert!(matches.is_err());
let error = matches.unwrap_err();
assert_eq!(error.kind, clap::ErrorKind::EmptyValue);
assert_eq!(error.info, Some(vec!["EVENT_STREAM_URL".to_string()]));
}

#[test]
fn url_option_cannot_be_empty() {
let matches = sub_sup_run(event_stream_enabled()).get_matches_from_safe(vec![
"run",
"--event-stream-application",
"MY_APP",
"--event-stream-environment",
"MY_ENV",
"--event-stream-token",
"MY_TOKEN",
"--event-stream-url",
"",
]);
assert!(matches.is_err());
let error = matches.unwrap_err();
Expand Down
12 changes: 8 additions & 4 deletions components/sup/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ lazy_static! {
/// static reference for access later.
pub fn init_stream(config: EventStreamConfig, event_core: EventCore) {
INIT.call_once(|| {
let conn_info = EventConnectionInfo::new(config.token);
let conn_info = EventConnectionInfo::new(config.token, config.url);
let event_stream = init_nats_stream(conn_info).expect("Could not start NATS thread");
EVENT_STREAM.set(event_stream);
EVENT_CORE.set(event_core);
Expand All @@ -88,6 +88,7 @@ pub struct EventStreamConfig {
application: String,
meta: EventStreamMetadata,
token: AutomateAuthToken,
url: String,
}

impl EventStreamConfig {
Expand All @@ -100,7 +101,10 @@ impl EventStreamConfig {
.map(str::to_string)
.expect("Required option for EventStream feature"),
meta: EventStreamMetadata::from_matches(m)?,
token: AutomateAuthToken::from_matches(m)?, })
token: AutomateAuthToken::from_matches(m)?,
url: m.value_of("EVENT_STREAM_URL")
.map(str::to_string)
.expect("Required option for EventStream feature"), })
}
}

Expand All @@ -117,10 +121,10 @@ pub struct EventConnectionInfo {
}

impl EventConnectionInfo {
pub fn new(auth_token: AutomateAuthToken) -> Self {
pub fn new(auth_token: AutomateAuthToken, cluster_uri: String) -> Self {
EventConnectionInfo { name: String::from("habitat"),
verbose: true,
cluster_uri: String::from("10.0.0.174:4222"),
cluster_uri,
cluster_id: String::from("event-service"),
auth_token }
}
Expand Down

0 comments on commit f3a4af4

Please sign in to comment.