diff --git a/lib/ansible/modules/cloud/azure/azure_rm_autoscale.py b/lib/ansible/modules/cloud/azure/azure_rm_autoscale.py index dacc56d525..8c3b38c2fe 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_autoscale.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_autoscale.py @@ -326,6 +326,7 @@ state: ''' # NOQA from ansible.module_utils.azure_rm_common import AzureRMModuleBase, format_resource_id +from ansible.module_utils._text import to_native from datetime import timedelta try: @@ -333,12 +334,12 @@ try: from msrestazure.azure_exceptions import CloudError from azure.mgmt.monitor.models import WebhookNotification, EmailNotification, AutoscaleNotification, RecurrentSchedule, MetricTrigger, \ ScaleAction, AutoscaleSettingResource, AutoscaleProfile, ScaleCapacity, TimeWindow, Recurrence, ScaleRule - from ansible.module_utils._text import to_native except ImportError: # This is handled in azure_rm_common pass +# duplicated in azure_rm_autoscale_facts def timedelta_to_minutes(time): if not time: return 0 diff --git a/lib/ansible/modules/cloud/azure/azure_rm_autoscale_facts.py b/lib/ansible/modules/cloud/azure/azure_rm_autoscale_facts.py index e3aaeb184b..26c6ce7b6d 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_autoscale_facts.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_autoscale_facts.py @@ -113,16 +113,97 @@ autoscales: ''' from ansible.module_utils.azure_rm_common import AzureRMModuleBase +from ansible.module_utils._text import to_native try: from msrestazure.azure_exceptions import CloudError from msrest.serialization import Model - from ansible.modules.cloud.azure.azure_rm_autoscale import auto_scale_to_dict except ImportError: # This is handled in azure_rm_common pass +# duplicated in azure_rm_autoscale +def timedelta_to_minutes(time): + if not time: + return 0 + return time.days * 1440 + time.seconds / 60.0 + time.microseconds / 60000000.0 + + +def get_enum_value(item): + if 'value' in dir(item): + return to_native(item.value) + return to_native(item) + + +def auto_scale_to_dict(instance): + if not instance: + return dict() + return dict( + id=to_native(instance.id or ''), + name=to_native(instance.name), + location=to_native(instance.location), + profiles=[profile_to_dict(p) for p in instance.profiles or []], + notifications=[notification_to_dict(n) for n in instance.notifications or []], + enabled=instance.enabled, + target=to_native(instance.target_resource_uri), + tags=instance.tags + ) + + +def rule_to_dict(rule): + if not rule: + return dict() + result = dict(metric_name=to_native(rule.metric_trigger.metric_name), + metric_resource_uri=to_native(rule.metric_trigger.metric_resource_uri), + time_grain=timedelta_to_minutes(rule.metric_trigger.time_grain), + statistic=get_enum_value(rule.metric_trigger.statistic), + time_window=timedelta_to_minutes(rule.metric_trigger.time_window), + time_aggregation=get_enum_value(rule.metric_trigger.time_aggregation), + operator=get_enum_value(rule.metric_trigger.operator), + threshold=float(rule.metric_trigger.threshold)) + if rule.scale_action and to_native(rule.scale_action.direction) != 'None': + result['direction'] = get_enum_value(rule.scale_action.direction) + result['type'] = get_enum_value(rule.scale_action.type) + result['value'] = to_native(rule.scale_action.value) + result['cooldown'] = timedelta_to_minutes(rule.scale_action.cooldown) + return result + + +def profile_to_dict(profile): + if not profile: + return dict() + result = dict(name=to_native(profile.name), + count=to_native(profile.capacity.default), + max_count=to_native(profile.capacity.maximum), + min_count=to_native(profile.capacity.minimum)) + + if profile.rules: + result['rules'] = [rule_to_dict(r) for r in profile.rules] + if profile.fixed_date: + result['fixed_date_timezone'] = profile.fixed_date.time_zone + result['fixed_date_start'] = profile.fixed_date.start + result['fixed_date_end'] = profile.fixed_date.end + if profile.recurrence: + if get_enum_value(profile.recurrence.frequency) != 'None': + result['recurrence_frequency'] = get_enum_value(profile.recurrence.frequency) + if profile.recurrence.schedule: + result['recurrence_timezone'] = to_native(str(profile.recurrence.schedule.time_zone)) + result['recurrence_days'] = [to_native(r) for r in profile.recurrence.schedule.days] + result['recurrence_hours'] = [to_native(r) for r in profile.recurrence.schedule.hours] + result['recurrence_mins'] = [to_native(r) for r in profile.recurrence.schedule.minutes] + return result + + +def notification_to_dict(notification): + if not notification: + return dict() + return dict(send_to_subscription_administrator=notification.email.send_to_subscription_administrator if notification.email else False, + send_to_subscription_co_administrators=notification.email.send_to_subscription_co_administrators if notification.email else False, + custom_emails=[to_native(e) for e in notification.email.custom_emails or []], + webhooks=[to_native(w.service_url) for w in notification.webhooks or []]) + + class AzureRMAutoScaleFacts(AzureRMModuleBase): def __init__(self): # define user inputs into argument diff --git a/lib/ansible/modules/cloud/azure/azure_rm_managed_disk.py b/lib/ansible/modules/cloud/azure/azure_rm_managed_disk.py index 36f9cf36d2..06e9e8d1cc 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_managed_disk.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_managed_disk.py @@ -145,6 +145,7 @@ except ImportError: pass +# duplicated in azure_rm_managed_disk_facts def managed_disk_to_dict(managed_disk): create_data = managed_disk.creation_data return dict( diff --git a/lib/ansible/modules/cloud/azure/azure_rm_managed_disk_facts.py b/lib/ansible/modules/cloud/azure/azure_rm_managed_disk_facts.py index c9b6ea3b3f..1be258c987 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_managed_disk_facts.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_managed_disk_facts.py @@ -79,12 +79,29 @@ from ansible.module_utils.azure_rm_common import AzureRMModuleBase try: from msrestazure.azure_exceptions import CloudError - from ansible.modules.cloud.azure.azure_rm_managed_disk import managed_disk_to_dict except: # handled in azure_rm_common pass +# duplicated in azure_rm_managed_disk +def managed_disk_to_dict(managed_disk): + create_data = managed_disk.creation_data + return dict( + id=managed_disk.id, + name=managed_disk.name, + location=managed_disk.location, + tags=managed_disk.tags, + create_option=create_data.create_option.value.lower(), + source_uri=create_data.source_uri, + source_resource_uri=create_data.source_resource_id, + disk_size_gb=managed_disk.disk_size_gb, + os_type=managed_disk.os_type.value if managed_disk.os_type else None, + storage_account_type=managed_disk.sku.name.value if managed_disk.sku else None, + managed_by=managed_disk.managed_by + ) + + class AzureRMManagedDiskFacts(AzureRMModuleBase): """Utility class to get managed disk facts"""