# --------------------------------------------------------------------------------------------
# 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 ScenarioTest, ResourceGroupPreparer, KeyVaultPreparer
from msrestazure.tools import resource_id


class SelfHelpScenario(ScenarioTest):

    @ResourceGroupPreparer()
    @KeyVaultPreparer()
    def test_help_discoverysolutions(self, resource_group, key_vault):

        self.kwargs.update({
            'scope': resource_id(resource_group=resource_group,
                                 subscription=self.get_subscription_id(),
                                 name=key_vault,
                                 namespace='Microsoft.KeyVault',
                                 type='vaults')
        })

        list_solution_discovery_result = self.cmd(
            'self-help discovery-solution list --scope {scope}', checks=[
                self.check(
                    '[0].type', 'Microsoft.Help/discoverySolutions', case_sensitive=False)
            ]).get_output_in_json()
        self.assertTrue(list_solution_discovery_result is not None)
        self.assertTrue(len(list_solution_discovery_result) >= 1)
        self.assertTrue("type" in list_solution_discovery_result[0])
        self.assertTrue("name" in list_solution_discovery_result[0])
        self.assertTrue("id" in list_solution_discovery_result[0])

    @ResourceGroupPreparer()
    @KeyVaultPreparer()
    def test_help_diagnostics(self, resource_group, key_vault):

        # Create diagnostic for keyVault resource.
        diagnostic_name = self.create_random_name(prefix='cli_test', length=15)
        self.kwargs.update({
            'scope': resource_id(resource_group=resource_group,
                                 subscription=self.get_subscription_id(),
                                 name=key_vault,
                                 namespace='Microsoft.KeyVault',
                                 type='vaults'),
            'diagnostic-name': diagnostic_name,
            'insights': "[{solutionId:Demo2InsightV2}]"
        })

        create_diagnostic_result = self.cmd(
            "self-help diagnostic create --diagnostic-name {diagnostic-name}  --insights {insights} --scope {scope}", checks=[
                self.check('name', '{diagnostic-name}'),
                self.check('type', 'Microsoft.Help/Diagnostics'),
            ])
        create_diagnostic_result = create_diagnostic_result.get_output_in_json()
        self.assertTrue(create_diagnostic_result is not None)
        self.assertTrue(create_diagnostic_result["acceptedAt"] is not None)

        # Get diagnostic for keyVault resource.
        get_diagnostic_result = self.cmd(
            "self-help diagnostic show --diagnostic-name {diagnostic-name} --scope {scope}", checks=[
                self.check('name', '{diagnostic-name}'),
                self.check('type', 'Microsoft.Help/Diagnostics'),
            ])
        get_diagnostic_result = get_diagnostic_result.get_output_in_json()
        self.assertTrue(get_diagnostic_result is not None)
        self.assertTrue(get_diagnostic_result["acceptedAt"] is not None)

    def test_help_check_name_diagnostics(self):

        # Create diagnostic for keyVault resource.
        self.kwargs.update({
            'scope': resource_id(subscription=self.get_subscription_id()),
            'diagnostic-name': self.create_random_name(prefix='cli_test', length=15),
        })

        self.cmd(
            "self-help check-name-availability --scope {scope} --name {diagnostic-name} --type 'Microsoft.Help/diagnostics'", checks=[
                self.check('nameAvailable', True),
            ])
