Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): Respect owner in app.yaml during app resolution #4822

Merged
merged 2 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions lib/backend-api/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,25 @@ pub async fn tag_package_release(
.map(|r| r.tag_package_release)
}

/// Get the currently logged in used, together with all accessible namespaces.
/// Get the currently logged in user.
pub async fn current_user(client: &WasmerClient) -> Result<Option<types::User>, anyhow::Error> {
client
.run_graphql(types::GetCurrentUser::build(()))
.await
.map(|x| x.viewer)
}

/// Get the currently logged in user, together with all accessible namespaces.
///
/// You can optionally filter the namespaces by the user role.
pub async fn current_user_with_namespaces(
client: &WasmerClient,
namespace_role: Option<types::GrapheneRole>,
) -> Result<types::UserWithNamespaces, anyhow::Error> {
client
.run_graphql(types::GetCurrentUser::build(types::GetCurrentUserVars {
namespace_role,
}))
.run_graphql(types::GetCurrentUserWithNamespaces::build(
types::GetCurrentUserWithNamespacesVars { namespace_role },
))
.await?
.viewer
.context("not logged in")
Expand Down Expand Up @@ -544,9 +552,11 @@ pub async fn user_accessible_apps(

// Get all aps in user-accessible namespaces.
let namespace_res = client
.run_graphql(types::GetCurrentUser::build(types::GetCurrentUserVars {
namespace_role: None,
}))
.run_graphql(types::GetCurrentUserWithNamespaces::build(
types::GetCurrentUserWithNamespacesVars {
namespace_role: None,
},
))
.await?;
let active_user = namespace_res.viewer.context("not logged in")?;
let namespace_names = active_user
Expand Down Expand Up @@ -655,9 +665,11 @@ pub async fn user_namespaces(
client: &WasmerClient,
) -> Result<Vec<types::Namespace>, anyhow::Error> {
let user = client
.run_graphql(types::GetCurrentUser::build(types::GetCurrentUserVars {
namespace_role: None,
}))
.run_graphql(types::GetCurrentUserWithNamespaces::build(
types::GetCurrentUserWithNamespacesVars {
namespace_role: None,
},
))
.await?
.viewer
.context("not logged in")?;
Expand Down
14 changes: 10 additions & 4 deletions lib/backend-api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,20 @@ mod queries {
Viewer,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query")]
pub struct GetCurrentUser {
pub viewer: Option<User>,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct GetCurrentUserVars {
pub struct GetCurrentUserWithNamespacesVars {
pub namespace_role: Option<GrapheneRole>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "GetCurrentUserVars")]
pub struct GetCurrentUser {
#[cynic(graphql_type = "Query", variables = "GetCurrentUserWithNamespacesVars")]
pub struct GetCurrentUserWithNamespaces {
pub viewer: Option<UserWithNamespaces>,
}

Expand Down Expand Up @@ -447,7 +453,7 @@ mod queries {
}

#[derive(cynic::QueryFragment, Debug, Clone, Serialize)]
#[cynic(graphql_type = "User", variables = "GetCurrentUserVars")]
#[cynic(graphql_type = "User", variables = "GetCurrentUserWithNamespacesVars")]
pub struct UserWithNamespaces {
pub id: cynic::Id,
pub username: String,
Expand Down
25 changes: 18 additions & 7 deletions lib/cli/src/commands/app/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub enum AppIdent {
/// Backend app VERSION id like "dav_xxysw34234"
AppVersionId(String),
NamespacedName(String, String),
Alias(String),
Name(String),
}

impl AppIdent {
Expand All @@ -40,9 +40,18 @@ impl AppIdent {
.with_context(|| format!("Could not query for app version id '{}'", id))?;
Ok(app)
}
AppIdent::Alias(name) => wasmer_api::query::get_app_by_alias(client, name.clone())
.await?
.with_context(|| format!("Could not find app with name '{name}'")),
AppIdent::Name(name) => {
// The API only allows to query by owner + name,
// so default to the current user as the owner.
// To to so the username must first be retrieved.
let user = wasmer_api::query::current_user(client)
.await?
.context("not logged in")?;

wasmer_api::query::get_app(client, user.username, name.clone())
.await?
.with_context(|| format!("Could not find app with name '{name}'"))
}
AppIdent::NamespacedName(owner, name) => {
wasmer_api::query::get_app(client, owner.clone(), name.clone())
.await?
Expand Down Expand Up @@ -80,7 +89,7 @@ impl std::str::FromStr for AppIdent {
}
}
} else {
Ok(Self::Alias(s.to_string()))
Ok(Self::Name(s.to_string()))
}
}
}
Expand Down Expand Up @@ -160,8 +169,10 @@ impl AppIdentOpts {

let ident = if let Some(id) = &config.app_id {
AppIdent::AppId(id.clone())
} else if let Some(owner) = &config.owner {
AppIdent::NamespacedName(owner.clone(), config.name.clone())
} else {
AppIdent::Alias(config.name.clone())
AppIdent::Name(config.name.clone())
};

Ok(ResolvedAppIdent::Config {
Expand Down Expand Up @@ -197,7 +208,7 @@ mod tests {
);
assert_eq!(
AppIdent::from_str("lala").unwrap(),
AppIdent::Alias("lala".to_string()),
AppIdent::Name("lala".to_string()),
);

assert_eq!(
Expand Down
Loading