Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/amg/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,7 @@ Release History
* `az grafana integrations`: rename to `az grafana integration`
* `az grafana integration monitor`: drop `--monitor-resource-group-name` option; use `--monitor-rg-name` instead
* `az grafana data-source`: drop lookup by numeric ID and switch to UID-based endpoints due to deprecated Grafana API

3.0.1
++++++
* `az grafana service-account token create`: fix duration calculation for year ('y') unit
2 changes: 1 addition & 1 deletion src/amg/azext_amg/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def load_arguments(self, _):
c.argument("components", get_enum_type(["dashboards", "datasources", "folders", "snapshots", "annotations"]), nargs='+', options_list=["-c", "--components"], help="grafana artifact types to backup")
c.argument("folders_to_include", nargs='+', options_list=["-i", "--folders-to-include"], help="folders to include in backup or sync")
c.argument("folders_to_exclude", nargs='+', options_list=["-e", "--folders-to-exclude"], help="folders to exclude in backup or sync")
c.argument("time_to_live", default="1d", help="The life duration. For example, 1d if your key is going to last fr one day. Supported units are: s,m,h,d,w,M,y")
c.argument("time_to_live", default="1d", help="The life duration. For example, 1d if your key is going to last for one day. Supported units are: s,m,h,d,w,M,y")
c.ignore("subscription") # a help argument

with self.argument_context("grafana backup") as c:
Expand Down
14 changes: 13 additions & 1 deletion src/amg/azext_amg/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ def _create_role_assignment(cli_ctx, principal_id, role_definition_id, scope):
import time
from azure.core.exceptions import HttpResponseError, ResourceExistsError

principal_id = str(principal_id) if principal_id is not None else principal_id
role_definition_id = str(role_definition_id) if role_definition_id is not None else role_definition_id
scope = str(scope) if scope is not None else scope

assignments_client = get_mgmt_service_client(cli_ctx, AuthorizationManagementClient).role_assignments
principal_types = [p.value for p in PrincipalType]
current_principal_type = principal_types.pop(0)
Expand Down Expand Up @@ -208,6 +212,14 @@ def _create_role_assignment(cli_ctx, principal_id, role_definition_id, scope):


def _delete_role_assignment(cli_ctx, principal_id, role_definition_id=None, scope=None):
if not principal_id:
return
principal_id = str(principal_id)
if role_definition_id:
role_definition_id = str(role_definition_id)
if scope:
scope = str(scope)

assignments_client = get_mgmt_service_client(cli_ctx, AuthorizationManagementClient).role_assignments
f = f"principalId eq '{principal_id}'"

Expand Down Expand Up @@ -571,7 +583,7 @@ def _convert_duration_to_seconds(time_to_live):
"d": 3600 * 24,
"w": 3600 * 24 * 7,
"M": 3600 * 24 * 30,
"y": 3600 * 24 * 30 * 365
"y": 3600 * 24 * 365
}
Comment thread
necusjz marked this conversation as resolved.
unit_name = time_to_live[len(time_to_live) - 1:]
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ interactions:
User-Agent:
- AZURECLI/2.71.0 azsdk-python-core/1.33.0 Python/3.9.13 (Windows-10-10.0.22621-SP0)
method: PUT
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_amg000001/providers/Microsoft.Monitor/accounts/clitestamwinteg000003?api-version=2023-04-03
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_amg000001/providers/Microsoft.Monitor/accounts/clitestamwinteg000003?api-version=2025-10-03
response:
body:
string: '{"properties":{"accountId":"02ed99a7-58e7-4cff-97cf-f10315d34cc9","metrics":{"prometheusQueryEndpoint":"https://clitestamwinteg000003-ehfje8hsg5fvgxf9.westcentralus.prometheus.monitor.azure.com","internalId":"mac_02ed99a7-58e7-4cff-97cf-f10315d34cc9"},"provisioningState":"Succeeded","defaultIngestionSettings":{"dataCollectionRuleResourceId":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MA_clitestamwinteg000003_westcentralus_managed/providers/Microsoft.Insights/dataCollectionRules/clitestamwinteg000003","dataCollectionEndpointResourceId":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MA_clitestamwinteg000003_westcentralus_managed/providers/Microsoft.Insights/dataCollectionEndpoints/clitestamwinteg000003"},"publicNetworkAccess":"Enabled"},"location":"westcentralus","id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/cli_test_amg000001/providers/microsoft.monitor/accounts/clitestamwinteg000003","name":"clitestamwinteg000003","type":"Microsoft.Monitor/accounts","etag":"\"25008288-0000-0600-0000-689bd2e20000\"","systemData":{"createdBy":"example@example.com","createdByType":"User","createdAt":"2025-08-12T23:48:35.5276267Z","lastModifiedBy":"example@example.com","lastModifiedByType":"User","lastModifiedAt":"2025-08-12T23:48:35.5276267Z"}}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ interactions:
User-Agent:
- AZURECLI/2.86.0 azsdk-python-core/1.39.0 Python/3.12.10 (Windows-11-10.0.26200-SP0)
method: PUT
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_amg000001/providers/Microsoft.Monitor/accounts/clitestmon000003?api-version=2023-04-03
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_test_amg000001/providers/Microsoft.Monitor/accounts/clitestmon000003?api-version=2025-10-03
response:
body:
string: '{"properties":{"accountId":"e8076b65-f6f5-4743-8945-e1c52868faf8","metrics":{"prometheusQueryEndpoint":"https://clitestmon000003-btcvcwdzh3dxcfe5.westcentralus.prometheus.monitor.azure.com","internalId":"mac_e8076b65-f6f5-4743-8945-e1c52868faf8"},"provisioningState":"Succeeded","defaultIngestionSettings":{"dataCollectionRuleResourceId":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MA_clitestmon000003_westcentralus_managed/providers/Microsoft.Insights/dataCollectionRules/clitestmon000003","dataCollectionEndpointResourceId":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MA_clitestmon000003_westcentralus_managed/providers/Microsoft.Insights/dataCollectionEndpoints/clitestmon000003"},"publicNetworkAccess":"Enabled"},"location":"westcentralus","id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/cli_test_amg000001/providers/microsoft.monitor/accounts/clitestmon000003","name":"clitestmon000003","type":"Microsoft.Monitor/accounts","etag":"\"0d0081bb-0000-0600-0000-6a0e74be0000\"","systemData":{"createdBy":"test@example.com","createdByType":"User","createdAt":"2026-05-21T02:57:59.3654647Z","lastModifiedBy":"test@example.com","lastModifiedByType":"User","lastModifiedAt":"2026-05-21T02:57:59.3654647Z"}}'
Expand Down
57 changes: 57 additions & 0 deletions src/amg/azext_amg/tests/latest/test_amg_unit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import os
import sys
import unittest

# Ensure src/amg (parent directory containing azext_amg) is in sys.path
amg_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..'))
if amg_path not in sys.path:
sys.path.insert(0, amg_path)

from azext_amg.custom import _convert_duration_to_seconds


class AmgUnitTest(unittest.TestCase):
def test_duration_conversion(self):
self.assertEqual(_convert_duration_to_seconds("1s"), 1)
self.assertEqual(_convert_duration_to_seconds("1m"), 60)
self.assertEqual(_convert_duration_to_seconds("1h"), 3600)
self.assertEqual(_convert_duration_to_seconds("1d"), 86400)
self.assertEqual(_convert_duration_to_seconds("1w"), 604800)
self.assertEqual(_convert_duration_to_seconds("1M"), 2592000)
self.assertEqual(_convert_duration_to_seconds("1y"), 31536000)
self.assertEqual(_convert_duration_to_seconds("10y"), 315360000)

def test_create_role_assignment_with_aaz_types(self):
from unittest.mock import MagicMock, patch
from azure.cli.core.aaz._field_value import AAZSimpleValue
from azext_amg.custom import _create_role_assignment

mock_cli_ctx = MagicMock()
mock_assignments_client = MagicMock()

# Simulate AAZSimpleValue types that previously caused JSON serialization errors
aaz_principal_id = AAZSimpleValue(None, "2b83823d-1afa-419f-8246-4b5d9df2f7e2")
aaz_role_id = AAZSimpleValue(None, "monitoring-reader-role-id")
aaz_scope = AAZSimpleValue(None, "/subscriptions/00000000-0000-0000-0000-000000000000")

with patch('azext_amg.custom.get_mgmt_service_client') as mock_get_client:
mock_client = MagicMock()
mock_client.role_assignments = mock_assignments_client
mock_get_client.return_value = mock_client

_create_role_assignment(mock_cli_ctx, aaz_principal_id, aaz_role_id, aaz_scope)

# Verify client.create was called with string parameters
self.assertTrue(mock_assignments_client.create.called)
call_kwargs = mock_assignments_client.create.call_args.kwargs
self.assertIsInstance(call_kwargs['parameters'].principal_id, str)
self.assertIsInstance(call_kwargs['parameters'].role_definition_id, str)
self.assertIsInstance(call_kwargs['scope'], str)
self.assertEqual(call_kwargs['parameters'].principal_id, "2b83823d-1afa-419f-8246-4b5d9df2f7e2")


2 changes: 1 addition & 1 deletion src/amg/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# TODO: Confirm this is the right version number you want and it matches your
# HISTORY.rst entry.
VERSION = '3.0.0'
VERSION = '3.0.1'

# The full list of classifiers is available at
# https://pypi.python.org/pypi?%3Aaction=list_classifiers
Expand Down
Loading