# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
#
# Code generated by aaz-dev-tools
# --------------------------------------------------------------------------------------------

from azure.cli.testsdk import *


class DataboxScenario(ScenarioTest):
    @ResourceGroupPreparer(name_prefix='cli_test_databox')
    @StorageAccountPreparer(parameter_name='storage_account_1')
    @StorageAccountPreparer(parameter_name='storage_account_2')
    def test_databox(self, storage_account_1, storage_account_2):
        self.kwargs.update({
            'job_name': self.create_random_name('job', 24),
            'job_name_2': self.create_random_name('job', 24),
            'job_name_3': self.create_random_name('job', 24),
            'storage_account_1': storage_account_1,
            'storage_account_2': storage_account_2,
            'managed-rg': self.create_random_name('rg-', 10)
        })

        # Create a databox job with sku 'DataBox' and transfer-type 'ImportToAzure'.
        self.cmd('databox job create '
                 '--resource-group {rg} '
                 '--name {job_name} '
                 '--location westus '
                 '--sku DataBox '
                 '--contact-name "Public SDK Test" '
                 '--phone 14258828080 '
                 '--email-list testing@microsoft.com '
                 '--street-address1 "1 MICROSOFT WAY" '
                 '--city Redmond '
                 '--state-or-province WA '
                 '--country US '
                 '--postal-code 98052 '
                 '--company-name Microsoft '
                 '--storage-account {storage_account_1} {storage_account_2} '
                 '--staging-storage-account {storage_account_1} '
                 '--resource-group-for-managed-disk {managed-rg} '
                 '--transfer-type ImportToAzure '
                 '--kek-type MicrosoftManaged',
                 checks=[
                     self.check('status', 'DeviceOrdered'),
                     self.check('transferType', 'ImportToAzure'),
                     self.check('details.keyEncryptionKey.kekType', 'MicrosoftManaged')
                 ])

        self.cmd('databox job update '
                 '--resource-group {rg} '
                 '--name {job_name} '
                 '--contact-name "Public SDK Test 1" '
                 '--email-list testing1@microsoft.com '
                 '--phone 14258828081',
                 checks=[
                     self.check('details.contactDetails.contactName', 'Public SDK Test 1'),
                     self.check('details.contactDetails.emailList[0]', 'testing1@microsoft.com'),
                     self.check('details.contactDetails.phone', '14258828081')
                 ])

        self.cmd('databox job show --resource-group {rg} --name {job_name}', checks=[
            self.check('name', '{job_name}'),
            self.check('isCancellable', True),
            self.check('isDeletable', False),
            self.check('details.contactDetails.contactName', 'Public SDK Test 1'),
            self.check('details.contactDetails.emailList[0]', 'testing1@microsoft.com'),
            self.check('status', 'DeviceOrdered'),
            self.check('transferType', 'ImportToAzure'),
            self.check('details.keyEncryptionKey.kekType', 'MicrosoftManaged')
        ])

        self.cmd('databox job list '
                 '--resource-group {rg}',
                 checks=[JMESPathCheck('length(@)', 1)])

        self.cmd('databox job cancel '
                 '--resource-group {rg} '
                 '--name {job_name} '
                 '--reason "CancelTest" '
                 '-y',
                 checks=[])

        self.cmd('databox job show '
                 '--resource-group {rg} '
                 '--name {job_name}',
                 checks=[
                     self.check('name', '{job_name}'),
                     self.check('isCancellable', False),
                     self.check('isDeletable', True)])

        self.cmd('databox job delete '
                 '--resource-group {rg} '
                 '--name {job_name} '
                 '-y',
                 checks=[])

        self.cmd('databox job show '
                 '--resource-group {rg} '
                 '--name {job_name}',
                 expect_failure=True)

        # Create another databox job with sku 'DataBoxDisk'.
        self.cmd('databox job create '
                 '--resource-group {rg} '
                 '--name {job_name_2} '
                 '--location westus '
                 '--sku DataBoxDisk '
                 '--expected-data-size 1 '
                 '--contact-name "Public SDK Test" '
                 '--phone 14258828080 '
                 '--email-list testing@microsoft.com '
                 '--street-address1 "1 MICROSOFT WAY" '
                 '--city Redmond '
                 '--state-or-province WA '
                 '--country US '
                 '--postal-code 98052 '
                 '--company-name Microsoft '
                 '--storage-account {storage_account_1} '
                 '--transfer-type ImportToAzure',
                 checks=[JMESPathCheck('status', 'DeviceOrdered')],
                 )

        self.cmd('databox job cancel '
                 '--resource-group {rg} '
                 '--name {job_name_2} '
                 '--reason "CancelTest" '
                 '-y',
                 checks=[])

        self.cmd('databox job delete '
                 '--resource-group {rg} '
                 '--name {job_name_2} '
                 '-y',
                 checks=[])

        self.cmd('databox job show '
                 '--resource-group {rg} '
                 '--name {job_name_2}',
                 expect_failure=True)

        # Create databox job with transfer-type 'ExportFromAzure'.
        self.cmd('databox job create '
                 '--resource-group {rg} '
                 '--name {job_name_3} '
                 '--location westus '
                 '--sku DataBox '
                 '--expected-data-size 1 '
                 '--contact-name "Public SDK Test" '
                 '--phone 14258828080 '
                 '--email-list testing@microsoft.com '
                 '--street-address1 "1 MICROSOFT WAY" '
                 '--city Redmond '
                 '--state-or-province WA '
                 '--country US '
                 '--postal-code 98052 '
                 '--company-name Microsoft '
                 '--storage-account {storage_account_1} '
                 '--transfer-type ExportFromAzure '
                 '--transfer-configuration-type TransferAll',
                 checks=[
                     self.check('status', 'DeviceOrdered'),
                     self.check('transferType', 'ExportFromAzure'),
                     self.check('details.dataExportDetails[0].transferConfiguration.transferConfigurationType',
                                'TransferAll')
                 ])

        self.cmd('databox job cancel --resource-group {rg} --name {job_name_3} --reason "CancelTest" -y')
        self.cmd('databox job delete --resource-group {rg} --name {job_name_3} -y')

        # DataBox service will create a lock 'DATABOX_SERVICE' on the storage account under the resource group when creating a job. In order to clean up the resource group, we need delete the lock first.
        self.cmd('lock delete '
                 '--name DATABOX_SERVICE '
                 '-g {rg} '
                 '--resource-name {storage_account_1} '
                 '--resource-type Microsoft.Storage/storageAccounts')

        self.cmd('lock delete '
                 '--name DATABOX_SERVICE '
                 '-g {rg} '
                 '--resource-name {storage_account_2} '
                 '--resource-type Microsoft.Storage/storageAccounts')

    @ResourceGroupPreparer(name_prefix='cli_test_databox')
    @StorageAccountPreparer(parameter_name='storage_account_1')
    def test_databox_custom_disk(self, storage_account_1):
        self.kwargs.update({
            'job_name': self.create_random_name('job', 24),
            'storage_account_1': storage_account_1,
            'managed-rg': self.create_random_name('rg-', 10),
            'device_serial_number': 'CLISERIAL789'
        })
        self.cmd('databox job create '
                 '--resource-group {rg} '
                 '--name {job_name} '
                 '--sku DataBoxCustomerDisk '
                 '--contact-name "Public SDK Test" '
                 '--phone 14258828080 '
                 '--email-list testing@microsoft.com '
                 '--street-address1 "1 MICROSOFT WAY" '
                 '--city Redmond '
                 '--state-or-province WA '
                 '--country US '
                 '--postal-code 98052 '
                 '--company-name Microsoft '
                 '--storage-account {storage_account_1} '
                 '--resource-group-for-managed-disk {managed-rg} '
                 '--transfer-type ImportToAzure '
                 '--data-box-customer-disk {{returnToCustomerPackageDetails:{{carrierName:carrier1,carrierAccountNumber:0000}},importDiskDetailsCollection:{{{device_serial_number}:{{ManifestFile:SampleManifest.xml,ManifestHash:xxxx,BitLockerKey:xxx}}}}}}',
                 checks=[
                     self.check('transferType', 'ImportToAzure'),
                     self.check('details.returnToCustomerPackageDetails.carrierName', 'carrier1')
                 ])
        self.cmd('databox job cancel --resource-group {rg} --name {job_name} --reason "CancelTest" -y')
        self.cmd('databox job delete --resource-group {rg} --name {job_name} -y')
        self.cmd('lock delete --name DATABOX_SERVICE -g {rg} --resource-name {storage_account_1} --resource-type Microsoft.Storage/storageAccounts')

    @ResourceGroupPreparer(name_prefix='cli_test_databox')
    @StorageAccountPreparer(parameter_name='storage_account_1')
    def test_databox_transfer_all_details(self, storage_account_1):
        self.kwargs.update({
            'job_name': self.create_random_name('job', 24),
            'job_name_2': self.create_random_name('job', 24),
            'storage_account_1': storage_account_1,
        })
        self.cmd('databox job create '
                 '--resource-group {rg} '
                 '--name {job_name} '
                 '--location westus '
                 '--sku DataBox '
                 '--expected-data-size 1 '
                 '--contact-name "Public SDK Test" '
                 '--phone 14258828080 '
                 '--email-list testing@microsoft.com '
                 '--street-address1 "1 MICROSOFT WAY" '
                 '--city Redmond '
                 '--state-or-province WA '
                 '--country US '
                 '--postal-code 98052 '
                 '--company-name Microsoft '
                 '--storage-account {storage_account_1} '
                 '--transfer-type ExportFromAzure '
                 '--transfer-configuration-type TransferAll '
                 '--transfer-all-blobs true',
                 checks=[
                     self.check('status', 'DeviceOrdered'),
                     self.check('transferType', 'ExportFromAzure'),
                     self.check('details.dataExportDetails[0].transferConfiguration.transferConfigurationType', 'TransferAll'),
                     self.check('details.dataExportDetails[0].transferConfiguration.transferAllDetails.include.transferAllBlobs', True),
                     self.check('details.dataExportDetails[0].transferConfiguration.transferAllDetails.include.transferAllFiles', False)
                 ])

        self.cmd('databox job create '
                 '--resource-group {rg} '
                 '--name {job_name_2} '
                 '--location westus '
                 '--sku DataBox '
                 '--expected-data-size 1 '
                 '--contact-name "Public SDK Test" '
                 '--phone 14258828080 '
                 '--email-list testing@microsoft.com '
                 '--street-address1 "1 MICROSOFT WAY" '
                 '--city Redmond '
                 '--state-or-province WA '
                 '--country US '
                 '--postal-code 98052 '
                 '--company-name Microsoft '
                 '--storage-account {storage_account_1} '
                 '--transfer-type ExportFromAzure '
                 '--transfer-configuration-type TransferAll '
                 '--transfer-all-files True',
                 checks=[
                     self.check('status', 'DeviceOrdered'),
                     self.check('transferType', 'ExportFromAzure'),
                     self.check('details.dataExportDetails[0].transferConfiguration.transferConfigurationType', 'TransferAll'),
                     self.check('details.dataExportDetails[0].transferConfiguration.transferAllDetails.include.transferAllBlobs', False),
                     self.check('details.dataExportDetails[0].transferConfiguration.transferAllDetails.include.transferAllFiles', True)
                 ])

        self.cmd('databox job cancel --resource-group {rg} --name {job_name} --reason "CancelTest" -y')
        self.cmd('databox job delete --resource-group {rg} --name {job_name} -y')
        self.cmd('databox job cancel --resource-group {rg} --name {job_name_2} --reason "CancelTest" -y')
        self.cmd('databox job delete --resource-group {rg} --name {job_name_2} -y')
        self.cmd('lock delete --name DATABOX_SERVICE -g {rg} --resource-name {storage_account_1} --resource-type Microsoft.Storage/storageAccounts')
