| 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_networkwatcher_info |
| 14 |
|
| 15 |
version_added: "2.5.0" |
| 16 |
|
| 17 |
short_description: Get or list the network watcher facts |
| 18 |
|
| 19 |
description: |
| 20 |
- Get or list the network watcher facts. |
| 21 |
|
| 22 |
options: |
| 23 |
resource_group: |
| 24 |
description: |
| 25 |
- The name of the resource group. |
| 26 |
type: str |
| 27 |
name: |
| 28 |
description: |
| 29 |
- Name of the network watcher. |
| 30 |
type: str |
| 31 |
tags: |
| 32 |
description: |
| 33 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 34 |
type: list |
| 35 |
elements: str |
| 36 |
|
| 37 |
extends_documentation_fragment: |
| 38 |
- azure.azcollection.azure |
| 39 |
|
| 40 |
author: |
| 41 |
- xuzhang3 (@xuzhang3) |
| 42 |
- Fred-sun (@Fred-sun) |
| 43 |
|
| 44 |
''' |
| 45 |
|
| 46 |
EXAMPLES = ''' |
| 47 |
- name: Get the network watcher facts |
| 48 |
azure_rm_networkwatcher_info: |
| 49 |
resource_group: myResourceGroup |
| 50 |
name: mywatcher01 |
| 51 |
|
| 52 |
- name: list the network watcher facts |
| 53 |
azure_rm_networkwatcher_info: |
| 54 |
resource_group: myResourceGroup |
| 55 |
|
| 56 |
- name: list the network watcher and filter by tags |
| 57 |
azure_rm_networkwatcher_info: |
| 58 |
tags: |
| 59 |
- key1 |
| 60 |
- key2 |
| 61 |
''' |
| 62 |
|
| 63 |
RETURN = ''' |
| 64 |
network_watchers: |
| 65 |
description: |
| 66 |
- The facts of the network watcher. |
| 67 |
returned: always |
| 68 |
type: complex |
| 69 |
contains: |
| 70 |
resource_group: |
| 71 |
description: |
| 72 |
- The resource group. |
| 73 |
type: str |
| 74 |
returned: always |
| 75 |
sample: NetworkWatcherRG |
| 76 |
id: |
| 77 |
description: |
| 78 |
- Resource ID. |
| 79 |
returned: always |
| 80 |
type: str |
| 81 |
sample: "/subscriptions/xxx-xxx/resourceGroups/NetworkWatcherRG/providers/Microsoft.Network/networkWatchers/netwatcher_eastus" |
| 82 |
location: |
| 83 |
description: |
| 84 |
- Resource location. |
| 85 |
returned: always |
| 86 |
type: str |
| 87 |
sample: eastus |
| 88 |
name: |
| 89 |
description: |
| 90 |
- Resource name. |
| 91 |
returned: always |
| 92 |
type: str |
| 93 |
sample: mynetworkwatcher01 |
| 94 |
tags: |
| 95 |
description: |
| 96 |
- Resource tags. |
| 97 |
returned: always |
| 98 |
type: dict |
| 99 |
sample: { 'key1':'value1' } |
| 100 |
type: |
| 101 |
description: |
| 102 |
- Resource type. |
| 103 |
returned: always |
| 104 |
type: str |
| 105 |
sample: "Microsoft.Network/networkWatchers" |
| 106 |
provisioning_state: |
| 107 |
description: |
| 108 |
- The provisioning state of the network watcher resource. |
| 109 |
type: str |
| 110 |
returned: always |
| 111 |
sample: Succeeded |
| 112 |
''' |
| 113 |
|
| 114 |
try: |
| 115 |
from azure.core.exceptions import ResourceNotFoundError |
| 116 |
from azure.mgmt.core.tools import parse_resource_id |
| 117 |
except Exception: |
| 118 |
|
| 119 |
pass |
| 120 |
|
| 121 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 122 |
|
| 123 |
|
| 124 |
class AzureRMNetworkWatcherInfo(AzureRMModuleBase): |
| 125 |
|
| 126 |
def __init__(self): |
| 127 |
|
| 128 |
self.module_arg_spec = dict( |
| 129 |
resource_group=dict(type='str'), |
| 130 |
name=dict(type='str'), |
| 131 |
tags=dict(type='list', elements='str') |
| 132 |
) |
| 133 |
|
| 134 |
self.results = dict( |
| 135 |
changed=False, |
| 136 |
network_watchers=[] |
| 137 |
) |
| 138 |
|
| 139 |
self.resource_group = None |
| 140 |
self.name = None |
| 141 |
self.tags = None |
| 142 |
|
| 143 |
super(AzureRMNetworkWatcherInfo, self).__init__(self.module_arg_spec, |
| 144 |
supports_check_mode=True, |
| 145 |
supports_tags=False, |
| 146 |
facts_module=True) |
| 147 |
|
| 148 |
def exec_module(self, **kwargs): |
| 149 |
for key in self.module_arg_spec: |
| 150 |
setattr(self, key, kwargs[key]) |
| 151 |
|
| 152 |
if self.name and self.resource_group: |
| 153 |
response = [self.get_by_name()] |
| 154 |
elif self.resource_group: |
| 155 |
response = self.list_by_resourcegroup() |
| 156 |
else: |
| 157 |
response = self.list_all() |
| 158 |
|
| 159 |
self.results['network_watchers'] = [self.to_dict(item) for item in response if response is not None] |
| 160 |
|
| 161 |
return self.results |
| 162 |
|
| 163 |
def get_by_name(self): |
| 164 |
response = None |
| 165 |
try: |
| 166 |
response = self.network_client.network_watchers.get(self.resource_group, self.name) |
| 167 |
|
| 168 |
except ResourceNotFoundError as exec: |
| 169 |
self.log("Failed to get network watchers, Exception as {0}".format(exec)) |
| 170 |
|
| 171 |
return response |
| 172 |
|
| 173 |
def list_by_resourcegroup(self): |
| 174 |
response = None |
| 175 |
try: |
| 176 |
response = self.network_client.network_watchers.list(self.resource_group) |
| 177 |
except Exception as exec: |
| 178 |
self.log("Faild to list network watchers by resource group, exception as {0}".format(exec)) |
| 179 |
return response |
| 180 |
|
| 181 |
def list_all(self): |
| 182 |
response = None |
| 183 |
try: |
| 184 |
response = self.network_client.network_watchers.list_all() |
| 185 |
except Exception as exc: |
| 186 |
self.fail("Failed to list all items - {0}".format(str(exc))) |
| 187 |
|
| 188 |
return response |
| 189 |
|
| 190 |
def to_dict(self, body): |
| 191 |
results = dict() |
| 192 |
if body is not None and self.has_tags(body.tags, self.tags): |
| 193 |
results = dict( |
| 194 |
resource_group=parse_resource_id(body.id).get('resource_group'), |
| 195 |
id=body.id, |
| 196 |
name=body.name, |
| 197 |
location=body.location, |
| 198 |
tags=body.tags, |
| 199 |
type=body.type, |
| 200 |
provisioning_state=body.provisioning_state, |
| 201 |
) |
| 202 |
return results |
| 203 |
return None |
| 204 |
|
| 205 |
|
| 206 |
def main(): |
| 207 |
AzureRMNetworkWatcherInfo() |
| 208 |
|
| 209 |
|
| 210 |
if __name__ == '__main__': |
| 211 |
main() |