| 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_monitorlogprofile |
| 14 |
version_added: "0.0.1" |
| 15 |
short_description: Manage Azure Monitor log profile |
| 16 |
description: |
| 17 |
- Create, update and delete instance of Azure Monitor log profile. |
| 18 |
|
| 19 |
options: |
| 20 |
name: |
| 21 |
description: |
| 22 |
- Unique name of the log profile to create or update. |
| 23 |
required: True |
| 24 |
type: str |
| 25 |
location: |
| 26 |
description: |
| 27 |
- Resource location. |
| 28 |
type: str |
| 29 |
locations: |
| 30 |
description: |
| 31 |
- List of regions for which Activity Log events should be stored. |
| 32 |
type: list |
| 33 |
elements: str |
| 34 |
categories: |
| 35 |
description: |
| 36 |
- List of categories of logs. These categories are created as is convenient to user. Some Values are C(Write), C(Delete) and/or C(Action). |
| 37 |
type: list |
| 38 |
elements: str |
| 39 |
retention_policy: |
| 40 |
description: |
| 41 |
- Retention policy for events in the log. |
| 42 |
type: dict |
| 43 |
suboptions: |
| 44 |
enabled: |
| 45 |
description: |
| 46 |
- Whether the retention policy is enabled. |
| 47 |
type: bool |
| 48 |
days: |
| 49 |
description: |
| 50 |
- The number of days for the retention. A value of 0 will retain the events indefinitely. |
| 51 |
type: int |
| 52 |
service_bus_rule_id: |
| 53 |
description: |
| 54 |
- The service bus rule ID of the service bus namespace in which you would like to have Event Hubs created for streaming in the Activity Log. |
| 55 |
- Format like {service_bus_resource_id}/authorizationrules{key_name}. |
| 56 |
type: str |
| 57 |
storage_account: |
| 58 |
description: |
| 59 |
- The storage account to which send the Activity Log. |
| 60 |
- It could be a resource ID. |
| 61 |
- It could be a dict containing I(resource_grorup) and I(name). |
| 62 |
type: raw |
| 63 |
state: |
| 64 |
description: |
| 65 |
- Assert the state of the log profile. |
| 66 |
- Use C(present) to create or update a log profile and C(absent) to delete it. |
| 67 |
default: present |
| 68 |
type: str |
| 69 |
choices: |
| 70 |
- absent |
| 71 |
- present |
| 72 |
|
| 73 |
extends_documentation_fragment: |
| 74 |
- azure.azcollection.azure |
| 75 |
- azure.azcollection.azure_tags |
| 76 |
|
| 77 |
author: |
| 78 |
- Yunge Zhu(@yungezz) |
| 79 |
|
| 80 |
''' |
| 81 |
|
| 82 |
EXAMPLES = ''' |
| 83 |
- name: Create a log profile |
| 84 |
azure_rm_monitorlogprofile: |
| 85 |
name: myProfile |
| 86 |
location: eastus |
| 87 |
locations: |
| 88 |
- eastus |
| 89 |
- westus |
| 90 |
categories: |
| 91 |
- Write |
| 92 |
- Action |
| 93 |
retention_policy: |
| 94 |
enabled: false |
| 95 |
days: 1 |
| 96 |
storage_account: |
| 97 |
resource_group: myResourceGroup |
| 98 |
name: myStorageAccount |
| 99 |
register: output |
| 100 |
|
| 101 |
- name: Delete a log profile |
| 102 |
azure_rm_monitorlogprofile: |
| 103 |
name: myProfile |
| 104 |
state: absent |
| 105 |
''' |
| 106 |
|
| 107 |
RETURN = ''' |
| 108 |
id: |
| 109 |
description: |
| 110 |
- ID of the log profile. |
| 111 |
returned: always |
| 112 |
type: str |
| 113 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/providers/microsoft.insights/logprofiles/myProfile |
| 114 |
|
| 115 |
''' |
| 116 |
|
| 117 |
|
| 118 |
try: |
| 119 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase, format_resource_id |
| 120 |
from azure.core.exceptions import HttpResponseError |
| 121 |
from azure.core.polling import LROPoller |
| 122 |
from azure.mgmt.core.tools import is_valid_resource_id |
| 123 |
from azure.mgmt.monitor.models import (RetentionPolicy, LogProfileResource) |
| 124 |
except ImportError: |
| 125 |
|
| 126 |
pass |
| 127 |
|
| 128 |
|
| 129 |
retention_policy_spec = dict( |
| 130 |
enabled=dict(type='bool'), |
| 131 |
days=dict(type='int') |
| 132 |
) |
| 133 |
|
| 134 |
|
| 135 |
def logprofile_to_dict(profile): |
| 136 |
return dict( |
| 137 |
id=profile.id, |
| 138 |
name=profile.name, |
| 139 |
location=profile.location, |
| 140 |
locations=profile.locations, |
| 141 |
categories=profile.categories, |
| 142 |
storage_account=profile.storage_account_id, |
| 143 |
service_bus_rule_id=profile.service_bus_rule_id, |
| 144 |
retention_policy=dict( |
| 145 |
enabled=profile.retention_policy.enabled, |
| 146 |
days=profile.retention_policy.days |
| 147 |
), |
| 148 |
tags=profile.tags if profile.tags else None |
| 149 |
) |
| 150 |
|
| 151 |
|
| 152 |
class Actions: |
| 153 |
NoAction, CreateOrUpdate, Delete = range(3) |
| 154 |
|
| 155 |
|
| 156 |
class AzureRMMonitorLogprofile(AzureRMModuleBase): |
| 157 |
"""Configuration class for an Azure RM Monitor log profile""" |
| 158 |
|
| 159 |
def __init__(self): |
| 160 |
self.module_arg_spec = dict( |
| 161 |
name=dict( |
| 162 |
type='str', |
| 163 |
required=True |
| 164 |
), |
| 165 |
location=dict( |
| 166 |
type='str' |
| 167 |
), |
| 168 |
locations=dict( |
| 169 |
type='list', |
| 170 |
elements='str' |
| 171 |
), |
| 172 |
categories=dict( |
| 173 |
type='list', |
| 174 |
elements='str' |
| 175 |
), |
| 176 |
retention_policy=dict( |
| 177 |
type='dict', |
| 178 |
options=retention_policy_spec |
| 179 |
), |
| 180 |
service_bus_rule_id=dict( |
| 181 |
type='str' |
| 182 |
), |
| 183 |
storage_account=dict( |
| 184 |
type='raw' |
| 185 |
), |
| 186 |
state=dict( |
| 187 |
type='str', |
| 188 |
default='present', |
| 189 |
choices=['present', 'absent'] |
| 190 |
) |
| 191 |
) |
| 192 |
|
| 193 |
self._client = None |
| 194 |
|
| 195 |
self.name = None |
| 196 |
self.location = None |
| 197 |
|
| 198 |
self.locations = None |
| 199 |
self.categories = None |
| 200 |
self.retention_policy = False |
| 201 |
self.service_bus_rule_id = None |
| 202 |
self.storage_account = None |
| 203 |
|
| 204 |
self.tags = None |
| 205 |
|
| 206 |
self.results = dict( |
| 207 |
changed=False, |
| 208 |
id=None |
| 209 |
) |
| 210 |
self.state = None |
| 211 |
|
| 212 |
super(AzureRMMonitorLogprofile, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 213 |
supports_check_mode=True, |
| 214 |
supports_tags=True) |
| 215 |
|
| 216 |
def exec_module(self, **kwargs): |
| 217 |
"""Main module execution method""" |
| 218 |
|
| 219 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 220 |
setattr(self, key, kwargs[key]) |
| 221 |
|
| 222 |
old_response = None |
| 223 |
response = None |
| 224 |
to_be_updated = False |
| 225 |
|
| 226 |
|
| 227 |
if self.storage_account: |
| 228 |
if isinstance(self.storage_account, dict): |
| 229 |
self.storage_account = format_resource_id(val=self.storage_account['name'], |
| 230 |
subscription_id=self.storage_account.get('subscription') or self.subscription_id, |
| 231 |
namespace='Microsoft.Storage', |
| 232 |
types='storageAccounts', |
| 233 |
resource_group=self.storage_account.get('resource_group')) |
| 234 |
elif not is_valid_resource_id(self.storage_account): |
| 235 |
self.fail("storage_account either be a resource id or a dict containing resource_group and name") |
| 236 |
|
| 237 |
|
| 238 |
old_response = self.get_logprofile() |
| 239 |
|
| 240 |
if old_response: |
| 241 |
self.results['id'] = old_response['id'] |
| 242 |
|
| 243 |
if self.state == 'present': |
| 244 |
|
| 245 |
if not old_response: |
| 246 |
self.log("Log profile instance doesn't exist") |
| 247 |
|
| 248 |
to_be_updated = True |
| 249 |
self.to_do = Actions.CreateOrUpdate |
| 250 |
|
| 251 |
else: |
| 252 |
|
| 253 |
self.log("Log profile instance already exists") |
| 254 |
|
| 255 |
update_tags, self.tags = self.update_tags(old_response.get('tags', None)) |
| 256 |
|
| 257 |
if update_tags: |
| 258 |
to_be_updated = True |
| 259 |
self.to_do = Actions.CreateOrUpdate |
| 260 |
|
| 261 |
|
| 262 |
if self.check_update(old_response): |
| 263 |
to_be_updated = True |
| 264 |
self.to_do = Actions.CreateOrUpdate |
| 265 |
|
| 266 |
elif self.state == 'absent': |
| 267 |
if old_response: |
| 268 |
self.log("Delete log profile instance") |
| 269 |
self.results['id'] = old_response['id'] |
| 270 |
to_be_updated = True |
| 271 |
self.to_do = Actions.Delete |
| 272 |
else: |
| 273 |
self.results['changed'] = False |
| 274 |
self.log("Log profile {0} not exists.".format(self.name)) |
| 275 |
|
| 276 |
if to_be_updated: |
| 277 |
self.log('Need to Create/Update log profile') |
| 278 |
self.results['changed'] = True |
| 279 |
|
| 280 |
if self.check_mode: |
| 281 |
return self.results |
| 282 |
|
| 283 |
if self.to_do == Actions.CreateOrUpdate: |
| 284 |
response = self.create_or_update_logprofile() |
| 285 |
self.results['id'] = response['id'] |
| 286 |
|
| 287 |
if self.to_do == Actions.Delete: |
| 288 |
self.delete_logprofile() |
| 289 |
self.log('Log profile instance deleted') |
| 290 |
|
| 291 |
return self.results |
| 292 |
|
| 293 |
def check_update(self, existing): |
| 294 |
if self.locations is not None and existing['locations'] != self.locations: |
| 295 |
self.log("locations diff: origin {0} / update {1}".format(existing['locations'], self.locations)) |
| 296 |
return True |
| 297 |
if self.retention_policy is not None: |
| 298 |
if existing['retention_policy']['enabled'] != self.retention_policy['enabled']: |
| 299 |
self.log("retention_policy diff: origin {0} / update {1}".format(str(existing['sku']['name']), str(self.retention_policy['enabled']))) |
| 300 |
return True |
| 301 |
if existing['retention_policy']['days'] != self.retention_policy['days']: |
| 302 |
self.log("retention_policy diff: origin {0} / update {1}".format(existing['retention_policy']['days'], str(self.retention_policy['days']))) |
| 303 |
return True |
| 304 |
if self.storage_account is not None and existing['storage_account'] != self.storage_account: |
| 305 |
self.log("storage_account diff: origin {0} / update {1}".format(existing['storage_account'], self.storage_account)) |
| 306 |
return True |
| 307 |
if self.service_bus_rule_id is not None and existing['service_bus_rule_id'] != self.service_bus_rule_id: |
| 308 |
self.log("service_bus_rule_id diff: origin {0} / update {1}".format(existing['service_bus_rule_id'], self.service_bus_rule_id)) |
| 309 |
return True |
| 310 |
return False |
| 311 |
|
| 312 |
def create_or_update_logprofile(self): |
| 313 |
''' |
| 314 |
Creates or Update log profile. |
| 315 |
|
| 316 |
:return: deserialized log profile state dictionary |
| 317 |
''' |
| 318 |
self.log( |
| 319 |
"Creating log profile instance {0}".format(self.name)) |
| 320 |
|
| 321 |
try: |
| 322 |
params = LogProfileResource( |
| 323 |
location=self.location, |
| 324 |
locations=self.locations, |
| 325 |
categories=self.categories, |
| 326 |
retention_policy=RetentionPolicy(days=self.retention_policy['days'], |
| 327 |
enabled=self.retention_policy['enabled']) if self.retention_policy else None, |
| 328 |
storage_account_id=self.storage_account if self.storage_account else None, |
| 329 |
service_bus_rule_id=self.service_bus_rule_id if self.service_bus_rule_id else None, |
| 330 |
tags=self.tags |
| 331 |
) |
| 332 |
|
| 333 |
response = self.monitor_log_profiles_client.log_profiles.create_or_update(log_profile_name=self.name, |
| 334 |
parameters=params) |
| 335 |
if isinstance(response, LROPoller): |
| 336 |
response = self.get_poller_result(response) |
| 337 |
|
| 338 |
except HttpResponseError as exc: |
| 339 |
self.log('Error attempting to create/update log profile.') |
| 340 |
self.fail("Error creating/updating log profile: {0}".format(str(exc))) |
| 341 |
return logprofile_to_dict(response) |
| 342 |
|
| 343 |
def delete_logprofile(self): |
| 344 |
''' |
| 345 |
Deletes specified log profile. |
| 346 |
|
| 347 |
:return: True |
| 348 |
''' |
| 349 |
self.log("Deleting the log profile instance {0}".format(self.name)) |
| 350 |
try: |
| 351 |
response = self.monitor_log_profiles_client.log_profiles.delete(log_profile_name=self.name) |
| 352 |
except HttpResponseError as e: |
| 353 |
self.log('Error attempting to delete the log profile.') |
| 354 |
self.fail( |
| 355 |
"Error deleting the log profile: {0}".format(str(e))) |
| 356 |
return True |
| 357 |
|
| 358 |
def get_logprofile(self): |
| 359 |
''' |
| 360 |
Gets the properties of the specified log profile. |
| 361 |
|
| 362 |
:return: log profile state dictionary |
| 363 |
''' |
| 364 |
self.log("Checking if the log profile {0} is present".format(self.name)) |
| 365 |
|
| 366 |
response = None |
| 367 |
|
| 368 |
try: |
| 369 |
response = self.monitor_log_profiles_client.log_profiles.get(log_profile_name=self.name) |
| 370 |
|
| 371 |
self.log("Response : {0}".format(response)) |
| 372 |
self.log("log profile : {0} found".format(response.name)) |
| 373 |
return logprofile_to_dict(response) |
| 374 |
|
| 375 |
except HttpResponseError: |
| 376 |
self.log("Didn't find log profile {0}".format(self.name)) |
| 377 |
|
| 378 |
return False |
| 379 |
|
| 380 |
|
| 381 |
def main(): |
| 382 |
"""Main execution""" |
| 383 |
AzureRMMonitorLogprofile() |
| 384 |
|
| 385 |
|
| 386 |
if __name__ == '__main__': |
| 387 |
main() |