Skip to content

Commit 96ef700

Browse files
authored
Merge pull request Nosmoht#7 from djcf/master
2 parents bceeb4c + a59cf67 commit 96ef700

File tree

3 files changed

+67
-24
lines changed

3 files changed

+67
-24
lines changed

README.md

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ Ensure zone is present
1212

1313
```yaml
1414
- powerdns_zone:
15-
name: zone01.internal.example.com
15+
name: zone01.internal.example.com.
1616
nameservers:
17-
- ns-01.example.com
18-
- ns-02.example.com
17+
- ns-01.example.com.
18+
- ns-02.example.com.
1919
kind: master
2020
state: present
2121
pdns_host: powerdns.example.com
@@ -38,37 +38,58 @@ Ensure zone is absent
3838
Ensure A record
3939
```yaml
4040
- powerdns_record:
41-
name: host01
41+
name: host01.zone01.internal.example.com.
4242
zone: zone01.internal.example.com
4343
type: A
4444
content: 192.168.1.234
4545
ttl: 1440
4646
pdns_host: powerdns.example.com
47-
pdns_port: 8081
47+
pdns_port: 443
4848
pdns_api_key: topsecret
49+
pdns_prot: https
4950
```
5051
5152
Ensure AAAA record
5253
```yaml
5354
- powerdns_record:
54-
name: host01
55+
name: host01.zone01.internal.example.com.
5556
zone: zone01.internal.example.com
5657
type: AAAA
5758
content: 2001:cdba:0000:0000:0000:0000:3257:9652
5859
ttl: 1440
5960
pdns_host: powerdns.example.com
60-
pdns_port: 8081
61+
pdns_port: 8443
6162
pdns_api_key: topsecret
63+
pdns_prot: https
64+
```
65+
66+
Do not verify SSL certificate (this is a security risk)
67+
68+
```yaml
69+
- powerdns_record:
70+
name: host01.zone01.internal.example.com.
71+
zone: zone01.internal.example.com
72+
type: AAAA
73+
content: 2001:cdba:0000:0000:0000:0000:3257:9652
74+
ttl: 1440
75+
pdns_host: powerdns.example.com
76+
pdns_port: 8443
77+
pdns_api_key: topsecret
78+
pdns_prot: https
79+
strict_ssl_checking: false
6280
```
6381
6482
Ensure CNAME record
6583
```yaml
6684
- powerdns_record:
67-
name: database
85+
name: database.zone01.internal.example.com.
6886
zone: zone01.internal.example.com
6987
type: CNAME
7088
content: host01.zone01.internal.example.com
7189
pdns_host: powerdns.example.com
72-
pdns_port: 8081
90+
pdns_port: 80
7391
pdns_api_key: topsecret
92+
pdns_prot: http
7493
```
94+
95+
Note the trailing '.' following most records, if not present will result in the error "Domain record is not canonical".

powerdns_record.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
pdns_api_key:
5959
description:
6060
- API Key to authenticate through PowerDNS API
61+
strict_ssl_checking:
62+
description:
63+
- Disables strict certificate checking
64+
default: false
6165
author: "Thomas Krahn (@nosmoht)"
6266
'''
6367

@@ -86,16 +90,22 @@ def __init__(self, url, status_code, message):
8690

8791

8892
class PowerDNSClient:
89-
def __init__(self, host, port, prot, api_key):
90-
self.url = '{prot}://{host}:{port}'.format(prot=prot, host=host, port=port)
93+
def __init__(self, host, port, prot, api_key, verify):
94+
self.url = '{prot}://{host}:{port}/api/v1'.format(prot=prot, host=host, port=port)
9195
self.headers = {'X-API-Key': api_key,
9296
'content-type': 'application/json',
9397
'accept': 'application/json'
9498
}
99+
self.verify = verify
95100

96101
def _handle_request(self, req):
97102
if req.status_code in [200, 201, 204]:
98-
return json.loads(req.text)
103+
if req.text:
104+
try:
105+
return json.loads(req.text)
106+
except Exception as e:
107+
print(e) # same as yield
108+
return dict()
99109
elif req.status_code == 404:
100110
error_message = 'Not found'
101111
else:
@@ -122,7 +132,7 @@ def _get_zone_url(self, server, name):
122132
return '{url}/{name}'.format(url=self._get_zones_url(server), name=name)
123133

124134
def get_zone(self, server, name):
125-
req = requests.get(url=self._get_zone_url(server, name), headers=self.headers)
135+
req = requests.get(url=self._get_zone_url(server, name), headers=self.headers, verify=self.verify)
126136
if req.status_code == 422: # zone does not exist
127137
return None
128138
return self._handle_request(req)
@@ -144,14 +154,14 @@ def create_record(self, server, zone, name, rtype, content, disabled, ttl):
144154
url = self._get_zone_url(server=server, name=zone)
145155
data = self._get_request_data(changetype='REPLACE', server=server, zone=zone, name=name, rtype=rtype,
146156
content=content, disabled=disabled, ttl=ttl)
147-
req = requests.patch(url=url, data=json.dumps(data), headers=self.headers)
157+
req = requests.patch(url=url, data=json.dumps(data), headers=self.headers, verify=self.verify)
148158
return self._handle_request(req)
149159

150160
def delete_record(self, server, zone, name, rtype):
151161
url = self._get_zone_url(server=server, name=zone)
152162
data = self._get_request_data(changetype='DELETE', server=server, zone=zone, name=name, rtype=rtype)
153163
# module.fail_json(msg=json.dumps(data))
154-
req = requests.patch(url=url, data=json.dumps(data), headers=self.headers)
164+
req = requests.patch(url=url, data=json.dumps(data), headers=self.headers, verify=self.verify)
155165
return self._handle_request(req)
156166

157167

@@ -230,14 +240,16 @@ def main():
230240
pdns_port=dict(type='int', default=8081),
231241
pdns_prot=dict(type='str', default='http', choices=['http', 'https']),
232242
pdns_api_key=dict(type='str', required=False),
243+
strict_ssl_checking=dict(type='boolean', default=True),
233244
),
234245
supports_check_mode=True,
235246
)
236247

237248
pdns_client = PowerDNSClient(host=module.params['pdns_host'],
238249
port=module.params['pdns_port'],
239250
prot=module.params['pdns_prot'],
240-
api_key=module.params['pdns_api_key'])
251+
api_key=module.params['pdns_api_key'],
252+
verify=module.params['strict_ssl_checking'])
241253

242254
try:
243255
changed, record = ensure(module, pdns_client)

powerdns_zone.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
pdns_api_key:
4343
description:
4444
- API Key to authenticate through PowerDNS API
45+
strict_ssl_checking:
46+
description:
47+
- Disables strict certificate checking
48+
default: false
4549
author: "Thomas Krahn (@nosmoht)"
4650
'''
4751

@@ -79,17 +83,21 @@ def __init__(self, url, status_code, message):
7983

8084

8185
class PowerDNSClient:
82-
def __init__(self, host, port, prot, api_key):
83-
self.url = '{prot}://{host}:{port}'.format(prot=prot, host=host, port=port)
86+
def __init__(self, host, port, prot, api_key, verify):
87+
self.url = '{prot}://{host}:{port}/api/v1'.format(prot=prot, host=host, port=port)
8488
self.headers = {'X-API-Key': api_key,
8589
'content-type': 'application/json',
8690
'accept': 'application/json'
8791
}
92+
self.verify = verify
8893

8994
def _handle_request(self, req):
9095
if req.status_code in [200, 201, 204]:
9196
if req.text:
92-
return json.loads(req.text)
97+
try:
98+
return json.loads(req.text)
99+
except Exception as e:
100+
print(e) # same as yield
93101
return dict()
94102
elif req.status_code == 404:
95103
error_message = 'Not found'
@@ -117,22 +125,22 @@ def _get_zone_url(self, server, name):
117125
return '{url}/{name}'.format(url=self._get_zones_url(server), name=name)
118126

119127
def get_zone(self, server, name):
120-
req = requests.get(url=self._get_zone_url(server, name), headers=self.headers)
128+
req = requests.get(url=self._get_zone_url(server, name), headers=self.headers, verify=self.verify)
121129
if req.status_code == 422: # zone does not exist
122130
return None
123131
return self._handle_request(req)
124132

125133
def create_zone(self, server, data):
126-
req = requests.post(url=self._get_zones_url(server, ), data=json.dumps(data), headers=self.headers)
134+
req = requests.post(url=self._get_zones_url(server, ), data=json.dumps(data), headers=self.headers, verify=self.verify)
127135
return self._handle_request(req)
128136

129137
def delete_zone(self, server, name):
130-
req = requests.delete(url=self._get_zone_url(server, name), headers=self.headers)
138+
req = requests.delete(url=self._get_zone_url(server, name), headers=self.headers, verify=self.verify)
131139
return self._handle_request(req)
132140

133141
def update_zone(self, server, zone):
134142
req = requests.patch(url=self._get_zone_url(server=server, name=zone.get('name')), data=zone,
135-
headers=self.headers)
143+
headers=self.headers, verify=self.verify)
136144
return self._handle_request(req)
137145

138146

@@ -206,14 +214,16 @@ def main():
206214
pdns_port=dict(type='int', default=8081),
207215
pdns_prot=dict(type='str', default='http', choices=['http', 'https']),
208216
pdns_api_key=dict(type='str', required=False),
217+
strict_ssl_checking=dict(type='boolean', default=True),
209218
),
210219
supports_check_mode=True,
211220
)
212221

213222
pdns_client = PowerDNSClient(host=module.params['pdns_host'],
214223
port=module.params['pdns_port'],
215224
prot=module.params['pdns_prot'],
216-
api_key=module.params['pdns_api_key'])
225+
api_key=module.params['pdns_api_key'],
226+
verify=module.params['strict_ssl_checking'])
217227

218228
try:
219229
changed, zone = ensure(module, pdns_client)

0 commit comments

Comments
 (0)