# --------------------------------------------------------------------------------------------
# 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 *
from azure.core.exceptions import (HttpResponseError, ResourceNotFoundError)


class DncScenario(ScenarioTest):

    ###
    # Controller tests
    ###

    @ResourceGroupPreparer(name_prefix='cli_test_dnc', location='centraluseuap')
    def test_dnc_controller_create(self, resource_group):
        self.kwargs.update({
            'controllerName': 'clitestcontroller',
            'rg': resource_group
        })

        self.cmd('dnc controller create --location "centraluseuap" --resource-name {controllerName} --resource-group {rg}', checks=[
            self.check('name', '{controllerName}'),
            self.check('properties.provisioningState', 'Succeeded'),
            self.exists('properties.resourceGuid'),
            self.exists('properties.dncAppId'),
            self.exists('properties.dncEndpoint')
        ])

    @ResourceGroupPreparer(name_prefix='cli_test_dnc', location='centraluseuap')
    def test_dnc_controller_create_test_purpose(self, resource_group):
        self.kwargs.update({
            'controllerName': 'clitestcontroller',
            'rg': resource_group
        })

        self.cmd('dnc controller create --location "centraluseuap" --resource-name {controllerName} --resource-group {rg} --purpose "test"', checks=[
            self.check('name', '{controllerName}'),
            self.check('properties.provisioningState', 'Succeeded'),
            self.exists('properties.resourceGuid'),
            self.exists('properties.dncAppId'),
            self.exists('properties.dncEndpoint')
        ])

    @ResourceGroupPreparer(name_prefix='cli_test_dnc', location='centraluseuap')
    def test_dnc_controller_delete(self, resource_group):
        self.kwargs.update({
            'controllerName': 'clitestcontroller',
            'rg': resource_group
        })

        self.cmd('dnc controller delete --resource-name {controllerName} --resource-group {rg} --yes')

    @ResourceGroupPreparer(name_prefix='cli_test_dnc', location='centraluseuap')
    def test_dnc_controller_show(self, resource_group):
        self.kwargs.update({
            'controllerName': 'clitestcontroller',
            'rg': resource_group
        })

        # setup
        self.cmd('dnc controller create --location "centraluseuap" --resource-name {controllerName} --resource-group {rg}', checks=[
            self.check('name', '{controllerName}'),
            self.check('properties.provisioningState', 'Succeeded'),
            self.exists('properties.resourceGuid'),
            self.exists('properties.dncAppId'),
            self.exists('properties.dncEndpoint')
        ])

        self.cmd('dnc controller show --resource-name {controllerName} --resource-group {rg}', checks=[
            self.check('name', '{controllerName}'),
            self.check('properties.provisioningState', 'Succeeded'),
            self.exists('properties.resourceGuid'),
            self.exists('properties.dncAppId'),
            self.exists('properties.dncEndpoint')
        ])


    ###
    # Delegated subnet service tests
    ###

    @ResourceGroupPreparer(name_prefix='cli_test_dnc', location='centraluseuap')
    @VirtualNetworkPreparer(name_prefix='cli_test_dnc', location='centraluseuap')
    def test_dnc_delegated_subnet_service_create(self, resource_group, virtual_network):
        self.kwargs.update({
            'controllerName': 'clitestcontroller',
            'rg': resource_group,
            'vn': virtual_network,
            'sid': self.get_subscription_id()
        })

        controllerDetailsId = self.cmd('dnc controller create --location "centraluseuap" --resource-name {controllerName} --resource-group {rg}').get_output_in_json()['id']
        subnetDetailsId = '/subscriptions/{sid}/resourceGroups/{rg}/providers/Microsoft.Network/virtualNetworks/{vn}/subnets/default'

        with self.assertRaises(HttpResponseError) as err:
            self.cmd(f'dnc delegated-subnet-service create --location "centraluseuap" --id {controllerDetailsId} --subnet-details-id {subnetDetailsId} --resource-group {self.kwargs["rg"]} --resource-name delegated1')

        # We expect this since we can't create one without a PrivateLinkService. We expect SAL creation to fail.
        # In this test we want to make sure the schema for the CLI arguments are correctly generated.
        self.assertIn("SAL creation failed", str(err.exception))

    @ResourceGroupPreparer(name_prefix='cli_test_dnc', location='centraluseuap')
    def test_dnc_delegated_subnet_service_delete(self, resource_group):
        self.kwargs.update({
            'controllerName': 'clitestcontroller',
            'rg': resource_group,
        })

        self.cmd('dnc delegated-subnet-service delete --resource-name delegated1 --resource-group {rg} --yes')

    @ResourceGroupPreparer(name_prefix='cli_test_dnc', location='centraluseuap')
    def test_dnc_delegated_subnet_service_show(self, resource_group):
        self.kwargs.update({
            'controllerName': 'clitestcontroller',
            'rg': resource_group,
        })

        with self.assertRaises(ResourceNotFoundError) as err:
            self.cmd('dnc delegated-subnet-service show --resource-name delegated1 --resource-group {rg}')

        # We expect this because we can't create one without a PrivateLinkService.
        # In this test we want to make sure the schema for the CLI arguments are correctly generated.
        self.assertIn("ResourceNotFound", str(err.exception))
    

    ###
    # Orchestrator Instance Service tests
    ###

    @ResourceGroupPreparer(name_prefix='cli_test_dnc', location='centraluseuap')
    def test_dnc_orchestrator_instance_service_create(self, resource_group):
        tenantId = self.cmd("az account show").get_output_in_json()["tenantId"]
        self.kwargs.update({
            'controllerName': 'clitestcontroller',
            'rg': resource_group,
            'sid': self.get_subscription_id(),
            'tid': tenantId
        })

        # setup
        self.cmd('dnc controller create --location "centraluseuap" --resource-name {controllerName} --resource-group {rg}')

        with self.assertRaises(HttpResponseError) as err:
            # --kind "Kubernetes" is implicit as default
            self.cmd('dnc orchestrator-instance-service create --type "SystemAssigned" --location "centraluseuap" --api-server-endpoint "https://testk8s.cloudapp.net" --cluster-root-ca "ddsadsad344mfdsfdl" --id "/subscriptions/{sid}/resourceGroups/{rg}/providers/Microsoft.DelegatedNetwork/controller/{controllerName}" --orchestrator-app-id "546192d7-503f-477a-9cfe-4efc3ee2b6e1" --orchestrator-tenant-id {tid} --priv-link-resource-id "/subscriptions/{sid}/resourceGroups/{rg}/providers/Microsoft.Network/privateLinkServices/plresource1" --resource-group {rg} --resource-name "testk8s1"')

        # We expect this because we don't have dependencies set up for the Kubernetes API server endpoint and private link resource.
        # In this test we want to make sure the schema for the CLI arguments are correctly generated.
        self.assertIn("Internal Server Error", str(err.exception))

    @ResourceGroupPreparer(name_prefix='cli_test_dnc', location='centraluseuap')
    def test_dnc_orchestrator_instance_service_delete(self, resource_group):
        self.kwargs.update({
            'rg': resource_group
        })

        self.cmd('dnc orchestrator-instance-service delete --resource-name testk8s1 --resource-group {rg} --yes')

    @ResourceGroupPreparer(name_prefix='cli_test_dnc', location='centraluseuap')
    def test_dnc_orchestrator_instance_service_show(self, resource_group):
        self.kwargs.update({
            'rg': resource_group
        })

        with self.assertRaises(ResourceNotFoundError) as err:
            self.cmd('dnc orchestrator-instance-service show --resource-name testk8s1 --resource-group {rg}')

        # We expect this because we can't create one without real dependencies.
        # In this test we want to make sure the schema for the CLI arguments are correctly generated.
        self.assertIn("ResourceNotFound", str(err.exception))