pub async fn sync_groups(db: &Database, groups: BTreeMap<String, GroupConfig>) {
    // Get everything we need to authenticate with GSuite.
    // Initialize the GSuite client.
    let gsuite_customer = env::var("GADMIN_ACCOUNT_ID").unwrap();
    let token = get_gsuite_token("").await;
    let gsuite = GSuite::new(&gsuite_customer, GSUITE_DOMAIN, token);

    // Get the GSuite groups.
    let gsuite_groups = gsuite.list_groups().await.unwrap();

    // Get all the groups.
    let db_groups = Groups::get_from_db(db);
    // Create a BTreeMap
    let mut group_map: BTreeMap<String, Group> = Default::default();
    for u in db_groups {
        group_map.insert(u.name.to_string(), u);
    }
    // Sync groups.
    for (_, mut group) in groups {
        group.expand();

        group.upsert(db).await;

        // Remove the group from the BTreeMap.
        group_map.remove(&group.name);
    }
    // Remove any groups that should no longer be in the database.
    // This is found by the remaining groups that are in the map since we removed
    // the existing repos from the map above.
    for (name, group) in group_map {
        println!("deleting group {} from the database, gsuite, etc", name);

        // Delete the group from the database and Airtable.
        group.delete(db).await;

        // Remove the group from GSuite.
        gsuite
            .delete_group(&format!("{}@{}", name, GSUITE_DOMAIN))
            .await
            .unwrap_or_else(|e| panic!("deleting group {} from gsuite failed: {}", name, e));
        println!("deleted group from gsuite: {}", name);
    }
    println!("updated configs groups in the database");

    // Update the groups in GSuite.
    // Get all the groups.
    let db_groups = Groups::get_from_db(db);
    // Create a BTreeMap
    let mut group_map: BTreeMap<String, Group> = Default::default();
    for u in db_groups {
        group_map.insert(u.name.to_string(), u);
    }
    // Iterate over the groups already in GSuite.
    for g in gsuite_groups {
        let name = g.name.to_string();

        // Check if we already have this group in our database.
        let group = if let Some(val) = group_map.get(&name) {
            val
        } else {
            // If the group does not exist in our map we need to delete
            // group from GSuite.
            println!("deleting group {} from gsuite", name);
            gsuite
                .delete_group(&format!("{}@{}", name, GSUITE_DOMAIN))
                .await
                .unwrap_or_else(|e| panic!("deleting group {} from gsuite failed: {}", name, e));
            println!("deleted group from gsuite: {}", name);
            continue;
        };

        // Update the group with the settings from the database for the group.
        let mut updated_group: GSuiteGroup = g.clone();
        updated_group.description = group.description.to_string();

        // Write the group aliases.
        let mut aliases: Vec<String> = Default::default();
        for alias in &group.aliases {
            aliases.push(format!("{}@{}", alias, GSUITE_DOMAIN));
        }
        updated_group.aliases = aliases;

        gsuite.update_group(&updated_group).await.unwrap_or_else(|e| panic!("updating group {} in gsuite failed: {}", name, e));

        update_group_aliases(&gsuite, &updated_group).await;

        // Update the groups settings.
        update_google_group_settings(&gsuite, &group).await;

        // Remove the group from the database map and continue.
        // This allows us to add all the remaining new groups after.
        group_map.remove(&name);

        println!("updated group in gsuite: {}", name);
    }

    // Create any remaining groups from the database  that we do not have in GSuite.
    for (name, group) in group_map {
        // Create the group.
        let mut g: GSuiteGroup = Default::default();

        // TODO: Make this more DRY since it is repeated above as well.
        g.name = group.name.to_string();
        g.email = format!("{}@{}", group.name, GSUITE_DOMAIN);
        g.description = group.description.to_string();

        // Write the group aliases.
        let mut aliases: Vec<String> = Default::default();
        for alias in &group.aliases {
            aliases.push(format!("{}@{}", alias, GSUITE_DOMAIN));
        }
        g.aliases = aliases;

        let new_group: GSuiteGroup = gsuite.create_group(&g).await.unwrap_or_else(|e| panic!("creating group {} in gsuite failed: {}", name, e));

        update_group_aliases(&gsuite, &new_group).await;

        // Update the groups settings.
        update_google_group_settings(&gsuite, &group).await;

        println!("created group in gsuite: {}", name);
    }

    // Update groups in airtable.
    Groups::get_from_db(db).update_airtable().await;
}
/// Sync our groups with our database and then update Airtable from the database.
Sync our groups with our database and then update Airtable from the database.
Sync our groups with our database and then update Airtable from the database .
pub async fn sync_groups(db: &Database, groups: BTreeMap<String, GroupConfig>) {
    // Get everything we need to authenticate with GSuite.
    // Initialize the GSuite client.
    let gsuite_customer = env::var("GADMIN_ACCOUNT_ID").unwrap();
    let token = get_gsuite_token("").await;
    let gsuite = GSuite::new(&gsuite_customer, GSUITE_DOMAIN, token);
    // Get the GSuite groups.
    let gsuite_groups = gsuite.list_groups().await.unwrap();
    // Get all the groups.
    let db_groups = Groups::get_from_db(db);
    // Create a BTreeMap
    let mut group_map: BTreeMap<String, Group> = Default::default();
    for u in db_groups {
        group_map.insert(u.name.to_string(), u);
    }
    // Sync groups.
    for (_, mut group) in groups {
        group.expand();
        group.upsert(db).await;
        // Remove the group from the BTreeMap.
        group_map.remove(&group.name);
    }
    // Remove any groups that should no longer be in the database.
    // This is found by the remaining groups that are in the map since we removed
    // the existing repos from the map above.
    for (name, group) in group_map {
        println!("deleting group {} from the database, gsuite, etc", name);
        // Delete the group from the database and Airtable.
        group.delete(db).await;
        // Remove the group from GSuite.
        gsuite
            .delete_group(&format!("{}@{}", name, GSUITE_DOMAIN))
            .await
            .unwrap_or_else(|e| panic!("deleting group {} from gsuite failed: {}", name, e));
        println!("deleted group from gsuite: {}", name);
    }
    println!("updated configs groups in the database");
    // Update the groups in GSuite.
    // Get all the groups.
    let db_groups = Groups::get_from_db(db);
    // Create a BTreeMap
    let mut group_map: BTreeMap<String, Group> = Default::default();
    for u in db_groups {
        group_map.insert(u.name.to_string(), u);
    }
    // Iterate over the groups already in GSuite.
    for g in gsuite_groups {
        let name = g.name.to_string();
        // Check if we already have this group in our database.
        let group = if let Some(val) = group_map.get(&name) {
            val
        } else {
            // If the group does not exist in our map we need to delete
            // group from GSuite.
            println!("deleting group {} from gsuite", name);
            gsuite
                .delete_group(&format!("{}@{}", name, GSUITE_DOMAIN))
                .await
                .unwrap_or_else(|e| panic!("deleting group {} from gsuite failed: {}", name, e));
            println!("deleted group from gsuite: {}", name);
            continue;
        };
        // Update the group with the settings from the database for the group.
        let mut updated_group: GSuiteGroup = g.clone();
        updated_group.description = group.description.to_string();
        // Write the group aliases.
        let mut aliases: Vec<String> = Default::default();
        for alias in &group.aliases {
            aliases.push(format!("{}@{}", alias, GSUITE_DOMAIN));
        }
        updated_group.aliases = aliases;
        gsuite.update_group(&updated_group).await.unwrap_or_else(|e| panic!("updating group {} in gsuite failed: {}", name, e));
        update_group_aliases(&gsuite, &updated_group).await;
        // Update the groups settings.
        update_google_group_settings(&gsuite, &group).await;
        // Remove the group from the database map and continue.
        // This allows us to add all the remaining new groups after.
        group_map.remove(&name);
        println!("updated group in gsuite: {}", name);
    }
    // Create any remaining groups from the database  that we do not have in GSuite.
    for (name, group) in group_map {
        // Create the group.
        let mut g: GSuiteGroup = Default::default();
        // TODO: Make this more DRY since it is repeated above as well.
        g.name = group.name.to_string();
        g.email = format!("{}@{}", group.name, GSUITE_DOMAIN);
        g.description = group.description.to_string();
        // Write the group aliases.
        let mut aliases: Vec<String> = Default::default();
        for alias in &group.aliases {
            aliases.push(format!("{}@{}", alias, GSUITE_DOMAIN));
        }
        g.aliases = aliases;
        let new_group: GSuiteGroup = gsuite.create_group(&g).await.unwrap_or_else(|e| panic!("creating group {} in gsuite failed: {}", name, e));
        update_group_aliases(&gsuite, &new_group).await;
        // Update the groups settings.
        update_google_group_settings(&gsuite, &group).await;
        println!("created group in gsuite: {}", name);
    }
    // Update groups in airtable.
    Groups::get_from_db(db).update_airtable().await;
}
pub async fn sync_groups ( db : & Database , groups : BTreeMap < String , GroupConfig > ) { // Get everything we need to authenticate with GSuite. // Initialize the GSuite client. let gsuite_customer = env :: var ( " " ) . unwrap ( ) ; let token = get_gsuite_token ( " " ) . await ; let gsuite = GSuite :: new ( & gsuite_customer , GSUITE_DOMAIN , token ) ; // Get the GSuite groups. let gsuite_groups = gsuite . list_groups ( ) . await . unwrap ( ) ; // Get all the groups. let db_groups = Groups :: get_from_db ( db ) ; // Create a BTreeMap let mut group_map : BTreeMap < String , Group > = Default :: default ( ) ; for u in db_groups { group_map . insert ( u . name . to_string ( ) , u ) ; } // Sync groups. for ( _ , mut group ) in groups { group . expand ( ) ; group . upsert ( db ) . await ; // Remove the group from the BTreeMap. group_map . remove ( & group . name ) ; } // Remove any groups that should no longer be in the database. // This is found by the remaining groups that are in the map since we removed // the existing repos from the map above. for ( name , group ) in group_map { println ! ( " " name ) ; // Delete the group from the database and Airtable. group . delete ( db ) . await ; // Remove the group from GSuite. gsuite . delete_group ( & format ! ( " " name GSUITE_DOMAIN ) ) . await . unwrap_or_else ( | e | panic ! ( " " name e ) ) ; println ! ( " " name ) ; } println ! ( " " ) ; // Update the groups in GSuite. // Get all the groups. let db_groups = Groups :: get_from_db ( db ) ; // Create a BTreeMap let mut group_map : BTreeMap < String , Group > = Default :: default ( ) ; for u in db_groups { group_map . insert ( u . name . to_string ( ) , u ) ; } // Iterate over the groups already in GSuite. for g in gsuite_groups { let name = g . name . to_string ( ) ; // Check if we already have this group in our database. let group = if let Some ( val ) = group_map . get ( & name ) { val } else { // If the group does not exist in our map we need to delete // group from GSuite. println ! ( " " name ) ; gsuite . delete_group ( & format ! ( " " name GSUITE_DOMAIN ) ) . await . unwrap_or_else ( | e | panic ! ( " " name e ) ) ; println ! ( " " name ) ; continue ; } ; // Update the group with the settings from the database for the group. let mut updated_group : GSuiteGroup = g . clone ( ) ; updated_group . description = group . description . to_string ( ) ; // Write the group aliases. let mut aliases : Vec < String > = Default :: default ( ) ; for alias in & group . aliases { aliases . push ( format ! ( " " alias GSUITE_DOMAIN ) ) ; } updated_group . aliases = aliases ; gsuite . update_group ( & updated_group ) . await . unwrap_or_else ( | e | panic ! ( " " name e ) ) ; update_group_aliases ( & gsuite , & updated_group ) . await ; // Update the groups settings. update_google_group_settings ( & gsuite , & group ) . await ; // Remove the group from the database map and continue. // This allows us to add all the remaining new groups after. group_map . remove ( & name ) ; println ! ( " " name ) ; } // Create any remaining groups from the database  that we do not have in GSuite. for ( name , group ) in group_map { // Create the group. let mut g : GSuiteGroup = Default :: default ( ) ; // TODO: Make this more DRY since it is repeated above as well. g . name = group . name . to_string ( ) ; g . email = format ! ( " " group name GSUITE_DOMAIN ) ; g . description = group . description . to_string ( ) ; // Write the group aliases. let mut aliases : Vec < String > = Default :: default ( ) ; for alias in & group . aliases { aliases . push ( format ! ( " " alias GSUITE_DOMAIN ) ) ; } g . aliases = aliases ; let new_group : GSuiteGroup = gsuite . create_group ( & g ) . await . unwrap_or_else ( | e | panic ! ( " " name e ) ) ; update_group_aliases ( & gsuite , & new_group ) . await ; // Update the groups settings. update_google_group_settings ( & gsuite , & group ) . await ; println ! ( " " name ) ; } // Update groups in airtable. Groups :: get_from_db ( db ) . update_airtable ( ) . await ; }
Sync our groups with our database and then update Airtable from the database.
Sync our groups with our database and then update Airtable from the database .
db
&Database
groups
BTreeMap<String, GroupConfig>
db
&Database
groups
BTreeMap<String, GroupConfig>