| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
|
| 9 |
DOCUMENTATION = ''' |
| 10 |
--- |
| 11 |
module: azure_rm_eventgrid_topic_info |
| 12 |
version_added: "3.13.0" |
| 13 |
short_description: List Eventgrid Topics |
| 14 |
description: |
| 15 |
- List Eventgrid Topics. |
| 16 |
options: |
| 17 |
name: |
| 18 |
description: |
| 19 |
- Specify the name of the Eventgrid topic. |
| 20 |
required: false |
| 21 |
type: str |
| 22 |
resource_group: |
| 23 |
description: |
| 24 |
- Name of a resource group to limit topics from. If not specified the subscription |
| 25 |
will be searched. |
| 26 |
required: false |
| 27 |
type: str |
| 28 |
odata_query: |
| 29 |
description: |
| 30 |
- The query used to filter the search results using OData syntax. |
| 31 |
required: false |
| 32 |
type: str |
| 33 |
tags: |
| 34 |
description: |
| 35 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 36 |
type: list |
| 37 |
elements: 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 a Topic |
| 48 |
azure.azcollection.azure_rm_eventgrid_topic_info: |
| 49 |
resource_group: rg1 |
| 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: true |
| 60 |
eventgrid_topics: |
| 61 |
description: |
| 62 |
- List of Eventgrid Topics. |
| 63 |
returned: always |
| 64 |
type: complex |
| 65 |
contains: |
| 66 |
data_residency_boundary: |
| 67 |
description: |
| 68 |
- Residncy boundary |
| 69 |
returned: always |
| 70 |
type: str |
| 71 |
sample: WithinGeopair |
| 72 |
disable_local_auth: |
| 73 |
description: |
| 74 |
- Whether local auth is disabled |
| 75 |
returned: always |
| 76 |
type: bool |
| 77 |
sample: false |
| 78 |
endpoint: |
| 79 |
description: |
| 80 |
- Rest API endpoint to use to post events |
| 81 |
returned: always |
| 82 |
type: str |
| 83 |
sample: https://myTopic-20070.westus3-1.eventgrid.azure.net/api/events |
| 84 |
id: |
| 85 |
description: |
| 86 |
- Resource ID. |
| 87 |
returned: always |
| 88 |
type: str |
| 89 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.EventGrid/topics/myTopic |
| 90 |
inbound_ip_rules: |
| 91 |
description: |
| 92 |
- Rules for network access |
| 93 |
type: complex |
| 94 |
returned: when-enabled |
| 95 |
contains: |
| 96 |
action: |
| 97 |
description: |
| 98 |
- What action will be taken, Allow or Disallow |
| 99 |
returned: always |
| 100 |
type: str |
| 101 |
sample: Allow |
| 102 |
ip_mask: |
| 103 |
description: |
| 104 |
- CIDR notation of the network to act on. |
| 105 |
returned: always |
| 106 |
type: str |
| 107 |
sample: 10.0.0.0/8 |
| 108 |
input_schema: |
| 109 |
description: |
| 110 |
- The input schema assigned to this Topic |
| 111 |
returned: always |
| 112 |
type: str |
| 113 |
sample: EventGridSchema |
| 114 |
location: |
| 115 |
description: |
| 116 |
- Resource location. |
| 117 |
returned: always |
| 118 |
type: str |
| 119 |
sample: westus3 |
| 120 |
metric_resource_id: |
| 121 |
description: |
| 122 |
- Metric Resource ID. |
| 123 |
returned: always |
| 124 |
type: str |
| 125 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 126 |
minimum_tls_version_allowed: |
| 127 |
description: |
| 128 |
- The minimum TLS version allowed |
| 129 |
returned: always |
| 130 |
type: str |
| 131 |
sample: 1.0 |
| 132 |
name: |
| 133 |
description: |
| 134 |
- Resource name. |
| 135 |
returned: always |
| 136 |
type: str |
| 137 |
sample: myTopic |
| 138 |
provisioning_state: |
| 139 |
description: |
| 140 |
- The provisioning state. |
| 141 |
returned: always |
| 142 |
type: str |
| 143 |
sample: Succeeded |
| 144 |
public_network_access: |
| 145 |
description: |
| 146 |
- Indicates if boot diagnostics are enabled. |
| 147 |
returned: always |
| 148 |
type: bool |
| 149 |
sample: true |
| 150 |
topic_keys: |
| 151 |
description: |
| 152 |
- Shared access keys to a topic |
| 153 |
type: complex |
| 154 |
returned: always |
| 155 |
contains: |
| 156 |
key1: |
| 157 |
description: |
| 158 |
- Shared access key 1 |
| 159 |
type: str |
| 160 |
returned: always |
| 161 |
sample: FYKuIOq0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxZEGV3gr |
| 162 |
key2: |
| 163 |
description: |
| 164 |
- Shared access key 2 |
| 165 |
type: str |
| 166 |
returned: always |
| 167 |
sample: D8QRdfVrxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxZEGdMMc |
| 168 |
type: |
| 169 |
description: |
| 170 |
- Type of Eventgrid |
| 171 |
type: str |
| 172 |
returned: always |
| 173 |
sample: Microsoft.EventGrid/topics |
| 174 |
''' |
| 175 |
|
| 176 |
|
| 177 |
try: |
| 178 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 179 |
from azure.mgmt.eventgrid import EventGridManagementClient |
| 180 |
from azure.core.exceptions import ResourceNotFoundError |
| 181 |
except ImportError: |
| 182 |
|
| 183 |
pass |
| 184 |
|
| 185 |
|
| 186 |
class AzureRMEventgridTopic(AzureRMModuleBase): |
| 187 |
|
| 188 |
@property |
| 189 |
def client(self): |
| 190 |
self.log('Getting client') |
| 191 |
if not self._client: |
| 192 |
self._client = self.get_mgmt_svc_client(EventGridManagementClient, |
| 193 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 194 |
return self._client |
| 195 |
|
| 196 |
def __init__(self): |
| 197 |
|
| 198 |
self.module_arg_spec = dict( |
| 199 |
name=dict( |
| 200 |
type='str', |
| 201 |
required=False |
| 202 |
), |
| 203 |
odata_query=dict( |
| 204 |
type='str', |
| 205 |
required=False |
| 206 |
), |
| 207 |
resource_group=dict( |
| 208 |
type='str', |
| 209 |
required=False |
| 210 |
), |
| 211 |
tags=dict( |
| 212 |
type='list', |
| 213 |
elements='str' |
| 214 |
), |
| 215 |
) |
| 216 |
|
| 217 |
mutually_exclusive = [('name', 'odata_query')] |
| 218 |
|
| 219 |
self._client = None |
| 220 |
|
| 221 |
self.name = None |
| 222 |
self.tags = None |
| 223 |
|
| 224 |
self.results = dict( |
| 225 |
changed=False, |
| 226 |
eventgrid_topics=[] |
| 227 |
) |
| 228 |
|
| 229 |
super(AzureRMEventgridTopic, self).__init__(self.module_arg_spec, |
| 230 |
supports_tags=False, |
| 231 |
supports_check_mode=True, |
| 232 |
mutually_exclusive=mutually_exclusive, |
| 233 |
facts_module=True, |
| 234 |
) |
| 235 |
|
| 236 |
def exec_module(self, **kwargs): |
| 237 |
|
| 238 |
for key in self.module_arg_spec: |
| 239 |
setattr(self, key, kwargs[key]) |
| 240 |
|
| 241 |
if self.name is not None and self.resource_group is not None: |
| 242 |
self.results['eventgrid_topics'] = self.get_item(self.resource_group, |
| 243 |
self.name) |
| 244 |
elif self.resource_group is not None: |
| 245 |
self.results['eventgrid_topics'] = self.list_by_resourcegroup(self.resource_group, |
| 246 |
self.odata_query) |
| 247 |
else: |
| 248 |
self.results['eventgrid_topics'] = self.list_items(self.odata_query) |
| 249 |
|
| 250 |
return self.results |
| 251 |
|
| 252 |
def list_by_resourcegroup(self, resource_group, odata_query=None): |
| 253 |
self.log('List all items by resource group') |
| 254 |
try: |
| 255 |
items = self.client.topics.list_by_resource_group(resource_group, odata_query) |
| 256 |
except ResourceNotFoundError as exc: |
| 257 |
self.fail("Failed to list all items in resource group - {0}".format(str(exc))) |
| 258 |
|
| 259 |
return self.filter_results(items) |
| 260 |
|
| 261 |
def list_items(self, odata_query=None): |
| 262 |
self.log('List all items by resource group') |
| 263 |
try: |
| 264 |
items = self.client.topics.list_by_subscription(odata_query) |
| 265 |
except ResourceNotFoundError as exc: |
| 266 |
self.fail("Failed to list all items in resource group - {0}".format(str(exc))) |
| 267 |
|
| 268 |
return self.filter_results(items) |
| 269 |
|
| 270 |
def get_item(self, resource_group, topic_name): |
| 271 |
''' |
| 272 |
Get the eventgrid topic |
| 273 |
|
| 274 |
:return: eventgrid topic dict |
| 275 |
''' |
| 276 |
try: |
| 277 |
response = self.client.topics.get(resource_group, topic_name) |
| 278 |
return self.filter_results([response]) |
| 279 |
except ResourceNotFoundError: |
| 280 |
self.log('Did not find the eventgrid_topic.') |
| 281 |
return [] |
| 282 |
except Exception as exc: |
| 283 |
self.fail('Error when get eventgrid_topic got Excetion as {0}'.format(str(exc))) |
| 284 |
|
| 285 |
def filter_results(self, items): |
| 286 |
results = [] |
| 287 |
for item in items: |
| 288 |
topic = self.format_eventgrid_topic(item) |
| 289 |
if topic and self.has_tags(topic.get('tags', {}), self.tags): |
| 290 |
resource_group = self.parse_resource_to_dict(topic.get('id')).get('resource_group') |
| 291 |
topic_keys = self.client.topics.list_shared_access_keys(resource_group, topic.get('name')) |
| 292 |
topic['topic_keys'] = topic_keys and topic_keys.as_dict() or {} |
| 293 |
results.append(topic) |
| 294 |
return results |
| 295 |
|
| 296 |
def format_eventgrid_topic(self, topic_data): |
| 297 |
results = topic_data and topic_data.as_dict() or {} |
| 298 |
return results |
| 299 |
|
| 300 |
|
| 301 |
def main(): |
| 302 |
AzureRMEventgridTopic() |
| 303 |
|
| 304 |
|
| 305 |
if __name__ == '__main__': |
| 306 |
main() |