/
ftd_data_dns_settings.py
179 lines (152 loc) · 7.29 KB
/
ftd_data_dns_settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/python
# Copyright (c) 2018 Cisco Systems, Inc.
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'network'}
DOCUMENTATION = """
---
module: ftd_data_dns_settings
short_description: Manages DataDNSSettings objects on Cisco FTD devices
version_added: "2.7"
author: "Cisco Systems, Inc."
options:
operation:
description:
- Specified the name of the operation to execute in the task.
required: true
register_as:
description:
- Specifies Ansible fact name that is used to register received response from the FTD device.
dnsServerGroup
description:
- DNS Server Group Object<br>Allowed types are: [DNSServerGroup]
expiryEntryTimer
description:
- Timer to remove the IP address of a resolved FQDN after its TTL expires. Valid values are from 1 to 65535,default 1 minute. Applicable only to active DNS<br>Field level constraints: must be between 1 and 65535 (inclusive). (Note: Additional constraints might exist)
filter
description:
- The criteria used to filter the models you are requesting. It should have the following format: {field}{operator}{value}[;{field}{operator}{value}]. Supported operators are: "!"(not equals), ":"(equals), "<"(null), "~"(similar), ">"(null). Supported fields are: "name".
id
description:
- A unique string identifier assigned by the system when the object is created. No assumption can be made on the format or content of this identifier. The identifier must be provided whenever attempting to modify/delete (or reference) an existing object.<br>Field level constraints: must match pattern ^((?!;).)*$. (Note: Additional constraints might exist)
interfaces
description:
- Collection of named interfaces to lookup<br>Allowed types are: [BridgeGroupInterface, PhysicalInterface, SubInterface]
limit
description:
- An integer representing the maximum amount of objects to return. If not specified, the maximum amount is 10
name
description:
- A unique name to identify this Data DNS Setting
offset
description:
- An integer representing the index of the first requested object. Index starts from 0. If not specified, the returned objects will start from index 0
pollTimer
description:
- Specifies the timer during which the FTD queries the DNS server to resolve the FQDN. Specifies the timer in minutes. Valid values are from 1 to 65535 minutes, default 240 minutes has effect only when at least one network object group has been activated. Applicable only to active DNS<br>Field level constraints: must be between 1 and 65535 (inclusive). (Note: Additional constraints might exist)
sort
description:
- The field used to sort the requested object list
type
description:
- A UTF8 string, all letters lower-case, that represents the class-type. This corresponds to the class name.
version
description:
- A unique string version assigned by the system when the object is created or modified. No assumption can be made on the format or content of this identifier. The identifier must be provided whenever attempting to modify/delete an existing object. As the version will change every time the object is modified, the value provided in this identifier must match exactly what is present in the system or the request will be rejected.
"""
EXAMPLES = """
- name: Fetch DataDNSSettings with a given name
ftd_data_dns_settings:
operation: "getDataDNSSettingsByName"
name: "Ansible DataDNSSettings"
"""
RETURN = """
response:
description: HTTP response returned from the API call.
returned: success
type: dict
error_code:
description: HTTP error code returned from the server.
returned: error
type: int
msg:
description: Error message returned from the server.
returned: error
type: dict
"""
import json
from ansible.module_utils.basic import AnsibleModule, to_text
from ansible.module_utils.http import iterate_over_pageable_resource
from ansible.module_utils.misc import dict_subset, construct_module_result, copy_identity_properties
from ansible.module_utils.six.moves.urllib.error import HTTPError
from ansible.module_utils.connection import Connection
class DataDNSSettingsResource(object):
def __init__(self, conn):
self._conn = conn
def editDataDNSSettings(self, params):
path_params = dict_subset(params, ['objId'])
body_params = dict_subset(params, ['dnsServerGroup', 'expiryEntryTimer', 'id', 'interfaces', 'name', 'pollTimer', 'type', 'version'])
return self._conn.send_request(
url_path='/devices/default/datadnssettings/{objId}',
http_method='PUT',
body_params=body_params,
path_params=path_params,
)
def getDataDNSSettings(self, params):
path_params = dict_subset(params, ['objId'])
return self._conn.send_request(
url_path='/devices/default/datadnssettings/{objId}',
http_method='GET',
path_params=path_params,
)
def getDataDNSSettingsList(self, params):
query_params = dict_subset(params, ['filter', 'limit', 'offset', 'sort'])
return self._conn.send_request(
url_path='/devices/default/datadnssettings',
http_method='GET',
query_params=query_params,
)
def getDataDNSSettingsByName(self, params):
search_params = params.copy()
search_params['filter'] = 'name:%s' % params['name']
item_generator = iterate_over_pageable_resource(self.getDataDNSSettingsList, search_params)
return next(item for item in item_generator if item['name'] == params['name'])
def editDataDNSSettingsByName(self, params):
existing_object = self.getDataDNSSettingsByName(params)
params = copy_identity_properties(existing_object, params)
return self.editDataDNSSettings(params)
def main():
fields = dict(
operation=dict(type='str', choices=['editDataDNSSettings', 'getDataDNSSettings', 'getDataDNSSettingsList', 'getDataDNSSettingsByName', 'editDataDNSSettingsByName'], required=True),
register_as=dict(type='str'),
dnsServerGroup=dict(type='dict'),
expiryEntryTimer=dict(type='int'),
filter=dict(type='str'),
id=dict(type='str'),
interfaces=dict(type='list'),
limit=dict(type='int'),
name=dict(type='str'),
objId=dict(type='str'),
offset=dict(type='int'),
pollTimer=dict(type='int'),
sort=dict(type='str'),
type=dict(type='str'),
version=dict(type='str'),
)
module = AnsibleModule(argument_spec=fields)
params = module.params
try:
conn = Connection(module._socket_path)
resource = DataDNSSettingsResource(conn)
resource_method_to_call = getattr(resource, params['operation'])
response = resource_method_to_call(params)
result = construct_module_result(response, params)
module.exit_json(**result)
except HTTPError as e:
err_msg = to_text(e.read())
module.fail_json(changed=False, msg=json.loads(err_msg) if err_msg else {}, error_code=e.code)
except Exception as e:
module.fail_json(changed=False, msg=str(e))
if __name__ == '__main__':
main()