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

feat(admin): change project owner #1725

Merged
merged 9 commits into from
Apr 5, 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions admin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ publish = false
shuttle-common = { workspace = true, features = ["models"] }

anyhow = { workspace = true }
bytes = { workspace = true }
clap = { workspace = true, features = ["env"] }
dirs = { workspace = true }
reqwest = { workspace = true, features = ["json"] }
Expand Down
6 changes: 6 additions & 0 deletions admin/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{fs, io, path::PathBuf};

use clap::{Error, Parser, Subcommand};
use shuttle_common::models::user::UserId;

#[derive(Parser, Debug)]
pub struct Args {
Expand All @@ -27,6 +28,11 @@ pub enum Command {
/// Manage project names
ProjectNames,

ChangeProjectOwner {
project_name: String,
new_user_id: UserId,
},

/// Viewing and managing stats
#[command(subcommand)]
Stats(StatsCommand),
Expand Down
29 changes: 22 additions & 7 deletions admin/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{Context, Result};
use anyhow::{bail, Context, Result};
use bytes::Bytes;
use serde::{de::DeserializeOwned, Serialize};
use shuttle_common::models::{admin::ProjectResponse, stats, ToJson};
use tracing::trace;
Expand Down Expand Up @@ -73,6 +74,15 @@ impl Client {
self.get("/admin/projects").await
}

pub async fn change_project_owner(&self, project_name: &str, new_user_id: &str) -> Result<()> {
self.get_raw(&format!(
"/admin/projects/change-owner/{project_name}/{new_user_id}"
))
.await?;

Ok(())
}

pub async fn get_load(&self) -> Result<stats::LoadResponse> {
self.get("/admin/stats/load").await
}
Expand Down Expand Up @@ -130,15 +140,20 @@ impl Client {
.context("failed to extract json body from delete response")
}

async fn get<R: DeserializeOwned>(&self, path: &str) -> Result<R> {
reqwest::Client::new()
async fn get_raw(&self, path: &str) -> Result<Bytes> {
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
let res = reqwest::Client::new()
.get(format!("{}{}", self.api_url, path))
.bearer_auth(&self.api_key)
.send()
.await
.context("failed to make get request")?
.to_json()
.await
.context("failed to post text body from response")
.context("making request")?;
if !res.status().is_success() {
bail!("API call returned non-2xx: {:?}", res);
}
res.bytes().await.context("getting response body")
}

async fn get<R: DeserializeOwned>(&self, path: &str) -> Result<R> {
serde_json::from_slice(&self.get_raw(path).await?).context("deserializing body")
}
}
4 changes: 4 additions & 0 deletions admin/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::{fs, path::PathBuf};

pub fn get_api_key() -> String {
if let Ok(s) = std::env::var("SHUTTLE_API_KEY") {
return s;
}

let data = fs::read_to_string(config_path()).expect("shuttle config file to exist");
let toml: toml::Value = toml::from_str(&data).expect("to parse shuttle config file");

Expand Down
10 changes: 10 additions & 0 deletions admin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ async fn main() {
client.idle_cch().await.expect("cch projects to be idled");
"Idled CCH projects".to_string()
}
Command::ChangeProjectOwner {
project_name,
new_user_id,
} => {
client
.change_project_owner(&project_name, &new_user_id)
.await
.unwrap();
format!("Changed project owner: {project_name} -> {new_user_id}")
}
};

println!("{res}");
Expand Down
Loading