Source code for saltext.namecheap.modules.namecheap_domains_dns
"""Namecheap DNS Management.. versionadded:: 2017.7.0Prerequisites-------------This module uses the ``requests`` Python module to communicate to the namecheapAPI.Configuration-------------The Namecheap username, API key and URL should be set in the minion configurationfile, or in the Pillar data... code-block:: yaml namecheap.name: companyname namecheap.key: a1b2c3d4e5f67a8b9c0d1e2f3 namecheap.client_ip: 162.155.30.172 #Real url namecheap.url: https://api.namecheap.com/xml.response #Sandbox url #namecheap.url: https://api.sandbox.namecheap.xml.response"""fromsaltext.namecheap.utilsimportnamecheap__virtualname__="namecheap_domains_dns"
[docs]def__virtual__():""" Check to make sure requests and xml are installed and requests """return__virtualname__
[docs]defget_hosts(sld,tld):""" Retrieves DNS host record settings for the requested domain. returns a dictionary of information about the requested domain sld SLD of the domain name tld TLD of the domain name CLI Example: .. code-block:: bash salt 'my-minion' namecheap_domains_dns.get_hosts sld tld """opts,url=namecheap.get_opts(__salt__["config.option"],"namecheap.domains.dns.gethosts")opts["TLD"]=tldopts["SLD"]=sldresponse_xml=namecheap.get_request(url,opts)ifresponse_xmlisNone:return{}domaindnsgethostsresult=response_xml.getElementsByTagName("DomainDNSGetHostsResult")[0]returnnamecheap.xml_to_dict(domaindnsgethostsresult)
[docs]defget_list(sld,tld):""" Gets a list of DNS servers associated with the requested domain. returns a dictionary of information about requested domain sld SLD of the domain name tld TLD of the domain name CLI Example: .. code-block:: bash salt 'my-minion' namecheap_domains_dns.get_list sld tld """opts,url=namecheap.get_opts(__salt__["config.option"],"namecheap.domains.dns.getlist")opts["TLD"]=tldopts["SLD"]=sldresponse_xml=namecheap.get_request(url,opts)ifresponse_xmlisNone:return{}domaindnsgetlistresult=response_xml.getElementsByTagName("DomainDNSGetListResult")[0]returnnamecheap.xml_to_dict(domaindnsgetlistresult)
[docs]defset_hosts(sld,tld,hosts):""" Sets DNS host records settings for the requested domain. returns True if the host records were set successfully sld SLD of the domain name tld TLD of the domain name hosts Must be passed as a list of Python dictionaries, with each dictionary containing the following keys: - **hostname** - **recordtype** - One of ``A``, ``AAAA``, ``CNAME``, ``MX``, ``MXE``, ``TXT``, ``URL``, ``URL301``, or ``FRAME`` - **address** - URL or IP address - **ttl** - An integer between 60 and 60000 (default: ``1800``) Additionally, the ``mxpref`` key can be present, but must be accompanied by an ``emailtype`` key. CLI Example: .. code-block:: bash salt 'my-minion' namecheap_domains_dns.set_hosts sld tld hosts """opts,url=namecheap.get_opts(__salt__["config.option"],"namecheap.domains.dns.setHosts")opts["SLD"]=sldopts["TLD"]=tldi=1forhostrecordinhosts:str_i=str(i)opts["HostName"+str_i]=hostrecord["hostname"]opts["RecordType"+str_i]=hostrecord["recordtype"]opts["Address"+str_i]=hostrecord["address"]if"ttl"inhostrecord:opts["TTL"+str_i]=hostrecord["ttl"]if"mxpref"inhostrecord:opts["MXPref"+str_i]=hostrecord["mxpref"]opts["EmailType"]=hostrecord["emailtype"]i+=1response_xml=namecheap.post_request(url,opts)ifresponse_xmlisNone:returnFalsednsresult=response_xml.getElementsByTagName("DomainDNSSetHostsResult")[0]returnnamecheap.string_to_value(dnsresult.getAttribute("IsSuccess"))
[docs]defset_custom(sld,tld,nameservers):""" Sets domain to use custom DNS servers. returns True if the custom nameservers were set successfully sld SLD of the domain name tld TLD of the domain name nameservers array of strings List of nameservers to be associated with this domain CLI Example: .. code-block:: bash salt 'my-minion' namecheap_domains_dns.set_custom sld tld nameserver """opts,url=namecheap.get_opts(__salt__["config.option"],"namecheap.domains.dns.setCustom")opts["SLD"]=sldopts["TLD"]=tldopts["Nameservers"]=",".join(nameservers)response_xml=namecheap.post_request(url,opts)ifresponse_xmlisNone:returnFalsednsresult=response_xml.getElementsByTagName("DomainDNSSetCustomResult")[0]returnnamecheap.string_to_value(dnsresult.getAttribute("Update"))
[docs]defset_default(sld,tld):""" Sets domain to use namecheap default DNS servers. Required for free services like Host record management, URL forwarding, email forwarding, dynamic DNS and other value added services. sld SLD of the domain name tld TLD of the domain name Returns ``True`` if the domain was successfully pointed at the default DNS servers. CLI Example: .. code-block:: bash salt 'my-minion' namecheap_domains_dns.set_default sld tld """opts,url=namecheap.get_opts(__salt__["config.option"],"namecheap.domains.dns.setDefault")opts["SLD"]=sldopts["TLD"]=tldresponse_xml=namecheap.post_request(url,opts)ifresponse_xmlisNone:returnFalsednsresult=response_xml.getElementsByTagName("DomainDNSSetDefaultResult")[0]returnnamecheap.string_to_value(dnsresult.getAttribute("Updated"))