Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions cloudflare.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@
from collections import namedtuple

import re
import json
import yaml
import requests
import logging
import salt.exceptions

Expand Down Expand Up @@ -169,7 +166,7 @@ def pure(self):
def data(self):
if self.type == "SRV":
service, proto, name = self.name.split(".", 2)
parts = self.content.split("\t")
parts = self.content.split()
if len(parts) == 3:
# record should look like this: "priority weight port target"
# cloudflare returns: "weight port target"
Expand All @@ -187,13 +184,13 @@ def data(self):
"target": target,
}
if self.type == "CAA":
parts = self.content.split(" ")
parts = self.content.split()
flags, tag, value = parts
return {
"name": self.name,
"flags": int(flags),
"tag": tag,
"value": value[1:-1],
"value": value.strip('"'),
}

def __str__(self):
Expand Down Expand Up @@ -258,25 +255,20 @@ def _request(self, uri, method="GET", json=None):
else:
headers = {"X-Auth-Email": self.auth_email, "X-Auth-Key": self.auth_key}

if method not in ["GET", "POST", "PUT", "DELETE"]:
raise Exception("Unknown request method: {0}".format(method))

logger.info("Sending request: {0} {1} data: {2}".format(method, uri, json))

if method == "GET":
resp = requests.get(uri, headers=headers)
elif method == "POST":
resp = requests.post(uri, headers=headers, json=json)
elif method == "PUT":
resp = requests.put(uri, headers=headers, json=json)
elif method == "DELETE":
resp = requests.delete(uri, headers=headers)
else:
raise Exception("Unknown request method: {0}".format(method))
data = __utils__["json.dumps"](json) if json else None
resp = __utils__["http.query"](uri, header_dict=headers, data=data, decode=True, method=method)

if not resp.ok:
if resp.get('error'):
raise Exception(
"Got HTTP code {0}: {1}".format(resp.status_code, resp.text)
"Got HTTP code {0}: {1}".format(resp.get('status'), resp.get('error'))
)

return resp.json()
return resp['dict']

def _add_record(self, record):
self._request(
Expand Down