Skip to content

Commit 5de00f6

Browse files
author
Kirill Logachev
authored
Azure sdk update (cloud-custodian#6544)
1 parent 86f6d83 commit 5de00f6

File tree

79 files changed

+2702
-4378
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+2702
-4378
lines changed

tools/c7n_azure/c7n_azure/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@
99
logging.getLogger("msrest").setLevel(logging.ERROR)
1010
logging.getLogger("keyring").setLevel(logging.WARNING)
1111
logging.getLogger("azure.storage.common.storageclient").setLevel(logging.WARNING)
12+
13+
# This logger is spamming INFO with a bunch of requests data
14+
logging.getLogger('azure.identity').setLevel(logging.WARNING)
15+
logging.getLogger('azure.core.pipeline.policies.http_logging_policy').setLevel(logging.WARNING)

tools/c7n_azure/c7n_azure/actions/delete.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def _process_resource(self, resource):
6464
if is_resource_group(resource):
6565
self.client.resource_groups.delete(resource['name'])
6666
else:
67-
self.client.resources.delete_by_id(resource['id'],
68-
self.session.resource_api_version(resource['id']))
67+
self.client.resources.begin_delete_by_id(
68+
resource['id'],
69+
self.session.resource_api_version(resource['id']))
6970
return "deleted"

tools/c7n_azure/c7n_azure/actions/logic_app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright The Cloud Custodian Authors.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
from azure.mgmt.logic import logic_management_client
4+
from azure.mgmt.logic import LogicManagementClient
55

66
from c7n.actions.webhook import Webhook
77
from c7n.utils import type_schema
@@ -59,9 +59,9 @@ def process(self, resources, event=None):
5959
def get_callback_url(self, resource_group, workflow_name):
6060
""" Gets the logic app invoke trigger with secrets using RBAC """
6161

62-
# type: logic_management_client.LogicManagementClient
62+
# type: LogicManagementClient
6363
client = self.manager.get_client(
64-
'azure.mgmt.logic.logic_management_client.LogicManagementClient')
64+
'azure.mgmt.logic.LogicManagementClient')
6565

6666
callback = client.workflow_triggers.list_callback_url(
6767
resource_group,

tools/c7n_azure/c7n_azure/azure_events.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,6 @@ def create(destination, name, subscription_id, session=None, event_filter=None):
139139
scope = '/subscriptions/%s' % subscription_id
140140

141141
client = s.client('azure.mgmt.eventgrid.EventGridManagementClient')
142-
event_subscription = client.event_subscriptions.create_or_update(scope, name, event_info)
142+
event_subscription = \
143+
client.event_subscriptions.begin_create_or_update(scope, name, event_info)
143144
return event_subscription.result()

tools/c7n_azure/c7n_azure/container_host/host.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,15 @@ def update_policies(self):
159159

160160
client.get_blob_to_path(container, blob.name, policy_path)
161161
self.load_policy(policy_path, policies_copy)
162-
self.blob_cache.update({blob.name: blob.properties.content_settings.content_md5})
162+
self.blob_cache.update({blob.name: blob.content_settings.content_md5})
163163

164164
# Assign our copy back over the original
165165
self.policies = policies_copy
166166

167167
def _get_new_blobs(self, blobs):
168168
new_blobs = []
169169
for blob in blobs:
170-
md5_hash = blob.properties.content_settings.content_md5
170+
md5_hash = blob.content_settings.content_md5
171171
if not md5_hash:
172172
blob, md5_hash = self._try_create_md5_content_hash(blob)
173173
if blob and md5_hash and md5_hash != self.blob_cache.get(blob.name):
@@ -191,7 +191,7 @@ def _try_create_md5_content_hash(self, blob):
191191
# Re-fetch the blob with the new hash
192192
hashed_blob = client.get_blob_properties(container, blob.name)
193193

194-
return hashed_blob, hashed_blob.properties.content_settings.content_md5
194+
return hashed_blob, hashed_blob.content_settings.content_md5
195195
except AzureHttpError as e:
196196
log.warning("Failed to apply a md5 content hash to policy {}. "
197197
"This policy will be skipped.".format(blob.name))

tools/c7n_azure/c7n_azure/filters.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
QueryDataset, QueryDefinition,
1313
QueryFilter, QueryGrouping,
1414
QueryTimePeriod, TimeframeType)
15-
from azure.mgmt.policyinsights import PolicyInsightsClient
1615
from c7n_azure.tags import TagHelper
1716
from c7n_azure.utils import (IpRangeHelper, Math, ResourceIdParser,
1817
StringUtils, ThreadHelper, now, utcnow, is_resource_group)
1918
from dateutil.parser import parse
20-
from msrest.exceptions import HttpOperationError
19+
from azure.core.exceptions import HttpResponseError
2120

2221
from c7n.filters import Filter, FilterValidationError, ValueFilter
2322
from 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:

tools/c7n_azure/c7n_azure/function_package.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ def _add_functions_required_files(
7878
auth_contents = s.get_functions_auth_string(target_sub_id)
7979
elif identity['type'] == AUTH_TYPE_MSI:
8080
auth_contents = json.dumps({
81-
'use_msi': True, 'subscription_id': target_sub_id})
81+
'use_msi': True, 'subscription_id': target_sub_id,
82+
'tenant_id': s.get_tenant_id()})
8283
elif identity['type'] == AUTH_TYPE_UAI:
8384
auth_contents = json.dumps({
8485
'use_msi': True, 'subscription_id': target_sub_id,
85-
'client_id': identity['client_id']})
86+
'client_id': identity['client_id'],
87+
'tenant_id': s.get_tenant_id()})
8688

8789
self.pkg.add_contents(dest=name + '/auth.json', contents=auth_contents)
8890
self.pkg.add_file(self.function_path,

tools/c7n_azure/c7n_azure/functionapp_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def get_storage_account_connection_string(id):
2929
rg_name = ResourceIdParser.get_resource_group(id)
3030
name = ResourceIdParser.get_resource_name(id)
3131
client = local_session(Session).client('azure.mgmt.storage.StorageManagementClient')
32-
obj = client.storage_accounts.list_keys(rg_name, name)
32+
obj = client.storage_accounts.list_keys(rg_name, name, expand=None)
3333

3434
connection_string = 'DefaultEndpointsProtocol={};AccountName={};AccountKey={}'.format(
3535
'https',
@@ -102,7 +102,7 @@ def publish_functions_package(cls, function_params, package):
102102

103103
cls.log.info('Publishing Function application')
104104

105-
publish_creds = web_client.web_apps.list_publishing_credentials(
105+
publish_creds = web_client.web_apps.begin_list_publishing_credentials(
106106
function_params.function_app['resource_group_name'],
107107
function_params.function_app['name']).result()
108108

tools/c7n_azure/c7n_azure/handler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from azure.common import AzureHttpError
1010
from msrestazure.azure_exceptions import CloudError
11+
from azure.core.exceptions import AzureError
1112

1213
from c7n.utils import reset_session_cache
1314
from c7n.config import Config
@@ -53,7 +54,7 @@ def run(event, context, subscription_id=None):
5354
for p in policies:
5455
try:
5556
p.push(event, context)
56-
except (CloudError, AzureHttpError) as error:
57+
except (CloudError, AzureHttpError, AzureError) as error:
5758
log.error("Unable to process policy: %s :: %s" % (p.name, error))
5859

5960
reset_session_cache()

tools/c7n_azure/c7n_azure/provisioning/app_insights.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright The Cloud Custodian Authors.
22
# SPDX-License-Identifier: Apache-2.0
3-
from msrestazure.azure_exceptions import CloudError
3+
from azure.core.exceptions import ResourceNotFoundError
44

55
from c7n_azure.provisioning.deployment_unit import DeploymentUnit
66
from c7n_azure.provisioning.resource_group import ResourceGroupUnit
@@ -16,7 +16,7 @@ def __init__(self):
1616
def _get(self, params):
1717
try:
1818
return self.client.components.get(params['resource_group_name'], params['name'])
19-
except CloudError:
19+
except ResourceNotFoundError:
2020
return None
2121

2222
def _provision(self, params):

0 commit comments

Comments
 (0)