Skip to content

Commit

Permalink
fix(dashboard,cli): deploy to the selected organization id (#983)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiiBz authored Jun 22, 2023
1 parent 7cf6208 commit b2c1129
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .changeset/curvy-pumas-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@lagon/dashboard': patch
'@lagon/cli': patch
---

Deploy to the specified organization instead of the current selected one
4 changes: 3 additions & 1 deletion crates/cli/src/commands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ pub async fn deploy(
);
println!();

let trpc_client = TrpcClient::new(config.clone());
let mut trpc_client = TrpcClient::new(config.clone());
trpc_client.set_organization_id(function_config.organization_id.clone());

let response = trpc_client
.query::<(), OrganizationsResponse>("organizationsList", None)
.await?;
Expand Down
4 changes: 3 additions & 1 deletion crates/cli/src/commands/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ pub async fn link(directory: Option<PathBuf>) -> Result<()> {
match !function_config.function_id.is_empty() {
true => Err(anyhow!("This directory is already linked to a Function")),
false => {
let trpc_client = TrpcClient::new(config);
let mut trpc_client = TrpcClient::new(config);
trpc_client.set_organization_id(function_config.organization_id.clone());

let response = trpc_client
.query::<(), OrganizationsResponse>("organizationsList", None)
.await?;
Expand Down
1 change: 1 addition & 0 deletions crates/cli/src/commands/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub async fn ls(directory: Option<PathBuf>) -> Result<()> {
let end_progress = print_progress("Fetching Deployments");

let function = TrpcClient::new(config)
.set_organization_id(function_config.organization_id.clone())
.query::<FunctionRequest, FunctionResponse>(
"functionGet",
Some(FunctionRequest {
Expand Down
1 change: 1 addition & 0 deletions crates/cli/src/commands/promote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub async fn promote(deployment_id: String, directory: Option<PathBuf>) -> Resul
println!();
let end_progress = print_progress("Promoting Deployment");
TrpcClient::new(config)
.set_organization_id(function_config.organization_id.clone())
.mutation::<PromoteDeploymentRequest, PromoteDeploymentResponse>(
"deploymentPromote",
PromoteDeploymentRequest {
Expand Down
1 change: 1 addition & 0 deletions crates/cli/src/commands/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub async fn rm(directory: Option<PathBuf>) -> Result<()> {
true => {
let end_progress = print_progress("Deleting Function");
TrpcClient::new(config)
.set_organization_id(function_config.organization_id.clone())
.mutation::<DeleteFunctionRequest, DeleteFunctionResponse>(
"functionDelete",
DeleteFunctionRequest {
Expand Down
1 change: 1 addition & 0 deletions crates/cli/src/commands/undeploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub async fn undeploy(deployment_id: String, directory: Option<PathBuf>) -> Resu
true => {
let end_progress = print_progress("Deleting Deployment");
TrpcClient::new(config)
.set_organization_id(function_config.organization_id.clone())
.mutation::<UndeployDeploymentRequest, UndeployDeploymentResponse>(
"deploymentUndeploy",
UndeployDeploymentRequest {
Expand Down
8 changes: 6 additions & 2 deletions crates/cli/src/utils/deployments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,11 @@ pub async fn create_deployment(

let end_progress = print_progress("Creating Deployment");

let trpc_client = Arc::new(TrpcClient::new(config));
let mut trpc_client = TrpcClient::new(config);
trpc_client.set_organization_id(function_config.organization_id.clone());

let trpc_client = Arc::new(trpc_client);

let response = trpc_client
.mutation::<CreateDeploymentRequest, CreateDeploymentResponse>(
"deploymentCreate",
Expand Down Expand Up @@ -440,7 +444,7 @@ pub async fn create_deployment(
.get(&asset)
.unwrap_or_else(|| panic!("Couldn't find asset {asset}"));

join_set.spawn(upload_asset(trpc_client.clone(), asset.clone(), url));
join_set.spawn(upload_asset(Arc::clone(&trpc_client), asset.clone(), url));
}

while let Some(res) = join_set.join_next().await {
Expand Down
35 changes: 25 additions & 10 deletions crates/cli/src/utils/trpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,23 @@ pub struct TrpcErrorResult {
pub struct TrpcClient {
pub client: Client,
config: Config,
organization_id: Option<String>,
}

impl TrpcClient {
pub fn new(config: Config) -> Self {
let client = ClientBuilder::new().use_rustls_tls().build().unwrap();

Self { client, config }
Self {
client,
config,
organization_id: None,
}
}

pub fn set_organization_id(&mut self, organization_id: String) -> &mut Self {
self.organization_id = Some(organization_id);
self
}

pub async fn query<T, R>(&self, key: &str, body: Option<T>) -> Result<TrpcResponse<R>>
Expand All @@ -46,17 +56,20 @@ impl TrpcClient {
None => String::new(),
};

let response = self
let mut builder = self
.client
.request(
"GET".parse()?,
format!("{}/api/trpc/{}{}", self.config.site_url.clone(), key, input,),
)
.header("content-type", "application/json")
.header("x-lagon-token", self.config.token.as_ref().unwrap())
.send()
.await?;
.header("x-lagon-token", self.config.token.as_ref().unwrap());

if let Some(organization_id) = &self.organization_id {
builder = builder.header("x-lagon-organization-id", organization_id);
}

let response = builder.send().await?;
let body = response.text().await?;

match serde_json::from_str::<TrpcResponse<R>>(&body) {
Expand All @@ -75,18 +88,20 @@ impl TrpcClient {
{
let body = serde_json::to_string(&body)?;

let response = self
let mut builder = self
.client
.request(
"POST".parse()?,
format!("{}/api/trpc/{}", self.config.site_url.clone(), key),
)
.header("content-type", "application/json")
.header("x-lagon-token", self.config.token.as_ref().unwrap())
.body(body)
.send()
.await?;
.header("x-lagon-token", self.config.token.as_ref().unwrap());

if let Some(organization_id) = &self.organization_id {
builder = builder.header("x-lagon-organization-id", organization_id);
}

let response = builder.body(body).send().await?;
let body = response.text().await?;

match serde_json::from_str::<TrpcResponse<R>>(&body) {
Expand Down
3 changes: 2 additions & 1 deletion packages/dashboard/pages/api/trpc/[trpc].ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const createContext = async ({
session: Session;
}> => {
const tokenValue = req.headers['x-lagon-token'] as string;
const organizationId = req.headers['x-lagon-organization-id'] as string | undefined;

// tokensAuthenticate needs to skip authentication
if (req.query.trpc === 'tokensAuthenticate') {
Expand Down Expand Up @@ -58,7 +59,7 @@ const createContext = async ({

const organization = await prisma.organization.findFirst({
where: {
id: token.user.currentOrganizationId ?? '',
id: organizationId ?? token.user.currentOrganizationId ?? '',
},
select: {
id: true,
Expand Down

1 comment on commit b2c1129

@vercel
Copy link

@vercel vercel bot commented on b2c1129 Jun 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

dashboard – ./packages/dashboard

dashboard-git-main-lagon.vercel.app
dash.lagon.app
dashboard-lagon.vercel.app

Please sign in to comment.