use crate::Bytes;
use crate::{
Client, Error, EventStream, ListFilter, ListOptions, Page, Reference, Result, StreamOptions,
WaitOptions, WorkspaceOptions, meta, proto,
};
use futures_util::StreamExt;
use std::{
collections::BTreeMap,
sync::{Arc, RwLock},
time::SystemTime,
};
pub use proto::workspace::status::State;
#[derive(Clone, Copy, Debug)]
pub enum SharingMode {
Members,
All,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum WorkspaceEvent {
Created(Workspace),
Updated {
workspace: Workspace,
previous: Option<Box<proto::Workspace>>,
},
Deleted(Workspace),
}
impl WorkspaceEvent {
pub fn workspace(&self) -> &Workspace {
match self {
Self::Created(w) | Self::Deleted(w) => w,
Self::Updated { workspace, .. } => workspace,
}
}
pub fn state_changed(&self) -> bool {
match self {
Self::Updated {
workspace,
previous: Some(previous),
} => {
previous
.status
.as_ref()
.map(|s| s.state())
.unwrap_or(State::Unknown)
!= workspace.state()
}
_ => true,
}
}
fn from_message(
client: &Client,
message: proto::WatchWorkspaceResponse,
) -> Result<Option<Self>> {
use proto::watch_workspace_response::Type;
Ok(match message.r#type {
Some(Type::Create(e)) => e
.item
.map(|w| Workspace::new(client.clone(), w))
.transpose()?
.map(Self::Created),
Some(Type::Update(e)) => match e.new_item {
Some(w) => Some(Self::Updated {
workspace: Workspace::new(client.clone(), w)?,
previous: e.old_item.map(Box::new),
}),
None => None,
},
Some(Type::Delete(e)) => e
.item
.map(|w| Workspace::new(client.clone(), w))
.transpose()?
.map(Self::Deleted),
None => None,
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum LogStage {
Unknown,
CloningRepo,
PullingImage,
BuildingImage,
Task,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LogStream {
Stdout,
Stderr,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct LogEntry {
pub at: Option<SystemTime>,
pub stage: LogStage,
pub stream: LogStream,
pub data: Bytes,
}
impl From<proto::ListenLogResponse> for LogEntry {
fn from(message: proto::ListenLogResponse) -> Self {
use proto::listen_log_response::{Mode, Type};
Self {
at: message
.created_at
.and_then(|t| SystemTime::try_from(t).ok()),
stage: match message.r#type() {
Type::CloningRepo => LogStage::CloningRepo,
Type::PullingImage => LogStage::PullingImage,
Type::BuildingImage => LogStage::BuildingImage,
Type::Task => LogStage::Task,
_ => LogStage::Unknown,
},
stream: if message.mode() == Mode::Stderr {
LogStream::Stderr
} else {
LogStream::Stdout
},
data: message.data,
}
}
}
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct StartOptions {
pub variables: BTreeMap<String, String>,
pub region: Option<Reference>,
}
impl StartOptions {
pub fn variable(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.variables.insert(name.into(), value.into());
self
}
pub fn region(mut self, v: impl Into<Reference>) -> Self {
self.region = Some(v.into());
self
}
pub(crate) fn config(&self) -> Result<proto::start_workspace_request::Config> {
for name in self.variables.keys() {
crate::error::nonempty(name, "run variable name")?;
}
Ok(proto::start_workspace_request::Config {
vars: self
.variables
.iter()
.map(|(name, value)| proto::workspace::spec::Var {
name: name.clone(),
value: value.clone(),
})
.collect(),
region_ref: self.region.as_ref().map(Reference::object).transpose()?,
})
}
}
#[derive(Clone, Debug)]
pub struct Workspaces {
client: Client,
}
impl Workspaces {
pub(crate) fn new(client: Client) -> Self {
Self { client }
}
pub async fn create(&self, mut options: WorkspaceOptions) -> Result<Workspace> {
options.validate()?;
if !options.git_provider.is_empty() {
return Err(Error::InvalidArgument(
"GitProvider is configured on Templates".into(),
));
}
let resource = proto::Workspace {
metadata: Some(meta::Metadata {
display_name: options.display_name,
..Default::default()
}),
spec: Some(options.spec),
status: Some(proto::workspace::Status {
template_ref: options
.template
.as_ref()
.map(Reference::object)
.transpose()?,
workspace_snapshot_ref: options
.snapshot
.as_ref()
.map(Reference::object)
.transpose()?,
..Default::default()
}),
..Default::default()
};
let resource = self
.client
.rpc(self.client.main_service().create_workspace(resource))
.await?;
Workspace::new(self.client.clone(), resource)
}
pub async fn run(&self, options: WorkspaceOptions) -> Result<Workspace> {
self.run_with(options, StartOptions::default(), WaitOptions::default())
.await
}
pub async fn run_with(
&self,
mut options: WorkspaceOptions,
start: StartOptions,
wait: WaitOptions,
) -> Result<Workspace> {
let wait = wait.validate()?;
options.validate()?;
start.config()?;
let deadline = wait
.timeout
.map(|t| {
tokio::time::Instant::now()
.checked_add(t)
.ok_or_else(|| Error::InvalidArgument("timeout is too large".into()))
})
.transpose()?;
let ws = self
.client
.operation_until(deadline, self.create(options))
.await?;
self.client
.operation_until(deadline, async {
ws.start(start).await?;
ws.wait_loop(WaitTarget::Running, wait).await
})
.await
.map_err(|source| Error::RunFailed {
workspace: Box::new(ws.clone()),
source: Box::new(source),
})?;
Ok(ws)
}
pub async fn get(&self, reference: impl Into<Reference>) -> Result<Workspace> {
let r = reference.into().get()?;
Workspace::new(
self.client.clone(),
self.client
.rpc(self.client.main_service().get_workspace(r))
.await?,
)
}
pub async fn update(&self, resource: proto::Workspace) -> Result<Workspace> {
let r = self
.client
.rpc(self.client.main_service().update_workspace(resource))
.await?;
Workspace::new(self.client.clone(), r)
}
pub async fn delete(&self, reference: impl Into<Reference>) -> Result<()> {
self.client
.rpc(
self.client
.main_service()
.delete_workspace(reference.into().delete()?),
)
.await?;
Ok(())
}
pub async fn list(&self, options: ListOptions) -> Result<Page<Workspace>> {
let filter = match &options.filter {
None => None,
Some(ListFilter::Space(r)) => {
Some(proto::list_workspace_options::Filter::SpaceRef(r.object()?))
}
Some(ListFilter::Template(r)) => Some(
proto::list_workspace_options::Filter::TemplateRef(r.object()?),
),
_ => {
return Err(Error::InvalidArgument(
"Workspaces support Space or Template filters".into(),
));
}
};
let page = self
.client
.rpc(
self.client
.main_service()
.list_workspace(proto::ListWorkspaceOptions {
common: Some(options.common()),
filter,
}),
)
.await?;
Ok(Page::new(
page.items
.into_iter()
.map(|r| Workspace::new(self.client.clone(), r))
.collect::<Result<_>>()?,
page.list_response_meta,
))
}
pub fn all(&self, mut options: ListOptions) -> EventStream<Workspace> {
let this = self.clone();
EventStream::new(
async_stream::try_stream! {loop{let page=this.list(options.clone()).await?;let empty=page.items.is_empty();for item in page.items{yield item;}if !options.next(page.info,empty)?{break;}}},
)
}
pub async fn watch(
&self,
reference: Option<Reference>,
options: StreamOptions,
) -> Result<EventStream<WorkspaceEvent>> {
let deadline = options.deadline()?;
let request = proto::WatchWorkspaceRequest {
workspace_ref: reference.as_ref().map(Reference::object).transpose()?,
};
let rpc = self
.client
.operation_until(deadline, async {
self.client
.rpc(self.client.main_service().watch_workspace(request))
.await
})
.await?;
let client = self.client.clone();
Ok(EventStream::new(
crate::stream::rpc_stream(self.client.clone(), rpc, deadline).filter_map(move |item| {
std::future::ready(match item {
Ok(message) => WorkspaceEvent::from_message(&client, message).transpose(),
Err(e) => Some(Err(e)),
})
}),
))
}
}
#[derive(Clone, Debug)]
pub struct Workspace {
pub(crate) client: Client,
resource: Arc<RwLock<proto::Workspace>>,
reference: Reference,
}
#[derive(Clone, Copy)]
enum WaitTarget {
Ready,
Running,
Stopped,
}
impl Workspace {
pub(crate) fn new(client: Client, resource: proto::Workspace) -> Result<Self> {
let md = resource
.metadata
.as_ref()
.ok_or_else(|| Error::Protocol("Workspace metadata is missing".into()))?;
let reference = if !md.uid.is_empty() {
Reference::uid(md.uid.clone())
} else {
Reference::name(md.name.clone())
};
reference.object()?;
Ok(Self {
client,
resource: Arc::new(RwLock::new(resource)),
reference,
})
}
fn read<T>(&self, f: impl FnOnce(&proto::Workspace) -> T) -> T {
f(&self.resource.read().unwrap_or_else(|e| e.into_inner()))
}
pub fn proto(&self) -> proto::Workspace {
self.resource
.read()
.unwrap_or_else(|e| e.into_inner())
.clone()
}
pub fn name(&self) -> String {
self.read(|w| {
w.metadata
.as_ref()
.map(|m| m.name.clone())
.unwrap_or_default()
})
}
pub fn uid(&self) -> String {
self.read(|w| {
w.metadata
.as_ref()
.map(|m| m.uid.clone())
.unwrap_or_default()
})
}
pub fn display_name(&self) -> String {
self.read(|w| {
w.metadata
.as_ref()
.map(|m| m.display_name.clone())
.unwrap_or_default()
})
}
pub fn created_at(&self) -> Option<crate::prost_types::Timestamp> {
self.read(|w| w.metadata.as_ref().and_then(|m| m.created_at))
}
pub fn state(&self) -> State {
self.read(|w| {
w.status
.as_ref()
.map(|s| s.state())
.unwrap_or(State::Unknown)
})
}
pub fn is_ready(&self) -> bool {
matches!(self.state(), State::Preparing | State::Running)
}
pub fn is_running(&self) -> bool {
self.state() == State::Running
}
pub fn is_starting(&self) -> bool {
matches!(
self.state(),
State::InitRequest
| State::Initializing
| State::PullingImage
| State::BuildingImage
| State::StartingRuntime
)
}
pub fn is_stopping(&self) -> bool {
matches!(self.state(), State::StoppingRequest | State::Stopping)
}
pub fn is_stopped(&self) -> bool {
self.state() == State::Stopped
}
pub fn hostname(&self) -> Option<String> {
let h = self.read(|w| {
w.status
.as_ref()
.map(|s| s.hostname.clone())
.unwrap_or_default()
});
(!h.is_empty()).then_some(h)
}
pub fn url(&self) -> Option<String> {
self.hostname().map(|h| format!("https://{h}"))
}
pub fn app_url(&self, name: &str) -> Result<Option<String>> {
crate::spec::validate_label(name)?;
let Some(h) = self.hostname() else {
return Ok(None);
};
let default = self
.applications()
.iter()
.any(|a| a.name == name && a.is_default);
Ok(Some(if default {
format!("https://{h}")
} else {
format!("https://{name}_{h}")
}))
}
pub fn port_url(&self, port: u16) -> Result<Option<String>> {
if port == 0 {
return Err(Error::InvalidArgument("port must be nonzero".into()));
}
Ok(self.hostname().map(|h| format!("https://port_{port}_{h}")))
}
pub fn spec(&self) -> proto::workspace::Spec {
self.read(|w| w.spec.clone().unwrap_or_default())
}
pub fn status(&self) -> proto::workspace::Status {
self.read(|w| w.status.clone().unwrap_or_default())
}
pub fn applications(&self) -> Vec<proto::workspace::spec::Application> {
self.read(|w| {
w.spec
.as_ref()
.map(|s| s.applications.clone())
.unwrap_or_default()
})
}
pub fn is_ephemeral(&self) -> bool {
self.read(|w| w.spec.as_ref().is_some_and(|s| s.is_ephemeral))
}
pub fn reference(&self) -> Reference {
self.reference.clone()
}
pub(crate) fn object(&self) -> Result<meta::ObjectReference> {
self.reference.object()
}
fn replace(&self, resource: proto::Workspace) {
*self.resource.write().unwrap_or_else(|e| e.into_inner()) = resource;
}
pub async fn refresh(&self) -> Result<()> {
let r = self
.client
.rpc(
self.client
.main_service()
.get_workspace(self.reference.get()?),
)
.await?;
self.replace(r);
Ok(())
}
pub async fn start(&self, options: StartOptions) -> Result<()> {
self.client
.operation(self.client.default_timeout(), async {
let config = options.config()?;
let started =
self.client
.rpc(self.client.main_service().start_workspace(
proto::StartWorkspaceRequest {
workspace_ref: Some(self.object()?),
config: Some(config),
},
))
.await;
match started {
Err(e) if e.code() != Some(tonic::Code::AlreadyExists) => return Err(e),
_ => {}
}
self.refresh().await
})
.await
}
pub async fn stop(&self) -> Result<()> {
self.client
.operation(self.client.default_timeout(), async {
self.client
.rpc(
self.client
.main_service()
.stop_workspace(proto::StopWorkspaceRequest {
workspace_ref: Some(self.object()?),
}),
)
.await?;
self.refresh().await
})
.await
}
pub async fn delete(&self) -> Result<()> {
self.client.workspaces().delete(&self.reference).await
}
pub async fn modify<F>(&self, edit: F) -> Result<()>
where
F: FnOnce(&mut proto::Workspace) -> Result<()> + Send,
{
self.client
.operation(self.client.default_timeout(), async {
self.refresh().await?;
let mut resource = self.proto();
edit(&mut resource)?;
let r = self
.client
.rpc(self.client.main_service().update_workspace(resource))
.await?;
self.replace(r);
Ok(())
})
.await
}
pub async fn wait_ready(&self, options: WaitOptions) -> Result<()> {
self.wait(WaitTarget::Ready, options).await
}
pub async fn wait_running(&self, options: WaitOptions) -> Result<()> {
self.wait(WaitTarget::Running, options).await
}
pub async fn wait_stopped(&self, options: WaitOptions) -> Result<()> {
self.wait(WaitTarget::Stopped, options).await
}
async fn wait(&self, target: WaitTarget, options: WaitOptions) -> Result<()> {
let options = options.validate()?;
self.client
.operation(options.timeout, self.wait_loop(target, options))
.await
}
async fn wait_loop(&self, target: WaitTarget, options: WaitOptions) -> Result<()> {
loop {
self.refresh().await?;
let ws = self.proto();
let s = ws.status.clone().unwrap_or_default();
let done = match target {
WaitTarget::Ready => matches!(s.state(), State::Preparing | State::Running),
WaitTarget::Running => s.state() == State::Running,
WaitTarget::Stopped => s.state() == State::Stopped,
};
let failure = match &s.run {
Some(run) => run.failure.as_ref(),
None => s.failure.as_ref(),
};
if done && (failure.is_none() || !matches!(target, WaitTarget::Stopped)) {
return Ok(());
}
if done
|| (!matches!(target, WaitTarget::Stopped)
&& (failure.is_some()
|| matches!(
s.state(),
State::StoppingRequest | State::Stopping | State::Stopped
)))
{
return Err(Error::WorkspaceFailed {
message: failure
.map(|f| f.message.clone())
.unwrap_or_else(|| format!("workspace is {}", s.state().as_str_name())),
workspace: Box::new(ws),
});
}
tokio::time::sleep(options.poll_interval).await;
}
}
pub async fn snapshot(&self, name: impl Into<String>) -> Result<proto::WorkspaceSnapshot> {
self.client.snapshots().create(name, &self.reference).await
}
pub async fn share_port(&self, name: &str, mode: SharingMode) -> Result<()> {
self.client
.operation(self.client.default_timeout(), async {
crate::spec::validate_label(name)?;
self.client
.rpc(self.client.main_service().share_workspace_port(
proto::ShareWorkspacePortRequest {
workspace_ref: Some(self.object()?),
application_name: name.into(),
mode: match mode {
SharingMode::Members => 1,
SharingMode::All => 2,
},
},
))
.await?;
self.refresh().await
})
.await
}
pub async fn unshare_port(&self, name: &str) -> Result<()> {
self.client
.operation(self.client.default_timeout(), async {
crate::spec::validate_label(name)?;
self.client
.rpc(self.client.main_service().unshare_workspace_port(
proto::UnshareWorkspacePortRequest {
workspace_ref: Some(self.object()?),
application_name: name.into(),
},
))
.await?;
self.refresh().await
})
.await
}
pub async fn watch(&self, options: StreamOptions) -> Result<EventStream<WorkspaceEvent>> {
self.client
.workspaces()
.watch(Some(self.reference()), options)
.await
}
pub async fn logs(&self, options: StreamOptions) -> Result<EventStream<LogEntry>> {
let deadline = options.deadline()?;
let request = proto::ListenLogRequest {
workspace_ref: Some(self.object()?),
};
let rpc = self
.client
.operation_until(deadline, async {
self.client
.rpc(self.client.workspace_service().listen_log(request))
.await
})
.await?;
Ok(EventStream::new(
crate::stream::rpc_stream(self.client.clone(), rpc, deadline)
.map(|item| item.map(LogEntry::from)),
))
}
pub fn exec(&self, command: impl Into<crate::Command>) -> crate::ExecBuilder {
crate::ExecBuilder::new(self.clone(), command.into())
}
pub fn files(&self) -> crate::Files {
crate::Files::new(self.clone())
}
pub fn terminals(&self) -> crate::Terminals {
crate::Terminals::new(self.clone())
}
}