Skip to content

Commit

Permalink
Add restart support for dash cli
Browse files Browse the repository at this point in the history
  • Loading branch information
HoKim98 committed May 18, 2023
1 parent d214e52 commit 56789a2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 31 deletions.
27 changes: 17 additions & 10 deletions dash/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ enum Commands {
Create(CommandSession),
Delete(CommandSession),
Exists(CommandSession),
Restart(CommandSession),
}

impl Commands {
Expand All @@ -72,15 +73,16 @@ impl Commands {
};

match self {
Self::Create(command) => command.create(kube).await,
Self::Delete(command) => command.delete(kube).await,
Self::Exists(command) => command.exists(kube).await,
Self::Create(command) => command.create(kube).await.into(),
Self::Delete(command) => command.delete(kube).await.into(),
Self::Exists(command) => command.exists(kube).await.into(),
Self::Restart(command) => command.restart(kube).await.into(),
}
}
}

/// Create a resource from a file or from stdin.
#[derive(Parser)]
#[derive(Clone, Parser)]
struct CommandSession {
/// Set a function name
#[arg(short, long, env = "DASH_FUNCTION", value_name = "NAME")]
Expand All @@ -96,10 +98,10 @@ struct CommandSession {
}

impl CommandSession {
async fn run<F, Fut, R>(self, kube: Client, f: F) -> SessionResult<Value>
async fn run<F, Fut, R>(self, kube: Client, f: F) -> Result<Value>
where
F: FnOnce(Client, SessionContextMetadata, Vec<InputFieldString>) -> Fut,
Fut: Future<Output = SessionResult<R>>,
Fut: Future<Output = Result<R>>,
R: Serialize,
{
let metadata = SessionContextMetadata {
Expand All @@ -110,29 +112,34 @@ impl CommandSession {
};
f(kube, metadata, self.inputs)
.await
.and_then(::serde_json::to_value)
.and_then(|value| ::serde_json::to_value(value).map_err(Into::into))
}

async fn create(self, kube: Client) -> SessionResult<Value> {
async fn create(self, kube: Client) -> Result<Value> {
self.run(kube, |kube, metadata, inputs| async move {
FunctionSession::create_raw(kube, &metadata, inputs).await
})
.await
}

async fn delete(self, kube: Client) -> SessionResult<Value> {
async fn delete(self, kube: Client) -> Result<Value> {
self.run(kube, |kube, metadata, inputs| async move {
FunctionSession::create_raw(kube, &metadata, inputs).await
})
.await
}

async fn exists(self, kube: Client) -> SessionResult<Value> {
async fn exists(self, kube: Client) -> Result<Value> {
self.run(kube, |kube, metadata, inputs| async move {
FunctionSession::exists_raw(kube, &metadata, inputs).await
})
.await
}

async fn restart(self, kube: Client) -> Result<Value> {
self.clone().delete(kube.clone()).await?;
self.create(kube).await
}
}

#[tokio::main]
Expand Down
2 changes: 1 addition & 1 deletion dash/gateway/src/routes/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ pub async fn post(kube: Data<Client>, name: Path<Name>, value: Json<Value>) -> i
}];

let result = FunctionSession::create_raw(kube, &metadata, inputs).await;
HttpResponse::from(result)
HttpResponse::from(SessionResult::from(result))
}
13 changes: 0 additions & 13 deletions dash/provider/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@ where
}
}

impl<T> SessionResult<T> {
pub fn and_then<F, T2, E>(self, f: F) -> SessionResult<T2>
where
F: FnOnce(T) -> Result<T2, E>,
E: ToString,
{
match self {
Self::Ok(e) => f(e).into(),
Self::Err(e) => SessionResult::Err(e),
}
}
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FunctionChannel {
Expand Down
11 changes: 4 additions & 7 deletions dash/provider/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{anyhow, Result};
use async_trait::async_trait;
use dash_api::function::FunctionActorSpec;
use dash_provider_api::{
FunctionChannel, FunctionChannelKind, SessionContext, SessionContextMetadata, SessionResult,
FunctionChannel, FunctionChannelKind, SessionContext, SessionContextMetadata,
};
use futures::TryFutureExt;
use kube::Client;
Expand Down Expand Up @@ -100,14 +100,13 @@ impl<'a> FunctionSession<'a> {
kube: Client,
metadata: &'a SessionContextMetadata,
inputs: Vec<InputField<Value>>,
) -> SessionResult<bool>
) -> Result<bool>
where
Self: FunctionSessionUpdateFields<Value>,
{
Self::load(kube, metadata)
.and_then(|session| session.try_exists_raw(inputs))
.await
.into()
}

async fn try_exists_raw<Value>(mut self, inputs: Vec<InputField<Value>>) -> Result<bool>
Expand All @@ -132,14 +131,13 @@ impl<'a> FunctionSession<'a> {
kube: Client,
metadata: &'a SessionContextMetadata,
inputs: Vec<InputField<Value>>,
) -> SessionResult
) -> Result<FunctionChannel>
where
Self: FunctionSessionUpdateFields<Value>,
{
Self::load(kube, metadata)
.and_then(|session| session.try_create_raw(inputs))
.await
.into()
}

async fn try_create_raw<Value>(
Expand Down Expand Up @@ -167,14 +165,13 @@ impl<'a> FunctionSession<'a> {
kube: Client,
metadata: &'a SessionContextMetadata,
inputs: Vec<InputField<Value>>,
) -> SessionResult
) -> Result<FunctionChannel>
where
Self: FunctionSessionUpdateFields<Value>,
{
Self::load(kube, metadata)
.and_then(|session| session.try_delete_raw(inputs))
.await
.into()
}

async fn try_delete_raw<Value>(
Expand Down

0 comments on commit 56789a2

Please sign in to comment.