File: test_mgmt_resource_deployment_scripts_test.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (109 lines) | stat: -rw-r--r-- 4,154 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

# covered ops:
#   deployment_scripts: 8/8

import unittest

import azure.core.exceptions
import azure.mgmt.resource
from devtools_testutils import AzureMgmtRecordedTestCase, RandomNameResourceGroupPreparer, recorded_by_proxy
import pytest


@pytest.mark.live_test_only
class TestMgmtResourceDeploymentScript(AzureMgmtRecordedTestCase):

    def setup_method(self, method):
        self.script_client = self.create_mgmt_client(
            azure.mgmt.resource.DeploymentScriptsClient, api_version="2019-10-01-preview"
        )

        # if self.is_live:
        #     from azure.mgmt.msi import ManagedServiceIdentityClient
        #     self.msi_client = self.create_mgmt_client(
        #         ManagedServiceIdentityClient,
        #     )

    @pytest.mark.skip(reason="authorization failed, need to add white_list")
    @RandomNameResourceGroupPreparer()
    @recorded_by_proxy
    def test_deployment_scripts(self, resource_group, location):
        SUBSCRIPTION = self.get_settings_value("SUBSCRIPTION_ID")
        script_name = "scripttest"
        identity_name = "uai"

        # Create identity
        if self.is_live:
            self.msi_client.user_assigned_identities.create_or_update(
                resource_group.name, identity_name, {"location": "westus", "tags": {"key1": "value1"}}
            )

        # Create script
        result = self.script_client.deployment_scripts.begin_create(
            resource_group.name,
            script_name,
            {
                "kind": "AzurePowerShell",
                "location": "westus",
                "identity": {
                    "type": "UserAssigned",
                    "user_assigned_identities": {
                        "/subscriptions/"
                        + SUBSCRIPTION
                        + "/resourceGroups/"
                        + resource_group.name
                        + "/providers/Microsoft.ManagedIdentity/userAssignedIdentities/uai": {}
                    },
                },
                "azPowerShellVersion": "3.0",
                "scriptContent": "Param([string]$Location,[string]$Name) $deploymentScriptOutputs['test'] = 'value' Get-AzResourceGroup -Location $Location -Name $Name",
                "arguments": "-Location 'westus' -Name \"*rg2\"",
                # "supportingScriptUris": [
                # "https://uri1.to.supporting.script",
                # "https://uri2.to.supporting.script"
                # ],
                "retentionInterval": "PT26H",
                "timeout": "PT30M",
                "cleanupPreference": "Always",
            },
        )

        # azure.core.exceptions.HttpResponseError: Operation returned an invalid status 'OK'
        try:
            result.result()
        except azure.core.exceptions.HttpResponseError:
            pass

        # Update script tags
        BODY = {"tags": {"key1": "value1"}}
        self.script_client.deployment_scripts.update(resource_group.name, script_name, BODY)

        # Get script
        self.script_client.deployment_scripts.get(resource_group.name, script_name)

        # List scripts by subscription
        self.script_client.deployment_scripts.list_by_subscription()

        # List scripts by resource group
        self.script_client.deployment_scripts.list_by_resource_group(resource_group.name)

        # Get script logs default
        self.script_client.deployment_scripts.get_logs_default(resource_group.name, script_name)

        # Get script logs
        self.script_client.deployment_scripts.get_logs(resource_group.name, script_name)

        # Delete script
        self.script_client.deployment_scripts.delete(resource_group.name, script_name)


# ------------------------------------------------------------------------------
if __name__ == "__main__":
    unittest.main()