1212 QueryDataset , QueryDefinition ,
1313 QueryFilter , QueryGrouping ,
1414 QueryTimePeriod , TimeframeType )
15- from azure .mgmt .policyinsights import PolicyInsightsClient
1615from c7n_azure .tags import TagHelper
1716from c7n_azure .utils import (IpRangeHelper , Math , ResourceIdParser ,
1817 StringUtils , ThreadHelper , now , utcnow , is_resource_group )
1918from dateutil .parser import parse
20- from msrest . exceptions import HttpOperationError
19+ from azure . core . exceptions import HttpResponseError
2120
2221from c7n .filters import Filter , FilterValidationError , ValueFilter
2322from c7n .filters .core import PolicyValidationError
@@ -186,7 +185,7 @@ def get_metric_data(self, resource):
186185 aggregation = self .aggregation ,
187186 filter = self .get_filter (resource )
188187 )
189- except HttpOperationError :
188+ except HttpResponseError :
190189 self .log .exception ("Could not get metric: %s on %s" % (
191190 self .metric , resource ['id' ]))
192191 return None
@@ -484,7 +483,7 @@ def process(self, resources, event=None):
484483 d .name in self .definitions ]
485484
486485 # Find non-compliant resources
487- client = PolicyInsightsClient ( s . get_credentials () )
486+ client = s . client ( 'azure.mgmt.policyinsights.PolicyInsightsClient' )
488487 query = client .policy_states .list_query_results_for_subscription (
489488 policy_states_resource = 'latest' , subscription_id = s .subscription_id ).value
490489 non_compliant = [f .resource_id .lower () for f in query
@@ -821,13 +820,11 @@ class CostFilter(ValueFilter):
821820
822821 - ``WeekToDate``
823822 - ``MonthToDate``
824- - ``YearToDate``
825823
826824 - All days in the previous calendar period:
827825
828- - ``TheLastWeek``
829826 - ``TheLastMonth``
830- - ``TheLastYear ``
827+ - ``TheLastBillingMonth ``
831828
832829 :examples:
833830
@@ -925,7 +922,7 @@ def _query_costs(self):
925922
926923 client = manager .get_client ('azure.mgmt.costmanagement.CostManagementClient' )
927924
928- aggregation = {'totalCost' : QueryAggregation (name = 'PreTaxCost' )}
925+ aggregation = {'totalCost' : QueryAggregation (name = 'PreTaxCost' , function = 'Sum' )}
929926
930927 grouping = [QueryGrouping (type = 'Dimension' ,
931928 name = 'ResourceGroupName' if is_resource_group else 'ResourceId' )]
@@ -950,22 +947,24 @@ def _query_costs(self):
950947 timeframe = 'Custom'
951948 time_period = QueryTimePeriod (from_property = start_time , to = end_time )
952949
953- definition = QueryDefinition (timeframe = timeframe , time_period = time_period , dataset = dataset )
950+ definition = QueryDefinition (type = 'ActualCost' ,
951+ timeframe = timeframe ,
952+ time_period = time_period ,
953+ dataset = dataset )
954954
955955 subscription_id = manager .get_session ().get_subscription_id ()
956956
957957 scope = '/subscriptions/' + subscription_id
958958
959- query = client .query .usage_by_scope (scope , definition )
959+ query = client .query .usage (scope , definition )
960960
961961 if hasattr (query , '_derserializer' ):
962962 original = query ._derserializer ._deserialize
963963 query ._derserializer ._deserialize = lambda target , data : \
964964 original (target , self .fix_wrap_rest_response (data ))
965965
966- result_list = list (query )[0 ]
967- result_list = [{result_list .columns [i ].name : v for i , v in enumerate (row )}
968- for row in result_list .rows ]
966+ result_list = [{query .columns [i ].name : v for i , v in enumerate (row )}
967+ for row in query .rows ]
969968
970969 for r in result_list :
971970 if 'ResourceGroupName' in r :
0 commit comments