def test_deployments_at_subscription(self):
        # for more sample templates, see https://github.com/Azure/azure-quickstart-templates
        deployment_name = self.get_resource_name("pytestlinked")

        # Check deployment existence
        deployment_exists = self.resource_client.deployments.check_existence_at_subscription_scope(
            deployment_name)
        # self.assertFalse(deployment_exists)
        from azure.mgmt.resource.resources.v2019_10_01 import models as models1001

        template = models1001.TemplateLink(
            uri=
            'https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/100-blank-template/azuredeploy.json'
        )
        parameters = models1001.ParametersLink(
            uri=
            'https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/100-blank-template/azuredeploy.json'
        )

        deployment_params = {
            'mode': 'Incremental',
            'template_link': {
                'uri':
                'https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/100-blank-template/azuredeploy.json'
            },
            'parameters_link': {
                'uri':
                'https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/100-blank-template/azuredeploy.json'
            }
        }

        # Create deployment
        deployment_create_result = self.resource_client.deployments.begin_create_or_update_at_subscription_scope(
            deployment_name,
            {
                "location": "West US",
                "properties": deployment_params
            },
            # deployment_params,
        )
        deployment_create_result = deployment_create_result.result()
        self.assertEqual(deployment_name, deployment_create_result.name)

        # List deployments at subscription
        deployment_list_result = self.resource_client.deployments.list_at_subscription_scope(
            # None,
        )
        deployment_list_result = list(deployment_list_result)
        # self.assertEqual(len(deployment_list_result), 1)
        # self.assertEqual(deployment_name, deployment_list_result[0].name)

        # Get deployment
        deployment_get_result = self.resource_client.deployments.get_at_subscription_scope(
            deployment_name, )
        self.assertEqual(deployment_name, deployment_get_result.name)

        # What if
        result = self.resource_client.deployments.begin_what_if_at_subscription_scope(
            deployment_name, {
                "location": "West US",
                "properties": deployment_params
            })
        result = result.result()

        # List deployment operations
        deployment_operations = list(
            self.resource_client.deployment_operations.
            list_at_subscription_scope(deployment_name))
        self.assertGreater(len(deployment_operations), 0)

        # Get deployment operations
        deployment_operation = deployment_operations[0]
        deployment_operation_get = self.resource_client.deployment_operations.get_at_subscription_scope(
            deployment_name, deployment_operation.operation_id)
        self.assertEqual(deployment_operation_get.operation_id,
                         deployment_operation.operation_id)

        # Should throw, since the deployment is done => cannot be cancelled
        with self.assertRaises(
                azure.core.exceptions.ResourceExistsError) as cm:
            self.resource_client.deployments.cancel_at_subscription_scope(
                deployment_name)
        self.assertIn('cannot be cancelled', cm.exception.message)

        # Validate
        result = self.resource_client.deployments.begin_validate_at_subscription_scope(
            deployment_name, {
                "location": "West US",
                "properties": deployment_params
            })
        validation = result.result()
        self.assertTrue(hasattr(validation, 'properties'))

        # Export template
        export = self.resource_client.deployments.export_template_at_subscription_scope(
            deployment_name)
        self.assertTrue(hasattr(export, 'template'))

        # Delete the template
        async_delete = self.resource_client.deployments.begin_delete_at_subscription_scope(
            deployment_name)
        async_delete.wait()
    def test_deployments_at_management_group(self):
        # create management group use track 1 version
        group_id = "20000000-0001-0000-0000-000000000123456"

        if self.is_live:
            result = self.mgmtgroup_client.management_groups.create_or_update(
                group_id, {
                    "name": group_id,
                })
            result = result.result()

        # for more sample templates, see https://github.com/Azure/azure-quickstart-templates
        deployment_name = self.get_resource_name("pytestlinked")

        # Check deployment existence
        deployment_exists = self.resource_client.deployments.check_existence_at_management_group_scope(
            group_id, deployment_name)
        # [ZIM] tis doesn't work for some reason
        # self.assertFalse(deployment_exists)

        from azure.mgmt.resource.resources.v2019_10_01 import models as models1001

        template = models1001.TemplateLink(
            uri=
            'https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/100-blank-template/azuredeploy.json'
        )
        parameters = models1001.ParametersLink(
            uri=
            'https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/100-blank-template/azuredeploy.json'
        )

        deployment_params = {
            "mode": "Incremental",
            "template_link": template,
            "parameters_link": parameters
        }

        # Create deployment
        deployment_create_result = self.resource_client.deployments.begin_create_or_update_at_management_group_scope(
            group_id,
            deployment_name,
            {
                "location": "West US",
                "properties": deployment_params
            },
            # deployment_params,
        )
        deployment_create_result = deployment_create_result.result()
        self.assertEqual(deployment_name, deployment_create_result.name)

        # List deployments at scope
        deployment_list_result = self.resource_client.deployments.list_at_management_group_scope(
            group_id,
            None,
        )
        deployment_list_result = list(deployment_list_result)
        ##  self.assertEqual(len(deployment_list_result), 1)
        self.assertEqual(deployment_name, deployment_list_result[0].name)

        # Get deployment
        deployment_get_result = self.resource_client.deployments.get_at_management_group_scope(
            group_id,
            deployment_name,
        )
        self.assertEqual(deployment_name, deployment_get_result.name)

        # List deployment operations
        deployment_operations = list(
            self.resource_client.deployment_operations.
            list_at_management_group_scope(group_id, deployment_name))
        self.assertGreater(len(deployment_operations), 0)

        # Get deployment operations
        deployment_operation = deployment_operations[0]
        deployment_operation_get = self.resource_client.deployment_operations.get_at_management_group_scope(
            group_id, deployment_name, deployment_operation.operation_id)
        self.assertEqual(deployment_operation_get.operation_id,
                         deployment_operation.operation_id)

        # Should throw, since the deployment is done => cannot be cancelled
        with self.assertRaises(
                azure.core.exceptions.ResourceExistsError) as cm:
            self.resource_client.deployments.cancel_at_management_group_scope(
                group_id, deployment_name)
        self.assertIn('cannot be cancelled', cm.exception.message)

        # Validate
        result = self.resource_client.deployments.begin_validate_at_management_group_scope(
            group_id, deployment_name, {
                "location": "West US",
                "properties": deployment_params
            })
        validation = result.result()
        self.assertTrue(hasattr(validation, 'properties'))

        # Export template
        export = self.resource_client.deployments.export_template_at_management_group_scope(
            group_id, deployment_name)
        self.assertTrue(hasattr(export, 'template'))

        # Delete the template
        async_delete = self.resource_client.deployments.begin_delete_at_management_group_scope(
            group_id, deployment_name)
        async_delete.wait()

        if self.is_live:
            # delete management group with track 1 version
            result = self.mgmtgroup_client.management_groups.delete(group_id)
            result = result.result()