# --------------------------------------------------------------------------------------------
# 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.core.exceptions import HttpResponseError

from azure.cli.testsdk.scenario_tests import AllowLargeResponse
from azure.cli.testsdk import (ScenarioTest, ResourceGroupPreparer)


class PaloAltoNetworksScenario(ScenarioTest):

    @AllowLargeResponse(size_kb=10240)
    @ResourceGroupPreparer(name_prefix='cli_test_palo_alto_networks_cloudngfw_firewall')
    def test_palo_alto_networks_cloudngfw_firewall(self, resource_group):
        self.kwargs.update({
            'cloudngfw_firewall_name': self.create_random_name('firewall', 15),
            'public_ip_name': 'fw0718-public-ip',
            'local_rulestack_name': 'test-local-rulestack',
            'prefix': 'test-publicip-prefix',
            'nsg': self.create_random_name('nsg', 10),
            'vnet': self.create_random_name('vnet', 10),
            'subnet1': self.create_random_name('subnet1', 12),
            'subnet2': self.create_random_name('subnet2', 12),
            'loc': 'centraluseuap'
        })
        self.cmd('az network nsg create -g {rg} -n {nsg}')
        self.cmd('az network vnet create -g {rg} -n {vnet} --address-prefix 10.0.0.0/16 --nsg {nsg} --subnet-name {subnet1} --subnet-prefixes 10.0.0.0/26')
        self.cmd('az network vnet subnet create -g {rg} --vnet-name {vnet} -n {subnet2} --address-prefixes 10.0.1.0/26')
        vnet = self.cmd('az network vnet show -g {rg} -n {vnet}').get_output_in_json()
        local_rulestack = self.cmd('az palo-alto cloudngfw local-rulestack create --local-rulestack-name testlrs -g {rg} -l eastus --default-mode "IPS" --scope "LOCAL" --pan-etag "9fa4e2c6-1b0c-11ee-b587-3e4c117c6534" --min-app-id-version "8595-7473" --security-services {{"vulnerability-profile":"BestPractice","anti-spyware-profile":"BestPractice","anti-virus-profile":"BestPractice","url-filtering-profile":"BestPractice","file-blocking-profile":"BestPractice","dns-subscription":"BestPractice"}}').get_output_in_json()
        self.kwargs.update({
            'public_ip_id': "/subscriptions/2bf4a339-294d-4c25-b0b2-ef649e9f5c27/resourceGroups/rheaTerraformTest/providers/Microsoft.Network/publicIPAddresses/fw0718-public-ip",
            'vnet_id': vnet['id'],
            'rulestack_id': local_rulestack['id'],
            'trust_subnet': vnet['subnets'][0]['id'],
            'un_trust_subnet': vnet['subnets'][1]['id']
        })
        self.cmd('az palo-alto cloudngfw firewall create '
                 '--name {cloudngfw_firewall_name} '
                 '-g {rg} '
                 '--location eastus '
                 '--associated-rulestack {{"location":eastus,"resource-id":{rulestack_id}}} '
                 '--dns-settings {{"enable-dns-proxy":DISABLED,"enabled-dns-type":CUSTOM}} '
                 '--is-panorama-managed FALSE '
                 '--marketplace-details {{"marketplace-subscription-status":Subscribed,"offer-id":pan_swfw_cloud_ngfw,"publisher-id":paloaltonetworks}} '
                 '--network-profile {{"egress-nat-ip":[],"enable-egress-nat":DISABLED,"network-type":VNET,"public-ips":[{{"address":"20.112.141.94","resource-id":{public_ip_id}}}],'
                 '"vnet-configuration":{{"ip-of-trust-subnet-for-udr":{{"address":10.0.0.0/16}},"trust-subnet":{{"resource-id":{trust_subnet}}},"un-trust-subnet":{{"resource-id":{un_trust_subnet}}},"vnet":{{"resource-id":{vnet_id}}}}}}} '
                 '--panorama-config {{"config-string":bas64EncodedString}} --plan-data {{"billing-cycle":MONTHLY,"plan-id":panw-cloud-ngfw-payg,"usage-type":PAYG}} --no-wait')
        self.cmd('az palo-alto cloudngfw firewall list --resource-group {rg}',
                 checks=[
                     self.check('length(@)', 1),
                 ])
        self.cmd('az palo-alto cloudngfw firewall show --resource-group {rg} -n {cloudngfw_firewall_name}',
                 checks=[
                     self.check('associatedRulestack.resourceId', '{rulestack_id}'),
                     self.check('dnsSettings.enableDnsProxy', 'DISABLED'),
                     self.check('isPanoramaManaged', "FALSE"),
                     self.check('location', "eastus"),
                     self.check('marketplaceDetails.marketplaceSubscriptionStatus', "Subscribed"),
                     self.check('marketplaceDetails.offerId', "pan_swfw_cloud_ngfw"),
                     self.check('marketplaceDetails.publisherId', "paloaltonetworks"),
                     self.check('name', '{cloudngfw_firewall_name}'),
                     self.check('networkProfile.enableEgressNat', "DISABLED"),
                     self.check('panoramaConfig.configString', "bas64EncodedString"),
                     self.check('planData.billingCycle', "MONTHLY")
                 ])
        self.cmd('az palo-alto cloudngfw firewall delete --resource-group {rg} -n {cloudngfw_firewall_name}')

    @AllowLargeResponse(size_kb=10240)
    def test_palo_alto_firewall_update(self):
        self.kwargs.update({
            'resource_group': 'rheaTerraformTest',
            'firewall_name': 'prodStabilityTest',
        })
        self.cmd('az palo-alto cloudngfw firewall update --name {firewall_name} -g {resource_group} --tags {{"tagName":"value"}}')
        self.cmd('az palo-alto cloudngfw firewall show --name {firewall_name} -g {resource_group}', self.check('tags.tagName', "value"))

    @AllowLargeResponse(size_kb=10240)
    def test_palo_alto_firewall_v2(self):
        self.kwargs.update({
            'loc': 'eastus',
            'resource_group': 'rheaTerraformTest',
            'firewall_name': 'prodStabilityTest',
            'workspace': self.create_random_name('workspace', 15),
        })
        self.cmd('az palo-alto cloudngfw firewall save-log-profile --resource-group {resource_group} -n {firewall_name} '
                 '--log-option "SAME_DESTINATION" --log-type "TRAFFIC" '
                 '--common-destination {{"monitor-configurations":{{"id":"/subscriptions/2bf4a339-294d-4c25-b0b2-ef649e9f5c27/resourceGroups/sudh_fire_test/providers/Microsoft.OperationalInsights/workspaces/testAnalyticsX","primary-key":"7R5uguOLkTF6zByLV9ef5402y4dQnhiSlNMTKNSpEbjZoBrGrNnM2rD4E7+0v1yFg5y6h3RlrIa7th7qJnoHcQ==","secondary-key":"7lmsLYTVizgkstJpno4fY6u36qmVw4rkaIfzhx82ymLIdYDy0wbq7Tdydkje3z50KbKsQnzhg8KAPFpEki/Buw==","subscription-id":"2bf4a339-294d-4c25-b0b2-ef649e9f5c27","workspace":"63e674d3-cc87-48c6-94cb-2d28a0b245fc"}}}}')
        self.cmd('az palo-alto cloudngfw firewall show-log-profile --resource-group {resource_group} -n {firewall_name} ', self.check('type(@)', 'object'))
        self.cmd('az palo-alto cloudngfw firewall show-support-info --resource-group {resource_group} -n {firewall_name}')

        self.cmd('az palo-alto cloudngfw firewall status list --resource-group {resource_group} -n {firewall_name}')
        self.cmd('az palo-alto cloudngfw firewall status default show --resource-group {resource_group} -n {firewall_name}')

    @AllowLargeResponse(size_kb=10240)
    @ResourceGroupPreparer(name_prefix='cli_test_palo_alto_networks_cloudngfw_local_rulestack')
    def test_palo_alto_networks_cloudngfw_local_rulestack(self, resource_group):
        self.kwargs.update({
            'local_rulestack_name': self.create_random_name('localrulestack', 20),
            'certificate_name': self.create_random_name('certificate', 15),
            'fqdnlist_name': self.create_random_name('fqdnlist', 15),
            'local_rule_name': self.create_random_name('local_rule', 15),
            'prefixlist_name': self.create_random_name('prefixlist', 15),
            'loc': 'eastus'
        })
        self.cmd('az palo-alto cloudngfw local-rulestack create -g {rg} -n {local_rulestack_name} '
                 '--identity {{"type":None}} '
                 '--location {loc} '
                 '--default-mode IPS '
                 '--description "local rulestacks" '
                 '--min-app-id-version "8595-7473" '
                 '--scope "LOCAL" '
                 '--security-services {{"vulnerability-profile":"BestPractice","anti-spyware-profile":"BestPractice","anti-virus-profile":"BestPractice","url-filtering-profile":"BestPractice","file-blocking-profile":"BestPractice","dns-subscription":"BestPractice"}}')
        self.cmd('az palo-alto cloudngfw local-rulestack update -g {rg} -n {local_rulestack_name} --tags {{"tag-name":"value"}}')
        self.cmd('az palo-alto cloudngfw local-rulestack list -g {rg}')
        self.cmd('az palo-alto cloudngfw local-rulestack show -g {rg} -n {local_rulestack_name}')
        self.cmd('az palo-alto cloudngfw local-rulestack show-change-log -g {rg} -n {local_rulestack_name}')
        self.cmd('az palo-alto cloudngfw local-rulestack show-support-info -g {rg} -n {local_rulestack_name}')
        self.cmd('az palo-alto cloudngfw local-rulestack list-app-id -g {rg} -n {local_rulestack_name}')
        self.cmd('az palo-alto cloudngfw local-rulestack list-country -g {rg} -n {local_rulestack_name}')
        self.cmd('az palo-alto cloudngfw local-rulestack list-firewall -g {rg} -n {local_rulestack_name}')
        self.cmd('az palo-alto cloudngfw local-rulestack list-predefined-url-category -g {rg} -n {local_rulestack_name}')
        self.cmd('az palo-alto cloudngfw local-rulestack revert -g {rg} -n {local_rulestack_name}')
        with self.assertRaisesRegex(HttpResponseError, 'ResourcePostActionFailed: Not Found'):
            self.cmd('az palo-alto cloudngfw local-rulestack list-advanced-security-object -g {rg} -n {local_rulestack_name} --type feeds')
        with self.assertRaisesRegex(HttpResponseError, 'ResourcePostActionFailed: Not Found'):
            self.cmd('az palo-alto cloudngfw local-rulestack list-security-service -g {rg} -n {local_rulestack_name} --type antiSpyware')

        self.cmd('az palo-alto cloudngfw local-rulestack certificate create -g {rg} '
                 '--local-rulestack-name {local_rulestack_name} '
                 '--name {certificate_name} '
                 '--audit-comment "comment" '
                 '--certificate-self-signed "TRUE" '
                 '--description "description" ')
        self.cmd('az palo-alto cloudngfw local-rulestack certificate list -g {rg} --local-rulestack-name {local_rulestack_name}')
        self.cmd('az palo-alto cloudngfw local-rulestack certificate show -g {rg} --local-rulestack-name {local_rulestack_name} --name {certificate_name}')
        self.cmd('az palo-alto cloudngfw local-rulestack certificate delete -g {rg} --local-rulestack-name {local_rulestack_name} --name {certificate_name}')

        self.cmd('az palo-alto cloudngfw local-rulestack fqdnlist create -g {rg} '
                 '--local-rulestack-name {local_rulestack_name} '
                 '--name {fqdnlist_name} '
                 '--audit-comment "string" '
                 '--description "description" '
                 '--fqdn-list ["string1","string2"]')
        self.cmd('az palo-alto cloudngfw local-rulestack fqdnlist list -g {rg} --local-rulestack-name {local_rulestack_name}')
        self.cmd('az palo-alto cloudngfw local-rulestack fqdnlist show  -g {rg} --local-rulestack-name {local_rulestack_name} --name {fqdnlist_name}')
        self.cmd('az palo-alto cloudngfw local-rulestack fqdnlist delete  -g {rg} --local-rulestack-name {local_rulestack_name} --name {fqdnlist_name}')

        self.cmd('az palo-alto cloudngfw local-rulestack prefixlist create -g {rg} '
                 '--local-rulestack-name {local_rulestack_name} '
                 '--name {prefixlist_name} '
                 '--audit-comment "comment" '
                 '--description "string" '
                 '--prefix-list ["1.0.0.0/24"]')
        self.cmd('az palo-alto cloudngfw local-rulestack prefixlist list -g {rg} --local-rulestack-name {local_rulestack_name} ')
        self.cmd('az palo-alto cloudngfw local-rulestack prefixlist show -g {rg} --local-rulestack-name {local_rulestack_name} --name {prefixlist_name}')
        self.cmd('az palo-alto cloudngfw local-rulestack prefixlist delete -g {rg} --local-rulestack-name {local_rulestack_name} --name {prefixlist_name}')

        self.cmd('az palo-alto cloudngfw local-rulestack local-rule create -g {rg} '
                 '--local-rulestack-name {local_rulestack_name} '
                 '--rule-name {local_rule_name} '
                 '--priority "1" '
                 '--action-type "Allow" '
                 '--enable-logging "DISABLED" '
                 '--rule-state "DISABLED" ')
        self.cmd('az palo-alto cloudngfw local-rulestack local-rule list -g {rg} --local-rulestack-name {local_rulestack_name}')
        self.cmd('az palo-alto cloudngfw local-rulestack local-rule show -g {rg} --local-rulestack-name {local_rulestack_name} --priority "1"')
        self.cmd('az palo-alto cloudngfw local-rulestack local-rule delete -g {rg} --local-rulestack-name {local_rulestack_name} --priority "1"')

        self.cmd('az palo-alto cloudngfw local-rulestack local-rule delete -g {rg} --local-rulestack-name {local_rulestack_name} --priority "1000000"')
        self.cmd('az palo-alto cloudngfw local-rulestack delete -g {rg} -n {local_rulestack_name}')

    @AllowLargeResponse(size_kb=10240)
    @ResourceGroupPreparer(name_prefix='cli_test_palo_alto_cloudngfw_local_rulestack_with_counter')
    def test_palo_alto_cloudngfw_local_rulestack_with_counter(self, resource_group):
        self.kwargs.update({
            'resource_group': "rheaTerraformTest",
            'local_rulestack_name': "fw0718-lrs",
            'priority': "1000000"
            })
        self.cmd('az palo-alto cloudngfw local-rulestack local-rule show-counter -g {resource_group} --local-rulestack-name {local_rulestack_name} --priority {priority}')
        self.cmd('az palo-alto cloudngfw local-rulestack local-rule refresh-counter -g {resource_group} --local-rulestack-name {local_rulestack_name} --priority {priority}')
        self.cmd('az palo-alto cloudngfw local-rulestack local-rule reset-counter -g {resource_group} --local-rulestack-name {local_rulestack_name} --priority {priority}')
