in libcloud/dns/drivers/durabledns.py [0:0]
def create_record(self, name, zone, type, data, extra=None):
"""
Create a new record.
:param name: Record name without the domain name (e.g. www).
Note: If you want to create a record for a base domain
name, you should specify empty string ('') for this
argument.
:type name: ``str``
:param zone: Zone where the requested record is created.
:type zone: :class:`Zone`
:param type: DNS record type (A, AAAA, ...).
:type type: :class:`RecordType`
:param data: Data for the record (depends on the record type).
:type data: ``str``
:param extra: Extra attributes (e.g. 'aux', 'ttl'). (optional)
:type extra: ``dict``
:rtype: :class:`Record`
"""
if extra is None:
extra = RECORD_EXTRA_PARAMS_DEFAULT_VALUES
else:
if "aux" not in extra:
extra["aux"] = RECORD_EXTRA_PARAMS_DEFAULT_VALUES.get("aux")
if "ttl" not in extra:
extra["ttl"] = RECORD_EXTRA_PARAMS_DEFAULT_VALUES.get("ttl")
extra["ddns_enabled"] = "N"
schema_params = SCHEMA_BUILDER_MAP.get("create_record")
attributes = schema_params.get("attributes")
schema = api_schema_builder(
schema_params.get("urn_nid"), schema_params.get("method"), attributes
)
params = {
"apiuser": self.key,
"apikey": self.secret,
"zonename": zone.id,
"name": name,
"type": type,
"data": data,
}
params.update(extra)
urn = list(schema)[0]
for child in urn:
key = child.tag.split(":")[2]
if key in attributes:
if isinstance(params.get(key), int):
child.text = "%d"
else:
child.text = "%s"
# We can't insert values directly in child.text because API raises
# and exception for values that need to be integers. And tostring
# method from ElementTree can't handle int values.
skel = ensure_string(tostring(schema)) # Deal with PY3
req_data = skel % (
self.key,
self.secret,
zone.id,
name,
type,
data,
extra.get("aux"),
extra.get("ttl"),
extra.get("ddns_enabled"),
)
action = "/services/dns/createRecord.php?"
headers = {"SOAPAction": "urn:createRecordwsdl#createRecord"}
try:
response = self.connection.request(
action=action, data=req_data, method="POST", headers=headers
)
objects = response.objects
except DurableDNSException as e:
# In DurableDNS is possible to create records with same data.
# Their ID's will be different but the API does not implement
# the RecordAlreadyExist exception. Only ZoneDoesNotExist will
# be handled.
if "Zone does not exist" in e.message:
raise ZoneDoesNotExistError(zone_id=zone.id, driver=self, value=e.message)
raise e
record_item = objects[0]
record_item["name"] = name
record_item["type"] = type
record_item["data"] = data
record_item["ttl"] = extra.get("ttl")
record_item["aux"] = extra.get("aux")
record = self._to_record(record_item, zone)
return record