| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
DOCUMENTATION = ''' |
| 9 |
--- |
| 10 |
module: azure_rm_eventgrid_subscription_info |
| 11 |
version_added: "3.13.0" |
| 12 |
short_description: List Eventgrid Event Subscriptions |
| 13 |
description: |
| 14 |
- List Eventgrid Event Subscriptions. |
| 15 |
options: |
| 16 |
source_resource_id: |
| 17 |
description: |
| 18 |
- Fully qualified identifier of the Azure resource whose event subscription |
| 19 |
needs to be listed. |
| 20 |
type: str |
| 21 |
required: false |
| 22 |
location: |
| 23 |
description: |
| 24 |
- List by region. |
| 25 |
type: str |
| 26 |
required: false |
| 27 |
resource_group: |
| 28 |
description: |
| 29 |
- Name of a resource group to limit event subscriptions from. If not specified the subscription |
| 30 |
will be searched. |
| 31 |
required: false |
| 32 |
type: str |
| 33 |
odata_query: |
| 34 |
description: |
| 35 |
- The query used to filter the search results using OData syntax. |
| 36 |
required: false |
| 37 |
type: str |
| 38 |
|
| 39 |
extends_documentation_fragment: |
| 40 |
- azure.azcollection.azure |
| 41 |
|
| 42 |
author: |
| 43 |
- Bill Peck (@p3ck) |
| 44 |
''' |
| 45 |
|
| 46 |
EXAMPLES = ''' |
| 47 |
- name: Get info on an event subscription |
| 48 |
azure.azcollection.azure_rm_eventgrid_subscription_info: |
| 49 |
location: westus3 |
| 50 |
odata_query: "contains(name, 'PATTERN')" |
| 51 |
''' |
| 52 |
|
| 53 |
RETURN = ''' |
| 54 |
changed: |
| 55 |
description: |
| 56 |
- Whether the resource is changed. |
| 57 |
returned: always |
| 58 |
type: bool |
| 59 |
sample: false |
| 60 |
eventgrid_subscriptions: |
| 61 |
description: |
| 62 |
- List of Eventgrid subscriptions. |
| 63 |
returned: always |
| 64 |
type: complex |
| 65 |
contains: |
| 66 |
destination: |
| 67 |
description: |
| 68 |
- Details of the destination |
| 69 |
returned: always |
| 70 |
type: complex |
| 71 |
contains: |
| 72 |
delivery_attribute_mappings: |
| 73 |
description: |
| 74 |
- Mapping attributes for delivery |
| 75 |
returned: always |
| 76 |
type: str |
| 77 |
endpoint_base_url: |
| 78 |
description: |
| 79 |
- The base URL of the endpoint |
| 80 |
returned: always |
| 81 |
type: str |
| 82 |
sample: https://event-grid-site-xxxxx20070.azurewebsites.net/api/updates |
| 83 |
endpoint_type: |
| 84 |
description: |
| 85 |
- The type of the endpoint |
| 86 |
returned: always |
| 87 |
type: str |
| 88 |
sample: WebHook |
| 89 |
endpoint_url: |
| 90 |
description: |
| 91 |
- The URL of the endpoint |
| 92 |
returned: always |
| 93 |
type: str |
| 94 |
sample: https://event-grid-site-xxxxx20070.azurewebsites.net/api/updates |
| 95 |
max_events_per_batch: |
| 96 |
description: |
| 97 |
- How many events will be batched |
| 98 |
returned: always |
| 99 |
type: int |
| 100 |
sample: 1 |
| 101 |
preferred_batch_size_in_kilobytes: |
| 102 |
description: |
| 103 |
- The preferred batch size in kilobytes |
| 104 |
returned: always |
| 105 |
type: int |
| 106 |
sample: 64 |
| 107 |
event_delivery_schema: |
| 108 |
description: |
| 109 |
- The type of Eventgrid Schema |
| 110 |
returned: always |
| 111 |
type: str |
| 112 |
sample: EventGridSchema |
| 113 |
filter: |
| 114 |
description: |
| 115 |
- Filter settings for which events to publish to endpoint |
| 116 |
returned: always |
| 117 |
type: complex |
| 118 |
contains: |
| 119 |
subject_begins_with: |
| 120 |
description: |
| 121 |
- Filter on Subject beginning with this value |
| 122 |
returned: always |
| 123 |
type: str |
| 124 |
subject_ends_with: |
| 125 |
description: |
| 126 |
- Filter on Subject ending with this value |
| 127 |
returned: always |
| 128 |
type: str |
| 129 |
id: |
| 130 |
description: |
| 131 |
- The ID of the Eventgrid Subscription |
| 132 |
returned: always |
| 133 |
type: str |
| 134 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/MyResourceGroup/providers/Microsoft.EventGrid/topics/topic-xxxxx20070/providers/Microsoft.EventGrid/eventSubscriptions/subscription-xxxxx20070 # nopep8: E501 |
| 135 |
name: |
| 136 |
description: |
| 137 |
- The name of the Eventgrid Subscription |
| 138 |
returned: always |
| 139 |
type: str |
| 140 |
sample: subscription-xxxxx20070 |
| 141 |
provisioning_state: |
| 142 |
description: |
| 143 |
- The provisioning state of this Eventgrid Subscription |
| 144 |
returned: always |
| 145 |
type: str |
| 146 |
sample: Succeeded |
| 147 |
retry_policy: |
| 148 |
description: |
| 149 |
- Retry policy for this Eventgrid Subscription |
| 150 |
returned: always |
| 151 |
type: complex |
| 152 |
contains: |
| 153 |
event_time_to_live_in_minutes: |
| 154 |
description: |
| 155 |
- Event time to live in minutes |
| 156 |
returned: always |
| 157 |
type: int |
| 158 |
sample: 1440 |
| 159 |
max_delivery_attempts: |
| 160 |
description: |
| 161 |
- Max attempts to deliver to endpoint |
| 162 |
returned: always |
| 163 |
type: int |
| 164 |
sample: 30 |
| 165 |
topic: |
| 166 |
description: |
| 167 |
- The source resource id that the Eventgrid Subscription is subscribed to |
| 168 |
returned: always |
| 169 |
type: str |
| 170 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/MyResourceGroup/providers/microsoft.eventgrid/topics/topic-xxxxx20070 |
| 171 |
type: |
| 172 |
description: |
| 173 |
- The type of Eventgrid Subscription |
| 174 |
returned: always |
| 175 |
type: str |
| 176 |
sample: Microsoft.EventGrid/eventSubscriptions |
| 177 |
''' |
| 178 |
|
| 179 |
DEFAULT_TOP = 100 |
| 180 |
EVENTGRID_NAMESPACE = "Microsoft.EventGrid" |
| 181 |
EVENTGRID_DOMAINS = "domains" |
| 182 |
EVENTGRID_TOPICS = "topics" |
| 183 |
|
| 184 |
try: |
| 185 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 186 |
from azure.mgmt.core.tools import parse_resource_id |
| 187 |
from azure.mgmt.eventgrid import EventGridManagementClient |
| 188 |
from azure.mgmt.eventgrid.models import ( |
| 189 |
WebHookEventSubscriptionDestination, |
| 190 |
StorageQueueEventSubscriptionDestination, |
| 191 |
) |
| 192 |
from azure.core.exceptions import ResourceNotFoundError |
| 193 |
except ImportError: |
| 194 |
|
| 195 |
pass |
| 196 |
|
| 197 |
|
| 198 |
class AzureRMEventgridSubscriptionInfo(AzureRMModuleBase): |
| 199 |
|
| 200 |
@property |
| 201 |
def client(self): |
| 202 |
self.log('Getting client') |
| 203 |
if not self._client: |
| 204 |
self._client = self.get_mgmt_svc_client(EventGridManagementClient, |
| 205 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 206 |
return self._client |
| 207 |
|
| 208 |
def __init__(self): |
| 209 |
|
| 210 |
self.module_arg_spec = dict( |
| 211 |
source_resource_id=dict( |
| 212 |
type='str', |
| 213 |
required=False |
| 214 |
), |
| 215 |
location=dict( |
| 216 |
type='str', |
| 217 |
required=False |
| 218 |
), |
| 219 |
odata_query=dict( |
| 220 |
type='str', |
| 221 |
required=False |
| 222 |
), |
| 223 |
resource_group=dict( |
| 224 |
type='str', |
| 225 |
required=False |
| 226 |
), |
| 227 |
) |
| 228 |
|
| 229 |
mutually_exclusive = [('source_resource_id', 'location')] |
| 230 |
required_one_of = [['source_resource_id', 'location']] |
| 231 |
|
| 232 |
self._client = None |
| 233 |
|
| 234 |
self.results = dict( |
| 235 |
changed=False, |
| 236 |
eventgrid_subscriptions=[] |
| 237 |
) |
| 238 |
|
| 239 |
super(AzureRMEventgridSubscriptionInfo, self).__init__(self.module_arg_spec, |
| 240 |
supports_tags=False, |
| 241 |
supports_check_mode=True, |
| 242 |
mutually_exclusive=mutually_exclusive, |
| 243 |
required_one_of=required_one_of, |
| 244 |
facts_module=True, |
| 245 |
) |
| 246 |
|
| 247 |
def exec_module(self, **kwargs): |
| 248 |
|
| 249 |
for key in self.module_arg_spec: |
| 250 |
setattr(self, key, kwargs[key]) |
| 251 |
|
| 252 |
if self.source_resource_id is not None: |
| 253 |
self.results['eventgrid_subscriptions'] = self.list_by_resource_id(self.source_resource_id, |
| 254 |
self.odata_query, |
| 255 |
DEFAULT_TOP) |
| 256 |
else: |
| 257 |
self.results['eventgrid_subscriptions'] = self.list_by_location(self.resource_group, |
| 258 |
self.location, |
| 259 |
self.odata_query, |
| 260 |
DEFAULT_TOP) |
| 261 |
|
| 262 |
return self.results |
| 263 |
|
| 264 |
def list_by_resource_id(self, source_resource_id, odata_query, top): |
| 265 |
self.log('List all items by resource id') |
| 266 |
try: |
| 267 |
items = self._list_event_subscriptions_by_resource_id(self.source_resource_id, |
| 268 |
self.odata_query, |
| 269 |
top) |
| 270 |
except ResourceNotFoundError as exc: |
| 271 |
self.fail("Failed to list all items in resource group - {0}".format(str(exc))) |
| 272 |
|
| 273 |
return self.filter_results(items) |
| 274 |
|
| 275 |
def list_by_location(self, resource_group_name, location, odata_query, top): |
| 276 |
self.log('List all items by location') |
| 277 |
try: |
| 278 |
if resource_group_name: |
| 279 |
items = self.client.event_subscriptions.list_regional_by_resource_group(resource_group_name, |
| 280 |
location, |
| 281 |
odata_query, |
| 282 |
top) |
| 283 |
else: |
| 284 |
items = self.client.event_subscriptions.list_regional_by_subscription(location, |
| 285 |
odata_query, |
| 286 |
top) |
| 287 |
except ResourceNotFoundError as exc: |
| 288 |
self.fail("Failed to list all items - {0}".format(str(exc))) |
| 289 |
|
| 290 |
return self.filter_results(items) |
| 291 |
|
| 292 |
def filter_results(self, items): |
| 293 |
results = [] |
| 294 |
for item in items: |
| 295 |
subscription = self.format_eventgrid_subscription(item) |
| 296 |
destination = item.destination |
| 297 |
if isinstance(destination, WebHookEventSubscriptionDestination): |
| 298 |
full_endpoint_url = self.client.event_subscriptions.get_full_url(item.topic, item.name) |
| 299 |
subscription["destination"]["endpoint_url"] = full_endpoint_url.endpoint_url |
| 300 |
|
| 301 |
if not isinstance(destination, StorageQueueEventSubscriptionDestination): |
| 302 |
delivery_attributes = self.client.event_subscriptions.get_delivery_attributes(item.topic, item.name) |
| 303 |
subscription["destination"]["delivery_attribute_mappings"] = delivery_attributes.as_dict() |
| 304 |
results.append(subscription) |
| 305 |
return results |
| 306 |
|
| 307 |
def format_eventgrid_subscription(self, event_subscription_data): |
| 308 |
results = event_subscription_data and event_subscription_data.as_dict() or {} |
| 309 |
return results |
| 310 |
|
| 311 |
def _list_event_subscriptions_by_resource_id(self, resource_id, oDataQuery, top): |
| 312 |
|
| 313 |
|
| 314 |
if resource_id is not None: |
| 315 |
id_parts = list(filter(None, resource_id.split('/'))) |
| 316 |
if len(id_parts) < 5: |
| 317 |
|
| 318 |
if id_parts[0].lower() != "subscriptions": |
| 319 |
self.fail('The specified value for source_resource_id is not in the' |
| 320 |
' expected format. It should start with /subscriptions.') |
| 321 |
|
| 322 |
subscription_id = id_parts[1] |
| 323 |
|
| 324 |
if len(id_parts) == 2: |
| 325 |
return self.client.event_subscriptions.list_global_by_subscription_for_topic_type( |
| 326 |
"Microsoft.Resources.Subscriptions", |
| 327 |
oDataQuery, |
| 328 |
top) |
| 329 |
|
| 330 |
if len(id_parts) == 4 and id_parts[2].lower() == "resourcegroups": |
| 331 |
resource_group_name = id_parts[3] |
| 332 |
if resource_group_name is None: |
| 333 |
self.fail('The specified value for source_resource_id is not' |
| 334 |
' in the expected format. A valid value for' |
| 335 |
' resource group must be provided.') |
| 336 |
return self.client.event_subscriptions.list_global_by_resource_group_for_topic_type( |
| 337 |
resource_group_name, |
| 338 |
"Microsoft.Resources.ResourceGroups", |
| 339 |
oDataQuery, |
| 340 |
top) |
| 341 |
|
| 342 |
id_parts = parse_resource_id(resource_id) |
| 343 |
subscription_id = id_parts.get('subscription') |
| 344 |
|
| 345 |
rg_name = id_parts.get('resource_group') |
| 346 |
resource_name = id_parts.get('name') |
| 347 |
namespace = id_parts.get('namespace') |
| 348 |
resource_type = id_parts.get('type') |
| 349 |
|
| 350 |
if (subscription_id is None or rg_name is None or resource_name is None or |
| 351 |
namespace is None or resource_type is None): |
| 352 |
self.fail('The specified value for source_resource_id is not in the expected format.') |
| 353 |
|
| 354 |
|
| 355 |
if (namespace.lower() == EVENTGRID_NAMESPACE.lower() and resource_type.lower() == EVENTGRID_DOMAINS.lower()): |
| 356 |
child_resource_type = id_parts.get('child_type_1') |
| 357 |
child_resource_name = id_parts.get('child_name_1') |
| 358 |
|
| 359 |
if (child_resource_type is not None and child_resource_type.lower() == EVENTGRID_TOPICS.lower() and |
| 360 |
child_resource_name is not None): |
| 361 |
return self.client.event_subscriptions.list_by_domain_topic(rg_name, resource_name, child_resource_name, oDataQuery, top) |
| 362 |
|
| 363 |
|
| 364 |
return self.client.event_subscriptions.list_by_resource( |
| 365 |
rg_name, |
| 366 |
namespace, |
| 367 |
resource_type, |
| 368 |
resource_name, |
| 369 |
oDataQuery, |
| 370 |
top) |
| 371 |
|
| 372 |
|
| 373 |
def main(): |
| 374 |
AzureRMEventgridSubscriptionInfo() |
| 375 |
|
| 376 |
|
| 377 |
if __name__ == '__main__': |
| 378 |
main() |