Skip to content

Commit 2f4f0ed

Browse files
authored
fix autoscale update for empty notification (#27597)
1 parent 8582779 commit 2f4f0ed

10 files changed

+1811
-2280
lines changed

src/azure-cli/azure/cli/command_modules/monitor/operations/autoscale_settings.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def update_add_actions(args):
5050
add_actions.append({
5151
"key": "email",
5252
"value": {
53-
"custom_emails": add_action_item_arr[1:]
53+
"customEmails": add_action_item_arr[1:]
5454
}
5555
})
5656
elif _type == "webhook":
@@ -182,30 +182,49 @@ def pre_instance_update(self, instance):
182182
profile.capacity.default = str(count)
183183
profile.capacity.minimum = str(min_count)
184184
profile.capacity.maximum = str(max_count)
185-
186-
if not instance.properties.notifications:
187-
return
188-
notification = next(x for x in instance.properties.notifications if x.operation.to_serialized_data().lower() == 'scale')
185+
updated_notification = None
186+
if instance.properties.notifications:
187+
retained_notification = []
188+
for x in instance.properties.notifications:
189+
note = x.to_serialized_data()
190+
if note['operation'].lower() == 'scale':
191+
updated_notification = note
192+
else:
193+
retained_notification.append(note)
194+
instance.properties.notifications = retained_notification
195+
else:
196+
instance.properties.notifications = []
197+
198+
if updated_notification is None:
199+
updated_notification = {
200+
"operation": "scale",
201+
"email": {
202+
"customEmails": []
203+
},
204+
"webhooks": []
205+
}
189206

190207
# process removals
191208
if len(remove_actions) > 0:
192209
removed_emails, removed_webhooks = _parse_action_removals(remove_actions)
193-
notification.email.custom_emails = \
194-
[x for x in notification.email.custom_emails if x not in removed_emails]
195-
notification.webhooks = \
196-
[x for x in notification.webhooks if x.service_uri not in removed_webhooks]
210+
updated_notification['email']['customEmails'] = \
211+
[x for x in updated_notification['email']['customEmails'] if x not in removed_emails]
212+
updated_notification['webhooks'] = \
213+
[x for x in updated_notification['webhooks'] if x['serviceUri'] not in removed_webhooks]
197214

198215
# process additions
199216
for action in add_actions:
200217
if action["key"] == "email":
201-
for email in action["value"]["custom_emails"]:
202-
notification.email.custom_emails.append(email)
218+
for email in action["value"]["customEmails"]:
219+
updated_notification['email']['customEmails'].append(email)
203220
elif action["key"] == "webhook":
204-
notification.webhooks.append(action["value"]) # please check snake case key here
221+
updated_notification['webhooks'].append(action["value"])
205222
if has_value(args.email_administrator):
206-
notification.email.send_to_subscription_administrator = args.email_administrator.to_serialized_data()
223+
updated_notification['email']['sendToSubscriptionAdministrator'] = args.email_administrator.to_serialized_data()
207224
if has_value(args.email_coadministrators):
208-
notification.email.send_to_subscription_co_administrators = args.email_coadministrators.to_serialized_data()
225+
updated_notification['email']['sendToSubscriptionCoAdministrators'] = args.email_coadministrators.to_serialized_data()
226+
227+
instance.properties.notifications.append(updated_notification)
209228

210229
if has_value(args.scale_look_ahead_time) and not has_value(args.scale_mode) \
211230
and not has_value(instance.properties.predictive_autoscale_policy):

src/azure-cli/azure/cli/command_modules/monitor/tests/latest/recordings/test_monitor_autoscale_basic.yaml

Lines changed: 216 additions & 256 deletions
Large diffs are not rendered by default.

src/azure-cli/azure/cli/command_modules/monitor/tests/latest/recordings/test_monitor_autoscale_fixed.yaml

Lines changed: 208 additions & 248 deletions
Large diffs are not rendered by default.

src/azure-cli/azure/cli/command_modules/monitor/tests/latest/recordings/test_monitor_autoscale_predictive_policy.yaml

Lines changed: 205 additions & 287 deletions
Large diffs are not rendered by default.

src/azure-cli/azure/cli/command_modules/monitor/tests/latest/recordings/test_monitor_autoscale_recurring.yaml

Lines changed: 227 additions & 309 deletions
Large diffs are not rendered by default.

src/azure-cli/azure/cli/command_modules/monitor/tests/latest/recordings/test_monitor_autoscale_refined.yaml

Lines changed: 216 additions & 298 deletions
Large diffs are not rendered by default.

src/azure-cli/azure/cli/command_modules/monitor/tests/latest/recordings/test_monitor_autoscale_rule_with_dimensions.yaml

Lines changed: 217 additions & 257 deletions
Large diffs are not rendered by default.

src/azure-cli/azure/cli/command_modules/monitor/tests/latest/recordings/test_monitor_autoscale_rules.yaml

Lines changed: 281 additions & 363 deletions
Large diffs are not rendered by default.

src/azure-cli/azure/cli/command_modules/monitor/tests/latest/recordings/test_monitor_autoscale_show_predictive_metric.yaml

Lines changed: 200 additions & 240 deletions
Large diffs are not rendered by default.

src/azure-cli/azure/cli/command_modules/monitor/tests/latest/test_monitor_autoscale.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_monitor_autoscale_basic(self, resource_group):
1515
self.kwargs.update({
1616
'vmss': 'vmss1'
1717
})
18-
self.cmd('vmss create -g {rg} -n {vmss} --image UbuntuLTS --admin-username testadmin --admin-password TestTest12#$')
18+
self.cmd('vmss create -g {rg} -n {vmss} --image Ubuntu2204 --admin-username testadmin --admin-password TestTest12#$')
1919
self.kwargs['vmss_id'] = self.cmd('vmss show -g {rg} -n {vmss}').get_output_in_json()['id']
2020

2121
self.cmd('monitor autoscale create --resource {vmss_id} --count 3 ', checks=[
@@ -55,7 +55,7 @@ def test_monitor_autoscale_refined(self, resource_group):
5555
self.kwargs.update({
5656
'vmss': 'vmss1'
5757
})
58-
self.cmd('vmss create -g {rg} -n {vmss} --image UbuntuLTS --admin-username testadmin --admin-password TestTest12#$')
58+
self.cmd('vmss create -g {rg} -n {vmss} --image Ubuntu2204 --admin-username testadmin --admin-password TestTest12#$')
5959
self.kwargs['vmss_id'] = self.cmd('vmss show -g {rg} -n {vmss}').get_output_in_json()['id']
6060

6161
self.cmd('monitor autoscale create --resource {vmss_id} --count 3 --action email abc@example.com efg@example.com opq@example.com --action webhook https://www.contoso.com/alert apiKey=value', checks=[
@@ -101,7 +101,7 @@ def test_monitor_autoscale_rules(self, resource_group):
101101
self.kwargs.update({
102102
'vmss': 'vmss1'
103103
})
104-
self.cmd('vmss create -g {rg} -n {vmss} --image UbuntuLTS --admin-username testadmin --admin-password TestTest12#$')
104+
self.cmd('vmss create -g {rg} -n {vmss} --image Ubuntu2204 --admin-username testadmin --admin-password TestTest12#$')
105105
self.kwargs['vmss_id'] = self.cmd('vmss show -g {rg} -n {vmss}').get_output_in_json()['id']
106106

107107
self.cmd('monitor autoscale create --resource {vmss_id} --min-count 1 --count 3 --max-count 5')
@@ -193,7 +193,7 @@ def test_monitor_autoscale_rule_with_dimensions(self, resource_group):
193193
'vmss': 'vmss1'
194194
})
195195
self.cmd(
196-
'vmss create -g {rg} -n {vmss} --image UbuntuLTS --admin-username testadmin --admin-password TestTest12#$ --instance-count 2')
196+
'vmss create -g {rg} -n {vmss} --image Ubuntu2204 --admin-username testadmin --admin-password TestTest12#$ --instance-count 2')
197197
self.kwargs['vmss_id'] = self.cmd('vmss show -g {rg} -n {vmss}').get_output_in_json()['id']
198198

199199
self.cmd('monitor autoscale create --resource {vmss_id} --min-count 1 --count 3 --max-count 5')
@@ -273,7 +273,7 @@ def test_monitor_autoscale_fixed(self, resource_group):
273273
'vmss': 'vmss1',
274274
'sched': 'Christmas'
275275
})
276-
self.cmd('vmss create -g {rg} -n {vmss} --image UbuntuLTS --admin-username testadmin --admin-password TestTest12#$')
276+
self.cmd('vmss create -g {rg} -n {vmss} --image Ubuntu2204 --admin-username testadmin --admin-password TestTest12#$')
277277
self.kwargs['vmss_id'] = self.cmd('vmss show -g {rg} -n {vmss}').get_output_in_json()['id']
278278

279279
self.cmd('monitor autoscale create --resource {vmss_id} --count 3')
@@ -310,7 +310,7 @@ def test_monitor_autoscale_recurring(self, resource_group):
310310
self.kwargs.update({
311311
'vmss': 'vmss1'
312312
})
313-
self.cmd('vmss create -g {rg} -n {vmss} --image UbuntuLTS --admin-username testname --admin-password TestTest12#$')
313+
self.cmd('vmss create -g {rg} -n {vmss} --image Ubuntu2204 --admin-username testname --admin-password TestTest12#$')
314314
self.kwargs['vmss_id'] = self.cmd('vmss show -g {rg} -n {vmss}').get_output_in_json()['id']
315315

316316
self.cmd('monitor autoscale create --resource {vmss_id} --count 3')
@@ -368,7 +368,7 @@ def test_monitor_autoscale_predictive_policy(self, resource_group):
368368
'new-scale-mode': 'ForecastOnly'
369369
})
370370
self.cmd(
371-
'vmss create -g {rg} -n {vmss} --image UbuntuLTS --admin-username testadmin --admin-password TestTest12#$')
371+
'vmss create -g {rg} -n {vmss} --image Ubuntu2204 --admin-username testadmin --admin-password TestTest12#$')
372372
self.kwargs['vmss_id'] = self.cmd('vmss show -g {rg} -n {vmss}').get_output_in_json()['id']
373373

374374
self.cmd('monitor autoscale create --resource {vmss_id} --count 3 --scale-mode {scale-mode}', checks=[
@@ -400,7 +400,7 @@ def test_monitor_autoscale_show_predictive_metric(self, resource_group):
400400
'scale-mode': 'Enabled',
401401
})
402402
self.cmd(
403-
'vmss create -g {rg} -n {vmss} --image UbuntuLTS --admin-username testadmin --admin-password TestTest12#$')
403+
'vmss create -g {rg} -n {vmss} --image Ubuntu2204 --admin-username testadmin --admin-password TestTest12#$')
404404
self.kwargs['vmss_id'] = self.cmd('vmss show -g {rg} -n {vmss}').get_output_in_json()['id']
405405

406406
self.cmd('monitor autoscale create --resource {vmss_id} --count 3 --max-count 5 --min-count 1 '

0 commit comments

Comments
 (0)