Skip to content

Commit d5c3578

Browse files
authored
[Monitor Query] Typing updates (#35180)
Regenerated with latest autorest and updated some samples. Changed the model types for status to use Literal with the corresponding enum values to improve typing. Signed-off-by: Paul Van Eck <[email protected]>
1 parent dccef72 commit d5c3578

29 files changed

+184
-142
lines changed

sdk/monitor/azure-monitor-query/README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,14 @@ query = """AzureActivity | take 5"""
147147

148148
try:
149149
response = client.query_resource(os.environ['LOGS_RESOURCE_ID'], query, timespan=timedelta(days=1))
150-
if response.status == LogsQueryStatus.PARTIAL:
150+
if response.status == LogsQueryStatus.SUCCESS:
151+
data = response.tables
152+
else:
153+
# LogsQueryPartialResult
151154
error = response.partial_error
152155
data = response.partial_data
153156
print(error)
154-
elif response.status == LogsQueryStatus.SUCCESS:
155-
data = response.tables
157+
156158
for table in data:
157159
df = pd.DataFrame(data=table.rows, columns=table.columns)
158160
print(df)
@@ -175,7 +177,7 @@ For example:
175177
import os
176178
import pandas as pd
177179
from datetime import datetime, timezone
178-
from azure.monitor.query import LogsQueryClient, LogsQueryStatus
180+
from azure.monitor.query import LogsQueryClient, LogsQueryResult
179181
from azure.identity import DefaultAzureCredential
180182
from azure.core.exceptions import HttpResponseError
181183

@@ -193,12 +195,14 @@ try:
193195
query=query,
194196
timespan=(start_time, end_time)
195197
)
196-
if response.status == LogsQueryStatus.PARTIAL:
198+
if response.status == LogsQueryStatus.SUCCESS:
199+
data = response.tables
200+
else:
201+
# LogsQueryPartialResult
197202
error = response.partial_error
198203
data = response.partial_data
199204
print(error)
200-
elif response.status == LogsQueryStatus.SUCCESS:
201-
data = response.tables
205+
202206
for table in data:
203207
df = pd.DataFrame(data=table.rows, columns=table.columns)
204208
print(df)
@@ -294,10 +298,7 @@ requests = [
294298
results = client.query_batch(requests)
295299

296300
for res in results:
297-
if res.status == LogsQueryStatus.FAILURE:
298-
# this will be a LogsQueryError
299-
print(res.message)
300-
elif res.status == LogsQueryStatus.PARTIAL:
301+
if res.status == LogsQueryStatus.PARTIAL:
301302
## this will be a LogsQueryPartialResult
302303
print(res.partial_error)
303304
for table in res.partial_data:
@@ -308,6 +309,9 @@ for res in results:
308309
table = res.tables[0]
309310
df = pd.DataFrame(table.rows, columns=table.columns)
310311
print(df)
312+
else:
313+
# this will be a LogsQueryError
314+
print(res.message)
311315

312316
```
313317

sdk/monitor/azure-monitor-query/azure/monitor/query/_exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# license information.
66
# --------------------------------------------------------------------------
77
import sys
8-
from typing import Any, List, Optional
8+
from typing import Any, List, Optional, Literal
99

1010
from ._enums import LogsQueryStatus
1111

@@ -27,7 +27,7 @@ class LogsQueryError:
2727
"""A human readable error message."""
2828
details: Optional[List[JSON]] = None
2929
"""A list of additional details about the error."""
30-
status: LogsQueryStatus
30+
status: Literal[LogsQueryStatus.FAILURE]
3131
"""Status for error item when iterating over list of results. Always "Failure" for an instance of a
3232
LogsQueryError."""
3333

sdk/monitor/azure-monitor-query/azure/monitor/query/_generated/aio/operations/_operations.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import datetime
1010
from io import IOBase
1111
import sys
12-
from typing import Any, Callable, Dict, IO, Optional, TypeVar, Union, cast, overload
12+
from typing import Any, Callable, Dict, IO, Optional, Type, TypeVar, Union, cast, overload
1313

1414
from azure.core.exceptions import (
1515
ClientAuthenticationError,
@@ -136,7 +136,7 @@ async def get(
136136
"statistics": {} # Optional. Statistics represented in JSON format.
137137
}
138138
"""
139-
error_map = {
139+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
140140
401: ClientAuthenticationError,
141141
404: ResourceNotFoundError,
142142
409: ResourceExistsError,
@@ -450,7 +450,7 @@ async def execute(
450450
"statistics": {} # Optional. Statistics represented in JSON format.
451451
}
452452
"""
453-
error_map = {
453+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
454454
401: ClientAuthenticationError,
455455
404: ResourceNotFoundError,
456456
409: ResourceExistsError,
@@ -581,7 +581,7 @@ async def resource_get(
581581
"statistics": {} # Optional. Statistics represented in JSON format.
582582
}
583583
"""
584-
error_map = {
584+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
585585
401: ClientAuthenticationError,
586586
404: ResourceNotFoundError,
587587
409: ResourceExistsError,
@@ -892,7 +892,7 @@ async def resource_execute(
892892
"statistics": {} # Optional. Statistics represented in JSON format.
893893
}
894894
"""
895-
error_map = {
895+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
896896
401: ClientAuthenticationError,
897897
404: ResourceNotFoundError,
898898
409: ResourceExistsError,
@@ -1286,7 +1286,7 @@ async def batch(self, body: Union[JSON, IO[bytes]], **kwargs: Any) -> JSON:
12861286
]
12871287
}
12881288
"""
1289-
error_map = {
1289+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
12901290
401: ClientAuthenticationError,
12911291
404: ResourceNotFoundError,
12921292
409: ResourceExistsError,
@@ -1415,7 +1415,7 @@ async def resource_get_xms(
14151415
"statistics": {} # Optional. Statistics represented in JSON format.
14161416
}
14171417
"""
1418-
error_map = {
1418+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
14191419
401: ClientAuthenticationError,
14201420
404: ResourceNotFoundError,
14211421
409: ResourceExistsError,
@@ -1726,7 +1726,7 @@ async def resource_execute_xms(
17261726
"statistics": {} # Optional. Statistics represented in JSON format.
17271727
}
17281728
"""
1729-
error_map = {
1729+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
17301730
401: ClientAuthenticationError,
17311731
404: ResourceNotFoundError,
17321732
409: ResourceExistsError,
@@ -2159,7 +2159,7 @@ async def get(self, workspace_id: str, **kwargs: Any) -> JSON:
21592159
]
21602160
}
21612161
"""
2162-
error_map = {
2162+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
21632163
401: ClientAuthenticationError,
21642164
404: ResourceNotFoundError,
21652165
409: ResourceExistsError,
@@ -2561,7 +2561,7 @@ async def post(self, workspace_id: str, **kwargs: Any) -> JSON:
25612561
]
25622562
}
25632563
"""
2564-
error_map = {
2564+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
25652565
401: ClientAuthenticationError,
25662566
404: ResourceNotFoundError,
25672567
409: ResourceExistsError,

sdk/monitor/azure-monitor-query/azure/monitor/query/_generated/metrics/aio/operations/_operations.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# --------------------------------------------------------------------------
99
import datetime
1010
import sys
11-
from typing import Any, AsyncIterable, Callable, Dict, Optional, TypeVar, cast
11+
from typing import Any, AsyncIterable, Callable, Dict, Optional, Type, TypeVar, cast
1212
import urllib.parse
1313

1414
from azure.core.async_paging import AsyncItemPaged, AsyncList
@@ -126,7 +126,7 @@ def list(self, resource_uri: str, *, metricnamespace: Optional[str] = None, **kw
126126

127127
cls: ClsType[JSON] = kwargs.pop("cls", None)
128128

129-
error_map = {
129+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
130130
401: ClientAuthenticationError,
131131
404: ResourceNotFoundError,
132132
409: ResourceExistsError,
@@ -367,7 +367,7 @@ async def list(
367367
for metrics.
368368
}
369369
"""
370-
error_map = {
370+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
371371
401: ClientAuthenticationError,
372372
404: ResourceNotFoundError,
373373
409: ResourceExistsError,
@@ -474,7 +474,7 @@ def list(self, resource_uri: str, *, start_time: Optional[str] = None, **kwargs:
474474

475475
cls: ClsType[JSON] = kwargs.pop("cls", None)
476476

477-
error_map = {
477+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
478478
401: ClientAuthenticationError,
479479
404: ResourceNotFoundError,
480480
409: ResourceExistsError,

sdk/monitor/azure-monitor-query/azure/monitor/query/_generated/metrics/batch/aio/operations/_operations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import datetime
1010
from io import IOBase
1111
import sys
12-
from typing import Any, Callable, Dict, IO, List, Optional, TypeVar, Union, cast, overload
12+
from typing import Any, Callable, Dict, IO, List, Optional, Type, TypeVar, Union, cast, overload
1313

1414
from azure.core.exceptions import (
1515
ClientAuthenticationError,
@@ -566,7 +566,7 @@ async def batch(
566566
]
567567
}
568568
"""
569-
error_map = {
569+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
570570
401: ClientAuthenticationError,
571571
404: ResourceNotFoundError,
572572
409: ResourceExistsError,

sdk/monitor/azure-monitor-query/azure/monitor/query/_generated/metrics/batch/operations/_operations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import datetime
1010
from io import IOBase
1111
import sys
12-
from typing import Any, Callable, Dict, IO, List, Optional, TypeVar, Union, cast, overload
12+
from typing import Any, Callable, Dict, IO, List, Optional, Type, TypeVar, Union, cast, overload
1313

1414
from azure.core.exceptions import (
1515
ClientAuthenticationError,
@@ -628,7 +628,7 @@ def batch(
628628
]
629629
}
630630
"""
631-
error_map = {
631+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
632632
401: ClientAuthenticationError,
633633
404: ResourceNotFoundError,
634634
409: ResourceExistsError,

sdk/monitor/azure-monitor-query/azure/monitor/query/_generated/metrics/operations/_operations.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# --------------------------------------------------------------------------
99
import datetime
1010
import sys
11-
from typing import Any, Callable, Dict, Iterable, Optional, TypeVar, cast
11+
from typing import Any, Callable, Dict, Iterable, Optional, Type, TypeVar, cast
1212
import urllib.parse
1313

1414
from azure.core.exceptions import (
@@ -244,7 +244,7 @@ def list(self, resource_uri: str, *, metricnamespace: Optional[str] = None, **kw
244244

245245
cls: ClsType[JSON] = kwargs.pop("cls", None)
246246

247-
error_map = {
247+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
248248
401: ClientAuthenticationError,
249249
404: ResourceNotFoundError,
250250
409: ResourceExistsError,
@@ -485,7 +485,7 @@ def list(
485485
for metrics.
486486
}
487487
"""
488-
error_map = {
488+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
489489
401: ClientAuthenticationError,
490490
404: ResourceNotFoundError,
491491
409: ResourceExistsError,
@@ -592,7 +592,7 @@ def list(self, resource_uri: str, *, start_time: Optional[str] = None, **kwargs:
592592

593593
cls: ClsType[JSON] = kwargs.pop("cls", None)
594594

595-
error_map = {
595+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
596596
401: ClientAuthenticationError,
597597
404: ResourceNotFoundError,
598598
409: ResourceExistsError,

sdk/monitor/azure-monitor-query/azure/monitor/query/_generated/operations/_operations.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import datetime
1010
from io import IOBase
1111
import sys
12-
from typing import Any, Callable, Dict, IO, Optional, TypeVar, Union, cast, overload
12+
from typing import Any, Callable, Dict, IO, Optional, Type, TypeVar, Union, cast, overload
1313

1414
from azure.core.exceptions import (
1515
ClientAuthenticationError,
@@ -341,7 +341,7 @@ def get(
341341
"statistics": {} # Optional. Statistics represented in JSON format.
342342
}
343343
"""
344-
error_map = {
344+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
345345
401: ClientAuthenticationError,
346346
404: ResourceNotFoundError,
347347
409: ResourceExistsError,
@@ -655,7 +655,7 @@ def execute(
655655
"statistics": {} # Optional. Statistics represented in JSON format.
656656
}
657657
"""
658-
error_map = {
658+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
659659
401: ClientAuthenticationError,
660660
404: ResourceNotFoundError,
661661
409: ResourceExistsError,
@@ -786,7 +786,7 @@ def resource_get(
786786
"statistics": {} # Optional. Statistics represented in JSON format.
787787
}
788788
"""
789-
error_map = {
789+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
790790
401: ClientAuthenticationError,
791791
404: ResourceNotFoundError,
792792
409: ResourceExistsError,
@@ -1097,7 +1097,7 @@ def resource_execute(
10971097
"statistics": {} # Optional. Statistics represented in JSON format.
10981098
}
10991099
"""
1100-
error_map = {
1100+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
11011101
401: ClientAuthenticationError,
11021102
404: ResourceNotFoundError,
11031103
409: ResourceExistsError,
@@ -1491,7 +1491,7 @@ def batch(self, body: Union[JSON, IO[bytes]], **kwargs: Any) -> JSON:
14911491
]
14921492
}
14931493
"""
1494-
error_map = {
1494+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
14951495
401: ClientAuthenticationError,
14961496
404: ResourceNotFoundError,
14971497
409: ResourceExistsError,
@@ -1620,7 +1620,7 @@ def resource_get_xms(
16201620
"statistics": {} # Optional. Statistics represented in JSON format.
16211621
}
16221622
"""
1623-
error_map = {
1623+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
16241624
401: ClientAuthenticationError,
16251625
404: ResourceNotFoundError,
16261626
409: ResourceExistsError,
@@ -1931,7 +1931,7 @@ def resource_execute_xms(
19311931
"statistics": {} # Optional. Statistics represented in JSON format.
19321932
}
19331933
"""
1934-
error_map = {
1934+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
19351935
401: ClientAuthenticationError,
19361936
404: ResourceNotFoundError,
19371937
409: ResourceExistsError,
@@ -2364,7 +2364,7 @@ def get(self, workspace_id: str, **kwargs: Any) -> JSON:
23642364
]
23652365
}
23662366
"""
2367-
error_map = {
2367+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
23682368
401: ClientAuthenticationError,
23692369
404: ResourceNotFoundError,
23702370
409: ResourceExistsError,
@@ -2766,7 +2766,7 @@ def post(self, workspace_id: str, **kwargs: Any) -> JSON:
27662766
]
27672767
}
27682768
"""
2769-
error_map = {
2769+
error_map: MutableMapping[int, Type[HttpResponseError]] = {
27702770
401: ClientAuthenticationError,
27712771
404: ResourceNotFoundError,
27722772
409: ResourceExistsError,

sdk/monitor/azure-monitor-query/azure/monitor/query/_models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import uuid
99
from datetime import datetime, timedelta
1010
import sys
11-
from typing import Any, Optional, List, Union, Tuple, Dict, Iterator
11+
from typing import Any, Optional, List, Union, Tuple, Dict, Iterator, Literal
1212

1313
from ._enums import LogsQueryStatus, MetricAggregationType, MetricClass, MetricNamespaceClassification, MetricUnit
1414
from ._exceptions import LogsQueryError
@@ -376,7 +376,7 @@ class LogsQueryResult:
376376
visualization: Optional[JSON] = None
377377
"""This will include a visualization property in the response that specifies the type of visualization selected
378378
by the query and any properties for that visualization."""
379-
status: LogsQueryStatus
379+
status: Literal[LogsQueryStatus.SUCCESS]
380380
"""The status of the result. Always 'Success' for an instance of a LogsQueryResult."""
381381

382382
def __init__(self, **kwargs: Any) -> None:
@@ -554,7 +554,7 @@ class LogsQueryPartialResult:
554554
selected by the query and any properties for that visualization."""
555555
partial_error: Optional[LogsQueryError] = None
556556
"""The partial error info."""
557-
status: LogsQueryStatus
557+
status: Literal[LogsQueryStatus.PARTIAL]
558558
"""The status of the result. Always 'PartialError' for an instance of a LogsQueryPartialResult."""
559559

560560
def __init__(self, **kwargs: Any) -> None:

0 commit comments

Comments
 (0)