From 0cf51d67769f52086bde4381a6202d98eec6547d Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Sat, 26 Nov 2022 04:25:53 +0000 Subject: [PATCH] add command to change user_tier values --- .../bin/web3_proxy_cli/change_user_tier.rs | 69 +++++++++++++++++++ web3_proxy/src/bin/web3_proxy_cli/main.rs | 7 ++ 2 files changed, 76 insertions(+) create mode 100644 web3_proxy/src/bin/web3_proxy_cli/change_user_tier.rs diff --git a/web3_proxy/src/bin/web3_proxy_cli/change_user_tier.rs b/web3_proxy/src/bin/web3_proxy_cli/change_user_tier.rs new file mode 100644 index 00000000..1f0f3ac3 --- /dev/null +++ b/web3_proxy/src/bin/web3_proxy_cli/change_user_tier.rs @@ -0,0 +1,69 @@ +use anyhow::Context; +use argh::FromArgs; +use entities::{rpc_key, user, user_tier}; +use log::{debug, info}; +use migration::sea_orm::{ + self, ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, IntoActiveModel, + QueryFilter, +}; +use uuid::Uuid; +use web3_proxy::frontend::authorization::RpcSecretKey; + +/// change a user's tier. +#[derive(FromArgs, PartialEq, Eq, Debug)] +#[argh(subcommand, name = "change_user_tier")] +pub struct ChangeUserTierCommand { + /// the title of the user tier you are going to modify. + #[argh(positional)] + user_tier_title: String, + + /// the amount of requests to allow per rate limit period + #[argh(option)] + max_requests_per_period: Option, + + /// the amount of concurret requests to allow from a single user + #[argh(option)] + max_concurrent_requests: Option, +} + +impl ChangeUserTierCommand { + // TODO: don't expose the RpcSecretKeys at all. Better to take a user/key id. this is definitely most convenient + + pub async fn main(self, db_conn: &DatabaseConnection) -> anyhow::Result<()> { + let user_tier = user_tier::Entity::find() + .filter(user_tier::Column::Title.eq(self.user_tier_title)) + .one(db_conn) + .await? + .context("No user tier found with that name")?; + + debug!("initial user_tier: {:#?}", user_tier); + + let mut user_tier = user_tier.into_active_model(); + + if let Some(max_requests_per_period) = self.max_requests_per_period { + if user_tier.max_requests_per_period == sea_orm::Set(Some(max_requests_per_period)) { + info!("max_requests_per_period already has this value"); + } else { + user_tier.max_requests_per_period = sea_orm::Set(Some(max_requests_per_period)); + + info!("changed max_requests_per_period") + } + } + + if let Some(max_concurrent_requests) = self.max_concurrent_requests { + if user_tier.max_concurrent_requests == sea_orm::Set(Some(max_concurrent_requests)) { + info!("max_concurrent_requests already has this value"); + } else { + user_tier.max_concurrent_requests = sea_orm::Set(Some(max_concurrent_requests)); + + info!("changed max_concurrent_requests") + } + } + + let user_tier = user_tier.save(db_conn).await?; + + debug!("new user_tier: {:#?}", user_tier); + + Ok(()) + } +} diff --git a/web3_proxy/src/bin/web3_proxy_cli/main.rs b/web3_proxy/src/bin/web3_proxy_cli/main.rs index 9fb4db2e..d913bf0d 100644 --- a/web3_proxy/src/bin/web3_proxy_cli/main.rs +++ b/web3_proxy/src/bin/web3_proxy_cli/main.rs @@ -1,3 +1,4 @@ +mod change_user_tier; mod change_user_tier_by_key; mod check_config; mod create_user; @@ -35,6 +36,7 @@ pub struct CliConfig { #[derive(FromArgs, PartialEq, Debug)] #[argh(subcommand)] enum SubCommand { + ChangeUserTier(change_user_tier::ChangeUserTierCommand), ChangeUserTierByKey(change_user_tier_by_key::ChangeUserTierByKeyCommand), CheckConfig(check_config::CheckConfigSubCommand), CreateUser(create_user::CreateUserSubCommand), @@ -76,6 +78,11 @@ async fn main() -> anyhow::Result<()> { }; match cli_config.sub_command { + SubCommand::ChangeUserTier(x) => { + let db_conn = get_db(cli_config.db_url, 1, 1).await?; + + x.main(&db_conn).await + } SubCommand::ChangeUserTierByKey(x) => { let db_conn = get_db(cli_config.db_url, 1, 1).await?;