# --------------------------------------------------------------------------
# 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 Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is
# regenerated.
# --------------------------------------------------------------------------
# pylint: disable=line-too-long

import os

from azure.cli.testsdk import ScenarioTest
from azure.cli.testsdk import ResourceGroupPreparer

# Test class for Scenario
class ManagedCCFsScenarioTest(ScenarioTest):
    def __init__(self, *args, **kwargs):
        super(ManagedCCFsScenarioTest, self).__init__(*args, **kwargs)

    # Create an instance with custom JS app and 3 nodes
    @ResourceGroupPreparer(name_prefix='synth_azcli_rg_', location='eastus', random_name_length=30)
    def test_managedccfs_create_custom_3nodes(self, resource_group):
        location = "southcentralus"
        tag_key = "dept"
        tag_value = "finance"
        tags = f"{tag_key}={tag_value}"
        member0_tag_group = "contoso"
        member0_tag_identifier = "member0"
        member0_cert = os.path.join(os.path.abspath(os.path.join(os.path.abspath(__file__), '..')), "member0_cert.pem")
        name = self.create_random_name("synth-azcli-", 30)

        self.kwargs.update({
            "name": name,
            "location": location,
            "tags": tags,
            "member0_tag_group": member0_tag_group,
            "member0_tag_identifier": member0_tag_identifier,
            "member0_cert": member0_cert,
            "rg": resource_group
        })

        self.cmd("confidentialledger managedccfs create -n {name} -l {location} -g {rg} --tags {tags} --members \"[{{certificate:'{member0_cert}',identifier:{member0_tag_identifier},group:{member0_tag_group}}}]\" ")
               
        azcli_instance = self.cmd('confidentialledger managedccfs show -g {rg} -n {name}').get_output_in_json()

        self.assertIsNotNone(azcli_instance)
        self.assertEqual(azcli_instance['properties']['appName'], name)
        self.assertEqual(azcli_instance['location'], location)
        self.assertEqual(azcli_instance['tags'][tag_key], tag_value)
        self.assertEqual(azcli_instance['properties']['appUri'], f"https://{name}.confidential-ledger.azure.com")
        self.assertEqual(azcli_instance['properties']['identityServiceUri'], f"https://identity.confidential-ledger.core.azure.com/ledgerIdentity/{name}")
        self.assertEqual(azcli_instance['properties']['deploymentType']['languageRuntime'], 'JS')
        self.assertEqual(azcli_instance['properties']['deploymentType']['appSourceUri'], 'customImage')
        self.assertEqual(azcli_instance['properties']['nodeCount'], 3)

        members = azcli_instance['properties']['memberIdentityCertificates']
        self.assertIsNotNone(members)
        self.assertEqual(len(members), 1)
        for member in members:
            self.assertEqual(member['tags']['identifier'],member0_tag_identifier)
            self.assertEqual(member['tags']['group'], member0_tag_group)

        # list will be enabled when the CP is fixed
        # self.cmd('confidentialledger managedccfs list --resource-group {rg}', checks=self.check('length(@)', 1))

        self.cmd('confidentialledger managedccfs delete --resource-group {rg} --name {name} --yes')

        # list will be enabled when the CP bug is fixed
        # self.cmd('confidentialledger managedccfs list --resource-group {rg}', checks=self.check('length(@)', 0))

    # Create an instance with the sample JS app
    # and multiple members
    @ResourceGroupPreparer(name_prefix='synth_azcli_rg_', location='eastus', random_name_length=30)
    def test_managedccfs_create_sample_3nodes(self, resource_group):
        location = "southcentralus"
        tag_key = "dept"
        tag_value = "finance"
        tags = f"{tag_key}={tag_value}"
        member0_tag_group = "contoso"
        member0_tag_identifier = "member0"
        member0_cert = os.path.join(os.path.abspath(os.path.join(os.path.abspath(__file__), '..')), "member0_cert.pem")

        member1_tag_group = "fabrikam"
        member1_tag_identifier = "member1"
        member1_cert = os.path.join(os.path.abspath(os.path.join(os.path.abspath(__file__), '..')), "member1_cert.pem")

        name = self.create_random_name("synth-azcli-", 30)

        self.kwargs.update({
           "name": name,
            "location": location,
            "tags": tags,
            "member0_tag_group": member0_tag_group,
            "member0_tag_identifier": member0_tag_identifier,
            "member0_cert": member0_cert,
            "member1_tag_group": member1_tag_group,
            "member1_tag_identifier": member1_tag_identifier,
            "member1_cert": member1_cert
        })

        response = self.cmd("confidentialledger managedccfs create --name {name} -g {rg} --location {location} "
                "--members \"[{{certificate:'{member0_cert}',identifier:{member0_tag_identifier},group:{member0_tag_group}}},{{certificate:'{member1_cert}',identifier:{member1_tag_identifier},group:{member1_tag_group}}}]\" "
                "--tags {tags} --app-type sample")
        
        azcli_instance = self.cmd('confidentialledger managedccfs show --resource-group {rg} --name {name}').get_output_in_json()
        self.assertIsNotNone(azcli_instance)
        self.assertEqual(azcli_instance['properties']['appName'], name)
        self.assertEqual(azcli_instance['location'], location)
        self.assertEqual(azcli_instance['tags'][tag_key], tag_value)
        self.assertEqual(azcli_instance['properties']['appUri'], f"https://{name}.confidential-ledger.azure.com")
        self.assertEqual(azcli_instance['properties']['identityServiceUri'], f"https://identity.confidential-ledger.core.azure.com/ledgerIdentity/{name}")
        self.assertEqual(azcli_instance['properties']['deploymentType']['languageRuntime'], "JS")
        self.assertEqual(azcli_instance['properties']['deploymentType']['appSourceUri'], "sample")
        self.assertEqual(azcli_instance['properties']['nodeCount'], 3)

        members = azcli_instance['properties']['memberIdentityCertificates']
        self.assertIsNotNone(members)
        self.assertEqual(len(members), 2)
        for member in members:
            self.assertIn(member['tags']['identifier'],[member0_tag_identifier,member1_tag_identifier])
            self.assertIn(member['tags']['group'],[member0_tag_group, member1_tag_group])

        # list will be enabled when the CP bug is fixed
        # self.cmd('confidentialledger managedccfs list --resource-group {rg}', checks=self.check('length(@)', 1))

        self.cmd('confidentialledger managedccfs delete --resource-group {rg} --name {name} --yes')

        # list will be enabled when the CP is fixed
        # self.cmd('confidentialledger managedccfs list --resource-group {rg}', checks=self.check('length(@)', 0))

    # Create an instance with custom JS app and 5 nodes
    @ResourceGroupPreparer(name_prefix='synth_azcli_rg_', location='eastus', random_name_length=30)
    def test_managedccfs_create_custom_5nodes(self, resource_group):
        location = "southcentralus"
        tag_key = "dept"
        tag_value = "finance"
        tags = f"{tag_key}={tag_value}"
        member0_tag_group = "contoso"
        member0_tag_identifier = "member0"
        member0_cert = os.path.join(os.path.abspath(os.path.join(os.path.abspath(__file__), '..')), "member0_cert.pem")
        name = self.create_random_name("synth-azcli-", 30)
        node_count = 5

        self.kwargs.update({
            "name": name,
            "location": location,
            "tags": tags,
            "member0_tag_group": member0_tag_group,
            "member0_tag_identifier": member0_tag_identifier,
            "member0_cert": member0_cert,
            "rg": resource_group,
            "node_count": node_count
        })

        self.cmd("confidentialledger managedccfs create -n {name} -l {location} -g {rg} --tags {tags} --node-count {node_count} --members \"[{{certificate:'{member0_cert}',identifier:{member0_tag_identifier},group:{member0_tag_group}}}]\" ")
        
        azcli_instance = self.cmd('confidentialledger managedccfs show --resource-group {rg} --name {name}').get_output_in_json()
        self.assertIsNotNone(azcli_instance)
        self.assertEqual(azcli_instance['properties']['appName'], name)
        self.assertEqual(azcli_instance['location'], location)
        self.assertEqual(azcli_instance['tags'][tag_key], tag_value)
        self.assertEqual(azcli_instance['properties']['appUri'], f"https://{name}.confidential-ledger.azure.com")
        self.assertEqual(azcli_instance['properties']['identityServiceUri'], f"https://identity.confidential-ledger.core.azure.com/ledgerIdentity/{name}")
        self.assertEqual(azcli_instance['properties']['deploymentType']['languageRuntime'], "JS")
        self.assertEqual(azcli_instance['properties']['deploymentType']['appSourceUri'], "customImage")
        self.assertEqual(azcli_instance['properties']['nodeCount'], node_count)
        members = azcli_instance['properties']['memberIdentityCertificates']
        self.assertIsNotNone(members)
        self.assertEqual(len(members), 1)
        for member in members:
            self.assertEqual(member['tags']['identifier'], member0_tag_identifier)
            self.assertEqual(member['tags']['group'], member0_tag_group)

        # list will be enabled when the CP is fixed
        # self.cmd('confidentialledger managedccfs list --resource-group {rg}', checks=self.check('length(@)', 1))

        self.cmd('confidentialledger managedccfs delete --resource-group {rg} --name {name} --yes')

        # list will be enabled when the CP bug is fixed
        # self.cmd('confidentialledger managedccfs list --resource-group {rg}', checks=self.check('length(@)', 0))

    # Create an instance with custom JS app, 3 nodes and no member group information
    @ResourceGroupPreparer(name_prefix='synth_azcli_rg_', location='eastus', random_name_length=30)
    def test_managedccfs_create_custom_3nodes_nogroup(self, resource_group):
        location = "southcentralus"
        tag_key = "dept"
        tag_value = "finance"
        tags = f"{tag_key}={tag_value}"
        member0_tag_identifier = "member0"
        member0_cert = os.path.join(os.path.abspath(os.path.join(os.path.abspath(__file__), '..')), "member0_cert.pem")
        name = self.create_random_name("synth-azcli-", 30)

        self.kwargs.update({
            "name": name,
            "location": location,
            "tags": tags,
            "member0_tag_identifier": member0_tag_identifier,
            "member0_cert": member0_cert,
            "rg": resource_group
        })

        self.cmd("confidentialledger managedccfs create -n {name} -l {location} -g {rg} --tags {tags} --members \"[{{certificate:'{member0_cert}',identifier:{member0_tag_identifier}}}]\" ")
               
        azcli_instance = self.cmd('confidentialledger managedccfs show -g {rg} -n {name}').get_output_in_json()

        self.assertIsNotNone(azcli_instance)
        self.assertEqual(azcli_instance['properties']['appName'], name)
        self.assertEqual(azcli_instance['location'], location)
        self.assertEqual(azcli_instance['tags'][tag_key], tag_value)
        self.assertEqual(azcli_instance['properties']['appUri'], f"https://{name}.confidential-ledger.azure.com")
        self.assertEqual(azcli_instance['properties']['identityServiceUri'], f"https://identity.confidential-ledger.core.azure.com/ledgerIdentity/{name}")
        self.assertEqual(azcli_instance['properties']['deploymentType']['languageRuntime'], 'JS')
        self.assertEqual(azcli_instance['properties']['deploymentType']['appSourceUri'], 'customImage')
        self.assertEqual(azcli_instance['properties']['nodeCount'], 3)

        members = azcli_instance['properties']['memberIdentityCertificates']
        self.assertIsNotNone(members)
        self.assertEqual(len(members), 1)
        for member in members:
            self.assertEqual(member['tags']['identifier'],member0_tag_identifier)

        # list will be enabled when the CP is fixed
        # self.cmd('confidentialledger managedccfs list --resource-group {rg}', checks=self.check('length(@)', 1))

        self.cmd('confidentialledger managedccfs delete --resource-group {rg} --name {name} --yes')

        # list will be enabled when the CP bug is fixed
        # self.cmd('confidentialledger managedccfs list --resource-group {rg}', checks=self.check('length(@)', 0))

    # Create an instance with custom JS app, 3 nodes and default location
    @ResourceGroupPreparer(name_prefix='synth_azcli_rg_', location='eastus', random_name_length=30)
    def test_managedccfs_create_custom_3nodes_defaultlocation(self, resource_group):
        location = "southcentralus"
        tag_key = "dept"
        tag_value = "finance"
        tags = f"{tag_key}={tag_value}"
        member0_tag_identifier = "member0"
        member0_cert = os.path.join(os.path.abspath(os.path.join(os.path.abspath(__file__), '..')), "member0_cert.pem")
        name = self.create_random_name("synth-azcli-", 30)

        self.kwargs.update({
            "name": name,
            "location": location,
            "tags": tags,
            "member0_tag_identifier": member0_tag_identifier,
            "member0_cert": member0_cert,
            "rg": resource_group
        })

        self.cmd("confidentialledger managedccfs create -n {name} -g {rg} --tags {tags} --members \"[{{certificate:'{member0_cert}',identifier:{member0_tag_identifier}}}]\" ")
               
        azcli_instance = self.cmd('confidentialledger managedccfs show -g {rg} -n {name}').get_output_in_json()

        self.assertIsNotNone(azcli_instance)
        self.assertEqual(azcli_instance['properties']['appName'], name)
        self.assertEqual(azcli_instance['location'], location)
        self.assertEqual(azcli_instance['tags'][tag_key], tag_value)
        self.assertEqual(azcli_instance['properties']['appUri'], f"https://{name}.confidential-ledger.azure.com")
        self.assertEqual(azcli_instance['properties']['identityServiceUri'], f"https://identity.confidential-ledger.core.azure.com/ledgerIdentity/{name}")
        self.assertEqual(azcli_instance['properties']['deploymentType']['languageRuntime'], 'JS')
        self.assertEqual(azcli_instance['properties']['deploymentType']['appSourceUri'], 'customImage')
        self.assertEqual(azcli_instance['properties']['nodeCount'], 3)

        members = azcli_instance['properties']['memberIdentityCertificates']
        self.assertIsNotNone(members)
        self.assertEqual(len(members), 1)
        for member in members:
            self.assertEqual(member['tags']['identifier'],member0_tag_identifier)

        # list will be enabled when the CP is fixed
        # self.cmd('confidentialledger managedccfs list --resource-group {rg}', checks=self.check('length(@)', 1))

        self.cmd('confidentialledger managedccfs delete --resource-group {rg} --name {name} --yes')

        # list will be enabled when the CP bug is fixed
        # self.cmd('confidentialledger managedccfs list --resource-group {rg}', checks=self.check('length(@)', 0))