1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
/**
 * A rust library for interacting with the GSuite APIs.
 *
 * For more information, the GSuite Directory API is documented at
 * [developers.google.com/admin-sdk/directory/v1/reference](https://developers.google.com/admin-sdk/directory/v1/reference)
 * and the Google Groups settings API is documented at
 * [developers.google.com/admin-sdk/groups-settings/v1/reference/groups](https://developers.google.com/admin-sdk/groups-settings/v1/reference/groups).
 *
 * Example:
 *
 * ```
 * use std::env;
 *
 * use gsuite_api::GSuite;
 * use yup_oauth2::{read_service_account_key, ServiceAccountAuthenticator};
 *
 * async fn get_drives() {
 *  // Get the GSuite credentials file.
 *  let gsuite_credential_file = env::var("GADMIN_CREDENTIAL_FILE").unwrap();
 *  let gsuite_subject = env::var("GADMIN_SUBJECT").unwrap();
 *  let gsuite_secret = read_service_account_key(gsuite_credential_file).await.expect("failed to read gsuite credential file");
 *  let auth = ServiceAccountAuthenticator::builder(gsuite_secret).subject(gsuite_subject.to_string()).build().await.expect("failed to create authenticator");
 *
 *  // Add the scopes to the secret and get the token.
 *  let token = auth.token(&[
 *      "https://www.googleapis.com/auth/admin.directory.group",
 *      "https://www.googleapis.com/auth/admin.directory.resource.calendar",
 *      "https://www.googleapis.com/auth/admin.directory.user",
 *      "https://www.googleapis.com/auth/apps.groups.settings",
 *  ]).await.expect("failed to get token");
 *
 *  if token.as_str().is_empty() {
 *      panic!("empty token is not valid");
 *  }
 *
 *  // Initialize the GSuite client.
 *  let gsuite_client = GSuite::new("customer_id", "domain", token);
 *
 *  // List users.
 *  let users = gsuite_client.list_users().await;
 *
 *  // Iterate over the users.
 *  for user in users {
 *     println!("{:?}", user);
 *  }
 * }
 * ```
 */
use std::collections::HashMap;
use std::rc::Rc;

use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use reqwest::{get, header, Client, Method, Request, StatusCode, Url};
use serde::{Deserialize, Serialize};
use yup_oauth2::AccessToken;

use cio_api::{BuildingConfig, ResourceConfig, UserConfig};

/// The endpoint for the GSuite Directory API.
const DIRECTORY_ENDPOINT: &str =
    "https://www.googleapis.com/admin/directory/v1/";

/// Endpoint for the Google Groups settings API.
const GROUPS_SETTINGS_ENDPOINT: &str =
    "https://www.googleapis.com/groups/v1/groups/";

/// Entrypoint for interacting with the GSuite APIs.
pub struct GSuite {
    customer: String,
    domain: String,

    token: AccessToken,

    client: Rc<Client>,
}

impl GSuite {
    /// Create a new GSuite client struct. It takes a type that can convert into
    /// an &str (`String` or `Vec<u8>` for example). As long as the function is
    /// given a valid API Key and Secret your requests will work.
    pub fn new(customer: &str, domain: &str, token: AccessToken) -> Self {
        let client = Client::builder().build().expect("creating client failed");
        Self {
            customer: customer.to_string(),
            domain: domain.to_string(),
            token,
            client: Rc::new(client),
        }
    }

    /// Get the currently set authorization token.
    pub fn get_token(&self) -> &AccessToken {
        &self.token
    }

    fn request<B>(
        &self,
        endpoint: &str,
        method: Method,
        path: &str,
        body: B,
        query: Option<&[(&str, &str)]>,
    ) -> Request
    where
        B: Serialize,
    {
        let base = Url::parse(endpoint).unwrap();
        let url = base.join(path).unwrap();

        // Check if the token is expired and panic.
        if self.token.is_expired() {
            panic!("token is expired");
        }

        let bt = format!("Bearer {}", self.token.as_str());
        let bearer = header::HeaderValue::from_str(&bt).unwrap();

        // Set the default headers.
        let mut headers = header::HeaderMap::new();
        headers.append(header::AUTHORIZATION, bearer);
        headers.append(
            header::CONTENT_TYPE,
            header::HeaderValue::from_static("application/json"),
        );
        headers.append(
            header::ACCEPT,
            header::HeaderValue::from_static("application/json"),
        );

        let mut rb = self.client.request(method.clone(), url).headers(headers);

        if let Some(val) = query {
            rb = rb.query(&val);
        }

        // Add the body, this is to ensure our GET and DELETE calls succeed.
        if method != Method::GET && method != Method::DELETE {
            rb = rb.json(&body);
        }

        // Build the request.
        rb.build().unwrap()
    }

    /// List Google groups.
    pub async fn list_groups(&self) -> Vec<Group> {
        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::GET,
            "groups",
            (),
            Some(&[("customer", &self.customer), ("domain", &self.domain)]),
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => panic!(
                "received response status: {:?}\nbody: {}",
                s,
                resp.text().await.unwrap()
            ),
        };

        // Try to deserialize the response.
        let value: Groups = resp.json().await.unwrap();

        value.groups.unwrap()
    }

    /// Get the settings for a Google group.
    pub async fn get_group_settings(&self, group_email: &str) -> GroupSettings {
        // Build the request.
        let request = self.request(
            GROUPS_SETTINGS_ENDPOINT,
            Method::GET,
            group_email,
            (),
            Some(&[("alt", "json")]),
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => panic!(
                "received response status: {:?}\nbody: {}",
                s,
                resp.text().await.unwrap()
            ),
        };

        // Try to deserialize the response.
        resp.json().await.unwrap()
    }

    /// Update a Google group.
    pub async fn update_group(&self, group: &Group) {
        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::PUT,
            &format!("groups/{}", group.id.as_ref().unwrap()),
            group,
            None,
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => panic!(
                "received response status: {:?}\nbody: {}",
                s,
                resp.text().await.unwrap()
            ),
        };
    }

    /// Update a Google group's settings.
    pub async fn update_group_settings(&self, settings: &GroupSettings) {
        // Build the request.
        let request = self.request(
            GROUPS_SETTINGS_ENDPOINT,
            Method::PUT,
            settings.email.as_ref().unwrap(),
            settings,
            Some(&[("alt", "json")]),
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => panic!(
                "received response status: {:?}\nbody: {}",
                s,
                resp.text().await.unwrap()
            ),
        };
    }

    /// Create a google group.
    pub async fn create_group(&self, group: &Group) -> Group {
        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::POST,
            "groups",
            group,
            None,
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => panic!(
                "received response status: {:?}\nbody: {}",
                s,
                resp.text().await.unwrap()
            ),
        };

        // Try to deserialize the response.
        resp.json().await.unwrap()
    }

    /// Update a Google group's aliases.
    pub async fn update_group_aliases<A>(&self, group_key: &str, aliases: A)
    where
        A: IntoIterator,
        A::Item: AsRef<str>,
    {
        for alias in aliases {
            self.update_group_alias(group_key, alias.as_ref()).await;
        }
    }

    /// Update an alias for a Google group.
    pub async fn update_group_alias(&self, group_key: &str, alias: &str) {
        let mut a: HashMap<&str, &str> = HashMap::new();
        a.insert("alias", alias);
        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::POST,
            &format!("groups/{}/aliases", group_key),
            a,
            None,
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                let body = resp.text().await.unwrap();

                if body.contains("duplicate") {
                    // Ignore the error because we don't care about if it is a duplicate.
                    return;
                }

                panic!(
                    "received response status: {:?}\nresponse body: {:?}",
                    s, body,
                );
            }
        };
    }

    /// Check if a user is a member of a Google group.
    pub async fn group_has_member(&self, group_id: &str, email: &str) -> bool {
        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::GET,
            &format!("groups/{}/hasMember/{}", group_id, email),
            (),
            None,
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => panic!(
                "received response status: {:?}\nbody: {}",
                s,
                resp.text().await.unwrap()
            ),
        };

        // Try to deserialize the response.
        let value: MembersHasMember = resp.json().await.unwrap();

        value.is_member.unwrap()
    }

    /// Update a member of a Google group.
    pub async fn group_update_member(
        &self,
        group_id: &str,
        email: &str,
        role: &str,
    ) {
        let mut member: Member = Default::default();
        member.role = Some(role.to_string());
        member.email = Some(email.to_string());
        member.delivery_settings = Some("ALL_MAIL".to_string());

        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::PUT,
            &format!("groups/{}/members/{}", group_id, email),
            member,
            None,
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => panic!(
                "received response status: {:?}\nbody: {}",
                s,
                resp.text().await.unwrap()
            ),
        };
    }

    /// Add a user as a member of a Google group.
    pub async fn group_insert_member(
        &self,
        group_id: &str,
        email: &str,
        role: &str,
    ) {
        let mut member: Member = Default::default();
        member.role = Some(role.to_string());
        member.email = Some(email.to_string());
        member.delivery_settings = Some("ALL_MAIL".to_string());

        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::POST,
            &format!("groups/{}/members", group_id),
            member,
            None,
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => panic!(
                "received response status: {:?}\nbody: {}",
                s,
                resp.text().await.unwrap()
            ),
        };
    }

    /// Remove a user as a member of a Google group.
    pub async fn group_remove_member(&self, group_id: &str, email: &str) {
        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::DELETE,
            &format!("groups/{}/members/{}", group_id, email),
            (),
            None,
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => panic!(
                "received response status: {:?}\nbody: {}",
                s,
                resp.text().await.unwrap()
            ),
        };
    }

    /// List users.
    pub async fn list_users(&self) -> Vec<User> {
        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::GET,
            "users",
            (),
            Some(&[
                ("customer", &self.customer),
                ("domain", &self.domain),
                ("projection", "full"),
            ]),
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => panic!(
                "received response status: {:?}\nbody: {}",
                s,
                resp.text().await.unwrap()
            ),
        };

        // Try to deserialize the response.
        let value: Users = resp.json().await.unwrap();

        value.users.unwrap()
    }

    /// Update a user.
    pub async fn update_user(&self, user: &User) {
        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::PUT,
            &format!("users/{}", user.id.as_ref().unwrap()),
            user,
            None,
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => panic!(
                "received response status: {:?}\nbody: {}",
                s,
                resp.text().await.unwrap()
            ),
        };
    }

    /// Create a user.
    pub async fn create_user(&self, user: &User) -> User {
        // Build the request.
        let request =
            self.request(DIRECTORY_ENDPOINT, Method::POST, "users", user, None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => panic!(
                "received response status: {:?}\nbody: {}",
                s,
                resp.text().await.unwrap()
            ),
        };

        // Try to deserialize the response.
        resp.json().await.unwrap()
    }

    /// Update a user's aliases.
    pub async fn update_user_aliases<A>(&self, user_id: &str, aliases: A)
    where
        A: IntoIterator,
        A::Item: AsRef<str>,
    {
        for alias in aliases {
            self.update_user_alias(user_id, alias.as_ref()).await;
        }
    }

    /// Update an alias for a user.
    pub async fn update_user_alias(&self, user_id: &str, alias: &str) {
        let mut a: HashMap<&str, &str> = HashMap::new();
        a.insert("alias", alias);
        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::POST,
            &format!("users/{}/aliases", user_id),
            a,
            None,
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                let body = resp.text().await.unwrap();

                if body.contains("duplicate") {
                    // Ignore the error because we don't care about if it is a duplicate.
                    return;
                }

                panic!(
                    "received response status: {:?}\nresponse body: {:?}",
                    s, body,
                );
            }
        };
    }

    /// List calendar resources.
    pub async fn list_calendar_resources(&self) -> Vec<CalendarResource> {
        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::GET,
            &format!("customer/{}/resources/calendars", self.customer),
            (),
            None,
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => panic!(
                "received response status: {:?}\nbody: {}",
                s,
                resp.text().await.unwrap()
            ),
        };

        // Try to deserialize the response.
        let value: CalendarResources = resp.json().await.unwrap();

        value.items.unwrap()
    }

    /// Update a calendar resource.
    pub async fn update_calendar_resource(&self, resource: &CalendarResource) {
        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::PUT,
            &format!(
                "customer/{}/resources/calendars/{}",
                self.customer, resource.id
            ),
            resource,
            None,
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => panic!(
                "received response status: {:?}\nbody: {}",
                s,
                resp.text().await.unwrap()
            ),
        };
    }

    /// Create a calendar resource.
    pub async fn create_calendar_resource(&self, resource: &CalendarResource) {
        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::POST,
            &format!("customer/{}/resources/calendars", self.customer),
            resource,
            None,
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => panic!(
                "received response status: {:?}\nbody: {}",
                s,
                resp.text().await.unwrap()
            ),
        };
    }

    /// List buildings.
    pub async fn list_buildings(&self) -> Vec<Building> {
        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::GET,
            &format!("customer/{}/resources/buildings", self.customer),
            (),
            None,
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => panic!(
                "received response status: {:?}\nbody: {}",
                s,
                resp.text().await.unwrap()
            ),
        };

        // Try to deserialize the response.
        let value: Buildings = resp.json().await.unwrap();

        value.buildings.unwrap()
    }

    /// Update a building.
    pub async fn update_building(&self, building: &Building) {
        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::PUT,
            &format!(
                "customer/{}/resources/buildings/{}",
                self.customer, building.id
            ),
            building,
            None,
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => panic!(
                "received response status: {:?}\nbody: {}",
                s,
                resp.text().await.unwrap()
            ),
        };
    }

    /// Create a building.
    pub async fn create_building(&self, building: &Building) {
        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::POST,
            &format!("customer/{}/resources/buildings", self.customer),
            building,
            None,
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => panic!(
                "received response status: {:?}\nbody: {}",
                s,
                resp.text().await.unwrap()
            ),
        };
    }
}

/// Generate a random string that we can use as a temporary password for new users
/// when we set up their account.
pub fn generate_password() -> String {
    thread_rng().sample_iter(&Alphanumeric).take(30).collect()
}

/// A Google group.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct Group {
    /// List of non editable aliases (Read-only)
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "nonEditableAliases"
    )]
    pub non_editable_aliases: Option<Vec<String>>,
    /// Kind of resource this is.
    pub kind: Option<String>,
    /// Description of the group
    pub description: Option<String>,
    /// Is the group created by admin (Read-only) *
    #[serde(skip_serializing_if = "Option::is_none", rename = "adminCreated")]
    pub admin_created: Option<bool>,
    /// Group direct members count
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "directMembersCount"
    )]
    pub direct_members_count: Option<String>,
    /// Email of Group
    pub email: Option<String>,
    /// ETag of the resource.
    pub etag: Option<String>,
    /// List of aliases (Read-only)
    pub aliases: Option<Vec<String>>,
    /// Unique identifier of Group (Read-only)
    pub id: Option<String>,
    /// Group name
    pub name: Option<String>,
}

/// A Google group's settings.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct GroupSettings {
    /// Permission to ban users. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanBanUsers"
    )]
    pub who_can_ban_users: Option<String>,
    /// Permission for content assistants. Possible values are: Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanAssistContent"
    )]
    pub who_can_assist_content: Option<String>,
    /// Are external members allowed to join the group.
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "allowExternalMembers"
    )]
    pub allow_external_members: Option<String>,
    /// Permission to enter free form tags for topics in a forum. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanEnterFreeFormTags"
    )]
    pub who_can_enter_free_form_tags: Option<String>,
    /// Permission to approve pending messages in the moderation queue. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanApproveMessages"
    )]
    pub who_can_approve_messages: Option<String>,
    /// Permission to mark a topic as a duplicate of another topic. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanMarkDuplicate"
    )]
    pub who_can_mark_duplicate: Option<String>,
    /// Permissions to join the group. Possible values are: ANYONE_CAN_JOIN ALL_IN_DOMAIN_CAN_JOIN INVITED_CAN_JOIN CAN_REQUEST_TO_JOIN
    #[serde(skip_serializing_if = "Option::is_none", rename = "whoCanJoin")]
    pub who_can_join: Option<String>,
    /// Permission to change tags and categories. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanModifyTagsAndCategories"
    )]
    pub who_can_modify_tags_and_categories: Option<String>,
    /// Permission to mark a topic as not needing a response. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanMarkNoResponseNeeded"
    )]
    pub who_can_mark_no_response_needed: Option<String>,
    /// Permission to unmark any post from a favorite reply. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanUnmarkFavoriteReplyOnAnyTopic"
    )]
    pub who_can_unmark_favorite_reply_on_any_topic: Option<String>,
    /// Permission for content moderation. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanModerateContent"
    )]
    pub who_can_moderate_content: Option<String>,
    /// Primary language for the group.
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "primaryLanguage"
    )]
    pub primary_language: Option<String>,
    /// Permission to mark a post for a topic they started as a favorite reply. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanMarkFavoriteReplyOnOwnTopic"
    )]
    pub who_can_mark_favorite_reply_on_own_topic: Option<String>,
    /// Permissions to view membership. Possible values are: ALL_IN_DOMAIN_CAN_VIEW ALL_MEMBERS_CAN_VIEW ALL_MANAGERS_CAN_VIEW ALL_OWNERS_CAN_VIEW
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanViewMembership"
    )]
    pub who_can_view_membership: Option<String>,
    /// If favorite replies should be displayed above other replies.
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "favoriteRepliesOnTop"
    )]
    pub favorite_replies_on_top: Option<String>,
    /// Permission to mark any other user's post as a favorite reply. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanMarkFavoriteReplyOnAnyTopic"
    )]
    pub who_can_mark_favorite_reply_on_any_topic: Option<String>,
    /// Whether to include custom footer.
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "includeCustomFooter"
    )]
    pub include_custom_footer: Option<String>,
    /// Permission to move topics out of the group or forum. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanMoveTopicsOut"
    )]
    pub who_can_move_topics_out: Option<String>,
    /// Default message deny notification message
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "defaultMessageDenyNotificationText"
    )]
    pub default_message_deny_notification_text: Option<String>,
    /// If this groups should be included in global address list or not.
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "includeInGlobalAddressList"
    )]
    pub include_in_global_address_list: Option<String>,
    /// If the group is archive only
    #[serde(skip_serializing_if = "Option::is_none", rename = "archiveOnly")]
    pub archive_only: Option<String>,
    /// Permission to delete topics. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanDeleteTopics"
    )]
    pub who_can_delete_topics: Option<String>,
    /// Permission to delete replies to topics. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanDeleteAnyPost"
    )]
    pub who_can_delete_any_post: Option<String>,
    /// If the contents of the group are archived.
    #[serde(skip_serializing_if = "Option::is_none", rename = "isArchived")]
    pub is_archived: Option<String>,
    /// Can members post using the group email address.
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "membersCanPostAsTheGroup"
    )]
    pub members_can_post_as_the_group: Option<String>,
    /// Permission to make topics appear at the top of the topic list. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanMakeTopicsSticky"
    )]
    pub who_can_make_topics_sticky: Option<String>,
    /// If any of the settings that will be merged have custom roles which is anything other than owners, managers, or group scopes.
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "customRolesEnabledForSettingsToBeMerged"
    )]
    pub custom_roles_enabled_for_settings_to_be_merged: Option<String>,
    /// Email id of the group
    pub email: Option<String>,
    /// Permission for who can discover the group. Possible values are: ALL_MEMBERS_CAN_DISCOVER ALL_IN_DOMAIN_CAN_DISCOVER ANYONE_CAN_DISCOVER
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanDiscoverGroup"
    )]
    pub who_can_discover_group: Option<String>,
    /// Permission to modify members (change member roles). Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanModifyMembers"
    )]
    pub who_can_modify_members: Option<String>,
    /// Moderation level for messages. Possible values are: MODERATE_ALL_MESSAGES MODERATE_NON_MEMBERS MODERATE_NEW_MEMBERS MODERATE_NONE
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "messageModerationLevel"
    )]
    pub message_moderation_level: Option<String>,
    /// Description of the group
    pub description: Option<String>,
    /// Permission to unassign any topic in a forum. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanUnassignTopic"
    )]
    pub who_can_unassign_topic: Option<String>,
    /// Whome should the default reply to a message go to. Possible values are: REPLY_TO_CUSTOM REPLY_TO_SENDER REPLY_TO_LIST REPLY_TO_OWNER REPLY_TO_IGNORE REPLY_TO_MANAGERS
    #[serde(skip_serializing_if = "Option::is_none", rename = "replyTo")]
    pub reply_to: Option<String>,
    /// Default email to which reply to any message should go.
    #[serde(skip_serializing_if = "Option::is_none", rename = "customReplyTo")]
    pub custom_reply_to: Option<String>,
    /// Should the member be notified if his message is denied by owner.
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "sendMessageDenyNotification"
    )]
    pub send_message_deny_notification: Option<String>,
    /// If a primary Collab Inbox feature is enabled.
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "enableCollaborativeInbox"
    )]
    pub enable_collaborative_inbox: Option<String>,
    /// Permission to contact owner of the group via web UI. Possible values are: ANYONE_CAN_CONTACT ALL_IN_DOMAIN_CAN_CONTACT ALL_MEMBERS_CAN_CONTACT ALL_MANAGERS_CAN_CONTACT
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanContactOwner"
    )]
    pub who_can_contact_owner: Option<String>,
    /// Default message display font. Possible values are: DEFAULT_FONT FIXED_WIDTH_FONT
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "messageDisplayFont"
    )]
    pub message_display_font: Option<String>,
    /// Permission to leave the group. Possible values are: ALL_MANAGERS_CAN_LEAVE ALL_OWNERS_CAN_LEAVE ALL_MEMBERS_CAN_LEAVE NONE_CAN_LEAVE
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanLeaveGroup"
    )]
    pub who_can_leave_group: Option<String>,
    /// Permissions to add members. Possible values are: ALL_MANAGERS_CAN_ADD ALL_OWNERS_CAN_ADD ALL_MEMBERS_CAN_ADD NONE_CAN_ADD
    #[serde(skip_serializing_if = "Option::is_none", rename = "whoCanAdd")]
    pub who_can_add: Option<String>,
    /// Permissions to post messages to the group. Possible values are: NONE_CAN_POST ALL_MANAGERS_CAN_POST ALL_MEMBERS_CAN_POST ALL_OWNERS_CAN_POST ALL_IN_DOMAIN_CAN_POST ANYONE_CAN_POST
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanPostMessage"
    )]
    pub who_can_post_message: Option<String>,
    /// Permission to move topics into the group or forum. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanMoveTopicsIn"
    )]
    pub who_can_move_topics_in: Option<String>,
    /// Permission to take topics in a forum. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanTakeTopics"
    )]
    pub who_can_take_topics: Option<String>,
    /// Name of the Group
    pub name: Option<String>,
    /// The type of the resource.
    pub kind: Option<String>,
    /// Maximum message size allowed.
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "maxMessageBytes"
    )]
    pub max_message_bytes: Option<i32>,
    /// Permissions to invite members. Possible values are: ALL_MEMBERS_CAN_INVITE ALL_MANAGERS_CAN_INVITE ALL_OWNERS_CAN_INVITE NONE_CAN_INVITE
    #[serde(skip_serializing_if = "Option::is_none", rename = "whoCanInvite")]
    pub who_can_invite: Option<String>,
    /// Permission to approve members. Possible values are: ALL_OWNERS_CAN_APPROVE ALL_MANAGERS_CAN_APPROVE ALL_MEMBERS_CAN_APPROVE NONE_CAN_APPROVE
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanApproveMembers"
    )]
    pub who_can_approve_members: Option<String>,
    /// Moderation level for messages detected as spam. Possible values are: ALLOW MODERATE SILENTLY_MODERATE REJECT
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "spamModerationLevel"
    )]
    pub spam_moderation_level: Option<String>,
    /// If posting from web is allowed.
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "allowWebPosting"
    )]
    pub allow_web_posting: Option<String>,
    /// Permission for membership moderation. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanModerateMembers"
    )]
    pub who_can_moderate_members: Option<String>,
    /// Permission to add references to a topic. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanAddReferences"
    )]
    pub who_can_add_references: Option<String>,
    /// Permissions to view group. Possible values are: ANYONE_CAN_VIEW ALL_IN_DOMAIN_CAN_VIEW ALL_MEMBERS_CAN_VIEW ALL_MANAGERS_CAN_VIEW ALL_OWNERS_CAN_VIEW
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanViewGroup"
    )]
    pub who_can_view_group: Option<String>,
    /// Is the group listed in groups directory
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "showInGroupGSuite"
    )]
    pub show_in_group_directory: Option<String>,
    /// Permission to post announcements, a special topic type. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanPostAnnouncements"
    )]
    pub who_can_post_announcements: Option<String>,
    /// Permission to lock topics. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanLockTopics"
    )]
    pub who_can_lock_topics: Option<String>,
    /// Permission to assign topics in a forum to another user. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanAssignTopics"
    )]
    pub who_can_assign_topics: Option<String>,
    /// Custom footer text.
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "customFooterText"
    )]
    pub custom_footer_text: Option<String>,
    /// Is google allowed to contact admins.
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "allowGoogleCommunication"
    )]
    pub allow_google_communication: Option<String>,
    /// Permission to hide posts by reporting them as abuse. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "whoCanHideAbuse"
    )]
    pub who_can_hide_abuse: Option<String>,
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
struct Groups {
    /// Token used to access next page of this result.
    #[serde(skip_serializing_if = "Option::is_none", rename = "nextPageToken")]
    pub next_page_token: Option<String>,
    /// Kind of resource this is.
    pub kind: Option<String>,
    /// ETag of the resource.
    pub etag: Option<String>,
    /// List of group objects.
    pub groups: Option<Vec<Group>>,
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
struct MembersHasMember {
    /// Identifies whether the given user is a member of the group. Membership can be direct or nested.
    #[serde(skip_serializing_if = "Option::is_none", rename = "isMember")]
    pub is_member: Option<bool>,
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
struct Members {
    /// Token used to access next page of this result.
    #[serde(skip_serializing_if = "Option::is_none", rename = "nextPageToken")]
    pub next_page_token: Option<String>,
    /// Kind of resource this is.
    pub kind: Option<String>,
    /// ETag of the resource.
    pub etag: Option<String>,
    /// List of member objects.
    pub members: Option<Vec<Member>>,
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
struct Member {
    /// Status of member (Immutable)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
    /// Kind of resource this is.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kind: Option<String>,
    /// Delivery settings of member
    pub delivery_settings: Option<String>,
    /// Email of member (Read-only)
    pub email: Option<String>,
    /// ETag of the resource.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub etag: Option<String>,
    /// Role of member
    pub role: Option<String>,
    /// Type of member (Immutable)
    #[serde(skip_serializing_if = "Option::is_none", rename = "type")]
    pub type_: Option<String>,
    /// Unique identifier of customer member (Read-only) Unique identifier of group (Read-only) Unique identifier of member (Read-only)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
}

/// A user.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct User {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub addresses: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", rename = "posixAccounts")]
    pub posix_accounts: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub phones: Option<Vec<UserPhone>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub locations: Option<Vec<UserLocation>>,
    /// Boolean indicating if the user is delegated admin (Read-only)
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "isDelegatedAdmin"
    )]
    pub is_delegated_admin: Option<bool>,
    /// ETag of the user's photo (Read-only)
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "thumbnailPhotoEtag"
    )]
    pub thumbnail_photo_etag: Option<String>,
    /// Indicates if user is suspended.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub suspended: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub keywords: Option<String>,
    /// Kind of resource this is.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kind: Option<String>,
    /// Unique identifier of User (Read-only)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// List of aliases (Read-only)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aliases: Option<Vec<String>>,
    /// List of non editable aliases (Read-only)
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "nonEditableAliases"
    )]
    pub non_editable_aliases: Option<Vec<String>>,
    /// Indicates if user is archived.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub archived: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none", rename = "deletionTime")]
    pub deletion_time: Option<String>,
    /// Suspension reason if user is suspended (Read-only)
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "suspensionReason"
    )]
    pub suspension_reason: Option<String>,
    /// Photo Url of the user (Read-only)
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "thumbnailPhotoUrl"
    )]
    pub thumbnail_photo_url: Option<String>,
    /// Is enrolled in 2-step verification (Read-only)
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "isEnrolledIn2Sv"
    )]
    pub is_enrolled_in2_sv: Option<bool>,
    /// Boolean indicating if user is included in Global Address List
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "includeInGlobalAddressList"
    )]
    pub include_in_global_address_list: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub relations: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub languages: Option<String>,
    /// Boolean indicating if the user is admin (Read-only)
    #[serde(skip_serializing_if = "Option::is_none", rename = "isAdmin")]
    pub is_admin: Option<bool>,
    /// ETag of the resource.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub etag: Option<String>,
    /// User's last login time. (Read-only)
    #[serde(skip_serializing_if = "Option::is_none", rename = "lastLoginTime")]
    pub last_login_time: Option<String>,
    /// OrgUnit of User
    #[serde(skip_serializing_if = "Option::is_none", rename = "orgUnitPath")]
    pub org_unit_path: Option<String>,
    /// Indicates if user has agreed to terms (Read-only)
    #[serde(skip_serializing_if = "Option::is_none", rename = "agreedToTerms")]
    pub agreed_to_terms: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none", rename = "externalIds")]
    pub external_ids: Option<String>,
    /// Boolean indicating if ip is whitelisted
    #[serde(skip_serializing_if = "Option::is_none", rename = "ipWhitelisted")]
    pub ip_whitelisted: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none", rename = "sshPublicKeys")]
    pub ssh_public_keys: Option<Vec<UserSSHKey>>,
    /// Custom fields of the user.
    #[serde(skip_serializing_if = "Option::is_none", rename = "customSchemas")]
    pub custom_schemas: Option<HashMap<String, UserCustomProperties>>,
    /// Is 2-step verification enforced (Read-only)
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "isEnforcedIn2Sv"
    )]
    pub is_enforced_in2_sv: Option<bool>,
    /// Is mailbox setup (Read-only)
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "isMailboxSetup"
    )]
    pub is_mailbox_setup: Option<bool>,
    /// User's password
    #[serde(skip_serializing_if = "Option::is_none")]
    pub password: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub emails: Option<Vec<UserEmail>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub organizations: Option<String>,
    /// username of User
    #[serde(skip_serializing_if = "Option::is_none", rename = "primaryEmail")]
    pub primary_email: Option<String>,
    /// Hash function name for password. Supported are MD5, SHA-1 and crypt
    #[serde(skip_serializing_if = "Option::is_none", rename = "hashFunction")]
    pub hash_function: Option<String>,
    /// User's name
    pub name: Option<UserName>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gender: Option<UserGender>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notes: Option<String>,
    /// User's G Suite account creation time. (Read-only)
    #[serde(skip_serializing_if = "Option::is_none", rename = "creationTime")]
    pub creation_time: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub websites: Option<String>,
    /// Boolean indicating if the user should change password in next login
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "changePasswordAtNextLogin"
    )]
    pub change_password_at_next_login: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ims: Option<String>,
    /// CustomerId of User (Read-only)
    #[serde(skip_serializing_if = "Option::is_none", rename = "customerId")]
    pub customer_id: Option<String>,
    /// Recovery email of the user
    #[serde(skip_serializing_if = "Option::is_none", rename = "recoveryEmail")]
    pub recovery_email: Option<String>,
    /// Recovery phone of the user
    #[serde(skip_serializing_if = "Option::is_none", rename = "recoveryPhone")]
    pub recovery_phone: Option<String>,
}

impl User {
    /// Update a user.
    pub async fn update(
        mut self,
        user: &UserConfig,
        domain: &str,
        change_password: bool,
    ) -> User {
        // TODO(cbiffle): use &mut self instead of consume-and-return
        // Set the settings for the user.
        self.name = Some(UserName {
            full_name: Some(format!("{} {}", user.first_name, user.last_name)),
            given_name: Some(user.first_name.clone()),
            family_name: Some(user.last_name.clone()),
        });

        if let Some(val) = &user.recovery_email {
            // Set the recovery email for the user.
            self.recovery_email = Some(val.clone());

            // Check if we have a home email set for the user and update it.
            let mut has_home_email = false;
            match self.emails {
                Some(mut emails) => {
                    for (index, email) in emails.iter().enumerate() {
                        match &email.typev {
                            Some(typev) => {
                                if typev == "home" {
                                    // Update the set home email.
                                    emails[index].address = val.clone();
                                    // Break the loop early.
                                    has_home_email = true;
                                    break;
                                }
                            }
                            None => (),
                        };
                    }

                    if !has_home_email {
                        // Set the home email for the user.
                        emails.push(UserEmail {
                            typev: Some("home".to_string()),
                            address: val.to_string(),
                            primary: Some(false),
                        });
                    }

                    // Set the emails.
                    self.emails = Some(emails);
                }
                None => {
                    self.emails = Some(vec![UserEmail {
                        typev: Some("home".to_string()),
                        address: val.to_string(),
                        primary: Some(false),
                    }]);
                }
            }
        } else {
            self.recovery_email = None;
        }

        if let Some(val) = &user.recovery_phone {
            // Set the recovery phone for the user.
            self.recovery_phone = Some(val.to_string());

            // Set the home phone for the user.
            self.phones = Some(vec![UserPhone {
                typev: "home".to_string(),
                value: val.to_string(),
                primary: true,
            }]);
        } else {
            self.recovery_phone = None;
        }

        self.primary_email = Some(format!("{}@{}", user.username, domain));

        // Write the user aliases.
        let mut aliases: Vec<String> = Default::default();
        for alias in user.aliases.as_ref().unwrap() {
            aliases.push(format!("{}@{}", alias, domain));
        }
        self.aliases = Some(aliases);

        if change_password {
            // Since we are creating a new user, we want to change their password
            // at the next login.
            self.change_password_at_next_login = Some(true);
            // Generate a password for the user.
            let password = generate_password();
            self.password = Some(password);
        }

        self.gender = if let Some(val) = &user.gender {
            let mut gender: UserGender = Default::default();
            gender.typev = val.to_string();
            Some(gender)
        } else {
            None
        };

        self.locations = if let Some(val) = &user.building {
            let mut location: UserLocation = Default::default();
            location.typev = "desk".to_string();
            location.building_id = Some(val.to_string());
            location.floor_name = Some("1".to_string());
            Some(vec![location])
        } else {
            None
        };

        let mut cs: HashMap<String, UserCustomProperties> = HashMap::new();
        if let Some(val) = &user.github {
            let mut gh: HashMap<String, String> = HashMap::new();
            gh.insert("GitHub_Username".to_string(), val.clone());
            cs.insert("Contact".to_string(), UserCustomProperties(Some(gh)));

            // Set their GitHub SSH Keys to their Google SSH Keys.
            let ssh_keys = get_github_user_public_ssh_keys(val).await;
            self.ssh_public_keys = Some(ssh_keys);
        }

        if let Some(val) = &user.chat {
            let mut chat: HashMap<String, String> = HashMap::new();
            chat.insert("Matrix_Chat_Username".to_string(), val.clone());
            cs.insert("Contact".to_string(), UserCustomProperties(Some(chat)));
        }

        // Set the custom schemas.
        self.custom_schemas = Some(cs);

        self
    }
}

/// Return a user's public ssh key's from GitHub by their GitHub handle.
async fn get_github_user_public_ssh_keys(handle: &str) -> Vec<UserSSHKey> {
    let body = get(&format!("https://github.com/{}.keys", handle))
        .await
        .unwrap()
        .text()
        .await
        .unwrap();

    body.lines()
        .filter_map(|key| {
            let kt = key.trim();
            if !kt.is_empty() {
                Some(UserSSHKey {
                    key: kt.to_string(),
                    expiration_time_usec: None,
                })
            } else {
                None
            }
        })
        .collect()
}

/// A user's email.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserEmail {
    #[serde(skip_serializing_if = "Option::is_none", rename = "type")]
    pub typev: Option<String>,
    pub address: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub primary: Option<bool>,
}

/// A user's phone.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserPhone {
    #[serde(rename = "type")]
    pub typev: String,
    pub value: String,
    pub primary: bool,
}

/// A user's name.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserName {
    /// Full Name
    #[serde(skip_serializing_if = "Option::is_none", rename = "fullName")]
    pub full_name: Option<String>,
    /// First Name
    #[serde(skip_serializing_if = "Option::is_none", rename = "givenName")]
    pub given_name: Option<String>,
    /// Last Name
    #[serde(skip_serializing_if = "Option::is_none", rename = "familyName")]
    pub family_name: Option<String>,
}

/// A user's ssh key.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserSSHKey {
    pub key: String,
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "expirationTimeUsec"
    )]
    pub expiration_time_usec: Option<i128>,
}

/// Custom properties for a user.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserCustomProperties(pub Option<HashMap<String, String>>);

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
struct Users {
    /// Token used to access next page of this result.
    #[serde(skip_serializing_if = "Option::is_none", rename = "nextPageToken")]
    pub next_page_token: Option<String>,
    /// Kind of resource this is.
    pub kind: Option<String>,
    /// ETag of the resource.
    pub etag: Option<String>,
    /// Event that triggered this response (only used in case of Push Response)
    pub trigger_event: Option<String>,
    /// List of user objects.
    pub users: Option<Vec<User>>,
}

/// A user's location.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserLocation {
    #[serde(rename = "type")]
    pub typev: String,
    pub area: String,
    /// Unique ID for the building a resource is located in.
    #[serde(skip_serializing_if = "Option::is_none", rename = "buildingId")]
    pub building_id: Option<String>,
    /// Name of the floor a resource is located on.
    #[serde(skip_serializing_if = "Option::is_none", rename = "floorName")]
    pub floor_name: Option<String>,
}

/// A user's gender.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserGender {
    #[serde(rename = "type")]
    pub typev: String,
}

/// A calendar resource.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct CalendarResource {
    /// The type of the resource. For calendar resources, the value is admin#directory#resources#calendars#CalendarResource.
    pub kind: Option<String>,
    /// Capacity of a resource, number of seats in a room.
    pub capacity: Option<i32>,
    /// The type of the calendar resource, intended for non-room resources.
    #[serde(skip_serializing_if = "Option::is_none", rename = "resourceType")]
    pub typev: Option<String>,
    /// Description of the resource, visible only to admins.
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "resourceDescription"
    )]
    pub description: Option<String>,
    /// The read-only auto-generated name of the calendar resource which includes metadata about the resource such as building name, floor, capacity, etc. For example, "NYC-2-Training Room 1A (16)".
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "generatedResourceName"
    )]
    pub generated_resource_name: Option<String>,
    /// ETag of the resource.
    pub etags: Option<String>,
    /// The category of the calendar resource. Either CONFERENCE_ROOM or OTHER. Legacy data is set to CATEGORY_UNKNOWN.
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "resourceCategory"
    )]
    pub category: Option<String>,
    /// The read-only email for the calendar resource. Generated as part of creating a new calendar resource.
    #[serde(skip_serializing_if = "Option::is_none", rename = "resourceEmail")]
    pub email: Option<String>,
    /// The name of the calendar resource. For example, "Training Room 1A".
    #[serde(rename = "resourceName")]
    pub name: String,
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "featureInstances"
    )]
    pub feature_instances: Option<Vec<CalendarFeatures>>,
    /// Name of the section within a floor a resource is located in.
    #[serde(skip_serializing_if = "Option::is_none", rename = "floorSection")]
    pub floor_section: Option<String>,
    /// The unique ID for the calendar resource.
    #[serde(rename = "resourceId")]
    pub id: String,
    /// Unique ID for the building a resource is located in.
    #[serde(skip_serializing_if = "Option::is_none", rename = "buildingId")]
    pub building_id: Option<String>,
    /// Name of the floor a resource is located on.
    #[serde(skip_serializing_if = "Option::is_none", rename = "floorName")]
    pub floor_name: Option<String>,
    /// Description of the resource, visible to users and admins.
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "userVisibleDescription"
    )]
    pub user_visible_description: Option<String>,
}

impl CalendarResource {
    /// Update a calendar resource.
    pub fn update(
        mut self,
        resource: &ResourceConfig,
        id: &str,
    ) -> CalendarResource {
        // TODO(cbiffle): the consume-and-return self pattern here complicates
        // things; use &mut self
        self.id = id.to_string();
        self.typev = Some(resource.typev.clone());
        self.name = resource.name.clone();
        self.building_id = Some(resource.building.clone());
        self.description = Some(resource.description.clone());
        self.user_visible_description = Some(resource.description.clone());
        self.capacity = Some(resource.capacity);
        self.floor_name = Some(resource.floor.clone());
        self.floor_section = Some(resource.section.clone());
        self.category = Some("CONFERENCE_ROOM".to_string());

        self
    }
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
struct CalendarResources {
    /// The continuation token, used to page through large result sets. Provide this value in a subsequent request to return the next page of results.
    #[serde(rename = "nextPageToken")]
    pub next_page_token: Option<String>,
    /// The CalendarResources in this page of results.
    pub items: Option<Vec<CalendarResource>>,
    /// Identifies this as a collection of CalendarResources. This is always admin#directory#resources#calendars#calendarResourcesList.
    pub kind: Option<String>,
    /// ETag of the resource.
    pub etag: Option<String>,
}

/// A feature of a calendar.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct CalendarFeature {
    /// The continuation token, used to page through large result sets. Provide this value in a subsequent request to return the next page of results.
    pub name: Option<String>,
    /// Identifies this as a collection of CalendarFeatures. This is always admin#directory#resources#calendars#calendarFeaturesList.
    pub kind: Option<String>,
    /// ETag of the resource.
    pub etags: Option<String>,
}

/// A calendar's features.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct CalendarFeatures {
    pub feature: Option<CalendarFeature>,
}

/// A building.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct Building {
    /// Kind of resource this is.
    pub kind: Option<String>,
    /// The building name as seen by users in Calendar. Must be unique for the customer. For example, "NYC-CHEL". The maximum length is 100 characters.
    #[serde(rename = "buildingName")]
    pub name: String,
    /// The geographic coordinates of the center of the building, expressed as latitude and longitude in decimal degrees.
    pub coordinates: Option<BuildingCoordinates>,
    /// ETag of the resource.
    pub etags: Option<String>,
    /// The postal address of the building. See PostalAddress for details. Note that only a single address line and region code are required.
    pub address: Option<BuildingAddress>,
    /// The display names for all floors in this building. The floors are expected to be sorted in ascending order, from lowest floor to highest floor. For example, ["B2", "B1", "L", "1", "2", "2M", "3", "PH"] Must contain at least one entry.
    #[serde(rename = "floorNames")]
    pub floor_names: Option<Vec<String>>,
    /// Unique identifier for the building. The maximum length is 100 characters.
    #[serde(rename = "buildingId")]
    pub id: String,
    /// A brief description of the building. For example, "Chelsea Market".
    pub description: Option<String>,
}

impl Building {
    /// Update a building.
    pub fn update(mut self, building: &BuildingConfig, id: &str) -> Building {
        // TOOD(cbiffle): use &mut self instead of consume-and-return
        self.id = id.to_string();
        self.name = building.name.clone();
        self.description = Some(building.description.clone());
        self.address = Some(BuildingAddress {
            address_lines: Some(vec![building.address.clone()]),
            locality: Some(building.city.clone()),
            administrative_area: Some(building.state.clone()),
            postal_code: Some(building.zipcode.clone()),
            region_code: Some(building.country.clone()),
            language_code: Some("en".to_string()),
            sublocality: None,
        });
        self.floor_names = Some(building.floors.clone());

        self
    }
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
struct Buildings {
    /// The continuation token, used to page through large result sets. Provide this value in a subsequent request to return the next page of results.
    #[serde(rename = "nextPageToken")]
    pub next_page_token: Option<String>,
    /// The Buildings in this page of results.
    pub buildings: Option<Vec<Building>>,
    /// ETag of the resource.
    pub etag: Option<String>,
    /// Kind of resource this is.
    pub kind: Option<String>,
}

/// A building's coordinates.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct BuildingCoordinates {
    /// Latitude in decimal degrees.
    pub latitude: Option<f64>,
    /// Longitude in decimal degrees.
    pub longitude: Option<f64>,
}

/// A building's address.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct BuildingAddress {
    /// Optional. BCP-47 language code of the contents of this address (if known).
    #[serde(rename = "languageCode")]
    pub language_code: Option<String>,
    /// Optional. Highest administrative subdivision which is used for postal addresses of a country or region.
    #[serde(rename = "administrativeArea")]
    pub administrative_area: Option<String>,
    /// Required. CLDR region code of the country/region of the address.
    #[serde(rename = "regionCode")]
    pub region_code: Option<String>,
    /// Optional. Generally refers to the city/town portion of the address. Examples: US city, IT comune, UK post town. In regions of the world where localities are not well defined or do not fit into this structure well, leave locality empty and use addressLines.
    pub locality: Option<String>,
    /// Optional. Postal code of the address.
    #[serde(rename = "postalCode")]
    pub postal_code: Option<String>,
    /// Optional. Sublocality of the address.
    pub sublocality: Option<String>,
    /// Unstructured address lines describing the lower levels of an address.
    #[serde(rename = "addressLines")]
    pub address_lines: Option<Vec<String>>,
}