| 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 |
| 14 |
version_added: "2.5.0" |
| 15 |
short_description: Manage the network watcher |
| 16 |
description: |
| 17 |
- Create, update or delete the network watcher. |
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- Name of resource group. |
| 22 |
required: true |
| 23 |
type: str |
| 24 |
location: |
| 25 |
description: |
| 26 |
- Valid Azure location. Defaults to location of the resource group. |
| 27 |
type: str |
| 28 |
name: |
| 29 |
description: |
| 30 |
- The name of the network watcher. |
| 31 |
required: true |
| 32 |
type: str |
| 33 |
state: |
| 34 |
description: |
| 35 |
- State of the Network Watcher. Use C(present) to create or update and C(absent) to delete. |
| 36 |
default: present |
| 37 |
type: str |
| 38 |
choices: |
| 39 |
- absent |
| 40 |
- present |
| 41 |
|
| 42 |
extends_documentation_fragment: |
| 43 |
- azure.azcollection.azure |
| 44 |
- azure.azcollection.azure_tags |
| 45 |
|
| 46 |
author: |
| 47 |
- xuzhang3 (@xuzhang3) |
| 48 |
- Fred-sun (@Fred-sun) |
| 49 |
|
| 50 |
''' |
| 51 |
|
| 52 |
EXAMPLES = ''' |
| 53 |
- name: Create a Network Watcher |
| 54 |
azure_rm_networkwatcher: |
| 55 |
resource_group: myResourceGroup |
| 56 |
name: myNetworkWatcher |
| 57 |
location: eastus |
| 58 |
tags: |
| 59 |
testing: testing |
| 60 |
delete: on-exit |
| 61 |
|
| 62 |
- name: Delete a Network Watcher |
| 63 |
azure_rm_networkwatcher: |
| 64 |
resource_group: myResourceGroup |
| 65 |
name: myNetworkWatcher |
| 66 |
state: absent |
| 67 |
''' |
| 68 |
RETURN = ''' |
| 69 |
state: |
| 70 |
description: |
| 71 |
- The facts of the network watcher. |
| 72 |
returned: always |
| 73 |
type: complex |
| 74 |
contains: |
| 75 |
resource_group: |
| 76 |
description: |
| 77 |
- The resource group. |
| 78 |
type: str |
| 79 |
returned: always |
| 80 |
sample: NetworkWatcherRG |
| 81 |
id: |
| 82 |
description: |
| 83 |
- Resource ID. |
| 84 |
returned: always |
| 85 |
type: str |
| 86 |
sample: "/subscriptions/xxx-xxx/resourceGroups/NetworkWatcherRG/providers/Microsoft.Network/networkWatchers/netwatcher_eastus" |
| 87 |
location: |
| 88 |
description: |
| 89 |
- Resource location. |
| 90 |
returned: always |
| 91 |
type: str |
| 92 |
sample: eastus |
| 93 |
name: |
| 94 |
description: |
| 95 |
- Resource name. |
| 96 |
returned: always |
| 97 |
type: str |
| 98 |
sample: mynetworkwatcher01 |
| 99 |
tags: |
| 100 |
description: |
| 101 |
- Resource tags. |
| 102 |
returned: always |
| 103 |
type: dict |
| 104 |
sample: { 'key1':'value1' } |
| 105 |
type: |
| 106 |
description: |
| 107 |
- Resource type. |
| 108 |
returned: always |
| 109 |
type: str |
| 110 |
sample: "Microsoft.Network/networkWatchers" |
| 111 |
provisioning_state: |
| 112 |
description: |
| 113 |
- The provisioning state of the network watcher resource. |
| 114 |
type: str |
| 115 |
returned: always |
| 116 |
sample: Succeeded |
| 117 |
''' |
| 118 |
|
| 119 |
try: |
| 120 |
from azure.core.exceptions import ResourceNotFoundError |
| 121 |
except ImportError: |
| 122 |
|
| 123 |
pass |
| 124 |
|
| 125 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 126 |
|
| 127 |
|
| 128 |
class AzureRMNetworkWatcher(AzureRMModuleBase): |
| 129 |
|
| 130 |
def __init__(self): |
| 131 |
|
| 132 |
self.module_arg_spec = dict( |
| 133 |
resource_group=dict(type='str', required=True), |
| 134 |
name=dict(type='str', required=True), |
| 135 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 136 |
location=dict(type='str'), |
| 137 |
) |
| 138 |
|
| 139 |
self.resource_group = None |
| 140 |
self.name = None |
| 141 |
self.state = None |
| 142 |
self.location = None |
| 143 |
self.tags = None |
| 144 |
|
| 145 |
self.results = dict( |
| 146 |
changed=False, |
| 147 |
state=dict() |
| 148 |
) |
| 149 |
|
| 150 |
super(AzureRMNetworkWatcher, self).__init__(self.module_arg_spec, |
| 151 |
supports_tags=True, |
| 152 |
supports_check_mode=True) |
| 153 |
|
| 154 |
def exec_module(self, **kwargs): |
| 155 |
|
| 156 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 157 |
setattr(self, key, kwargs[key]) |
| 158 |
|
| 159 |
resource_group = self.get_resource_group(self.resource_group) |
| 160 |
if not self.location: |
| 161 |
|
| 162 |
self.location = resource_group.location |
| 163 |
|
| 164 |
changed = False |
| 165 |
results = dict() |
| 166 |
|
| 167 |
old_response = self.get_by_name() |
| 168 |
|
| 169 |
if old_response is not None: |
| 170 |
if self.state == 'present': |
| 171 |
update_tags, new_tags = self.update_tags(old_response['tags']) |
| 172 |
if update_tags: |
| 173 |
changed = True |
| 174 |
if not self.check_mode: |
| 175 |
results = self.update_watcher_tags(dict(tags=new_tags)) |
| 176 |
else: |
| 177 |
results = old_response |
| 178 |
else: |
| 179 |
changed = True |
| 180 |
if not self.check_mode: |
| 181 |
results = self.delete_network_watcher() |
| 182 |
else: |
| 183 |
if self.state == 'present': |
| 184 |
changed = True |
| 185 |
if not self.check_mode: |
| 186 |
results = self.create_or_update(dict(tags=self.tags, location=self.location)) |
| 187 |
else: |
| 188 |
changed = False |
| 189 |
self.log("The Network Watcher is not exists") |
| 190 |
|
| 191 |
self.results['changed'] = changed |
| 192 |
self.results['state'] = results |
| 193 |
|
| 194 |
return self.results |
| 195 |
|
| 196 |
def get_by_name(self): |
| 197 |
response = None |
| 198 |
try: |
| 199 |
response = self.network_client.network_watchers.get(self.resource_group, self.name) |
| 200 |
|
| 201 |
except ResourceNotFoundError as exec: |
| 202 |
self.log("Failed to get network watcher, Exception as {0}".format(exec)) |
| 203 |
|
| 204 |
return self.to_dict(response) |
| 205 |
|
| 206 |
def create_or_update(self, body): |
| 207 |
response = None |
| 208 |
try: |
| 209 |
response = self.to_dict(self.network_client.network_watchers.create_or_update(self.resource_group, self.name, body)) |
| 210 |
except Exception as exc: |
| 211 |
self.fail("Error creating Network Watcher {0} - {1}".format(self.name, str(exc))) |
| 212 |
|
| 213 |
return response |
| 214 |
|
| 215 |
def update_watcher_tags(self, body): |
| 216 |
response = None |
| 217 |
try: |
| 218 |
response = self.network_client.network_watchers.update_tags(self.resource_group, self.name, body) |
| 219 |
except Exception as exc: |
| 220 |
self.fail("Error updating Network Watcher {0} - {1}".format(self.name, str(exc))) |
| 221 |
return self.to_dict(response) |
| 222 |
|
| 223 |
def delete_network_watcher(self): |
| 224 |
try: |
| 225 |
self.network_client.network_watchers.begin_delete(self.resource_group, self.name) |
| 226 |
except Exception as exc: |
| 227 |
self.fail("Error deleting Network Watcher {0} - {1}".format(self.name, str(exc))) |
| 228 |
|
| 229 |
def to_dict(self, body): |
| 230 |
results = None |
| 231 |
if body is not None: |
| 232 |
results = dict( |
| 233 |
resource_group=self.resource_group, |
| 234 |
id=body.id, |
| 235 |
name=body.name, |
| 236 |
location=body.location, |
| 237 |
tags=body.tags, |
| 238 |
type=body.type, |
| 239 |
provisioning_state=body.provisioning_state, |
| 240 |
) |
| 241 |
return results |
| 242 |
|
| 243 |
|
| 244 |
def main(): |
| 245 |
AzureRMNetworkWatcher() |
| 246 |
|
| 247 |
|
| 248 |
if __name__ == '__main__': |
| 249 |
main() |