# --------------------------------------------------------------------------------------------
# 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 *
import time

class ElasticSanScenario(ScenarioTest):
    @ResourceGroupPreparer(location='eastus2euap', name_prefix='clitest.rg.testelasticsan')
    def test_elastic_san_scenarios(self, resource_group):
        self.kwargs.update({
            "san_name": self.create_random_name('elastic-san', 24)
        })
        self.cmd('az elastic-san create -n {san_name} -g {rg} --tags {{key1810:aaaa}} -l southcentralusstg '
                 '--base-size-tib 23 --extended-capacity-size-tib 14 '
                 '--sku {{name:Premium_LRS,tier:Premium}}',
                 checks=[JMESPathCheck('name', self.kwargs.get('san_name', '')),
                         JMESPathCheck('location', "southcentralusstg"),
                         JMESPathCheck('tags', {"key1810": "aaaa"}),
                         JMESPathCheck('baseSizeTiB', 23),
                         JMESPathCheck('extendedCapacitySizeTiB', 14),
                         JMESPathCheck('sku', {"name": "Premium_LRS",
                                               "tier": "Premium"})
                         ])
        self.cmd('az elastic-san show -g {rg} -n {san_name}',
                 checks=[JMESPathCheck('name', self.kwargs.get('san_name', '')),
                         JMESPathCheck('location', "southcentralusstg"),
                         JMESPathCheck('tags', {"key1810": "aaaa"}),
                         JMESPathCheck('baseSizeTiB', 23),
                         JMESPathCheck('extendedCapacitySizeTiB', 14),
                         JMESPathCheck('sku',  {"name": "Premium_LRS",
                                                "tier": "Premium"})
                         ])
        self.cmd('az elastic-san list -g {rg}', checks=[JMESPathCheck('length(@)', 1)])
        self.cmd('az elastic-san list-sku')
        self.cmd('az elastic-san update -n {san_name} -g {rg} --tags {{key1710:bbbb}} '
                 '--base-size-tib 25 --extended-capacity-size-tib 15',
                 checks=[JMESPathCheck('name', self.kwargs.get('san_name', '')),
                         JMESPathCheck('tags', {"key1710": "bbbb"}),
                         JMESPathCheck('baseSizeTiB', 25),
                         JMESPathCheck('extendedCapacitySizeTiB', 15)])
        self.cmd('az elastic-san delete -g {rg} -n {san_name} -y')
        time.sleep(20)
        self.cmd('az elastic-san list -g {rg}', checks=[JMESPathCheck('length(@)', 0)])

    @ResourceGroupPreparer(location='eastus2euap', name_prefix='clitest.rg.testelasticsan.volumegroup')
    def test_elastic_san_volume_group_and_volume_scenarios(self, resource_group):
        self.kwargs.update({
            "san_name": self.create_random_name('elastic-san', 24),
            "vg_name": self.create_random_name('volume-group', 24),
            "vnet_name": self.create_random_name('vnet', 24),
            "subnet_name": self.create_random_name('subnet', 24),
            "subnet_name_2": self.create_random_name('subnet', 24),
            "volume_name": self.create_random_name('volume', 24)
        })
        self.cmd('az elastic-san create -n {san_name} -g {rg} --tags {{key1810:aaaa}} -l southcentralusstg '
                 '--base-size-tib 23 --extended-capacity-size-tib 14 '
                 '--sku {{name:Premium_LRS,tier:Premium}}')
        subnet_id = self.cmd('az network vnet create -g {rg} -n {vnet_name} --address-prefix 10.0.0.0/16 '
                             '--subnet-name {subnet_name} '
                             '--subnet-prefix 10.0.0.0/24').get_output_in_json()["newVNet"]["subnets"][0]["id"]
        self.kwargs.update({"subnet_id": subnet_id})
        self.cmd('az elastic-san volume-group create -e {san_name} -n {vg_name} -g {rg} '
                 '--encryption EncryptionAtRestWithPlatformKey --protocol-type Iscsi '
                 '--network-acls {{virtual-network-rules:[{{id:{subnet_id},action:Allow}}]}}')
        self.cmd('az elastic-san volume-group show -g {rg} -e {san_name} -n {vg_name}',
                 checks=[JMESPathCheck('name', self.kwargs.get('vg_name', '')),
                         JMESPathCheck('encryption', "EncryptionAtRestWithPlatformKey"),
                         JMESPathCheck('protocolType', "iSCSI"),
                         JMESPathCheck('networkAcls', {"virtualNetworkRules":[{
                             "action": "Allow",
                             "id": subnet_id,
                             "state": "Provisioning",
                             "resourceGroup": self.kwargs.get('rg','')}]})])
        self.cmd('az elastic-san volume-group list -g {rg} -e {san_name}', checks=[JMESPathCheck('length(@)', 1)])

        subnet_id_2 = self.cmd('az network vnet subnet create -g {rg} --vnet-name {vnet_name} --name {subnet_name_2} '
                               '--address-prefixes 10.0.1.0/24 '
                               '--service-endpoints Microsoft.Storage').get_output_in_json()["id"]
        self.kwargs.update({"subnet_id_2": subnet_id_2})
        self.cmd('az elastic-san volume-group update -e {san_name} -n {vg_name} -g {rg} '
                 '--protocol-type None '
                 '--network-acls {{virtual-network-rules:[{{id:{subnet_id_2},action:Allow}}]}}',
                 checks=[JMESPathCheck('protocolType', "None"),
                         JMESPathCheck('networkAcls.virtualNetworkRules[0].id', subnet_id_2)])

        self.cmd('az elastic-san volume create -g {rg} -e {san_name} -v {vg_name} -n {volume_name} --size-gib 2')
        self.cmd('az elastic-san volume show -g {rg} -e {san_name} -v {vg_name} -n {volume_name}',
                 checks=[JMESPathCheck('name', self.kwargs.get('volume_name', '')),
                         JMESPathCheck('sizeGiB', 2)])
        self.cmd('az elastic-san volume list -g {rg} -e {san_name} -v {vg_name}',
                 checks=[JMESPathCheck('length(@)', 1)])
        self.cmd('az elastic-san volume update -g {rg} -e {san_name} -v {vg_name} -n {volume_name} --size-gib 3',
                 checks=[JMESPathCheck('sizeGiB', 3)])
        self.cmd('az elastic-san volume delete -g {rg} -e {san_name} -v {vg_name} -n {volume_name} -y')
        self.cmd('az elastic-san volume list -g {rg} -e {san_name} -v {vg_name}',
                 checks=[JMESPathCheck('length(@)', 0)])

        self.cmd('az elastic-san volume-group delete -g {rg} -e {san_name} -n {vg_name} -y')
        time.sleep(20)
        self.cmd('az elastic-san delete -g {rg} -n {san_name} -y')
