Skip to content

File Operations

Copyright 2023 Infoblox

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

1
https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

NiosFileopMixin

NiosFileopMixin class

Source code in src/ibx_sdk/nios/fileop.py
  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
class NiosFileopMixin:
    """
    NiosFileopMixin class
    """

    def csv_export(self, wapi_object: str, filename: Optional[str] = None) -> None:
        """
        Exports data in CSV format for the specified WAPI object and saves it to a file.

        The method performs a CSV export operation for a specified WAPI object using the file
        operation capabilities provided by the WAPI (Web Application Programming Interface),
        downloads the resulting file from a given URL, and saves it to a user-specified filename
        or a default filename derived from the download URL. Finally, it finalizes the download
        process by notifying the completion.

        Parameters:
            wapi_object (str): The name of the WAPI object(s) whose data is to be exported.
            filename (Optional[str]): The optional local file path to save the downloaded CSV
                data. If not provided, the filename is automatically derived from the
                downloaded URL.

        Raises:
            WapiRequestException: If an HTTP request fails during the CSV export process.
        """
        if filename:
            (_, filename) = os.path.split(filename)
            filename = os.path.join(_, filename.replace("-", "_"))

        # Call WAPI fileop  csv_export function
        logging.info("performing csv export for %s object(s)", wapi_object)
        payload = {"_object": wapi_object}
        try:
            response = self.post(  # ty:ignore[unresolved-attribute]
                "fileop",
                params={"_function": "csv_export"},
                json=payload,
            )
            logging.debug(response.text)
            response.raise_for_status()
            try:
                obj = response.json()
            except httpx.DecodingError as exc:
                logging.error(f"DecodingError: {exc}")
                raise WapiRequestException(response.text) from exc
        except httpx.TimeoutException as exc:
            logging.error(f"Timeout error: {exc}")
            raise WapiRequestException(exc) from exc
        except httpx.HTTPStatusError as exc:
            logging.error(f"HTTP error: {exc}")
            raise WapiRequestException(exc) from exc
        except httpx.RequestError as exc:
            logging.error(f"Request error: {exc}")
            raise WapiRequestException(exc) from exc

        download_url = obj.get("url")
        download_token = obj.get("token")

        logging.info("downloading data from %s", download_url)

        if not filename:
            filename = util.extract_filename_from_url(download_url)

        self.__download_file(download_url, filename)

        self.__download_complete(download_token, filename)

    def file_download(
        self,
        token: str,
        url: str,
        filename: str | None = None,
    ) -> None:
        """
        file_download downloads the generated file from the NIOS Grid using a token and url

        Args:
            token: Authentication token required for the download completion.
            url: URL of the file to be downloaded.
            filename: Optional; name for the downloaded file. If not provided, it will be extracted
            from the URL.

        Returns:
            None
        """
        logging.info("downloading data from %s", url)
        if not filename:
            filename = util.extract_filename_from_url(url)

        try:
            self.__download_file(url, filename)
        except httpx.TimeoutException as exc:
            logging.error(f"Timeout error: {exc}")
            raise WapiRequestException(exc) from exc
        except httpx.HTTPStatusError as exc:
            logging.error(f"HTTP error: {exc}")
            raise WapiRequestException(exc) from exc
        except httpx.RequestError as exc:
            logging.error(f"Request error: {exc}")
            raise WapiRequestException(exc) from exc

        try:
            self.__download_complete(token, filename)
        except httpx.TimeoutException as exc:
            logging.error(f"Timeout error: {exc}")
            raise WapiRequestException(exc) from exc
        except httpx.HTTPStatusError as exc:
            logging.error(f"HTTP error: {exc}")
            raise WapiRequestException(exc) from exc
        except httpx.RequestError as exc:
            logging.error(f"Request error: {exc}")
            raise WapiRequestException(exc) from exc

    def file_upload(self, filename: str) -> str | None:
        """
        Perform a file upload into the NIOS Grid.

        Args:
            filename: The path of the file to be uploaded.

        Returns:
            str: The token received upon successful upload initialization.

        Raises:
            WapiRequestException: If there is a request exception during the upload process.
        """
        (path, filename) = os.path.split(filename)
        valid_filename = filename.replace("-", "_")

        # Call WAPI fileop Upload INIT
        logging.info("step 1 - request uploadinit %s", filename)
        try:
            obj = self.__upload_init(filename=valid_filename)
        except httpx.TimeoutException as exc:
            logging.error(f"Timeout error: {exc}")
            raise WapiRequestException(exc) from exc
        except httpx.HTTPStatusError as exc:
            logging.error(f"HTTP error: {exc}")
            raise WapiRequestException(exc) from exc
        except httpx.RequestError as exc:
            logging.error(f"Request error: {exc}")
            raise WapiRequestException(exc) from exc

        upload_url = obj.get("url")
        token = obj.get("token")

        if not token or not upload_url:
            raise ValueError("Invalid token or upload URL")

        # specify a file handle for the file data to be uploaded
        with open(os.path.join(path, filename), "rb") as fh:
            # reset to top of the file
            fh.seek(0)
            upload_file = {"file": fh.read()}

            # Upload the contents of the CSV file
            logging.info("step 2 - post the files using the upload_url provided")
            try:
                self.__upload_file(upload_url, upload_file)
            except httpx.TimeoutException as exc:
                logging.error(f"Timeout error: {exc}")
                raise WapiRequestException(exc) from exc
            except httpx.HTTPStatusError as exc:
                logging.error(f"HTTP error: {exc}")
                raise WapiRequestException(exc) from exc
            except httpx.RequestError as exc:
                logging.error(f"Request error: {exc}")
                raise WapiRequestException(exc) from exc
        return token

    def upload_certificate(
        self,
        member: str,
        filename: str,
        certificate_usage: SupportedCertTypes = "ADMIN",
    ):
        """
        Upload an SSL Certificate file to the Grid

        Args:
            member: The member identifier to which the certificate will be uploaded.
            filename: The filename of the certificate to be uploaded.
            certificate_usage: The usage type of the certificate. Default is "ADMIN".

        Raises:
            WapiRequestException: If there is an error during the request to upload the certificate.
        """
        token = self.file_upload(filename=filename)

        # submit task to CSV Job Manager
        logging.info(
            "step 3 - upload %s certificate on %s",
            certificate_usage,
            member,
        )
        payload = {
            "certificate_usage": certificate_usage,
            "member": member,
            "token": token,
        }
        try:
            res = self.post(  # pyright: ignore[reportAttributeAccessIssue]
                "fileop",
                params={"_function": "uploadcertificate"},
                json=payload,
            )
            logging.debug(pprint.pformat(res.text))
            res.raise_for_status()
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)

    def csv_import(
        self,
        task_operation: CsvOperation,
        csv_import_file: str,
        exit_on_error: bool = False,
    ) -> dict:
        """
        Perform a CSV import task using the NIOS CSV Task Manager

        Args:
            task_operation (CsvOperation): The operation to be performed on the CSV file. Should be
                                           a value from the `CsvOperation` enum.
            csv_import_file (str): The path to the CSV file to be imported.
            exit_on_error (bool): Indicates whether the program should exit if an error occurs
                                  during the import process. Default value is `False`.

        Returns:
            A dictionary containing the result of the CSV import task.

        Raises:
            httpx.RequestError: If an error occurs while making HTTP requests.
        """
        token = self.file_upload(filename=csv_import_file)
        if not token:
            raise ValueError("Invalid token")

        # submit task to CSV Job Manager
        logging.info(
            "step 3 - execute the csv_import %s job on %s",
            task_operation,
            csv_import_file,
        )
        try:
            csvtask = self.__csv_import(task_operation.upper(), token, exit_on_error)
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)
        else:
            return csvtask

    def csvtask_status(self, csvtask: dict) -> dict:
        """
        Fetch the status of a CSV submitted task

        Args:
            csvtask (dict): The dictionary containing the information about the CSV import task.

        Returns:
            dict: The JSON response containing the status of the CSV import task.

        Raises:
            RequestException: If there is an error in the request.

        Description:
            This method is used to check the status of a CSV import task. It takes a dictionary,
            `csvtask`, as input, which contains the information about the CSV import task. The
            `csvtask` dictionary should have the following structure:

            {
                'csv_import_task': {
                    '_ref': '<CSV import task reference>'
                }
            }

            The method first retrieves the `_ref` value from the `csvtask` dictionary to identify
            the import task. It then sends a request to retrieve the status of the import task
            using the `get` method.

            If the status request is successful, the JSON response containing the status is
            logged using the `debug` level.

            Finally, the method returns the JSON response containing the status of the CSV import
            task.

            If there is any error in the request, a `RequestException` is raised and logged using
            the `error` level.

        Example usage:

        ```python
        csvtask = {
            'csv_import_task': {
                '_ref': '12345678'
            }
        }
        status = csvtask_status(csvtask)
        ```
        """
        _ref = csvtask["csv_import_task"]["_ref"]
        logging.debug("Checking status of csvimporttask %s", _ref)
        try:
            res = self.get(_ref)  # ty:ignore[unresolved-attribute]
            res.raise_for_status()
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)
        else:
            logging.debug(res.json())

        return res.json()

    def get_csv_errors_file(self, filename: str, job_id: str) -> None:
        """
        Fetches the csv-errors file for a specific job ID.

        Args:
            filename (str): The filename to be used when saving the csv-errors file.
            job_id (str): The job ID for which the csv-errors file should be fetched.

        Returns:
            None

        Raises:
            httpx.RequestError: If there is an error during the request.

        """
        logging.debug("fetching csv-errors file for job id %s", job_id)
        payload = {"import_id": job_id}
        try:
            res = self.post(  # ty:ignore[unresolved-attribute]
                "fileop",
                params={"_function": "csv_error_log"},
                json=payload,
            )
            logging.debug(pprint.pformat(res.text))
            res.raise_for_status()
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)

        obj = res.json()
        token = obj.get("token")
        download_url = obj.get("url")

        csv_error_file = f"csv-errors-{filename}.csv"
        try:
            self.__download_file(download_url, csv_error_file)
            res.raise_for_status()
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)

        # We're done - so post to downloadcomplete function
        try:
            self.__download_complete(token, csv_error_file)
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)

    def download_certificate(
        self,
        member: str,
        certificate_usage: SupportedCertTypes = "ADMIN",
    ):
        """
        Download SSL certificate from the Grid.

        Args:
            member: The identifier of the member for whom the certificate is being downloaded.
            certificate_usage: The type of certificate to be downloaded (e.g., "ADMIN").
        """
        logging.info(
            "Downloading %s certificate for %s",
            certificate_usage,
            member,
        )
        payload = {
            "member": member,
            "certificate_usage": certificate_usage,
        }
        logging.debug("json payload %s", payload)

        try:
            res = self.post(  # ty:ignore[unresolved-attribute]
                "fileop",
                params={"_function": "downloadcertificate"},
                json=payload,
            )
            logging.debug(res.text)
            res.raise_for_status()
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)

        obj = res.json()
        download_url = obj.get("url")
        download_token = obj.get("token")

        self.file_download(token=download_token, url=download_url)

    def generate_selfsigned_cert(
        self,
        cn: str,
        member: str,
        days_valid: int = 365,
        algorithm: SupportedAlgorithms = "SHA-256",
        certificate_usage: SupportedCertUsages = "ADMIN",
        comment: Optional[str] = None,
        country: Optional[str] = None,
        email: Optional[str] = None,
        key_size: Optional[SupportedKeySizes] = 2048,
        locality: Optional[str] = None,
        org: Optional[str] = None,
        org_unit: Optional[str] = None,
        state: Optional[str] = None,
        subject_alternative_names: Optional[list[dict]] = None,
    ):
        """
        Generate a Self-Signed Certificate on the Grid.

        Args:
            cn: The common name for the certificate.
            member: The member name for the certificate.
            days_valid: The number of days the certificate is valid for. Default is 365.
            algorithm: The algorithm used for certificate generation. Default is "SHA-256".
            certificate_usage: The usage type of the certificate. Default is "ADMIN".
            comment: Optional comment associated with the certificate.
            country: Optional country code.
            email: Optional email address.
            key_size: The size of the key used in certificate generation. Default is 2048.
            locality: Optional locality (e.g., city).
            org: Optional organization name.
            org_unit: Optional organizational unit.
            state: Optional state or province.
            subject_alternative_names: Optional list of subject alternative names.

        """
        logging.info("generating self-signed certificate for %s", member)
        payload = {
            "cn": cn,
            "member": member,
            "algorithm": algorithm,
            "certificate_usage": certificate_usage,
            "days_valid": days_valid,
        }
        if comment:
            payload["comment"] = comment
        if country:
            payload["country"] = country
        if email:
            payload["email"] = email
        if key_size:
            payload["key_size"] = key_size
        if locality:
            payload["locality"] = locality
        if org:
            payload["org"] = org
        if org_unit:
            payload["org_unit"] = org_unit
        if state:
            payload["state"] = state
        if subject_alternative_names:
            payload["subject_alternative_names"] = subject_alternative_names
        logging.debug("json payload %s", payload)

        try:
            res = self.post(  # ty:ignore[unresolved-attribute]
                "fileop",
                params={"_function": "generateselfsignedcert"},
                json=payload,
            )
            logging.debug(res.text)
            res.raise_for_status()
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)

        obj = res.json()
        download_url = obj.get("url")
        download_token = obj.get("token")

        self.file_download(token=download_token, url=download_url)

    def generate_csr(
        self,
        cn: str,
        member: str,
        algorithm: SupportedAlgorithms = "SHA-256",
        certificate_usage: SupportedCertUsages = "ADMIN",
        comment: Optional[str] = None,
        country: Optional[str] = None,
        email: Optional[str] = None,
        key_size: Optional[SupportedKeySizes] = 2048,
        locality: Optional[str] = None,
        org: Optional[str] = None,
        org_unit: Optional[str] = None,
        state: Optional[str] = None,
        subject_alternative_names: Optional[list[dict]] = None,
    ) -> None:
        """
        Generate a Certificate Signing Request

        Generate and download a CSR for a member of the grid. Once the CSR is generated it is
        downloaded and saved locally to the current working directory.

        Args:
            cn: Common Name for the certificate.
            member: The member for which the certificate is being generated.
            algorithm: Algorithm used for certificate generation, default is "SHA-256".
            certificate_usage: Purpose of the certificate, default is "ADMIN".
            comment: Optional comment for the certificate.
            country: Optional country code for the certificate.
            email: Optional email address for the certificate.
            key_size: Optional key size for the certificate, default is 2048.
            locality: Optional locality or city for the certificate.
            org: Optional organization name for the certificate.
            org_unit: Optional organizational unit for the certificate.
            state: Optional state or province for the certificate.
            subject_alternative_names: Optional list of subject alternative names (SANs) for the
                    certificate.

        Returns:
            None
        """
        logging.info(locals())
        logging.info("generating %s csr for %s", certificate_usage, member)
        payload = {
            "cn": cn,
            "member": member,
            "algorithm": algorithm,
            "certificate_usage": certificate_usage,
        }
        # optional params
        for param in [
            "country",
            "email",
            "key_size",
            "comment",
            "locality",
            "org",
            "org_unit",
            "state",
            "subject_alternative_names",
        ]:
            if locals()[param] is not None:
                payload[param] = locals()[param]

        logging.debug("json payload %s", payload)

        try:
            res = self.post(  # ty:ignore[unresolved-attribute]
                "fileop",
                params={"_function": "generatecsr"},
                json=payload,
            )
            logging.debug(res.text)
            res.raise_for_status()
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)

        obj = res.json()
        download_url = obj.get("url")
        download_token = obj.get("token")

        self.file_download(token=download_token, url=download_url)

    def get_log_files(
        self,
        log_type: LogType,
        filename: Optional[str] = None,
        endpoint: Optional[str] = None,
        include_rotated: bool = False,
        member: Optional[str] = None,
        msserver: Optional[str] = None,
        node_type: Optional[Literal["ACTIVE", "BACKUP"]] = None,
    ):
        """
        Fetch the log files for the provided member or msserver

        Args:
            log_type (LogType): The type of log files to fetch.
            filename (str): The name of the log file to download (Default value = None)
            endpoint (str): The specific endpoint for which to fetch log files. (Default: None)
            include_rotated (bool): Whether to include rotated log files. (Default: False)
            member (str): The member for which to fetch log files. (Default: None)
            msserver (str): The msserver for which to fetch log files. (Default: None)
            node_type: The type of node for which to fetch log files. Can be 'ACTIVE' or
                       'BACKUP'. (Default: None)
        """
        logging.info("fetching %s log files for %s", log_type, member)
        payload = {
            "log_type": log_type,
            "include_rotated": include_rotated,
        }

        if endpoint:
            payload["endpoint"] = endpoint
        if member:
            payload["member"] = member
        if node_type:
            payload["node_type"] = node_type
        if msserver:
            payload["msserver"] = msserver

        logging.debug("json payload %s", payload)

        try:
            res = self.post(  # ty:ignore[unresolved-attribute]
                "fileop",
                params={"_function": "get_log_files"},
                json=payload,
            )
            logging.debug(res.text)
            res.raise_for_status()
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)

        obj = res.json()
        download_url = obj.get("url")
        download_token = obj.get("token")

        self.file_download(token=download_token, url=download_url, filename=filename)

    def get_support_bundle(
        self,
        member: str,
        filename: Optional[str] = None,
        cached_zone_data: bool = False,
        core_files: bool = False,
        log_files: bool = False,
        nm_snmp_logs: bool = False,
        recursive_cache_file: bool = False,
        remote_url: Optional[str] = None,
        rotate_log_files: bool = False,
    ):
        """
        Get the support bundle for a member.

        Args:
            member (str): The member for which to retrieve the support bundle.
            filename (str, optional): The filename of the support bundle.
            cached_zone_data (bool, optional): Whether to include cached zone data in the support
                                               bundle. Defaults to False.
            core_files (bool, optional): Whether to include core files in the support bundle.
                                         Defaults to False.
            log_files (bool, optional): Whether to include log files in the support bundle.
                                        Defaults to False.
            nm_snmp_logs (bool, optional): Whether to include NM SNMP logs in the support bundle.
                                           Defaults to False.
            recursive_cache_file (bool, optional): Whether to include recursive cache file in the
                                                   support bundle. Defaults to False.
            remote_url (str, optional): The remote URL where the support bundle will be uploaded.
                                        Defaults to None.
            rotate_log_files (bool, optional): Whether to rotate log files before creating the
                                               support bundle. Defaults to False.

        Raises:
            httpx.RequestError: If an error occurs during the request.

        """
        logging.info("performing get_support_bundle for %s object(s)", member)
        payload = {
            "member": member,
            "cached_zone_data": cached_zone_data,
            "core_files": core_files,
            "log_files": log_files,
            "nm_snmp_logs": nm_snmp_logs,
            "recursive_cache_file": recursive_cache_file,
            "rotate_log_files": rotate_log_files,
        }
        if remote_url:
            payload["remote_url"] = remote_url
        logging.debug(pprint.pformat(payload))
        try:
            res = self.post(  # ty:ignore[unresolved-attribute]
                "fileop",
                params={"_function": "get_support_bundle"},
                json=payload,
                timeout=None,
            )
            logging.debug(res.text)
            res.raise_for_status()
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)

        obj = res.json()
        download_url = obj.get("url")
        download_token = obj.get("token")

        self.file_download(token=download_token, url=download_url, filename=filename)

    def grid_backup(self, filename: Optional[str] = None) -> None:
        """
        Perform a NIOS Grid Backup.

        Args:
            filename: str, optional. The name of the backup file. Default is 'database.bak'.

        Returns:
            None

        Raises:
            httpx.RequestError: If an error occurs during the backup process.
        """
        payload = {"type": "BACKUP"}

        logging.info("step 1 - request gridbackup %s", filename)
        try:
            res = self.__getgriddata(payload)
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)

        token = res.get("token")
        download_url = res.get("url")

        logging.info("step 2 - saving backup to %s", filename)
        if token and download_url:
            self.file_download(token=token, url=download_url, filename=filename)

    def grid_restore(
        self,
        filename: str = "database.bak",
        mode: GridRestoreMode = "NORMAL",
        keep_grid_ip: bool = False,
    ):
        """
        Perform a NIOS Grid restore of a database using a given file.

        Args:
            filename (str): The filename of the database file to be restored. Default is
                            "database.bak".
            mode (GridRestoreMode): The restore mode to be used. Default is "NORMAL".
            keep_grid_ip (bool): Indicates whether to keep the grid IP address. Default is False.

        """
        token = self.file_upload(filename=filename)
        if not token:
            raise ValueError("Failed to upload file - no token received")

        # Execute the restore
        logging.info("step 3 - execute the grid restore")
        try:
            self.__restore_database(keep_grid_ip, mode, token)
        except httpx.RequestError as exc:
            logging.error("step 3 - Error: %s", exc)
            raise WapiRequestException(exc)
        logging.info("Grid restore successful!")

    def member_config(
        self,
        member: str,
        conf_type: MemberDataType,
        filename: Optional[str] = None,
        remote_url: str | None = None,
    ) -> None:
        """
        Fetch member configuration file for given service type.

        Args:
            member: A string representing the grid member.
            conf_type: An enum representing the type of config file.
            filename: A string value of the filename to save
            remote_url: An optional string representing the remote URL.

        Returns:
            A string representing the downloaded file.

        """
        logging.info(
            "fetching %s config file for grid member %s",
            conf_type,
            member,
        )
        payload = {"member": member, "type": conf_type}
        if remote_url:
            payload["remote_url"] = remote_url
        try:
            res = self.post(  # type:ignore[attr-defined]
                "fileop",
                params={"_function": "getmemberdata"},
                json=payload,
            )
            logging.debug(res.text)
            res.raise_for_status()
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)

        obj = res.json()
        download_url = obj.get("url")
        download_token = obj.get("token")

        self.file_download(token=download_token, url=download_url, filename=filename)

    def get_lease_history(
        self,
        member: str,
        start_time: int | None = None,
        end_time: int | None = None,
        remove_url: str | None = None,
    ) -> None:
        """
        fetch DHCP lease history files from a NIOS Grid Member

        Args:
            member: A string representing the grid member that the DHCP lease history is being fetched from.
            start_time: An optional integer representing the start time in epoch format. Defaults to None.
            end_time: An optional integer representing the end time in epoch format. Defaults to None.
            remove_url: An optional string representing the remove URL. Defaults to None.

        Returns:
            A string representing the filename of the downloaded DHCP lease history file.

        Raises:
            WapiRequestException: If there is an error in the API request.

        """
        logging.info("fetching DHCP lease history from grid member %s", member)
        payload = {"member": member}
        if start_time is not None:
            payload["start_time"] = start_time
        if end_time is not None:
            payload["end_time"] = end_time
        if remove_url is not None:
            payload["remove_url"] = remove_url
        try:
            res = self.post(  # ty:ignore[unresolved-attribute]
                "fileop",
                params={"_function": "getleasehistoryfiles"},
                json=payload,
            )
            logging.debug(res.text)
            res.raise_for_status()
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)

        obj = res.json()
        download_url = obj.get("url")
        download_token = obj.get("token")

        self.file_download(token=download_token, url=download_url)

    def __csv_import(
        self,
        task_operation: str,
        upload_token: str,
        exit_on_error: bool = False,
    ) -> dict:
        headers = {"content-type": "application/json"}

        # set the request parameters
        payload = {
            "action": "START",
            "doimport": True,
            "on_error": "STOP" if exit_on_error else "CONTINUE",
            "operation": task_operation,
            "separator": "COMMA",
            "token": upload_token,
        }

        # Update the operation if the user passes in MERGE or OVERRIDE directly
        if task_operation == "MERGE":
            payload["operation"] = "UPDATE"
            payload["update_method"] = "MERGE"
        elif task_operation == "OVERRIDE":
            payload["operation"] = "UPDATE"
            payload["update_method"] = "OVERRIDE"

        # start the CSV task in job manager
        try:
            res = self.post(  # ty:ignore[unresolved-attribute]
                "fileop",
                params={"_function": "csv_import"},
                json=payload,
                headers=headers,
                timeout=None,
            )
            logging.debug(pprint.pformat(res.text))
            res.raise_for_status()
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)

        return res.json()

    def __download_complete(self, token: str, filename: str) -> None:
        header = {"Content-type": "application/json"}
        payload = {"token": token}
        try:
            res = self.post(  # ty:ignore[unresolved-attribute]
                "fileop",
                params={"_function": "downloadcomplete"},
                json=payload,
                headers=header,
            )
            logging.info("file %s download complete", filename)
            res.raise_for_status()
        except httpx.TimeoutException as exc:
            logging.error(f"Timeout error: {exc}")
            raise WapiRequestException(exc) from exc
        except httpx.HTTPStatusError as exc:
            logging.error(f"HTTP error: {exc}")
            raise WapiRequestException(exc) from exc
        except httpx.RequestError as exc:
            logging.error(f"Request error: {exc}")
            raise WapiRequestException(exc) from exc

    def __download_file(self, download_url, filename=None) -> None:
        download_url = self.__update_url(url=download_url)
        header = {"Content-type": "application/force-download"}
        logging.info(download_url)
        self.conn.verify = self.ssl_verify  # ty:ignore[unresolved-attribute]
        with self.conn.stream(  # ty:ignore[unresolved-attribute]
            "GET",
            download_url,
            headers=header,
        ) as res:
            res.raise_for_status()
            with open(file=filename, mode="wb") as file_out:  # ty:ignore[no-matching-overload]
                for chunk in res.iter_bytes(chunk_size=1024):
                    file_out.write(chunk)

    def __getgriddata(self, payload: dict) -> dict:
        headers = {"content-type": "application/json"}
        try:
            res = self.post(  # ty:ignore[unresolved-attribute]
                "fileop",
                params={"_function": "getgriddata"},
                json=payload,
                headers=headers,
                timeout=None,
            )
            logging.debug(pprint.pformat(res.text))
            res.raise_for_status()
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)

        return res.json()

    def __restore_database(
        self, keep_grid_ip: bool, mode: str, upload_token: str
    ) -> dict:
        # set content type back to JSON
        headers = {"content-type": "application/json"}

        # set the request parameters
        payload = {
            "keep_grid_ip": keep_grid_ip,
            "mode": mode,
            "token": upload_token,
        }

        # start the restore
        try:
            res = self.post(  # ty:ignore[unresolved-attribute]
                "fileop",
                params={"_function": "restoredatabase"},
                json=payload,
                headers=headers,
                timeout=None,
            )
            logging.debug(pprint.pformat(res.text))
            res.raise_for_status()
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)
        return res

    def __update_url(self, url: str) -> str:
        if self.grid_mgr in url:
            return url
        elif "[" in url and "]" in url:
            ipv6_pattern = r"https://(\[[a-zA-Z0-9:]+\])"
            return re.sub(ipv6_pattern, f"https://{self.grid_mgr}", url)
        else:
            ipv4_pattern = r"https://(\d{1,3}\.){3}\d{1,3}"
            return re.sub(ipv4_pattern, f"https://{self.grid_mgr}", url)

    def __upload_file(self, upload_url: str, upload_file: dict) -> None:
        upload_url = self.__update_url(upload_url)
        logging.debug(upload_url)
        try:
            res = self.conn.post(  # ty:ignore[unresolved-attribute]
                upload_url,
                files=upload_file,
                timeout=None,
            )
            logging.debug(pprint.pformat(res.text))
            res.raise_for_status()
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)

    def __upload_init(self, filename: str) -> dict:
        headers = {"content-type": "application/json"}
        payload = {"filename": filename}
        try:
            res = self.post(  # pyright: ignore[reportAttributeAccessIssue]
                "fileop",
                params={"_function": "uploadinit"},
                headers=headers,
                json=payload,
            )
            logging.debug(pprint.pformat(res.text))
            res.raise_for_status()
        except httpx.RequestError as exc:
            logging.error(exc)
            raise WapiRequestException(exc)

        return res.json()

csv_export(wapi_object, filename=None)

Exports data in CSV format for the specified WAPI object and saves it to a file.

The method performs a CSV export operation for a specified WAPI object using the file operation capabilities provided by the WAPI (Web Application Programming Interface), downloads the resulting file from a given URL, and saves it to a user-specified filename or a default filename derived from the download URL. Finally, it finalizes the download process by notifying the completion.

Parameters:

Name Type Description Default
wapi_object str

The name of the WAPI object(s) whose data is to be exported.

required
filename Optional[str]

The optional local file path to save the downloaded CSV data. If not provided, the filename is automatically derived from the downloaded URL.

None

Raises:

Type Description
WapiRequestException

If an HTTP request fails during the CSV export process.

Source code in src/ibx_sdk/nios/fileop.py
def csv_export(self, wapi_object: str, filename: Optional[str] = None) -> None:
    """
    Exports data in CSV format for the specified WAPI object and saves it to a file.

    The method performs a CSV export operation for a specified WAPI object using the file
    operation capabilities provided by the WAPI (Web Application Programming Interface),
    downloads the resulting file from a given URL, and saves it to a user-specified filename
    or a default filename derived from the download URL. Finally, it finalizes the download
    process by notifying the completion.

    Parameters:
        wapi_object (str): The name of the WAPI object(s) whose data is to be exported.
        filename (Optional[str]): The optional local file path to save the downloaded CSV
            data. If not provided, the filename is automatically derived from the
            downloaded URL.

    Raises:
        WapiRequestException: If an HTTP request fails during the CSV export process.
    """
    if filename:
        (_, filename) = os.path.split(filename)
        filename = os.path.join(_, filename.replace("-", "_"))

    # Call WAPI fileop  csv_export function
    logging.info("performing csv export for %s object(s)", wapi_object)
    payload = {"_object": wapi_object}
    try:
        response = self.post(  # ty:ignore[unresolved-attribute]
            "fileop",
            params={"_function": "csv_export"},
            json=payload,
        )
        logging.debug(response.text)
        response.raise_for_status()
        try:
            obj = response.json()
        except httpx.DecodingError as exc:
            logging.error(f"DecodingError: {exc}")
            raise WapiRequestException(response.text) from exc
    except httpx.TimeoutException as exc:
        logging.error(f"Timeout error: {exc}")
        raise WapiRequestException(exc) from exc
    except httpx.HTTPStatusError as exc:
        logging.error(f"HTTP error: {exc}")
        raise WapiRequestException(exc) from exc
    except httpx.RequestError as exc:
        logging.error(f"Request error: {exc}")
        raise WapiRequestException(exc) from exc

    download_url = obj.get("url")
    download_token = obj.get("token")

    logging.info("downloading data from %s", download_url)

    if not filename:
        filename = util.extract_filename_from_url(download_url)

    self.__download_file(download_url, filename)

    self.__download_complete(download_token, filename)

csv_import(task_operation, csv_import_file, exit_on_error=False)

Perform a CSV import task using the NIOS CSV Task Manager

Parameters:

Name Type Description Default
task_operation CsvOperation

The operation to be performed on the CSV file. Should be a value from the CsvOperation enum.

required
csv_import_file str

The path to the CSV file to be imported.

required
exit_on_error bool

Indicates whether the program should exit if an error occurs during the import process. Default value is False.

False

Returns:

Type Description
dict

A dictionary containing the result of the CSV import task.

Raises:

Type Description
RequestError

If an error occurs while making HTTP requests.

Source code in src/ibx_sdk/nios/fileop.py
def csv_import(
    self,
    task_operation: CsvOperation,
    csv_import_file: str,
    exit_on_error: bool = False,
) -> dict:
    """
    Perform a CSV import task using the NIOS CSV Task Manager

    Args:
        task_operation (CsvOperation): The operation to be performed on the CSV file. Should be
                                       a value from the `CsvOperation` enum.
        csv_import_file (str): The path to the CSV file to be imported.
        exit_on_error (bool): Indicates whether the program should exit if an error occurs
                              during the import process. Default value is `False`.

    Returns:
        A dictionary containing the result of the CSV import task.

    Raises:
        httpx.RequestError: If an error occurs while making HTTP requests.
    """
    token = self.file_upload(filename=csv_import_file)
    if not token:
        raise ValueError("Invalid token")

    # submit task to CSV Job Manager
    logging.info(
        "step 3 - execute the csv_import %s job on %s",
        task_operation,
        csv_import_file,
    )
    try:
        csvtask = self.__csv_import(task_operation.upper(), token, exit_on_error)
    except httpx.RequestError as exc:
        logging.error(exc)
        raise WapiRequestException(exc)
    else:
        return csvtask

csvtask_status(csvtask)

Fetch the status of a CSV submitted task

Parameters:

Name Type Description Default
csvtask dict

The dictionary containing the information about the CSV import task.

required

Returns:

Name Type Description
dict dict

The JSON response containing the status of the CSV import task.

Raises:

Type Description
RequestException

If there is an error in the request.

Description

This method is used to check the status of a CSV import task. It takes a dictionary, csvtask, as input, which contains the information about the CSV import task. The csvtask dictionary should have the following structure:

{ 'csv_import_task': { '_ref': '' } }

The method first retrieves the _ref value from the csvtask dictionary to identify the import task. It then sends a request to retrieve the status of the import task using the get method.

If the status request is successful, the JSON response containing the status is logged using the debug level.

Finally, the method returns the JSON response containing the status of the CSV import task.

If there is any error in the request, a RequestException is raised and logged using the error level.

Example usage:

1
2
3
4
5
6
csvtask = {
    'csv_import_task': {
        '_ref': '12345678'
    }
}
status = csvtask_status(csvtask)
Source code in src/ibx_sdk/nios/fileop.py
def csvtask_status(self, csvtask: dict) -> dict:
    """
    Fetch the status of a CSV submitted task

    Args:
        csvtask (dict): The dictionary containing the information about the CSV import task.

    Returns:
        dict: The JSON response containing the status of the CSV import task.

    Raises:
        RequestException: If there is an error in the request.

    Description:
        This method is used to check the status of a CSV import task. It takes a dictionary,
        `csvtask`, as input, which contains the information about the CSV import task. The
        `csvtask` dictionary should have the following structure:

        {
            'csv_import_task': {
                '_ref': '<CSV import task reference>'
            }
        }

        The method first retrieves the `_ref` value from the `csvtask` dictionary to identify
        the import task. It then sends a request to retrieve the status of the import task
        using the `get` method.

        If the status request is successful, the JSON response containing the status is
        logged using the `debug` level.

        Finally, the method returns the JSON response containing the status of the CSV import
        task.

        If there is any error in the request, a `RequestException` is raised and logged using
        the `error` level.

    Example usage:

    ```python
    csvtask = {
        'csv_import_task': {
            '_ref': '12345678'
        }
    }
    status = csvtask_status(csvtask)
    ```
    """
    _ref = csvtask["csv_import_task"]["_ref"]
    logging.debug("Checking status of csvimporttask %s", _ref)
    try:
        res = self.get(_ref)  # ty:ignore[unresolved-attribute]
        res.raise_for_status()
    except httpx.RequestError as exc:
        logging.error(exc)
        raise WapiRequestException(exc)
    else:
        logging.debug(res.json())

    return res.json()

download_certificate(member, certificate_usage='ADMIN')

Download SSL certificate from the Grid.

Parameters:

Name Type Description Default
member str

The identifier of the member for whom the certificate is being downloaded.

required
certificate_usage SupportedCertTypes

The type of certificate to be downloaded (e.g., "ADMIN").

'ADMIN'
Source code in src/ibx_sdk/nios/fileop.py
def download_certificate(
    self,
    member: str,
    certificate_usage: SupportedCertTypes = "ADMIN",
):
    """
    Download SSL certificate from the Grid.

    Args:
        member: The identifier of the member for whom the certificate is being downloaded.
        certificate_usage: The type of certificate to be downloaded (e.g., "ADMIN").
    """
    logging.info(
        "Downloading %s certificate for %s",
        certificate_usage,
        member,
    )
    payload = {
        "member": member,
        "certificate_usage": certificate_usage,
    }
    logging.debug("json payload %s", payload)

    try:
        res = self.post(  # ty:ignore[unresolved-attribute]
            "fileop",
            params={"_function": "downloadcertificate"},
            json=payload,
        )
        logging.debug(res.text)
        res.raise_for_status()
    except httpx.RequestError as exc:
        logging.error(exc)
        raise WapiRequestException(exc)

    obj = res.json()
    download_url = obj.get("url")
    download_token = obj.get("token")

    self.file_download(token=download_token, url=download_url)

file_download(token, url, filename=None)

file_download downloads the generated file from the NIOS Grid using a token and url

Parameters:

Name Type Description Default
token str

Authentication token required for the download completion.

required
url str

URL of the file to be downloaded.

required
filename str | None

Optional; name for the downloaded file. If not provided, it will be extracted

None

Returns:

Type Description
None

None

Source code in src/ibx_sdk/nios/fileop.py
def file_download(
    self,
    token: str,
    url: str,
    filename: str | None = None,
) -> None:
    """
    file_download downloads the generated file from the NIOS Grid using a token and url

    Args:
        token: Authentication token required for the download completion.
        url: URL of the file to be downloaded.
        filename: Optional; name for the downloaded file. If not provided, it will be extracted
        from the URL.

    Returns:
        None
    """
    logging.info("downloading data from %s", url)
    if not filename:
        filename = util.extract_filename_from_url(url)

    try:
        self.__download_file(url, filename)
    except httpx.TimeoutException as exc:
        logging.error(f"Timeout error: {exc}")
        raise WapiRequestException(exc) from exc
    except httpx.HTTPStatusError as exc:
        logging.error(f"HTTP error: {exc}")
        raise WapiRequestException(exc) from exc
    except httpx.RequestError as exc:
        logging.error(f"Request error: {exc}")
        raise WapiRequestException(exc) from exc

    try:
        self.__download_complete(token, filename)
    except httpx.TimeoutException as exc:
        logging.error(f"Timeout error: {exc}")
        raise WapiRequestException(exc) from exc
    except httpx.HTTPStatusError as exc:
        logging.error(f"HTTP error: {exc}")
        raise WapiRequestException(exc) from exc
    except httpx.RequestError as exc:
        logging.error(f"Request error: {exc}")
        raise WapiRequestException(exc) from exc

file_upload(filename)

Perform a file upload into the NIOS Grid.

Parameters:

Name Type Description Default
filename str

The path of the file to be uploaded.

required

Returns:

Name Type Description
str str | None

The token received upon successful upload initialization.

Raises:

Type Description
WapiRequestException

If there is a request exception during the upload process.

Source code in src/ibx_sdk/nios/fileop.py
def file_upload(self, filename: str) -> str | None:
    """
    Perform a file upload into the NIOS Grid.

    Args:
        filename: The path of the file to be uploaded.

    Returns:
        str: The token received upon successful upload initialization.

    Raises:
        WapiRequestException: If there is a request exception during the upload process.
    """
    (path, filename) = os.path.split(filename)
    valid_filename = filename.replace("-", "_")

    # Call WAPI fileop Upload INIT
    logging.info("step 1 - request uploadinit %s", filename)
    try:
        obj = self.__upload_init(filename=valid_filename)
    except httpx.TimeoutException as exc:
        logging.error(f"Timeout error: {exc}")
        raise WapiRequestException(exc) from exc
    except httpx.HTTPStatusError as exc:
        logging.error(f"HTTP error: {exc}")
        raise WapiRequestException(exc) from exc
    except httpx.RequestError as exc:
        logging.error(f"Request error: {exc}")
        raise WapiRequestException(exc) from exc

    upload_url = obj.get("url")
    token = obj.get("token")

    if not token or not upload_url:
        raise ValueError("Invalid token or upload URL")

    # specify a file handle for the file data to be uploaded
    with open(os.path.join(path, filename), "rb") as fh:
        # reset to top of the file
        fh.seek(0)
        upload_file = {"file": fh.read()}

        # Upload the contents of the CSV file
        logging.info("step 2 - post the files using the upload_url provided")
        try:
            self.__upload_file(upload_url, upload_file)
        except httpx.TimeoutException as exc:
            logging.error(f"Timeout error: {exc}")
            raise WapiRequestException(exc) from exc
        except httpx.HTTPStatusError as exc:
            logging.error(f"HTTP error: {exc}")
            raise WapiRequestException(exc) from exc
        except httpx.RequestError as exc:
            logging.error(f"Request error: {exc}")
            raise WapiRequestException(exc) from exc
    return token

generate_csr(cn, member, algorithm='SHA-256', certificate_usage='ADMIN', comment=None, country=None, email=None, key_size=2048, locality=None, org=None, org_unit=None, state=None, subject_alternative_names=None)

Generate a Certificate Signing Request

Generate and download a CSR for a member of the grid. Once the CSR is generated it is downloaded and saved locally to the current working directory.

Parameters:

Name Type Description Default
cn str

Common Name for the certificate.

required
member str

The member for which the certificate is being generated.

required
algorithm SupportedAlgorithms

Algorithm used for certificate generation, default is "SHA-256".

'SHA-256'
certificate_usage SupportedCertUsages

Purpose of the certificate, default is "ADMIN".

'ADMIN'
comment Optional[str]

Optional comment for the certificate.

None
country Optional[str]

Optional country code for the certificate.

None
email Optional[str]

Optional email address for the certificate.

None
key_size Optional[SupportedKeySizes]

Optional key size for the certificate, default is 2048.

2048
locality Optional[str]

Optional locality or city for the certificate.

None
org Optional[str]

Optional organization name for the certificate.

None
org_unit Optional[str]

Optional organizational unit for the certificate.

None
state Optional[str]

Optional state or province for the certificate.

None
subject_alternative_names Optional[list[dict]]

Optional list of subject alternative names (SANs) for the certificate.

None

Returns:

Type Description
None

None

Source code in src/ibx_sdk/nios/fileop.py
def generate_csr(
    self,
    cn: str,
    member: str,
    algorithm: SupportedAlgorithms = "SHA-256",
    certificate_usage: SupportedCertUsages = "ADMIN",
    comment: Optional[str] = None,
    country: Optional[str] = None,
    email: Optional[str] = None,
    key_size: Optional[SupportedKeySizes] = 2048,
    locality: Optional[str] = None,
    org: Optional[str] = None,
    org_unit: Optional[str] = None,
    state: Optional[str] = None,
    subject_alternative_names: Optional[list[dict]] = None,
) -> None:
    """
    Generate a Certificate Signing Request

    Generate and download a CSR for a member of the grid. Once the CSR is generated it is
    downloaded and saved locally to the current working directory.

    Args:
        cn: Common Name for the certificate.
        member: The member for which the certificate is being generated.
        algorithm: Algorithm used for certificate generation, default is "SHA-256".
        certificate_usage: Purpose of the certificate, default is "ADMIN".
        comment: Optional comment for the certificate.
        country: Optional country code for the certificate.
        email: Optional email address for the certificate.
        key_size: Optional key size for the certificate, default is 2048.
        locality: Optional locality or city for the certificate.
        org: Optional organization name for the certificate.
        org_unit: Optional organizational unit for the certificate.
        state: Optional state or province for the certificate.
        subject_alternative_names: Optional list of subject alternative names (SANs) for the
                certificate.

    Returns:
        None
    """
    logging.info(locals())
    logging.info("generating %s csr for %s", certificate_usage, member)
    payload = {
        "cn": cn,
        "member": member,
        "algorithm": algorithm,
        "certificate_usage": certificate_usage,
    }
    # optional params
    for param in [
        "country",
        "email",
        "key_size",
        "comment",
        "locality",
        "org",
        "org_unit",
        "state",
        "subject_alternative_names",
    ]:
        if locals()[param] is not None:
            payload[param] = locals()[param]

    logging.debug("json payload %s", payload)

    try:
        res = self.post(  # ty:ignore[unresolved-attribute]
            "fileop",
            params={"_function": "generatecsr"},
            json=payload,
        )
        logging.debug(res.text)
        res.raise_for_status()
    except httpx.RequestError as exc:
        logging.error(exc)
        raise WapiRequestException(exc)

    obj = res.json()
    download_url = obj.get("url")
    download_token = obj.get("token")

    self.file_download(token=download_token, url=download_url)

generate_selfsigned_cert(cn, member, days_valid=365, algorithm='SHA-256', certificate_usage='ADMIN', comment=None, country=None, email=None, key_size=2048, locality=None, org=None, org_unit=None, state=None, subject_alternative_names=None)

Generate a Self-Signed Certificate on the Grid.

Parameters:

Name Type Description Default
cn str

The common name for the certificate.

required
member str

The member name for the certificate.

required
days_valid int

The number of days the certificate is valid for. Default is 365.

365
algorithm SupportedAlgorithms

The algorithm used for certificate generation. Default is "SHA-256".

'SHA-256'
certificate_usage SupportedCertUsages

The usage type of the certificate. Default is "ADMIN".

'ADMIN'
comment Optional[str]

Optional comment associated with the certificate.

None
country Optional[str]

Optional country code.

None
email Optional[str]

Optional email address.

None
key_size Optional[SupportedKeySizes]

The size of the key used in certificate generation. Default is 2048.

2048
locality Optional[str]

Optional locality (e.g., city).

None
org Optional[str]

Optional organization name.

None
org_unit Optional[str]

Optional organizational unit.

None
state Optional[str]

Optional state or province.

None
subject_alternative_names Optional[list[dict]]

Optional list of subject alternative names.

None
Source code in src/ibx_sdk/nios/fileop.py
def generate_selfsigned_cert(
    self,
    cn: str,
    member: str,
    days_valid: int = 365,
    algorithm: SupportedAlgorithms = "SHA-256",
    certificate_usage: SupportedCertUsages = "ADMIN",
    comment: Optional[str] = None,
    country: Optional[str] = None,
    email: Optional[str] = None,
    key_size: Optional[SupportedKeySizes] = 2048,
    locality: Optional[str] = None,
    org: Optional[str] = None,
    org_unit: Optional[str] = None,
    state: Optional[str] = None,
    subject_alternative_names: Optional[list[dict]] = None,
):
    """
    Generate a Self-Signed Certificate on the Grid.

    Args:
        cn: The common name for the certificate.
        member: The member name for the certificate.
        days_valid: The number of days the certificate is valid for. Default is 365.
        algorithm: The algorithm used for certificate generation. Default is "SHA-256".
        certificate_usage: The usage type of the certificate. Default is "ADMIN".
        comment: Optional comment associated with the certificate.
        country: Optional country code.
        email: Optional email address.
        key_size: The size of the key used in certificate generation. Default is 2048.
        locality: Optional locality (e.g., city).
        org: Optional organization name.
        org_unit: Optional organizational unit.
        state: Optional state or province.
        subject_alternative_names: Optional list of subject alternative names.

    """
    logging.info("generating self-signed certificate for %s", member)
    payload = {
        "cn": cn,
        "member": member,
        "algorithm": algorithm,
        "certificate_usage": certificate_usage,
        "days_valid": days_valid,
    }
    if comment:
        payload["comment"] = comment
    if country:
        payload["country"] = country
    if email:
        payload["email"] = email
    if key_size:
        payload["key_size"] = key_size
    if locality:
        payload["locality"] = locality
    if org:
        payload["org"] = org
    if org_unit:
        payload["org_unit"] = org_unit
    if state:
        payload["state"] = state
    if subject_alternative_names:
        payload["subject_alternative_names"] = subject_alternative_names
    logging.debug("json payload %s", payload)

    try:
        res = self.post(  # ty:ignore[unresolved-attribute]
            "fileop",
            params={"_function": "generateselfsignedcert"},
            json=payload,
        )
        logging.debug(res.text)
        res.raise_for_status()
    except httpx.RequestError as exc:
        logging.error(exc)
        raise WapiRequestException(exc)

    obj = res.json()
    download_url = obj.get("url")
    download_token = obj.get("token")

    self.file_download(token=download_token, url=download_url)

get_csv_errors_file(filename, job_id)

Fetches the csv-errors file for a specific job ID.

Parameters:

Name Type Description Default
filename str

The filename to be used when saving the csv-errors file.

required
job_id str

The job ID for which the csv-errors file should be fetched.

required

Returns:

Type Description
None

None

Raises:

Type Description
RequestError

If there is an error during the request.

Source code in src/ibx_sdk/nios/fileop.py
def get_csv_errors_file(self, filename: str, job_id: str) -> None:
    """
    Fetches the csv-errors file for a specific job ID.

    Args:
        filename (str): The filename to be used when saving the csv-errors file.
        job_id (str): The job ID for which the csv-errors file should be fetched.

    Returns:
        None

    Raises:
        httpx.RequestError: If there is an error during the request.

    """
    logging.debug("fetching csv-errors file for job id %s", job_id)
    payload = {"import_id": job_id}
    try:
        res = self.post(  # ty:ignore[unresolved-attribute]
            "fileop",
            params={"_function": "csv_error_log"},
            json=payload,
        )
        logging.debug(pprint.pformat(res.text))
        res.raise_for_status()
    except httpx.RequestError as exc:
        logging.error(exc)
        raise WapiRequestException(exc)

    obj = res.json()
    token = obj.get("token")
    download_url = obj.get("url")

    csv_error_file = f"csv-errors-{filename}.csv"
    try:
        self.__download_file(download_url, csv_error_file)
        res.raise_for_status()
    except httpx.RequestError as exc:
        logging.error(exc)
        raise WapiRequestException(exc)

    # We're done - so post to downloadcomplete function
    try:
        self.__download_complete(token, csv_error_file)
    except httpx.RequestError as exc:
        logging.error(exc)
        raise WapiRequestException(exc)

get_lease_history(member, start_time=None, end_time=None, remove_url=None)

fetch DHCP lease history files from a NIOS Grid Member

Parameters:

Name Type Description Default
member str

A string representing the grid member that the DHCP lease history is being fetched from.

required
start_time int | None

An optional integer representing the start time in epoch format. Defaults to None.

None
end_time int | None

An optional integer representing the end time in epoch format. Defaults to None.

None
remove_url str | None

An optional string representing the remove URL. Defaults to None.

None

Returns:

Type Description
None

A string representing the filename of the downloaded DHCP lease history file.

Raises:

Type Description
WapiRequestException

If there is an error in the API request.

Source code in src/ibx_sdk/nios/fileop.py
def get_lease_history(
    self,
    member: str,
    start_time: int | None = None,
    end_time: int | None = None,
    remove_url: str | None = None,
) -> None:
    """
    fetch DHCP lease history files from a NIOS Grid Member

    Args:
        member: A string representing the grid member that the DHCP lease history is being fetched from.
        start_time: An optional integer representing the start time in epoch format. Defaults to None.
        end_time: An optional integer representing the end time in epoch format. Defaults to None.
        remove_url: An optional string representing the remove URL. Defaults to None.

    Returns:
        A string representing the filename of the downloaded DHCP lease history file.

    Raises:
        WapiRequestException: If there is an error in the API request.

    """
    logging.info("fetching DHCP lease history from grid member %s", member)
    payload = {"member": member}
    if start_time is not None:
        payload["start_time"] = start_time
    if end_time is not None:
        payload["end_time"] = end_time
    if remove_url is not None:
        payload["remove_url"] = remove_url
    try:
        res = self.post(  # ty:ignore[unresolved-attribute]
            "fileop",
            params={"_function": "getleasehistoryfiles"},
            json=payload,
        )
        logging.debug(res.text)
        res.raise_for_status()
    except httpx.RequestError as exc:
        logging.error(exc)
        raise WapiRequestException(exc)

    obj = res.json()
    download_url = obj.get("url")
    download_token = obj.get("token")

    self.file_download(token=download_token, url=download_url)

get_log_files(log_type, filename=None, endpoint=None, include_rotated=False, member=None, msserver=None, node_type=None)

Fetch the log files for the provided member or msserver

Parameters:

Name Type Description Default
log_type LogType

The type of log files to fetch.

required
filename str

The name of the log file to download (Default value = None)

None
endpoint str

The specific endpoint for which to fetch log files. (Default: None)

None
include_rotated bool

Whether to include rotated log files. (Default: False)

False
member str

The member for which to fetch log files. (Default: None)

None
msserver str

The msserver for which to fetch log files. (Default: None)

None
node_type Optional[Literal['ACTIVE', 'BACKUP']]

The type of node for which to fetch log files. Can be 'ACTIVE' or 'BACKUP'. (Default: None)

None
Source code in src/ibx_sdk/nios/fileop.py
def get_log_files(
    self,
    log_type: LogType,
    filename: Optional[str] = None,
    endpoint: Optional[str] = None,
    include_rotated: bool = False,
    member: Optional[str] = None,
    msserver: Optional[str] = None,
    node_type: Optional[Literal["ACTIVE", "BACKUP"]] = None,
):
    """
    Fetch the log files for the provided member or msserver

    Args:
        log_type (LogType): The type of log files to fetch.
        filename (str): The name of the log file to download (Default value = None)
        endpoint (str): The specific endpoint for which to fetch log files. (Default: None)
        include_rotated (bool): Whether to include rotated log files. (Default: False)
        member (str): The member for which to fetch log files. (Default: None)
        msserver (str): The msserver for which to fetch log files. (Default: None)
        node_type: The type of node for which to fetch log files. Can be 'ACTIVE' or
                   'BACKUP'. (Default: None)
    """
    logging.info("fetching %s log files for %s", log_type, member)
    payload = {
        "log_type": log_type,
        "include_rotated": include_rotated,
    }

    if endpoint:
        payload["endpoint"] = endpoint
    if member:
        payload["member"] = member
    if node_type:
        payload["node_type"] = node_type
    if msserver:
        payload["msserver"] = msserver

    logging.debug("json payload %s", payload)

    try:
        res = self.post(  # ty:ignore[unresolved-attribute]
            "fileop",
            params={"_function": "get_log_files"},
            json=payload,
        )
        logging.debug(res.text)
        res.raise_for_status()
    except httpx.RequestError as exc:
        logging.error(exc)
        raise WapiRequestException(exc)

    obj = res.json()
    download_url = obj.get("url")
    download_token = obj.get("token")

    self.file_download(token=download_token, url=download_url, filename=filename)

get_support_bundle(member, filename=None, cached_zone_data=False, core_files=False, log_files=False, nm_snmp_logs=False, recursive_cache_file=False, remote_url=None, rotate_log_files=False)

Get the support bundle for a member.

Parameters:

Name Type Description Default
member str

The member for which to retrieve the support bundle.

required
filename str

The filename of the support bundle.

None
cached_zone_data bool

Whether to include cached zone data in the support bundle. Defaults to False.

False
core_files bool

Whether to include core files in the support bundle. Defaults to False.

False
log_files bool

Whether to include log files in the support bundle. Defaults to False.

False
nm_snmp_logs bool

Whether to include NM SNMP logs in the support bundle. Defaults to False.

False
recursive_cache_file bool

Whether to include recursive cache file in the support bundle. Defaults to False.

False
remote_url str

The remote URL where the support bundle will be uploaded. Defaults to None.

None
rotate_log_files bool

Whether to rotate log files before creating the support bundle. Defaults to False.

False

Raises:

Type Description
RequestError

If an error occurs during the request.

Source code in src/ibx_sdk/nios/fileop.py
def get_support_bundle(
    self,
    member: str,
    filename: Optional[str] = None,
    cached_zone_data: bool = False,
    core_files: bool = False,
    log_files: bool = False,
    nm_snmp_logs: bool = False,
    recursive_cache_file: bool = False,
    remote_url: Optional[str] = None,
    rotate_log_files: bool = False,
):
    """
    Get the support bundle for a member.

    Args:
        member (str): The member for which to retrieve the support bundle.
        filename (str, optional): The filename of the support bundle.
        cached_zone_data (bool, optional): Whether to include cached zone data in the support
                                           bundle. Defaults to False.
        core_files (bool, optional): Whether to include core files in the support bundle.
                                     Defaults to False.
        log_files (bool, optional): Whether to include log files in the support bundle.
                                    Defaults to False.
        nm_snmp_logs (bool, optional): Whether to include NM SNMP logs in the support bundle.
                                       Defaults to False.
        recursive_cache_file (bool, optional): Whether to include recursive cache file in the
                                               support bundle. Defaults to False.
        remote_url (str, optional): The remote URL where the support bundle will be uploaded.
                                    Defaults to None.
        rotate_log_files (bool, optional): Whether to rotate log files before creating the
                                           support bundle. Defaults to False.

    Raises:
        httpx.RequestError: If an error occurs during the request.

    """
    logging.info("performing get_support_bundle for %s object(s)", member)
    payload = {
        "member": member,
        "cached_zone_data": cached_zone_data,
        "core_files": core_files,
        "log_files": log_files,
        "nm_snmp_logs": nm_snmp_logs,
        "recursive_cache_file": recursive_cache_file,
        "rotate_log_files": rotate_log_files,
    }
    if remote_url:
        payload["remote_url"] = remote_url
    logging.debug(pprint.pformat(payload))
    try:
        res = self.post(  # ty:ignore[unresolved-attribute]
            "fileop",
            params={"_function": "get_support_bundle"},
            json=payload,
            timeout=None,
        )
        logging.debug(res.text)
        res.raise_for_status()
    except httpx.RequestError as exc:
        logging.error(exc)
        raise WapiRequestException(exc)

    obj = res.json()
    download_url = obj.get("url")
    download_token = obj.get("token")

    self.file_download(token=download_token, url=download_url, filename=filename)

grid_backup(filename=None)

Perform a NIOS Grid Backup.

Parameters:

Name Type Description Default
filename Optional[str]

str, optional. The name of the backup file. Default is 'database.bak'.

None

Returns:

Type Description
None

None

Raises:

Type Description
RequestError

If an error occurs during the backup process.

Source code in src/ibx_sdk/nios/fileop.py
def grid_backup(self, filename: Optional[str] = None) -> None:
    """
    Perform a NIOS Grid Backup.

    Args:
        filename: str, optional. The name of the backup file. Default is 'database.bak'.

    Returns:
        None

    Raises:
        httpx.RequestError: If an error occurs during the backup process.
    """
    payload = {"type": "BACKUP"}

    logging.info("step 1 - request gridbackup %s", filename)
    try:
        res = self.__getgriddata(payload)
    except httpx.RequestError as exc:
        logging.error(exc)
        raise WapiRequestException(exc)

    token = res.get("token")
    download_url = res.get("url")

    logging.info("step 2 - saving backup to %s", filename)
    if token and download_url:
        self.file_download(token=token, url=download_url, filename=filename)

grid_restore(filename='database.bak', mode='NORMAL', keep_grid_ip=False)

Perform a NIOS Grid restore of a database using a given file.

Parameters:

Name Type Description Default
filename str

The filename of the database file to be restored. Default is "database.bak".

'database.bak'
mode GridRestoreMode

The restore mode to be used. Default is "NORMAL".

'NORMAL'
keep_grid_ip bool

Indicates whether to keep the grid IP address. Default is False.

False
Source code in src/ibx_sdk/nios/fileop.py
def grid_restore(
    self,
    filename: str = "database.bak",
    mode: GridRestoreMode = "NORMAL",
    keep_grid_ip: bool = False,
):
    """
    Perform a NIOS Grid restore of a database using a given file.

    Args:
        filename (str): The filename of the database file to be restored. Default is
                        "database.bak".
        mode (GridRestoreMode): The restore mode to be used. Default is "NORMAL".
        keep_grid_ip (bool): Indicates whether to keep the grid IP address. Default is False.

    """
    token = self.file_upload(filename=filename)
    if not token:
        raise ValueError("Failed to upload file - no token received")

    # Execute the restore
    logging.info("step 3 - execute the grid restore")
    try:
        self.__restore_database(keep_grid_ip, mode, token)
    except httpx.RequestError as exc:
        logging.error("step 3 - Error: %s", exc)
        raise WapiRequestException(exc)
    logging.info("Grid restore successful!")

member_config(member, conf_type, filename=None, remote_url=None)

Fetch member configuration file for given service type.

Parameters:

Name Type Description Default
member str

A string representing the grid member.

required
conf_type MemberDataType

An enum representing the type of config file.

required
filename Optional[str]

A string value of the filename to save

None
remote_url str | None

An optional string representing the remote URL.

None

Returns:

Type Description
None

A string representing the downloaded file.

Source code in src/ibx_sdk/nios/fileop.py
def member_config(
    self,
    member: str,
    conf_type: MemberDataType,
    filename: Optional[str] = None,
    remote_url: str | None = None,
) -> None:
    """
    Fetch member configuration file for given service type.

    Args:
        member: A string representing the grid member.
        conf_type: An enum representing the type of config file.
        filename: A string value of the filename to save
        remote_url: An optional string representing the remote URL.

    Returns:
        A string representing the downloaded file.

    """
    logging.info(
        "fetching %s config file for grid member %s",
        conf_type,
        member,
    )
    payload = {"member": member, "type": conf_type}
    if remote_url:
        payload["remote_url"] = remote_url
    try:
        res = self.post(  # type:ignore[attr-defined]
            "fileop",
            params={"_function": "getmemberdata"},
            json=payload,
        )
        logging.debug(res.text)
        res.raise_for_status()
    except httpx.RequestError as exc:
        logging.error(exc)
        raise WapiRequestException(exc)

    obj = res.json()
    download_url = obj.get("url")
    download_token = obj.get("token")

    self.file_download(token=download_token, url=download_url, filename=filename)

upload_certificate(member, filename, certificate_usage='ADMIN')

Upload an SSL Certificate file to the Grid

Parameters:

Name Type Description Default
member str

The member identifier to which the certificate will be uploaded.

required
filename str

The filename of the certificate to be uploaded.

required
certificate_usage SupportedCertTypes

The usage type of the certificate. Default is "ADMIN".

'ADMIN'

Raises:

Type Description
WapiRequestException

If there is an error during the request to upload the certificate.

Source code in src/ibx_sdk/nios/fileop.py
def upload_certificate(
    self,
    member: str,
    filename: str,
    certificate_usage: SupportedCertTypes = "ADMIN",
):
    """
    Upload an SSL Certificate file to the Grid

    Args:
        member: The member identifier to which the certificate will be uploaded.
        filename: The filename of the certificate to be uploaded.
        certificate_usage: The usage type of the certificate. Default is "ADMIN".

    Raises:
        WapiRequestException: If there is an error during the request to upload the certificate.
    """
    token = self.file_upload(filename=filename)

    # submit task to CSV Job Manager
    logging.info(
        "step 3 - upload %s certificate on %s",
        certificate_usage,
        member,
    )
    payload = {
        "certificate_usage": certificate_usage,
        "member": member,
        "token": token,
    }
    try:
        res = self.post(  # pyright: ignore[reportAttributeAccessIssue]
            "fileop",
            params={"_function": "uploadcertificate"},
            json=payload,
        )
        logging.debug(pprint.pformat(res.text))
        res.raise_for_status()
    except httpx.RequestError as exc:
        logging.error(exc)
        raise WapiRequestException(exc)