| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
--- |
| 12 |
module: azure_rm_ipgroup_info |
| 13 |
|
| 14 |
version_added: "1.6.0" |
| 15 |
|
| 16 |
short_description: Get IP group facts |
| 17 |
|
| 18 |
description: |
| 19 |
- Get facts for specified IP group or all IP groups in a given resource group. |
| 20 |
|
| 21 |
options: |
| 22 |
resource_group: |
| 23 |
description: |
| 24 |
- Name of the resource group. |
| 25 |
type: str |
| 26 |
name: |
| 27 |
description: |
| 28 |
- Name of the IP group. |
| 29 |
type: str |
| 30 |
tags: |
| 31 |
description: |
| 32 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 33 |
type: list |
| 34 |
elements: str |
| 35 |
|
| 36 |
extends_documentation_fragment: |
| 37 |
- azure.azcollection.azure |
| 38 |
|
| 39 |
author: |
| 40 |
- Aparna Patil (@techcon65) |
| 41 |
|
| 42 |
''' |
| 43 |
|
| 44 |
EXAMPLES = ''' |
| 45 |
- name: Get facts for one IP group |
| 46 |
azure_rm_ipgroup_info: |
| 47 |
resource_group: myAzureResourceGroup |
| 48 |
name: myipgroup |
| 49 |
|
| 50 |
- name: Get facts for all IP groups in resource group |
| 51 |
azure_rm_ipgroup_info: |
| 52 |
resource_group: myAzureResourceGroup |
| 53 |
''' |
| 54 |
|
| 55 |
RETURN = ''' |
| 56 |
ipgroups: |
| 57 |
description: |
| 58 |
- Gets a list of IP groups. |
| 59 |
returned: always |
| 60 |
type: list |
| 61 |
elements: dict |
| 62 |
sample: [ |
| 63 |
{ |
| 64 |
"etag": "c67388ea-6dab-481b-9387-bd441c0d32f8", |
| 65 |
"firewalls": [], |
| 66 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/MyAzureResourceGroup/providers/ |
| 67 |
Microsoft.Network/ipGroups/myipgroup", |
| 68 |
"ip_addresses": [ |
| 69 |
"13.64.39.16/32", |
| 70 |
"40.74.146.80/31", |
| 71 |
"40.74.147.32/28" |
| 72 |
], |
| 73 |
"location": "eastus", |
| 74 |
"name": "myipgroup", |
| 75 |
"provisioning_state": "Succeeded", |
| 76 |
"tags": { |
| 77 |
"key1": "value1" |
| 78 |
} |
| 79 |
} |
| 80 |
] |
| 81 |
''' |
| 82 |
|
| 83 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 84 |
|
| 85 |
try: |
| 86 |
from azure.core.exceptions import ResourceNotFoundError |
| 87 |
except Exception: |
| 88 |
|
| 89 |
pass |
| 90 |
|
| 91 |
AZURE_OBJECT_CLASS = 'IpGroup' |
| 92 |
|
| 93 |
|
| 94 |
class AzureRMIPGroupInfo(AzureRMModuleBase): |
| 95 |
|
| 96 |
def __init__(self): |
| 97 |
|
| 98 |
|
| 99 |
self.module_arg_spec = dict( |
| 100 |
name=dict(type='str'), |
| 101 |
resource_group=dict(type='str'), |
| 102 |
tags=dict(type='list', elements='str') |
| 103 |
) |
| 104 |
|
| 105 |
|
| 106 |
self.results = dict( |
| 107 |
changed=False |
| 108 |
) |
| 109 |
|
| 110 |
self.name = None |
| 111 |
self.resource_group = None |
| 112 |
self.tags = None |
| 113 |
|
| 114 |
super(AzureRMIPGroupInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True) |
| 115 |
|
| 116 |
def exec_module(self, **kwargs): |
| 117 |
|
| 118 |
for key in self.module_arg_spec: |
| 119 |
setattr(self, key, kwargs[key]) |
| 120 |
|
| 121 |
results = [] |
| 122 |
|
| 123 |
if self.name is not None: |
| 124 |
|
| 125 |
results = self.get_item() |
| 126 |
elif self.resource_group: |
| 127 |
|
| 128 |
results = self.list_resource_group() |
| 129 |
else: |
| 130 |
|
| 131 |
results = self.list_items() |
| 132 |
|
| 133 |
self.results['ipgroups'] = self.curated_items(results) |
| 134 |
|
| 135 |
return self.results |
| 136 |
|
| 137 |
def get_item(self): |
| 138 |
self.log('Get properties for {0}'.format(self.name)) |
| 139 |
item = None |
| 140 |
results = [] |
| 141 |
|
| 142 |
try: |
| 143 |
item = self.network_client.ip_groups.get(self.resource_group, self.name) |
| 144 |
except ResourceNotFoundError: |
| 145 |
pass |
| 146 |
|
| 147 |
|
| 148 |
if item and self.has_tags(item.tags, self.tags): |
| 149 |
results = [item] |
| 150 |
return results |
| 151 |
|
| 152 |
def list_resource_group(self): |
| 153 |
self.log('List all IP groups for resource group - {0}'.format(self.resource_group)) |
| 154 |
try: |
| 155 |
response = self.network_client.ip_groups.list_by_resource_group(self.resource_group) |
| 156 |
except ResourceNotFoundError as exc: |
| 157 |
self.fail("Failed to list for resource group {0} - {1}".format(self.resource_group, str(exc))) |
| 158 |
|
| 159 |
results = [] |
| 160 |
for item in response: |
| 161 |
if self.has_tags(item.tags, self.tags): |
| 162 |
results.append(item) |
| 163 |
return results |
| 164 |
|
| 165 |
def list_items(self): |
| 166 |
self.log('List all IP groups for a subscription ') |
| 167 |
try: |
| 168 |
response = self.network_client.ip_groups.list() |
| 169 |
except ResourceNotFoundError as exc: |
| 170 |
self.fail("Failed to list all items - {0}".format(str(exc))) |
| 171 |
|
| 172 |
results = [] |
| 173 |
for item in response: |
| 174 |
if self.has_tags(item.tags, self.tags): |
| 175 |
results.append(item) |
| 176 |
return results |
| 177 |
|
| 178 |
def curated_items(self, raws): |
| 179 |
return [self.ipgroup_to_dict(item) for item in raws] if raws else [] |
| 180 |
|
| 181 |
def ipgroup_to_dict(self, ipgroup): |
| 182 |
result = dict( |
| 183 |
id=ipgroup.id, |
| 184 |
name=ipgroup.name, |
| 185 |
location=ipgroup.location, |
| 186 |
tags=ipgroup.tags, |
| 187 |
ip_addresses=ipgroup.ip_addresses, |
| 188 |
provisioning_state=ipgroup.provisioning_state, |
| 189 |
firewalls=[dict(id=x.id) for x in ipgroup.firewalls], |
| 190 |
etag=ipgroup.etag |
| 191 |
) |
| 192 |
return result |
| 193 |
|
| 194 |
|
| 195 |
def main(): |
| 196 |
AzureRMIPGroupInfo() |
| 197 |
|
| 198 |
|
| 199 |
if __name__ == '__main__': |
| 200 |
main() |