Skip to content

Commit

Permalink
Merge pull request #6568 from habitat-sh/afiune/add-site-tag-to-event…
Browse files Browse the repository at this point in the history
…-metadata

Implement Site field in EventMetadata message
  • Loading branch information
Salim Afiune authored May 17, 2019
2 parents a3fa61a + 3835fbf commit fc8f02f
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 72 deletions.
167 changes: 95 additions & 72 deletions components/hab/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,7 @@ fn arg_cache_key_path(help_text: &'static str) -> Arg<'static, 'static> {
}

fn arg_target() -> Arg<'static, 'static> {
Arg::with_name("PKG_TARGET").required(false)
.takes_value(true)
Arg::with_name("PKG_TARGET").takes_value(true)
.validator(valid_target)
.env(PACKAGE_TARGET_ENVVAR)
.help("A package target (ex: x86_64-windows) (default: system \
Expand Down Expand Up @@ -1055,7 +1054,11 @@ pub fn sub_sup_run(feature_flags: FeatureFlag) -> App<'static, 'static> {
"The interval (seconds) on which to run health checks [default: 30]")
);

maybe_add_event_stream_options(sub, feature_flags)
if feature_flags.contains(FeatureFlag::EVENT_STREAM) {
add_event_stream_options(sub)
} else {
sub
}
}

pub fn sub_sup_sh() -> App<'static, 'static> {
Expand Down Expand Up @@ -1173,75 +1176,55 @@ fn sub_svc_unload(feature_flags: FeatureFlag) -> App<'static, 'static> {
//
// For now, though, these at least provide a place to supply the
// information; we can revise as we go.
fn maybe_add_event_stream_options(mut app: App<'static, 'static>,
feature_flags: FeatureFlag)
-> App<'static, 'static> {
if feature_flags.contains(FeatureFlag::EVENT_STREAM) {
app = app.arg(Arg::with_name("EVENT_STREAM_APPLICATION").help("The name of the \
application for event \
stream purposes. This is \
distinct from the \
`--application` flag \
(which may be going \
away), and will be \
attached to all events \
generated by this \
Supervisor.")
.long("event-stream-application")
.required(true)
.takes_value(true)
.validator(non_empty));
app = app.arg(Arg::with_name("EVENT_STREAM_ENVIRONMENT").help("The name of the \
environment for event \
stream purposes. This is \
distinct from the \
`--environment` flag \
(which may be going \
away), and will be \
attached to all events \
generated by this \
Supervisor.")
.long("event-stream-environment")
.required(true)
.takes_value(true)
.validator(non_empty));
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
fn add_event_stream_options(app: App<'static, 'static>) -> App<'static, 'static> {
app.arg(Arg::with_name("EVENT_STREAM_APPLICATION").help("The name of the application for \
event stream purposes. This is \
distinct from the `--application` \
flag (which may be going away), and \
will be attached to all events \
generated by this Supervisor.")
.long("event-stream-application")
.required(true)
.takes_value(true)
.validator(non_empty))
.arg(Arg::with_name("EVENT_STREAM_ENVIRONMENT").help("The name of the environment for \
event stream purposes. This is \
distinct from the `--environment` \
flag (which may be going away), and \
will be attached to all events \
generated by this Supervisor.")
.long("event-stream-environment")
.required(true)
.takes_value(true)
.validator(non_empty))
.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))
.arg(Arg::with_name("EVENT_STREAM_SITE").help("The name of the site where this Supervisor \
is running. It is used for event stream \
purposes.")
.long("event-stream-site")
.takes_value(true)
.validator(non_empty))
.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))
.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))
}

// CLAP Validation Functions
Expand Down Expand Up @@ -1755,6 +1738,46 @@ mod tests {
assert_eq!(error.kind, clap::ErrorKind::ValueValidation);
}

#[test]
fn site_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",
"127.0.0.1:4222",
"--event-stream-site",
]);
assert!(matches.is_err());
let error = matches.unwrap_err();
assert_eq!(error.kind, clap::ErrorKind::EmptyValue);
assert_eq!(error.info, Some(vec!["EVENT_STREAM_SITE".to_string()]));
}

#[test]
fn site_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",
"127.0.0.1:4222",
"--event-stream-site",
"",
]);
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![
Expand Down
1 change: 1 addition & 0 deletions components/sup/protocols/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ message EventMetadata {
string environment = 5;
map<string, string> meta = 6;
string fqdn = 7;
string site = 8;
}

message ServiceMetadata {
Expand Down
4 changes: 4 additions & 0 deletions components/sup/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub fn init_stream(config: EventStreamConfig, event_core: EventCore) -> Result<(
pub struct EventStreamConfig {
environment: String,
application: String,
site: Option<String>,
meta: EventStreamMetadata,
token: AutomateAuthToken,
url: String,
Expand All @@ -111,6 +112,7 @@ impl<'a> From<&'a ArgMatches<'a>> for EventStreamConfig {
application: m.value_of("EVENT_STREAM_APPLICATION")
.map(str::to_string)
.expect("Required option for EventStream feature"),
site: m.value_of("EVENT_STREAM_SITE").map(str::to_string),
meta: EventStreamMetadata::from(m),
token: AutomateAuthToken::from(m),
url: m.value_of("EVENT_STREAM_URL")
Expand Down Expand Up @@ -156,6 +158,7 @@ pub struct EventCore {
fqdn: String,
application: String,
environment: String,
site: Option<String>,
meta: EventStreamMetadata,
}

Expand All @@ -166,6 +169,7 @@ impl EventCore {
fqdn,
environment: config.environment.clone(),
application: config.application.clone(),
site: config.site.clone(),
meta: config.meta.clone() }
}
}
Expand Down
1 change: 1 addition & 0 deletions components/sup/src/event/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl EventCore {
fqdn: self.fqdn.clone(),
application: self.application.clone(),
environment: self.environment.clone(),
site: self.site.clone().unwrap_or_default(),
occurred_at: None,
meta: self.meta.clone().into(), }
}
Expand Down

0 comments on commit fc8f02f

Please sign in to comment.