| 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_resourcehealthstates_info |
| 14 |
|
| 15 |
version_added: "3.3.0" |
| 16 |
|
| 17 |
short_description: Get or list the current availability status for the resource |
| 18 |
|
| 19 |
description: |
| 20 |
- Get or list the current availability status for the resource. |
| 21 |
|
| 22 |
options: |
| 23 |
resource_group: |
| 24 |
description: |
| 25 |
- Name of resource group. |
| 26 |
type: str |
| 27 |
resource_uri: |
| 28 |
description: |
| 29 |
- The fully qualified ID of the resource, including the resource name and resource type. |
| 30 |
- Currently the API support not nested and one nesting level resource types, |
| 31 |
such as '/subscriptions/{subscriptionId}/resourceGroups/{resource-group-name}/providers/{resource-provider-name}/ |
| 32 |
{resource-type}/{resource-name}' and '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/ |
| 33 |
providers/{resource-provider-name}/{parentResourceType}/{parentResourceName}/{resourceType}/{resourceName}'. |
| 34 |
type: str |
| 35 |
list_all: |
| 36 |
description: |
| 37 |
- Whether lists all historical availability transitions and impacting events for a single resource. |
| 38 |
type: bool |
| 39 |
default: False |
| 40 |
|
| 41 |
extends_documentation_fragment: |
| 42 |
- azure.azcollection.azure |
| 43 |
|
| 44 |
author: |
| 45 |
- xuzhang3 (@xuzhang3) |
| 46 |
- Fred-sun (@Fred-sun) |
| 47 |
''' |
| 48 |
|
| 49 |
EXAMPLES = ''' |
| 50 |
- name: Lists the current availability status for all the resources in the subscription. |
| 51 |
azure_rm_resourcehealthstates_info: |
| 52 |
|
| 53 |
- name: Lists the current availability status for all the resources in the resource group. |
| 54 |
azure_rm_resourcehealthstates_info: |
| 55 |
resource_group: "{{ resource_group }}" |
| 56 |
|
| 57 |
- name: Gets current availability status for a single resource. |
| 58 |
azure_rm_resourcehealthstates_info: |
| 59 |
resource_uri: "/subscriptions/xxxx-xxx/resourcegroups/v-xisurg/providers/microsoft.compute/virtualmachines/testvm" |
| 60 |
|
| 61 |
- name: Lists all historical availability transitions and impacting events for a single resource. |
| 62 |
azure_rm_resourcehealthstates_info: |
| 63 |
resource_uri: "/subscriptions/xxxx-xxx/resourcegroups/v-xisurg/providers/microsoft.compute/virtualmachines/testvm" |
| 64 |
list_all: true |
| 65 |
''' |
| 66 |
|
| 67 |
RETURN = ''' |
| 68 |
health_states: |
| 69 |
description: |
| 70 |
- The facts of availabilityStatus of a resource. |
| 71 |
type: complex |
| 72 |
returned: always |
| 73 |
contains: |
| 74 |
id: |
| 75 |
description: |
| 76 |
- Azure Resource Manager Identity for the availabilityStatuses resource. |
| 77 |
type: str |
| 78 |
returned: always |
| 79 |
sample: "/subscriptions/xxxx-xxx/resourcegroups/defaultresourcegroup-wus2/providers/microsoft.operationalinsights/workspaces/ |
| 80 |
defaultworkspace-e393adb3-b5be-4789-bdc9-848367f0d152-wus2/providers/Microsoft.ResourceHealth/availabilityStatuses/current" |
| 81 |
location: |
| 82 |
description: |
| 83 |
- Azure Resource Manager geo location of the resource. |
| 84 |
type: str |
| 85 |
returned: always |
| 86 |
sample: westus2 |
| 87 |
type: |
| 88 |
description: |
| 89 |
- Microsoft.ResourceHealth/AvailabilityStatuses. |
| 90 |
type: str |
| 91 |
returned: always |
| 92 |
sample: "Microsoft.ResourceHealth/AvailabilityStatuses" |
| 93 |
properties: |
| 94 |
description: |
| 95 |
- Properties of availability state. |
| 96 |
type: complex |
| 97 |
contains: |
| 98 |
availability_state: |
| 99 |
description: |
| 100 |
- Availability status of the resource. |
| 101 |
type: str |
| 102 |
returned: always |
| 103 |
sample: "Unknown" |
| 104 |
category: |
| 105 |
description: |
| 106 |
- When a context field is set to Platform, this field will reflect if the event was planned or unplanned. |
| 107 |
- If the context field does not have a value of Platform, then this field will be ignored. |
| 108 |
type: str |
| 109 |
returned: always |
| 110 |
sample: "Not Applicable" |
| 111 |
context: |
| 112 |
description: |
| 113 |
- This field is connected to the category field in this object. |
| 114 |
type: str |
| 115 |
returned: always |
| 116 |
sample: "Not Applicable" |
| 117 |
occured_time: |
| 118 |
description: |
| 119 |
- Timestamp for when last change in health status occurred. |
| 120 |
type: str |
| 121 |
returned: always |
| 122 |
sample: "2025-03-05T01:50:29.67632Z" |
| 123 |
reason_chronicity: |
| 124 |
description: |
| 125 |
- Chronicity of the availability transition. |
| 126 |
type: str |
| 127 |
returned: always |
| 128 |
sample: "Transient" |
| 129 |
reason_type: |
| 130 |
description: |
| 131 |
- When the resource's availabilityState is Unavailable, it describes where the health impacting event was originated. |
| 132 |
type: str |
| 133 |
returned: always |
| 134 |
sample: "" |
| 135 |
reported_time: |
| 136 |
description: |
| 137 |
- Timestamp for when the health was last checked. |
| 138 |
type: str |
| 139 |
returned: always |
| 140 |
sample: "2025-03-05T01:50:29.67632Z" |
| 141 |
summary: |
| 142 |
description: |
| 143 |
- Summary description of the availability status. |
| 144 |
type: str |
| 145 |
returned: always |
| 146 |
sample: "We're currently unable to determine the health of this workspace. |
| 147 |
Either no queries were run on this workspace, or no data was ingested to this workspace recently." |
| 148 |
title: |
| 149 |
description: |
| 150 |
- Title description of the availability status. |
| 151 |
type: str |
| 152 |
returned: always |
| 153 |
sample: Unkown |
| 154 |
''' |
| 155 |
|
| 156 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 157 |
|
| 158 |
|
| 159 |
class AzureRMResourceHealthStatesInfo(AzureRMModuleBase): |
| 160 |
|
| 161 |
def __init__(self): |
| 162 |
|
| 163 |
self.module_args = dict( |
| 164 |
resource_group=dict(type='str'), |
| 165 |
resource_uri=dict(type='str'), |
| 166 |
list_all=dict(type='bool', default=False) |
| 167 |
) |
| 168 |
|
| 169 |
self.results = dict( |
| 170 |
changed=False, |
| 171 |
health_states=[] |
| 172 |
) |
| 173 |
|
| 174 |
self.resource_group = None |
| 175 |
self.resource_uri = None |
| 176 |
self.list_all = None |
| 177 |
required_if = [('list_all', True, ['resource_uri'])] |
| 178 |
|
| 179 |
super(AzureRMResourceHealthStatesInfo, self).__init__( |
| 180 |
derived_arg_spec=self.module_args, |
| 181 |
supports_check_mode=True, |
| 182 |
supports_tags=False, |
| 183 |
required_if=required_if, |
| 184 |
facts_module=True |
| 185 |
) |
| 186 |
|
| 187 |
def exec_module(self, **kwargs): |
| 188 |
for key in self.module_args: |
| 189 |
setattr(self, key, kwargs[key]) |
| 190 |
|
| 191 |
if self.resource_uri is not None: |
| 192 |
if self.list_all: |
| 193 |
response = self.list_by_resource_uri(self.resource_uri) |
| 194 |
else: |
| 195 |
response = self.get_by_resource(self.resource_uri) |
| 196 |
elif self.resource_group is not None: |
| 197 |
response = self.list_by_resource_group(self.resource_group) |
| 198 |
else: |
| 199 |
response = self.list_by_subscription_id() |
| 200 |
|
| 201 |
self.results['health_states'] = [item.as_dict() for item in response] |
| 202 |
return self.results |
| 203 |
|
| 204 |
def get_by_resource(self, resource_uri): |
| 205 |
''' |
| 206 |
Gets current availability status for a single resource. |
| 207 |
:return: AvailabilityStatus or the result of cls(response) |
| 208 |
''' |
| 209 |
try: |
| 210 |
response = [self.resourcehealth_client.availability_statuses.get_by_resource(resource_uri)] |
| 211 |
except Exception as exc: |
| 212 |
self.fail('Error when gets current availability status for a single resource. Error msg: {0}'.format(exc.message or str(exc))) |
| 213 |
|
| 214 |
return response |
| 215 |
|
| 216 |
def list_by_resource_uri(self, resource_uri): |
| 217 |
''' |
| 218 |
Lists all historical availability transitions and impacting events for a single resource |
| 219 |
:return: An iterator like instance of either AvailabilityStatus or the result of cls(response) |
| 220 |
''' |
| 221 |
try: |
| 222 |
response = self.resourcehealth_client.availability_statuses.list(resource_uri) |
| 223 |
except Exception as exc: |
| 224 |
self.fail('Error when lists all historical availability transitions and impacting events for a single resource. Error msg: {0}'.format(exc)) |
| 225 |
|
| 226 |
return response |
| 227 |
|
| 228 |
def list_by_resource_group(self, resource_group): |
| 229 |
''' |
| 230 |
Lists the current availability status for all the resources in the resource group. |
| 231 |
:return: An iterator like instance of either AvailabilityStatus or the result of cls(response) |
| 232 |
''' |
| 233 |
try: |
| 234 |
response = self.resourcehealth_client.availability_statuses.list_by_resource_group(resource_group) |
| 235 |
except Exception as exc: |
| 236 |
self.fail('Error when lists the current availability status for all the resources in the resource group. Error msg: {0}'.format(exc)) |
| 237 |
|
| 238 |
return response |
| 239 |
|
| 240 |
def list_by_subscription_id(self): |
| 241 |
''' |
| 242 |
Lists the current availability status for all the resources in the subscription. |
| 243 |
:return: An iterator like instance of either AvailabilityStatus or the result of cls(response) |
| 244 |
''' |
| 245 |
try: |
| 246 |
response = self.resourcehealth_client.availability_statuses.list_by_subscription_id() |
| 247 |
except Exception as exc: |
| 248 |
self.fail('Error when lists the current availability status for all the resources in the subscription Error msg: {0}'.format(exc)) |
| 249 |
|
| 250 |
return response |
| 251 |
|
| 252 |
|
| 253 |
def main(): |
| 254 |
"""Main module execution code path""" |
| 255 |
|
| 256 |
AzureRMResourceHealthStatesInfo() |
| 257 |
|
| 258 |
|
| 259 |
if __name__ == '__main__': |
| 260 |
main() |