Skip to content

Commit

Permalink
fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
dzlk17 committed Oct 30, 2024
1 parent 0a97651 commit 7e1dc67
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 26 deletions.
1 change: 1 addition & 0 deletions database/migrations/0002_team.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CREATE TABLE team(
team_id TEXT NOT NULL UNIQUE,
grafana_id TEXT NOT NULL UNIQUE,
team_name TEXT NOT NULL,
personal BOOLEAN NOT NULL,
subscription subscription,
Expand Down
13 changes: 10 additions & 3 deletions database/src/tables/team/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ impl Db {
tx: Option<&mut Transaction<'_, sqlx::Postgres>>,
team_id: &String,
) -> Result<Option<Team>, DbError> {
let query = format!("SELECT * FROM {TEAM_TABLE_NAME} WHERE team_id = $1 AND deactivated_at IS NULL");
let query = format!(
"SELECT * FROM {TEAM_TABLE_NAME} WHERE team_id = $1 AND deactivated_at IS NULL"
);
let typed_query = query_as::<_, Team>(&query);

match tx {
Expand Down Expand Up @@ -100,7 +102,9 @@ impl Db {
}

pub async fn get_team_by_admin_id(&self, admin_id: &String) -> Result<Option<Team>, DbError> {
let query = format!("SELECT * FROM {TEAM_TABLE_NAME} WHERE team_admin_id = $1 AND deactivated_at IS NULL");
let query = format!(
"SELECT * FROM {TEAM_TABLE_NAME} WHERE team_admin_id = $1 AND deactivated_at IS NULL"
);
let typed_query = query_as::<_, Team>(&query);

return typed_query
Expand All @@ -113,6 +117,9 @@ impl Db {
let query = format!("SELECT * FROM {TEAM_TABLE_NAME} WHERE deactivated_at IS NULL");
let typed_query = query_as::<_, Team>(&query);

return typed_query.fetch_all(&self.connection_pool).await.map_err(|e| e.into());
return typed_query
.fetch_all(&self.connection_pool)
.await
.map_err(|e| e.into());
}
}
4 changes: 3 additions & 1 deletion database/src/tables/team/table_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use sqlx::{

pub const TEAM_TABLE_NAME: &str = "team";
pub const TEAM_KEYS: &str =
"team_id, team_name, personal, subscription, team_admin_id, registration_timestamp, deactivated_at";
"team_id, grafana_id, team_name, personal, subscription, team_admin_id, registration_timestamp, deactivated_at";

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Team {
pub team_id: String,
pub grafana_id: String,
pub personal: bool,
pub team_name: String,
// Subscription is required to get access to the statistics
Expand All @@ -25,6 +26,7 @@ impl FromRow<'_, PgRow> for Team {
fn from_row(row: &sqlx::postgres::PgRow) -> std::result::Result<Self, sqlx::Error> {
Ok(Team {
team_id: row.get("team_id"),
grafana_id: row.get("grafana_id"),
team_name: row.get("team_name"),
personal: row.get("personal"),
subscription: row.get("subscription"),
Expand Down
24 changes: 22 additions & 2 deletions database/src/tables/team/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ impl Db {
team: &Team,
) -> Result<(), DbError> {
let query_body = format!(
"INSERT INTO {TEAM_TABLE_NAME} ({TEAM_KEYS}) VALUES ($1, $2, $3, $4, $5, $6, NULL)"
"INSERT INTO {TEAM_TABLE_NAME} ({TEAM_KEYS}) VALUES ($1, $2, $3, $4, $5, $6, $7, NULL)"
);

let query_result = query(&query_body)
.bind(&team.team_id)
.bind(&team.grafana_id)
.bind(&team.team_name)
.bind(&team.personal)
.bind(&team.subscription)
Expand All @@ -37,11 +38,12 @@ impl Db {

pub async fn create_new_team(&self, team: &Team) -> Result<(), DbError> {
let query_body = format!(
"INSERT INTO {TEAM_TABLE_NAME} ({TEAM_KEYS}) VALUES ($1, $2, $3, $4, $5, $6, NULL)"
"INSERT INTO {TEAM_TABLE_NAME} ({TEAM_KEYS}) VALUES ($1, $2, $3, $4, $5, $6, $7, NULL)"
);

let query_result = query(&query_body)
.bind(&team.team_id)
.bind(&team.grafana_id)
.bind(&team.team_name)
.bind(&team.personal)
.bind(&team.subscription)
Expand Down Expand Up @@ -96,6 +98,23 @@ impl Db {
Err(e) => Err(e).map_err(|e| e.into()),
}
}

pub async fn update_grafana_id(&self, team_id: &str, grafana_id: &str) -> Result<(), DbError> {
let query_body = format!(
"UPDATE {TEAM_TABLE_NAME} SET grafana_id = $1 WHERE team_id = $2 AND deactivated_at IS NULL",
);

let query_result = query(&query_body)
.bind(grafana_id)
.bind(team_id)
.execute(&self.connection_pool)
.await;

match query_result {
Ok(_) => Ok(()),
Err(e) => Err(e).map_err(|e| e.into()),
}
}
}

#[cfg(feature = "cloud_integration_tests")]
Expand All @@ -121,6 +140,7 @@ mod tests {
// Create team and register app
let team = Team {
team_id: "test_team_id".to_string(),
grafana_id: "test_grafana_id".to_string(),
team_name: "test_team_name".to_string(),
personal: false,
subscription: None,
Expand Down
1 change: 1 addition & 0 deletions database/src/tables/user_app_privileges/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ impl Db {
for row in rows {
let team = Team {
team_id: row.get("team_id"),
grafana_id: row.get("grafana_id"),
personal: row.get("personal"),
team_name: row.get("team_name"),
subscription: row.get("subscription"),
Expand Down
33 changes: 19 additions & 14 deletions server/src/bin/grafana_restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ async fn main() {
)
.await
{
Ok(id) => id,
Ok(id) => {
if let Err(err) = db.update_grafana_id(&team.team_id, &id.to_string()).await {
panic!("Failed to update grafana id in database: {:?}", err);
}
}
Err(err) => {
panic!("Failed to create team in grafana: {:?}", err);
}
Expand All @@ -59,19 +63,20 @@ async fn main() {
.into_iter()
.collect();

for user_id in unique_user_ids {
let user_email = match db.get_user_by_user_id(&user_id).await {
Ok(Some(user)) => user.email,
Ok(None) => {
panic!("User not found. User ID: {:?}", user_id);
}
Err(e) => {
panic!("Failed to get user. Error: {:?}", e);
}
};
let users_emails = match db.get_users_emails_by_ids(&unique_user_ids).await {
Ok(emails) => emails,
Err(e) => {
panic!("Failed to get users emails. Error: {:?}", e);
}
};
for (_, user_email) in users_emails {
// add users to teams
match handle_grafana_add_user_to_team(&grafana_client_conf, &team.team_id, &user_email)
.await
match handle_grafana_add_user_to_team(
&grafana_client_conf,
&team.grafana_id,
&user_email,
)
.await
{
Ok(id) => id,
Err(err) => {
Expand All @@ -93,7 +98,7 @@ async fn main() {
&grafana_client_conf,
&app_id,
&app_name,
&team.team_id,
&team.grafana_id,
)
.await
{
Expand Down
18 changes: 17 additions & 1 deletion server/src/http/cloud/accept_team_invite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,26 @@ pub async fn accept_team_invite(
}
}

let grafana_team_id = match db.get_team_by_team_id(None, &request.team_id).await {
Ok(Some(team)) => team.grafana_id,
Ok(None) => {
return Err((
StatusCode::BAD_REQUEST,
CloudApiErrors::TeamDoesNotExist.to_string(),
));
}
Err(err) => {
error!("Failed to get team by team_id: {:?}", err);
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
CloudApiErrors::DatabaseError.to_string(),
));
}
};
// Grafana add user to the team
if is_env_production() {
if let Err(err) =
handle_grafana_add_user_to_team(&grafana_conf, &request.team_id, &user.email).await
handle_grafana_add_user_to_team(&grafana_conf, &grafana_team_id, &user.email).await
{
error!("Failed to add user to the team in grafana: {:?}", err);
return Err((
Expand Down
2 changes: 1 addition & 1 deletion server/src/http/cloud/delete_team.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub async fn delete_team(
// Grafana, delete team
// TODO, fix this by fixing methods for setting up grafana datasource
if is_env_production() {
if let Err(err) = handle_grafana_delete_team(&grafana_conf, &request.team_id).await {
if let Err(err) = handle_grafana_delete_team(&grafana_conf, &team.grafana_id).await {
error!("Failed to delete team from grafana: {:?}", err);
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
Expand Down
2 changes: 1 addition & 1 deletion server/src/http/cloud/leave_team.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub async fn leave_team(
if is_env_production() {
if let Err(err) = handle_grafana_remove_user_from_team(
&grafana_conf,
&request.team_id,
&team.grafana_id,
&user.email,
)
.await
Expand Down
2 changes: 1 addition & 1 deletion server/src/http/cloud/register_new_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub async fn register_new_app(
&grafana_conf,
&request.app_name,
&app_id,
&team.team_id,
&team.grafana_id,
)
.await
{
Expand Down
6 changes: 5 additions & 1 deletion server/src/http/cloud/register_new_team.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use openapi::apis::configuration::Configuration;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use ts_rs::TS;
use uuid7::uuid7;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS, Validate)]
#[ts(export)]
Expand Down Expand Up @@ -141,10 +142,13 @@ pub async fn register_new_team(
}
}
}
// Generate a new app id
let team_id = uuid7().to_string();

// Create a new team
let team = Team {
team_id: grafana_team_id.to_string(),
team_id: team_id.to_string(),
grafana_id: grafana_team_id.to_string(),
team_name: request.team_name.clone(),
team_admin_id: user_id.clone(),
subscription: None,
Expand Down
2 changes: 1 addition & 1 deletion server/src/http/cloud/remove_user_from_team.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub async fn remove_user_from_team(
if is_env_production() {
if let Err(err) = handle_grafana_remove_user_from_team(
&grafana_conf,
&request.team_id,
&team.grafana_id,
&request.user_email,
)
.await
Expand Down

0 comments on commit 7e1dc67

Please sign in to comment.