
    Vh^                         d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
Z
d dlZd dlmZmZ d dlmZ d dlmZ d dlmZ d d	lmZ  G d
 de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: dnsmadeeasy
short_description: Interface with dnsmadeeasy.com (a DNS hosting service)
description:
  - 'Manages DNS records using the v2 REST API of the DNS Made Easy service. It handles records only; there is no manipulation
    of domains or monitor/account support yet. See: U(https://www.dnsmadeeasy.com/integration/restapi/).'
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  account_key:
    description:
      - Account API Key.
    required: true
    type: str

  account_secret:
    description:
      - Account Secret Key.
    required: true
    type: str

  domain:
    description:
      - Domain to work with. Can be the domain name (for example V(mydomain.com)) or the numeric ID of the domain in DNS Made
        Easy (for example V(839989)) for faster resolution.
    required: true
    type: str

  sandbox:
    description:
      - Decides if the sandbox API should be used. Otherwise (default) the production API of DNS Made Easy is used.
    type: bool
    default: false

  record_name:
    description:
      - Record name to get/create/delete/update. If record_name is not specified; all records for the domain will be returned
        in "result" regardless of the state argument.
    type: str

  record_type:
    description:
      - Record type.
    choices: ['A', 'AAAA', 'CNAME', 'ANAME', 'HTTPRED', 'MX', 'NS', 'PTR', 'SRV', 'TXT']
    type: str

  record_value:
    description:
      - 'Record value. HTTPRED: <redirection URL>, MX: <priority> <target name>, NS: <name server>, PTR: <target name>, SRV:
        <priority> <weight> <port> <target name>, TXT: <text value>".'
      - If record_value is not specified; no changes will be made and the record will be returned in 'result' (in other words,
        this module can be used to fetch a record's current ID, type, and ttl).
    type: str

  record_ttl:
    description:
      - Record's "Time-To-Live". Number of seconds the record remains cached in DNS servers.
    default: 1800
    type: int

  state:
    description:
      - Whether the record should exist or not.
    required: true
    choices: ['present', 'absent']
    type: str

  validate_certs:
    description:
      - If V(false), SSL certificates will not be validated. This should only be used on personally controlled sites using
        self-signed certificates.
    type: bool
    default: true

  monitor:
    description:
      - If V(true), add or change the monitor. This is applicable only for A records.
    type: bool
    default: false

  systemDescription:
    description:
      - Description used by the monitor.
    default: ''
    type: str

  maxEmails:
    description:
      - Number of emails sent to the contact list by the monitor.
    default: 1
    type: int

  protocol:
    description:
      - Protocol used by the monitor.
    default: 'HTTP'
    choices: ['TCP', 'UDP', 'HTTP', 'DNS', 'SMTP', 'HTTPS']
    type: str

  port:
    description:
      - Port used by the monitor.
    default: 80
    type: int

  sensitivity:
    description:
      - Number of checks the monitor performs before a failover occurs where Low = 8, Medium = 5,and High = 3.
    default: 'Medium'
    choices: ['Low', 'Medium', 'High']
    type: str

  contactList:
    description:
      - Name or ID of the contact list that the monitor will notify.
      - The default V('') means the Account Owner.
    type: str

  httpFqdn:
    description:
      - The fully qualified domain name used by the monitor.
    type: str

  httpFile:
    description:
      - The file at the Fqdn that the monitor queries for HTTP or HTTPS.
    type: str

  httpQueryString:
    description:
      - The string in the httpFile that the monitor queries for HTTP or HTTPS.
    type: str

  failover:
    description:
      - If V(true), add or change the failover. This is applicable only for A records.
    type: bool
    default: false

  autoFailover:
    description:
      - If true, fallback to the primary IP address is manual after a failover.
      - If false, fallback to the primary IP address is automatic after a failover.
    type: bool
    default: false

  ip1:
    description:
      - Primary IP address for the failover.
      - Required if adding or changing the monitor or failover.
    type: str

  ip2:
    description:
      - Secondary IP address for the failover.
      - Required if adding or changing the failover.
    type: str

  ip3:
    description:
      - Tertiary IP address for the failover.
    type: str

  ip4:
    description:
      - Quaternary IP address for the failover.
    type: str

  ip5:
    description:
      - Quinary IP address for the failover.
    type: str

notes:
  - The DNS Made Easy service requires that machines interacting with the API have the proper time and timezone set. Be sure
    you are within a few seconds of actual time by using NTP.
  - This module returns record(s) and monitor(s) in the RV(ignore:result) element when O(state=present). These values can
    be be registered and used in your playbooks.
  - Only A records can have a O(monitor) or O(failover).
  - To add failover, the O(failover), O(autoFailover), O(port), O(protocol), O(ip1), and O(ip2) options are required.
  - To add monitor, the O(monitor), O(port), O(protocol), O(maxEmails), O(systemDescription), and O(ip1) options are required.
  - The monitor and the failover will share O(port), O(protocol), and O(ip1) options.
requirements: [hashlib, hmac]
author: "Brice Burgess (@briceburg)"
a5  
- name: Fetch my.com domain records
  community.general.dnsmadeeasy:
    account_key: key
    account_secret: secret
    domain: my.com
    state: present
  register: response

- name: Create a record
  community.general.dnsmadeeasy:
    account_key: key
    account_secret: secret
    domain: my.com
    state: present
    record_name: test
    record_type: A
    record_value: 127.0.0.1

- name: Update the previously created record
  community.general.dnsmadeeasy:
    account_key: key
    account_secret: secret
    domain: my.com
    state: present
    record_name: test
    record_value: 192.0.2.23

- name: Fetch a specific record
  community.general.dnsmadeeasy:
    account_key: key
    account_secret: secret
    domain: my.com
    state: present
    record_name: test
  register: response

- name: Delete a record
  community.general.dnsmadeeasy:
    account_key: key
    account_secret: secret
    domain: my.com
    record_type: A
    state: absent
    record_name: test

- name: Add a failover
  community.general.dnsmadeeasy:
    account_key: key
    account_secret: secret
    domain: my.com
    state: present
    record_name: test
    record_type: A
    record_value: 127.0.0.1
    failover: true
    ip1: 127.0.0.2
    ip2: 127.0.0.3

- name: Add a failover
  community.general.dnsmadeeasy:
    account_key: key
    account_secret: secret
    domain: my.com
    state: present
    record_name: test
    record_type: A
    record_value: 127.0.0.1
    failover: true
    ip1: 127.0.0.2
    ip2: 127.0.0.3
    ip3: 127.0.0.4
    ip4: 127.0.0.5
    ip5: 127.0.0.6

- name: Add a monitor
  community.general.dnsmadeeasy:
    account_key: key
    account_secret: secret
    domain: my.com
    state: present
    record_name: test
    record_type: A
    record_value: 127.0.0.1
    monitor: true
    ip1: 127.0.0.2
    protocol: HTTP # default
    port: 80 # default
    maxEmails: 1
    systemDescription: Monitor Test A record
    contactList: my contact list

- name: Add a monitor with http options
  community.general.dnsmadeeasy:
    account_key: key
    account_secret: secret
    domain: my.com
    state: present
    record_name: test
    record_type: A
    record_value: 127.0.0.1
    monitor: true
    ip1: 127.0.0.2
    protocol: HTTP # default
    port: 80 # default
    maxEmails: 1
    systemDescription: Monitor Test A record
    contactList: 1174 # contact list id
    httpFqdn: http://my.com
    httpFile: example
    httpQueryString: some string

- name: Add a monitor and a failover
  community.general.dnsmadeeasy:
    account_key: key
    account_secret: secret
    domain: my.com
    state: present
    record_name: test
    record_type: A
    record_value: 127.0.0.1
    failover: true
    ip1: 127.0.0.2
    ip2: 127.0.0.3
    monitor: true
    protocol: HTTPS
    port: 443
    maxEmails: 1
    systemDescription: monitoring my.com status
    contactList: emergencycontacts

- name: Remove a failover
  community.general.dnsmadeeasy:
    account_key: key
    account_secret: secret
    domain: my.com
    state: present
    record_name: test
    record_type: A
    record_value: 127.0.0.1
    failover: false

- name: Remove a monitor
  community.general.dnsmadeeasy:
    account_key: key
    account_secret: secret
    domain: my.com
    state: present
    record_name: test
    record_type: A
    record_value: 127.0.0.1
    monitor: false
N)strftimegmtime)AnsibleModule)	fetch_url)	urlencode)string_typesc                       e Zd Zd Zd Zd Zd ZddZd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)DME2c                    || _         || _        || _        |r1d| _        | j                   j	                  d| j                  z         nd| _        t        |      | _        d | _        d | _        d | _	        d | _
        d | _        | j                  j                         s#| j                  | j                        d   | _        dt        | j                        z   dz   | _        d| _        d	| _        y )
Nz)https://api.sandbox.dnsmadeeasy.com/V2.0/z;Sandbox is enabled. All actions are made against the URL %s)warningz!https://api.dnsmadeeasy.com/V2.0/idzdns/managed/z/recordsmonitorcontactList)moduleapisecretbaseurlwarnstrdomain
domain_map
record_maprecordsall_recordscontactList_mapisdigitgetDomainByName
record_urlmonitor_urlcontactList_url)selfapikeyr   r   sandboxr   s         q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/dnsmadeeasy.py__init__zDME2.__init__w  s    FDLKK%beieqeq%qr>DL&k# {{""$..t{{;DADK(3t{{+;;jH$,    c                 j    | j                         }| j                  |      }| j                  ||dd}|S )Nzapplication/json)zx-dnsme-apiKeyzx-dnsme-hmaczx-dnsme-requestDatezcontent-type)	_get_date_create_hashr   )r$   currTime
hashstringheaderss       r'   _headerszDME2._headers  s<    >>#&&x0
%)XX#-*2#57 r)   c                 r    t        j                  t         j                  d       t        dt	                     S )NCz%a, %d %b %Y %H:%M:%S GMT)locale	setlocaleLC_TIMEr   r   r$   s    r'   r+   zDME2._get_date  s&    -3VX>>r)   c                     t        j                  | j                  j                         |j                         t        j
                        j                         S N)hmacnewr   encodehashlibsha1	hexdigest)r$   rightnows     r'   r,   zDME2._create_hash  s6    xx**,hoo.?NXXZZr)   Nc                 `   | j                   |z   }|rt        |t              st        |      }t	        | j
                  |||| j                               \  }}|d   dvr*| j
                  j                  |d|d   d|d          	 t        j                  |      S # t        $ r i cY S w xY w)N)datamethodr/   status)         z
 returned z, with body: msgrG   )r   
isinstancer   r
   r	   r   r0   	fail_jsonjsonload	Exception)r$   resourcerB   rA   urlresponseinfos          r'   queryz
DME2.query  s    llX%
46T?D"4;;$vW[WdWdWfg$>0KK!!dS[n^bch^i&j!k	99X&& 	I	s   
B B-,B-c                 t    | j                   s| j                  d       | j                  j                  |d      S )Nr   F)r   _instMapdomainsget)r$   	domain_ids     r'   	getDomainzDME2.getDomain  ,    MM(#||	511r)   c                     | j                   s| j                  d       | j                  | j                   j                  |d            S )Nr   r   )r   rT   rX   rV   )r$   domain_names     r'   r    zDME2.getDomainByName  s5    MM(#~~doo11+qABBr)   c                 ,    | j                  dd      d   S )Nzdns/managedGETrA   )rR   r6   s    r'   
getDomainszDME2.getDomains  s    zz-/77r)   c                 t    | j                   s| j                  d       | j                  j                  |d      S )NrecordF)r   rT   r   rV   r$   	record_ids     r'   	getRecordzDME2.getRecord  rY   r)   c                    | j                   s| j                         | _         |dv r'| j                   D ]  }|d   |k(  s|d   |k(  s|c S  y|dv r}| j                   D ]m  }|dk(  r|j                  d      d   }n3|d	k(  rd
j                  |      }n|dk(  r|j                  d      d   }n|}|d   |k(  sY|d   |k(  sb|d   |k(  sk|c S  yt	        d      )N)CNAMEANAMEHTTPREDPTRnametypeF)AAAAAMXNSTXTSRVrm       ro   z"{0}"rp      valuezrecord_type not yet supported)r   
getRecordssplitformatrM   )r$   record_namerecord_typerecord_valueresultrt   s         r'   getMatchingRecordzDME2.getMatchingRecord  s   #0D>>** "&>[0VF^{5R!M" CC** "$&(..s3A6E E)#NN<8E E)(..s3A6E(E&>[0VF^{5RW]^eWfjoWo!M" ;<<r)   c                 @    | j                  | j                  d      d   S Nr]   rA   rR   r!   r6   s    r'   ru   zDME2.getRecords  s    zz$//51&99r)   c                     i }i } t        | d|j                         z   dz                D ]  }|d   ||d   <   |||d   <    t        | |dz   |       t        | |dz   |       y )NrV   sr   ri   _map)getattrtitlesetattr)r$   rj   mapresultsr{   s        r'   rT   zDME2._instMap  s|     @gdEDJJL$83$>?A 	+F"(,Cv$*GF4L!	+ 	dVmS)dSj'*r)   c                 0    t        j                  |d      S N),:)
separatorsrK   dumpsr$   rA   s     r'   prepareRecordzDME2.prepareRecord      zz$:66r)   c                 <    | j                  | j                  d|      S )NPOSTr   r   s     r'   createRecordzDME2.createRecord  s    zz$//6488r)   c                 Z    | j                  | j                  dz   t        |      z   d|      S N/PUTrR   r!   r   r$   rb   rA   s      r'   updateRecordzDME2.updateRecord  s'    zz$//C/#i.@%NNr)   c                 X    | j                  | j                  dz   t        |      z   d      S )Nr   DELETEr   ra   s     r'   deleteRecordzDME2.deleteRecord  s%    zz$//C/#i.@(KKr)   c                 X    | j                  | j                  dz   t        |      z   d      S )Nr   r]   rR   r"   r   ra   s     r'   
getMonitorzDME2.getMonitor  s'    zz$**S03y>A5IIr)   c                 Z    | j                  | j                  dz   t        |      z   d|      S r   r   r   s      r'   updateMonitorzDME2.updateMonitor	  s)    zz$**S03y>A5$OOr)   c                 0    t        j                  |d      S r   r   r   s     r'   prepareMonitorzDME2.prepareMonitor  r   r)   c                 t    | j                   s| j                  d       | j                  j                  |d      S )Nr   F)r   rT   contactListsrV   )r$   contact_list_ids     r'   getContactListzDME2.getContactList  s0    ##MM-(  $$_e<<r)   c                 @    | j                  | j                  d      d   S r~   )rR   r#   r6   s    r'   getContactlistszDME2.getContactlists  s    zz$..6v>>r)   c                     | j                   s| j                  d       | j                  | j                   j                  |d            S )Nr   r   )r   rT   r   rV   )r$   ri   s     r'   getContactListByNamezDME2.getContactListByName  s;    ##MM-(""4#7#7#;#;D!#DEEr)   r8   )__name__
__module____qualname__r(   r0   r+   r,   rR   rX   r    r^   rc   r|   ru   rT   r   r   r   r   r   r   r   r   r   r    r)   r'   r   r   u  sw    -6?[2C82=6:+79OLJP7=?Fr)   r   c                     t        t        dYi dt        dd      dt        dd      dt        d      dt        dd	
      dt        dddg      dt        d      dt        dg d      dt        d      dt        ddd      dt        dd	
      dt        d      dt        dd
      dt        dg d       d!t        d"d
      d#t        d$g d%       d&t        d       d't        d      d(t        d      d)t        d      d*t        dd	
      d+t        dd	
      d,t        d      d-t        d      d.t        d      d/t        d      d0t        d      d1t        dd	
      g d2gd*dg d3gddg d4gg5      } t        dd6d7d8d9d:      }t        d;d9d7%      }t        | j                  d   | j                  d   | j                  d   | j                  d   |       }| j                  d   }| j                  d   }| j                  d   }| j                  d   }|7|j	                         }|s| j                  d<=       | j                  d|>       |j                  |||      }	d?|i}
d@D ]0  }| j                  |   | j                  |   |
|t        dA      d  <   2 |
dB   dCk(  r4|
dD   j                  dE      dF   |
dG<   |
dD   j                  dE      d   |
dD<   |
dB   dHk(  rh|
dD   j                  dE      dF   |
dI<   |
dD   j                  dE      d   |
dJ<   |
dD   j                  dE      d6   |
d!<   |
dD   j                  dE      d7   |
dD<   t               }t               }|	r-|	dB   dKk(  r%|	j                  d      r|j                  |	dL         }dMD ]  }| j                  |   |dk(  r|| j                  |      |dN<   .|d#k(  r|| j                  |      ||<   I|d&k(  rp| j                  |   }|j                         sK|dk7  rF|j                  |      }|s!| j                  dOj                  |      =       |j                  dLd      }||dP<   | j                  |   ||<    d}|	rG|
D ]1  }t        |	|         j!                  dQ      t        |
|         k7  s0d}3 t        |	dL         |
dL<   d}|r3|D ].  }t        |j                  |            t        ||         k7  s-d}0 |dk(  rtdD|
vrF|	s&| j                  dR|dS| j                  d   dT=       | j                  dt        |	|U      >       |	s|j#                  |j%                  |
            }|j                  d      rH|dKk(  rC|j'                  |dL   |j)                  |            }| j                  dt        ||U      >       n| j                  dt        ||U      >       d}|r&|j+                  |	dL   |j%                  |
             d}|r&|j'                  |dV   |j)                  |             d}|r| j                  dt        |
|U      >       | j                  dt        |	|U      >       y |dk(  r=d}|	r&|j-                  |	dL          | j                  dW       | j                  |W       y | j                  dX|z  =       y )ZNaccount_keyT)requiredno_logaccount_secretr   )r   r&   Fbool)defaultrj   statepresentabsent)r   choicesrx   ry   )
rk   rl   re   rf   rg   rm   rn   rh   rp   ro   rz   
record_ttli  int)r   r   rj   r   systemDescription )r   	maxEmailsrr   protocolHTTP)TCPUDPr   DNSSMTPHTTPS)r   r   portP   sensitivityMedium)Lowr   Highr   httpFqdnhttpFilehttpQueryStringfailoverautoFailoverip1ip2ip3ip4ip5validate_certs)rz   r   ry   )r   r   r   r   r   )r   r   r   r   r   )argument_specrequired_togetherrequired_if   rs               zYThe requested domain name is not accessible with this api_key; try using its ID if known.rH   )changedr{   ri   )rz   ry   r   record_rj   rm   rt   rq   r   mxLevelrp   priorityweightrk   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   
protocolIdzContact list {0} does not existcontactListId"zA record with name 'z' does not exist for domain 'z.')r`   r   recordId)r   z/'%s' is an unknown value for the state argumentr   )r   dictr   paramsru   rJ   	exit_jsonr|   lenrv   rV   r   r   r   rw   r   stripr   r   r   r   r   r   )r   	protocolssensitivitiesDMEr   rx   ry   rz   domain_recordscurrent_record
new_recordicurrent_monitornew_monitorr   contact_listrecord_changedmonitor_changedr`   r   updatedr   s                         r'   mainr   #  s    
d48
d;
 &
 V4	

 y(.CD
 e,
 e 6h i
 u-
 UDuE
 V4
 #2.
 151
 &2`a
 bu-
  X7PQ!
" T*#
$ 5)%
& 5)'
( !%0)
* %f5+
, e&9-
. e$/
0 e$1
2 e$3
4 e$5
6 e$7
8  6:9
> :
 QR[\
E&FP qqBIQqq1M
v}}]+V]].!"(--"96==;SU[]CMM'"E--.K--.K==0L )o  q~> **;\RN+&J: >}}Q'-3]]1-=JqY)*> &T! *7 3 9 9# >q A
9(177<Q?
7 &U"!+G!4!:!:3!?!B
:)'288=a@
8'066s;A>
6(177<Q?
7 fO&K.0C7N<N<Ny<Y..)=>M 2 =='J,5fmmA6F,GL)m#!.v}}Q/?!@Am#"(--"2&..0_5J#&#;#;O#LL'((-N-U-UVe-f(g&2&6&6tR&@O/>O, "(q!1A+20 N 	&A >!$%++C0C
14FF!%		&
 ~d34
4O 	'A?&&q)*c+a..AA"&	'
 	*$!  U`bhbobopxbyz ! |U4~Wf3gh %%c&7&7
&CDFy)kS.@++F4L#:L:L[:YZ  d&RY6Z [  d&Ra6b c ^D133D3DZ3PQGoj93;M;Mk;Z[GT$jR]2^_ 	t>Sb/cd	(	^D12T* 	) 	AEI 	 	Kr)   __main__)
__future__r   r   r   rj   __metaclass__DOCUMENTATIONEXAMPLESrK   r<   r9   r3   timer   r   ansible.module_utils.basicr   ansible.module_utils.urlsr	   +ansible.module_utils.six.moves.urllib.parser
   ansible.module_utils.sixr   objectr   r   r   r   r)   r'   <module>r	     so    A @}~X|     ! 4 / A 1gF6 gF\hKV zF r)   