| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_autoscale |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Manage Azure autoscale setting |
| 16 |
description: |
| 17 |
- Create, delete an autoscale setting. |
| 18 |
options: |
| 19 |
target: |
| 20 |
description: |
| 21 |
- The identifier of the resource to apply autoscale setting. |
| 22 |
- It could be the resource id string. |
| 23 |
- It also could be a dict contains the C(name), C(subscription_id), C(namespace), C(types), C(resource_group) of the resource. |
| 24 |
type: raw |
| 25 |
resource_group: |
| 26 |
required: true |
| 27 |
description: |
| 28 |
- Resource group of the resource. |
| 29 |
type: str |
| 30 |
enabled: |
| 31 |
type: bool |
| 32 |
description: |
| 33 |
- Specifies whether automatic scaling is enabled for the resource. |
| 34 |
default: true |
| 35 |
profiles: |
| 36 |
description: |
| 37 |
- The collection of automatic scaling profiles that specify different scaling parameters for different time periods. |
| 38 |
- A maximum of 20 profiles can be specified. |
| 39 |
type: list |
| 40 |
elements: dict |
| 41 |
suboptions: |
| 42 |
name: |
| 43 |
required: true |
| 44 |
description: |
| 45 |
- The name of the profile. |
| 46 |
type: str |
| 47 |
count: |
| 48 |
required: true |
| 49 |
description: |
| 50 |
- The number of instances that will be set if metrics are not available for evaluation. |
| 51 |
- The default is only used if the current instance count is lower than the default. |
| 52 |
type: str |
| 53 |
min_count: |
| 54 |
description: |
| 55 |
- The minimum number of instances for the resource. |
| 56 |
type: str |
| 57 |
max_count: |
| 58 |
description: |
| 59 |
- The maximum number of instances for the resource. |
| 60 |
- The actual maximum number of instances is limited by the cores that are available in the subscription. |
| 61 |
type: str |
| 62 |
recurrence_frequency: |
| 63 |
default: None |
| 64 |
description: |
| 65 |
- How often the schedule profile should take effect. |
| 66 |
- If this value is C(Week), meaning each week will have the same set of profiles. |
| 67 |
- This element is not used if the FixedDate element is used. |
| 68 |
type: str |
| 69 |
choices: |
| 70 |
- None |
| 71 |
- Second |
| 72 |
- Minute |
| 73 |
- Hour |
| 74 |
- Day |
| 75 |
- Week |
| 76 |
- Month |
| 77 |
- Year |
| 78 |
recurrence_timezone: |
| 79 |
description: |
| 80 |
- The timezone of repeating times at which this profile begins. |
| 81 |
- This element is not used if the FixedDate element is used. |
| 82 |
type: str |
| 83 |
recurrence_days: |
| 84 |
description: |
| 85 |
- The days of repeating times at which this profile begins. |
| 86 |
- This element is not used if the FixedDate element is used. |
| 87 |
type: list |
| 88 |
elements: str |
| 89 |
recurrence_hours: |
| 90 |
description: |
| 91 |
- The hours of repeating times at which this profile begins. |
| 92 |
- This element is not used if the FixedDate element is used. |
| 93 |
type: list |
| 94 |
elements: str |
| 95 |
recurrence_mins: |
| 96 |
description: |
| 97 |
- The mins of repeating times at which this profile begins. |
| 98 |
- This element is not used if the FixedDate element is used. |
| 99 |
type: list |
| 100 |
elements: str |
| 101 |
fixed_date_timezone: |
| 102 |
description: |
| 103 |
- The specific date-time timezone for the profile. |
| 104 |
- This element is not used if the Recurrence element is used. |
| 105 |
type: str |
| 106 |
fixed_date_start: |
| 107 |
description: |
| 108 |
- The specific date-time start for the profile. |
| 109 |
- This element is not used if the Recurrence element is used. |
| 110 |
type: str |
| 111 |
fixed_date_end: |
| 112 |
description: |
| 113 |
- The specific date-time end for the profile. |
| 114 |
- This element is not used if the Recurrence element is used. |
| 115 |
type: str |
| 116 |
rules: |
| 117 |
description: |
| 118 |
- The collection of rules that provide the triggers and parameters for the scaling action. |
| 119 |
- A maximum of 10 rules can be specified. |
| 120 |
type: list |
| 121 |
elements: dict |
| 122 |
suboptions: |
| 123 |
time_aggregation: |
| 124 |
default: Average |
| 125 |
description: |
| 126 |
- How the data that is collected should be combined over time. |
| 127 |
type: str |
| 128 |
choices: |
| 129 |
- Average |
| 130 |
- Minimum |
| 131 |
- Maximum |
| 132 |
- Total |
| 133 |
- Count |
| 134 |
time_window: |
| 135 |
required: true |
| 136 |
description: |
| 137 |
- The range of time(minutes) in which instance data is collected. |
| 138 |
- This value must be greater than the delay in metric collection, which can vary from resource-to-resource. |
| 139 |
- Must be between 5 ~ 720. |
| 140 |
type: float |
| 141 |
direction: |
| 142 |
description: |
| 143 |
- Whether the scaling action increases or decreases the number of instances. |
| 144 |
type: str |
| 145 |
choices: |
| 146 |
- Increase |
| 147 |
- Decrease |
| 148 |
metric_name: |
| 149 |
required: true |
| 150 |
description: |
| 151 |
- The name of the metric that defines what the rule monitors. |
| 152 |
type: str |
| 153 |
metric_resource_uri: |
| 154 |
description: |
| 155 |
- The resource identifier of the resource the rule monitors. |
| 156 |
type: str |
| 157 |
dimensions: |
| 158 |
description: |
| 159 |
- List of dimension conditions. |
| 160 |
type: list |
| 161 |
elements: dict |
| 162 |
suboptions: |
| 163 |
dimension_name: |
| 164 |
description: |
| 165 |
- Name of the dimension. |
| 166 |
type: str |
| 167 |
required: true |
| 168 |
operator: |
| 169 |
description: |
| 170 |
- The dimension operator. Only C(Equals) and C(NotEquals) are supported. |
| 171 |
choices: |
| 172 |
- Equals |
| 173 |
- NotEquals |
| 174 |
type: str |
| 175 |
required: true |
| 176 |
values: |
| 177 |
description: |
| 178 |
- List of dimension values. |
| 179 |
type: list |
| 180 |
elements: str |
| 181 |
required: true |
| 182 |
divide_per_instance: |
| 183 |
description: |
| 184 |
- A value indicating whether metric should divide per instance. |
| 185 |
type: bool |
| 186 |
metric_namespace: |
| 187 |
description: |
| 188 |
- The namespace of the metric that defines what the rule monitors. |
| 189 |
type: str |
| 190 |
value: |
| 191 |
description: |
| 192 |
- The number of instances that are involved in the scaling action. |
| 193 |
- This value must be 1 or greater. |
| 194 |
type: str |
| 195 |
operator: |
| 196 |
default: GreaterThan |
| 197 |
description: |
| 198 |
- The operator that is used to compare the metric data and the threshold. |
| 199 |
type: str |
| 200 |
choices: |
| 201 |
- Equals |
| 202 |
- NotEquals |
| 203 |
- GreaterThan |
| 204 |
- GreaterThanOrEqual |
| 205 |
- LessThan |
| 206 |
- LessThanOrEqual |
| 207 |
cooldown: |
| 208 |
description: |
| 209 |
- The amount of time (minutes) to wait since the last scaling action before this action occurs. |
| 210 |
- It must be between 1 ~ 10080. |
| 211 |
type: float |
| 212 |
time_grain: |
| 213 |
required: true |
| 214 |
description: |
| 215 |
- The granularity(minutes) of metrics the rule monitors. |
| 216 |
- Must be one of the predefined values returned from metric definitions for the metric. |
| 217 |
- Must be between 1 ~ 720. |
| 218 |
type: float |
| 219 |
statistic: |
| 220 |
default: Average |
| 221 |
description: |
| 222 |
- How the metrics from multiple instances are combined. |
| 223 |
type: str |
| 224 |
choices: |
| 225 |
- Average |
| 226 |
- Min |
| 227 |
- Max |
| 228 |
- Sum |
| 229 |
threshold: |
| 230 |
default: 70 |
| 231 |
description: |
| 232 |
- The threshold of the metric that triggers the scale action. |
| 233 |
type: float |
| 234 |
type: |
| 235 |
description: |
| 236 |
- The type of action that should occur when the scale rule fires. |
| 237 |
type: str |
| 238 |
choices: |
| 239 |
- PercentChangeCount |
| 240 |
- ExactCount |
| 241 |
- ChangeCount |
| 242 |
notifications: |
| 243 |
description: |
| 244 |
- The collection of notifications. |
| 245 |
type: list |
| 246 |
elements: dict |
| 247 |
suboptions: |
| 248 |
custom_emails: |
| 249 |
description: |
| 250 |
- The custom e-mails list. This value can be null or empty, in which case this attribute will be ignored. |
| 251 |
type: list |
| 252 |
elements: str |
| 253 |
send_to_subscription_administrator: |
| 254 |
type: bool |
| 255 |
default: False |
| 256 |
description: |
| 257 |
- A value indicating whether to send email to subscription administrator. |
| 258 |
aliases: |
| 259 |
- email_admin |
| 260 |
webhooks: |
| 261 |
description: |
| 262 |
- The list of webhook notifications service uri. |
| 263 |
type: list |
| 264 |
elements: str |
| 265 |
send_to_subscription_co_administrators: |
| 266 |
type: bool |
| 267 |
default: False |
| 268 |
description: |
| 269 |
- A value indicating whether to send email to subscription co-administrators. |
| 270 |
aliases: |
| 271 |
- email_co_admin |
| 272 |
state: |
| 273 |
default: present |
| 274 |
description: |
| 275 |
- Assert the state of the virtual network. Use C(present) to create or update and C(absent) to delete. |
| 276 |
type: str |
| 277 |
choices: |
| 278 |
- present |
| 279 |
- absent |
| 280 |
location: |
| 281 |
description: |
| 282 |
- location of the resource. |
| 283 |
type: str |
| 284 |
name: |
| 285 |
required: true |
| 286 |
description: |
| 287 |
- name of the resource. |
| 288 |
type: str |
| 289 |
|
| 290 |
|
| 291 |
extends_documentation_fragment: |
| 292 |
- azure.azcollection.azure |
| 293 |
- azure.azcollection.azure_tags |
| 294 |
|
| 295 |
author: |
| 296 |
- Yuwei Zhou (@yuwzho) |
| 297 |
|
| 298 |
''' |
| 299 |
|
| 300 |
EXAMPLES = ''' |
| 301 |
- name: Create an auto scale |
| 302 |
azure_rm_autoscale: |
| 303 |
target: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVmss" |
| 304 |
enabled: true |
| 305 |
profiles: |
| 306 |
- count: '1' |
| 307 |
recurrence_days: |
| 308 |
- Monday |
| 309 |
name: Auto created scale condition |
| 310 |
recurrence_timezone: China Standard Time |
| 311 |
recurrence_mins: |
| 312 |
- '0' |
| 313 |
min_count: '1' |
| 314 |
max_count: '1' |
| 315 |
recurrence_frequency: Week |
| 316 |
recurrence_hours: |
| 317 |
- '18' |
| 318 |
name: scale |
| 319 |
resource_group: myResourceGroup |
| 320 |
|
| 321 |
- name: Create an auto scale with complicated profile |
| 322 |
azure_rm_autoscale: |
| 323 |
target: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets |
| 324 |
/myVmss" |
| 325 |
enabled: true |
| 326 |
profiles: |
| 327 |
- count: '1' |
| 328 |
recurrence_days: |
| 329 |
- Monday |
| 330 |
name: Auto created scale condition 0 |
| 331 |
rules: |
| 332 |
- time_aggregation: Average |
| 333 |
time_window: 10 |
| 334 |
direction: Increase |
| 335 |
metric_name: Percentage CPU |
| 336 |
metric_resource_uri: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtua |
| 337 |
lMachineScaleSets/vmss" |
| 338 |
value: '1' |
| 339 |
threshold: 70 |
| 340 |
cooldown: 5 |
| 341 |
time_grain: 1 |
| 342 |
statistic: Average |
| 343 |
operator: GreaterThan |
| 344 |
type: ChangeCount |
| 345 |
dimensions: |
| 346 |
- dimension_name: AppName |
| 347 |
operator: Equals |
| 348 |
values: |
| 349 |
- App1 |
| 350 |
- App2 |
| 351 |
max_count: '1' |
| 352 |
recurrence_mins: |
| 353 |
- '0' |
| 354 |
min_count: '1' |
| 355 |
recurrence_timezone: China Standard Time |
| 356 |
recurrence_frequency: Week |
| 357 |
recurrence_hours: |
| 358 |
- '6' |
| 359 |
notifications: |
| 360 |
- email_admin: true |
| 361 |
email_co_admin: false |
| 362 |
custom_emails: |
| 363 |
- [email protected] |
| 364 |
name: scale |
| 365 |
resource_group: myResourceGroup |
| 366 |
|
| 367 |
- name: Delete an Azure Auto Scale Setting |
| 368 |
azure_rm_autoscale: |
| 369 |
state: absent |
| 370 |
resource_group: myResourceGroup |
| 371 |
name: scale |
| 372 |
''' |
| 373 |
|
| 374 |
RETURN = ''' |
| 375 |
state: |
| 376 |
description: Current state of the resource. |
| 377 |
returned: always |
| 378 |
type: dict |
| 379 |
sample: { |
| 380 |
"changed": false, |
| 381 |
"enabled": true, |
| 382 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/microsoft.insights/autoscalesettings/scale", |
| 383 |
"location": "eastus", |
| 384 |
"name": "scale", |
| 385 |
"notifications": [ |
| 386 |
{ |
| 387 |
"custom_emails": [ |
| 388 |
"[email protected]" |
| 389 |
], |
| 390 |
"send_to_subscription_administrator": true, |
| 391 |
"send_to_subscription_co_administrators": false, |
| 392 |
"webhooks": [] |
| 393 |
} |
| 394 |
], |
| 395 |
"profiles": [ |
| 396 |
{ |
| 397 |
"count": "1", |
| 398 |
"max_count": "1", |
| 399 |
"min_count": "1", |
| 400 |
"name": "Auto created scale condition 0", |
| 401 |
"recurrence_days": [ |
| 402 |
"Monday" |
| 403 |
], |
| 404 |
"recurrence_frequency": "Week", |
| 405 |
"recurrence_hours": [ |
| 406 |
"6" |
| 407 |
], |
| 408 |
"recurrence_mins": [ |
| 409 |
"0" |
| 410 |
], |
| 411 |
"recurrence_timezone": "China Standard Time", |
| 412 |
"rules": [ |
| 413 |
{ |
| 414 |
"cooldown": 5.0, |
| 415 |
"dimensions": [ |
| 416 |
{ |
| 417 |
"dimension_name": "AppName", |
| 418 |
"operator": "Equals", |
| 419 |
"values": [ |
| 420 |
"App1", |
| 421 |
"App2" |
| 422 |
] |
| 423 |
} |
| 424 |
], |
| 425 |
"divide_per_instance": true, |
| 426 |
"metric_namespace": "Fredtest", |
| 427 |
"direction": "Increase", |
| 428 |
"metric_name": "Percentage CPU", |
| 429 |
"metric_resource_uri": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsof |
| 430 |
t.Compute/virtualMachineScaleSets/MyVmss", |
| 431 |
"operator": "GreaterThan", |
| 432 |
"statistic": "Average", |
| 433 |
"threshold": 70.0, |
| 434 |
"time_aggregation": "Average", |
| 435 |
"time_grain": 1.0, |
| 436 |
"time_window": 10.0, |
| 437 |
"type": "ChangeCount", |
| 438 |
"value": "1" |
| 439 |
} |
| 440 |
] |
| 441 |
} |
| 442 |
], |
| 443 |
"target": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScale |
| 444 |
Sets/myVmss" |
| 445 |
} |
| 446 |
''' |
| 447 |
|
| 448 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase, format_resource_id |
| 449 |
from ansible.module_utils._text import to_native |
| 450 |
from datetime import timedelta |
| 451 |
|
| 452 |
try: |
| 453 |
from azure.mgmt.monitor.models import WebhookNotification, EmailNotification, AutoscaleNotification, RecurrentSchedule, MetricTrigger, \ |
| 454 |
ScaleAction, AutoscaleSettingResource, AutoscaleProfile, ScaleCapacity, TimeWindow, Recurrence, ScaleRule, ScaleRuleMetricDimension |
| 455 |
except ImportError: |
| 456 |
|
| 457 |
pass |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
def timedelta_to_minutes(time): |
| 462 |
if not time: |
| 463 |
return 0 |
| 464 |
return time.days * 1440 + time.seconds / 60.0 + time.microseconds / 60000000.0 |
| 465 |
|
| 466 |
|
| 467 |
def get_enum_value(item): |
| 468 |
if hasattr(item, 'value'): |
| 469 |
return to_native(item.value) |
| 470 |
return to_native(item) |
| 471 |
|
| 472 |
|
| 473 |
def auto_scale_to_dict(instance): |
| 474 |
if not instance: |
| 475 |
return dict() |
| 476 |
return dict( |
| 477 |
id=to_native(instance.id or ''), |
| 478 |
name=to_native(instance.name), |
| 479 |
location=to_native(instance.location), |
| 480 |
profiles=[profile_to_dict(p) for p in instance.profiles or []], |
| 481 |
notifications=[notification_to_dict(n) for n in instance.notifications or []], |
| 482 |
enabled=instance.enabled, |
| 483 |
target=to_native(instance.target_resource_uri), |
| 484 |
tags=instance.tags |
| 485 |
) |
| 486 |
|
| 487 |
|
| 488 |
def rule_to_dict(rule): |
| 489 |
if not rule: |
| 490 |
return dict() |
| 491 |
result = dict(metric_name=to_native(rule.metric_trigger.metric_name), |
| 492 |
metric_resource_uri=to_native(rule.metric_trigger.metric_resource_uri), |
| 493 |
time_grain=timedelta_to_minutes(rule.metric_trigger.time_grain), |
| 494 |
statistic=get_enum_value(rule.metric_trigger.statistic), |
| 495 |
time_window=timedelta_to_minutes(rule.metric_trigger.time_window), |
| 496 |
time_aggregation=get_enum_value(rule.metric_trigger.time_aggregation), |
| 497 |
operator=get_enum_value(rule.metric_trigger.operator), |
| 498 |
threshold=float(rule.metric_trigger.threshold), |
| 499 |
metric_namespace=rule.metric_trigger.metric_namespace, |
| 500 |
dimensions=[], |
| 501 |
divide_per_instance=rule.metric_trigger.divide_per_instance) |
| 502 |
if rule.metric_trigger.dimensions: |
| 503 |
for item in rule.metric_trigger.dimensions: |
| 504 |
result['dimensions'].append(dict(dimension_name=item.dimension_name, operator=item.operator, values=item.values)) |
| 505 |
|
| 506 |
if rule.scale_action and to_native(rule.scale_action.direction) != 'None': |
| 507 |
result['direction'] = get_enum_value(rule.scale_action.direction) |
| 508 |
result['type'] = get_enum_value(rule.scale_action.type) |
| 509 |
result['value'] = to_native(rule.scale_action.value) |
| 510 |
result['cooldown'] = timedelta_to_minutes(rule.scale_action.cooldown) |
| 511 |
return result |
| 512 |
|
| 513 |
|
| 514 |
def profile_to_dict(profile): |
| 515 |
if not profile: |
| 516 |
return dict() |
| 517 |
result = dict(name=to_native(profile.name), |
| 518 |
count=to_native(profile.capacity.default), |
| 519 |
max_count=to_native(profile.capacity.maximum), |
| 520 |
min_count=to_native(profile.capacity.minimum)) |
| 521 |
|
| 522 |
if profile.rules: |
| 523 |
result['rules'] = [rule_to_dict(r) for r in profile.rules] |
| 524 |
if profile.fixed_date: |
| 525 |
result['fixed_date_timezone'] = profile.fixed_date.time_zone |
| 526 |
result['fixed_date_start'] = profile.fixed_date.start |
| 527 |
result['fixed_date_end'] = profile.fixed_date.end |
| 528 |
if profile.recurrence: |
| 529 |
if get_enum_value(profile.recurrence.frequency) != 'None': |
| 530 |
result['recurrence_frequency'] = get_enum_value(profile.recurrence.frequency) |
| 531 |
if profile.recurrence.schedule: |
| 532 |
result['recurrence_timezone'] = to_native(str(profile.recurrence.schedule.time_zone)) |
| 533 |
result['recurrence_days'] = [to_native(r) for r in profile.recurrence.schedule.days] |
| 534 |
result['recurrence_hours'] = [to_native(r) for r in profile.recurrence.schedule.hours] |
| 535 |
result['recurrence_mins'] = [to_native(r) for r in profile.recurrence.schedule.minutes] |
| 536 |
return result |
| 537 |
|
| 538 |
|
| 539 |
def notification_to_dict(notification): |
| 540 |
if not notification: |
| 541 |
return dict() |
| 542 |
return dict(send_to_subscription_administrator=notification.email.send_to_subscription_administrator if notification.email else False, |
| 543 |
send_to_subscription_co_administrators=notification.email.send_to_subscription_co_administrators if notification.email else False, |
| 544 |
custom_emails=[to_native(e) for e in notification.email.custom_emails or []], |
| 545 |
webhooks=[to_native(w.service_uri) for w in notification.webhooks or []]) |
| 546 |
|
| 547 |
|
| 548 |
rule_spec = dict( |
| 549 |
metric_name=dict(type='str', required=True), |
| 550 |
metric_resource_uri=dict(type='str'), |
| 551 |
time_grain=dict(type='float', required=True), |
| 552 |
statistic=dict(type='str', choices=['Average', 'Min', 'Max', 'Sum'], default='Average'), |
| 553 |
time_window=dict(type='float', required=True), |
| 554 |
time_aggregation=dict(type='str', choices=['Average', 'Minimum', 'Maximum', 'Total', 'Count'], default='Average'), |
| 555 |
operator=dict(type='str', |
| 556 |
choices=['Equals', 'NotEquals', 'GreaterThan', 'GreaterThanOrEqual', 'LessThan', 'LessThanOrEqual'], |
| 557 |
default='GreaterThan'), |
| 558 |
threshold=dict(type='float', default=70), |
| 559 |
direction=dict(type='str', choices=['Increase', 'Decrease']), |
| 560 |
type=dict(type='str', choices=['PercentChangeCount', 'ExactCount', 'ChangeCount']), |
| 561 |
value=dict(type='str'), |
| 562 |
cooldown=dict(type='float'), |
| 563 |
metric_namespace=dict(type='str'), |
| 564 |
divide_per_instance=dict(type='bool'), |
| 565 |
dimensions=dict( |
| 566 |
type='list', |
| 567 |
elements='dict', |
| 568 |
options=dict( |
| 569 |
dimension_name=dict(type='str', required=True), |
| 570 |
operator=dict(type='str', required=True, choices=['Equals', 'NotEquals']), |
| 571 |
values=dict(type='list', elements='str', required=True), |
| 572 |
) |
| 573 |
) |
| 574 |
) |
| 575 |
|
| 576 |
|
| 577 |
profile_spec = dict( |
| 578 |
name=dict(type='str', required=True), |
| 579 |
count=dict(type='str', required=True), |
| 580 |
max_count=dict(type='str'), |
| 581 |
min_count=dict(type='str'), |
| 582 |
rules=dict(type='list', elements='dict', options=rule_spec), |
| 583 |
fixed_date_timezone=dict(type='str'), |
| 584 |
fixed_date_start=dict(type='str'), |
| 585 |
fixed_date_end=dict(type='str'), |
| 586 |
recurrence_frequency=dict(type='str', choices=['None', 'Second', 'Minute', 'Hour', 'Day', 'Week', 'Month', 'Year'], default='None'), |
| 587 |
recurrence_timezone=dict(type='str'), |
| 588 |
recurrence_days=dict(type='list', elements='str'), |
| 589 |
recurrence_hours=dict(type='list', elements='str'), |
| 590 |
recurrence_mins=dict(type='list', elements='str') |
| 591 |
) |
| 592 |
|
| 593 |
|
| 594 |
notification_spec = dict( |
| 595 |
send_to_subscription_administrator=dict(type='bool', aliases=['email_admin'], default=False), |
| 596 |
send_to_subscription_co_administrators=dict(type='bool', aliases=['email_co_admin'], default=False), |
| 597 |
custom_emails=dict(type='list', elements='str'), |
| 598 |
webhooks=dict(type='list', elements='str') |
| 599 |
) |
| 600 |
|
| 601 |
|
| 602 |
class AzureRMAutoScale(AzureRMModuleBase): |
| 603 |
|
| 604 |
def __init__(self): |
| 605 |
|
| 606 |
self.module_arg_spec = dict( |
| 607 |
resource_group=dict(type='str', required=True), |
| 608 |
name=dict(type='str', required=True), |
| 609 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 610 |
location=dict(type='str'), |
| 611 |
target=dict(type='raw'), |
| 612 |
profiles=dict(type='list', elements='dict', options=profile_spec), |
| 613 |
enabled=dict(type='bool', default=True), |
| 614 |
notifications=dict(type='list', elements='dict', options=notification_spec) |
| 615 |
) |
| 616 |
|
| 617 |
self.results = dict( |
| 618 |
changed=False |
| 619 |
) |
| 620 |
|
| 621 |
required_if = [ |
| 622 |
('state', 'present', ['target', 'profiles']) |
| 623 |
] |
| 624 |
|
| 625 |
self.resource_group = None |
| 626 |
self.name = None |
| 627 |
self.state = None |
| 628 |
self.location = None |
| 629 |
self.tags = None |
| 630 |
self.target = None |
| 631 |
self.profiles = None |
| 632 |
self.notifications = None |
| 633 |
self.enabled = None |
| 634 |
|
| 635 |
super(AzureRMAutoScale, self).__init__(self.module_arg_spec, supports_check_mode=True, required_if=required_if) |
| 636 |
|
| 637 |
def exec_module(self, **kwargs): |
| 638 |
|
| 639 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 640 |
setattr(self, key, kwargs[key]) |
| 641 |
|
| 642 |
results = None |
| 643 |
changed = False |
| 644 |
|
| 645 |
self.log('Fetching auto scale settings {0}'.format(self.name)) |
| 646 |
results = self.get_auto_scale() |
| 647 |
if results and self.state == 'absent': |
| 648 |
|
| 649 |
changed = True |
| 650 |
if not self.check_mode: |
| 651 |
self.delete_auto_scale() |
| 652 |
elif self.state == 'present': |
| 653 |
|
| 654 |
if not self.location: |
| 655 |
|
| 656 |
resource_group = self.get_resource_group(self.resource_group) |
| 657 |
self.location = resource_group.location |
| 658 |
|
| 659 |
resource_id = self.target |
| 660 |
if isinstance(self.target, dict): |
| 661 |
resource_id = format_resource_id(val=self.target['name'], |
| 662 |
subscription_id=self.target.get('subscription_id') or self.subscription_id, |
| 663 |
namespace=self.target['namespace'], |
| 664 |
types=self.target['types'], |
| 665 |
resource_group=self.target.get('resource_group') or self.resource_group) |
| 666 |
self.target = resource_id |
| 667 |
resource_name = self.name |
| 668 |
|
| 669 |
def create_rule_instance(params): |
| 670 |
rule = params.copy() |
| 671 |
rule['metric_resource_uri'] = rule.get('metric_resource_uri', self.target) |
| 672 |
rule['time_grain'] = timedelta(minutes=rule.get('time_grain', 0)) |
| 673 |
rule['time_window'] = timedelta(minutes=rule.get('time_window', 0)) |
| 674 |
rule['cooldown'] = timedelta(minutes=rule.get('cooldown', 0)) |
| 675 |
rule['dimensions'] = [ScaleRuleMetricDimension(**item) for item in rule.get('dimensions')] if rule.get('dimensions') else None |
| 676 |
return ScaleRule(metric_trigger=MetricTrigger(**rule), scale_action=ScaleAction(**rule)) |
| 677 |
|
| 678 |
profiles = [AutoscaleProfile(name=p.get('name'), |
| 679 |
capacity=ScaleCapacity(minimum=p.get('min_count'), |
| 680 |
maximum=p.get('max_count'), |
| 681 |
default=p.get('count')), |
| 682 |
rules=[create_rule_instance(r) for r in p.get('rules') or []], |
| 683 |
fixed_date=TimeWindow(time_zone=p.get('fixed_date_timezone'), |
| 684 |
start=p.get('fixed_date_start'), |
| 685 |
end=p.get('fixed_date_end')) if p.get('fixed_date_timezone') else None, |
| 686 |
recurrence=Recurrence(frequency=p.get('recurrence_frequency'), |
| 687 |
schedule=(RecurrentSchedule(time_zone=p.get('recurrence_timezone'), |
| 688 |
days=p.get('recurrence_days'), |
| 689 |
hours=p.get('recurrence_hours'), |
| 690 |
minutes=p.get('recurrence_mins')))) |
| 691 |
if p.get('recurrence_frequency') and p['recurrence_frequency'] != 'None' else None) |
| 692 |
for p in self.profiles or []] |
| 693 |
|
| 694 |
notifications = [AutoscaleNotification(email=EmailNotification(**n), |
| 695 |
webhooks=[WebhookNotification(service_uri=w) for w in n.get('webhooks') or []]) |
| 696 |
for n in self.notifications or []] |
| 697 |
|
| 698 |
if not results: |
| 699 |
|
| 700 |
changed = True |
| 701 |
else: |
| 702 |
|
| 703 |
resource_name = results.name_properties_name or self.name |
| 704 |
update_tags, tags = self.update_tags(results.tags) |
| 705 |
if update_tags: |
| 706 |
changed = True |
| 707 |
self.tags = tags |
| 708 |
if self.target != results.target_resource_uri: |
| 709 |
changed = True |
| 710 |
if self.enabled != results.enabled: |
| 711 |
changed = True |
| 712 |
profile_result_set = set([str(profile_to_dict(p)) for p in results.profiles or []]) |
| 713 |
if profile_result_set != set([str(profile_to_dict(p)) for p in profiles]): |
| 714 |
changed = True |
| 715 |
notification_result_set = set([str(notification_to_dict(n)) for n in results.notifications or []]) |
| 716 |
if notification_result_set != set([str(notification_to_dict(n)) for n in notifications]): |
| 717 |
changed = True |
| 718 |
if changed: |
| 719 |
|
| 720 |
results = AutoscaleSettingResource(location=self.location, |
| 721 |
tags=self.tags, |
| 722 |
profiles=profiles, |
| 723 |
notifications=notifications, |
| 724 |
enabled=self.enabled, |
| 725 |
name_properties_name=resource_name, |
| 726 |
target_resource_uri=self.target) |
| 727 |
if not self.check_mode: |
| 728 |
results = self.create_or_update_auto_scale(results) |
| 729 |
|
| 730 |
self.results = auto_scale_to_dict(results) |
| 731 |
self.results['changed'] = changed |
| 732 |
return self.results |
| 733 |
|
| 734 |
def get_auto_scale(self): |
| 735 |
try: |
| 736 |
return self.monitor_autoscale_settings_client.autoscale_settings.get(self.resource_group, self.name) |
| 737 |
except Exception as exc: |
| 738 |
self.log('Error: failed to get auto scale settings {0} - {1}'.format(self.name, str(exc))) |
| 739 |
return None |
| 740 |
|
| 741 |
def create_or_update_auto_scale(self, param): |
| 742 |
try: |
| 743 |
return self.monitor_autoscale_settings_client.autoscale_settings.create_or_update(self.resource_group, self.name, param) |
| 744 |
except Exception as exc: |
| 745 |
self.fail("Error creating auto scale settings {0} - {1}".format(self.name, str(exc))) |
| 746 |
|
| 747 |
def delete_auto_scale(self): |
| 748 |
self.log('Deleting auto scale settings {0}'.format(self.name)) |
| 749 |
try: |
| 750 |
return self.monitor_autoscale_settings_client.autoscale_settings.delete(self.resource_group, self.name) |
| 751 |
except Exception as exc: |
| 752 |
self.fail("Error deleting auto scale settings {0} - {1}".format(self.name, str(exc))) |
| 753 |
|
| 754 |
|
| 755 |
def main(): |
| 756 |
AzureRMAutoScale() |
| 757 |
|
| 758 |
|
| 759 |
if __name__ == '__main__': |
| 760 |
main() |