
    Vh                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZmZ d dlmZ d dlmZmZ d d	lmZ d
 Z G d de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionaj  
module: cloudflare_dns
author:
  - Michael Gruener (@mgruener)
short_description: Manage Cloudflare DNS records
description:
  - 'Manages DNS records using the Cloudflare API, see the docs: U(https://api.cloudflare.com/).'
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  api_token:
    description:
      - API token.
      - Required for API token authentication.
      - "You can obtain your API token from the bottom of the Cloudflare 'My Account' page, found here: U(https://dash.cloudflare.com/)."
      - Can be specified in E(CLOUDFLARE_TOKEN) environment variable since community.general 2.0.0.
    type: str
    version_added: '0.2.0'
  account_api_key:
    description:
      - Account API key.
      - Required for API keys authentication.
      - "You can obtain your API key from the bottom of the Cloudflare 'My Account' page, found here: U(https://dash.cloudflare.com/)."
    type: str
    aliases: [account_api_token]
  account_email:
    description:
      - Account email. Required for API keys authentication.
    type: str
  algorithm:
    description:
      - Algorithm number.
      - Required for O(type=DS) and O(type=SSHFP) when O(state=present).
    type: int
  cert_usage:
    description:
      - Certificate usage number.
      - Required for O(type=TLSA) when O(state=present).
    type: int
    choices: [0, 1, 2, 3]
  comment:
    description:
      - Comments or notes about the DNS record.
    type: str
    version_added: 10.1.0
  flag:
    description:
      - Issuer Critical Flag.
      - Required for O(type=CAA) when O(state=present).
    type: int
    choices: [0, 1]
    version_added: 8.0.0
  tag:
    description:
      - CAA issue restriction.
      - Required for O(type=CAA) when O(state=present).
    type: str
    choices: [issue, issuewild, iodef]
    version_added: 8.0.0
  hash_type:
    description:
      - Hash type number.
      - Required for O(type=DS), O(type=SSHFP) and O(type=TLSA) when O(state=present).
    type: int
    choices: [1, 2]
  key_tag:
    description:
      - DNSSEC key tag.
      - Needed for O(type=DS) when O(state=present).
    type: int
  port:
    description:
      - Service port.
      - Required for O(type=SRV) and O(type=TLSA).
    type: int
  priority:
    description:
      - Record priority.
      - Required for O(type=MX) and O(type=SRV).
    default: 1
    type: int
  proto:
    description:
      - Service protocol. Required for O(type=SRV) and O(type=TLSA).
      - Common values are TCP and UDP.
    type: str
  proxied:
    description:
      - Proxy through Cloudflare network or just use DNS.
    type: bool
    default: false
  record:
    description:
      - Record to add.
      - Required if O(state=present).
      - Default is V(@) (that is, the zone name).
    type: str
    default: '@'
    aliases: [name]
  selector:
    description:
      - Selector number.
      - Required for O(type=TLSA) when O(state=present).
    choices: [0, 1]
    type: int
  service:
    description:
      - Record service.
      - Required for O(type=SRV).
    type: str
  solo:
    description:
      - Whether the record should be the only one for that record type and record name.
      - Only use with O(state=present).
      - This will delete all other records with the same record name and type.
    type: bool
  state:
    description:
      - Whether the record(s) should exist or not.
    type: str
    choices: [absent, present]
    default: present
  tags:
    description:
      - Custom tags for the DNS record.
    type: list
    elements: str
    version_added: 10.1.0
  timeout:
    description:
      - Timeout for Cloudflare API calls.
    type: int
    default: 30
  ttl:
    description:
      - The TTL to give the new record.
      - Must be between V(120) and V(2,147,483,647) seconds, or V(1) for automatic.
    type: int
    default: 1
  type:
    description:
      - The type of DNS record to create. Required if O(state=present).
      - Support for V(SPF) has been removed from community.general 9.0.0 since that record type is no longer supported by
        CloudFlare.
    type: str
    choices: [A, AAAA, CNAME, DS, MX, NS, SRV, SSHFP, TLSA, CAA, TXT]
  value:
    description:
      - The record value.
      - Required for O(state=present).
    type: str
    aliases: [content]
  weight:
    description:
      - Service weight.
      - Required for O(type=SRV).
    type: int
    default: 1
  zone:
    description:
      - The name of the Zone to work with (for example V(example.com)).
      - The Zone must already exist.
    type: str
    required: true
    aliases: [domain]
a  
- name: Create a test.example.net A record to point to 127.0.0.1
  community.general.cloudflare_dns:
    zone: example.net
    record: test
    type: A
    value: 127.0.0.1
    account_email: test@example.com
    account_api_key: dummyapitoken
  register: record

- name: Create a record using api token
  community.general.cloudflare_dns:
    zone: example.net
    record: test
    type: A
    value: 127.0.0.1
    api_token: dummyapitoken

- name: Create a record with comment and tags
  community.general.cloudflare_dns:
    zone: example.net
    record: test
    type: A
    value: 127.0.0.1
    comment: Local test website
    tags:
      - test
      - local
    api_token: dummyapitoken

- name: Create a example.net CNAME record to example.com
  community.general.cloudflare_dns:
    zone: example.net
    type: CNAME
    value: example.com
    account_email: test@example.com
    account_api_key: dummyapitoken
    state: present

- name: Change its TTL
  community.general.cloudflare_dns:
    zone: example.net
    type: CNAME
    value: example.com
    ttl: 600
    account_email: test@example.com
    account_api_key: dummyapitoken
    state: present

- name: Delete the record
  community.general.cloudflare_dns:
    zone: example.net
    type: CNAME
    value: example.com
    account_email: test@example.com
    account_api_key: dummyapitoken
    state: absent

- name: Create a example.net CNAME record to example.com and proxy through Cloudflare's network
  community.general.cloudflare_dns:
    zone: example.net
    type: CNAME
    value: example.com
    proxied: true
    account_email: test@example.com
    account_api_key: dummyapitoken
    state: present

# This deletes all other TXT records named "test.example.net"
- name: Create TXT record "test.example.net" with value "unique value"
  community.general.cloudflare_dns:
    domain: example.net
    record: test
    type: TXT
    value: unique value
    solo: true
    account_email: test@example.com
    account_api_key: dummyapitoken
    state: present

- name: Create an SRV record _foo._tcp.example.net
  community.general.cloudflare_dns:
    domain: example.net
    service: foo
    proto: tcp
    port: 3500
    priority: 10
    weight: 20
    type: SRV
    value: fooserver.example.net

- name: Create a SSHFP record login.example.com
  community.general.cloudflare_dns:
    zone: example.com
    record: login
    type: SSHFP
    algorithm: 4
    hash_type: 2
    value: 9dc1d6742696d2f51ca1f1a78b3d16a840f7d111eb9454239e70db31363f33e1

- name: Create a TLSA record _25._tcp.mail.example.com
  community.general.cloudflare_dns:
    zone: example.com
    record: mail
    port: 25
    proto: tcp
    type: TLSA
    cert_usage: 3
    selector: 1
    hash_type: 1
    value: 6b76d034492b493e15a7376fccd08e63befdad0edab8e442562f532338364bf3

- name: Create a CAA record subdomain.example.com
  community.general.cloudflare_dns:
    zone: example.com
    record: subdomain
    type: CAA
    flag: 0
    tag: issue
    value: ca.example.com

- name: Create a DS record for subdomain.example.com
  community.general.cloudflare_dns:
    zone: example.com
    record: subdomain
    type: DS
    key_tag: 5464
    algorithm: 8
    hash_type: 2
    value: B4EB5AC4467D2DFB3BAF9FB9961DC1B6FED54A58CDFAA3E465081EC86F89BFAB
a  
record:
  description: A dictionary containing the record data.
  returned: success, except on record deletion
  type: complex
  contains:
    comment:
      description: Comments or notes about the DNS record.
      returned: success
      type: str
      sample: Domain verification record
      version_added: 10.1.0
    comment_modified_on:
      description: When the record comment was last modified. Omitted if there is no comment.
      returned: success
      type: str
      sample: "2024-01-01T05:20:00.12345Z"
      version_added: 10.1.0
    content:
      description: The record content (details depend on record type).
      returned: success
      type: str
      sample: 192.0.2.91
    created_on:
      description: The record creation date.
      returned: success
      type: str
      sample: "2016-03-25T19:09:42.516553Z"
    data:
      description: Additional record data.
      returned: success, if type is SRV, DS, SSHFP TLSA or CAA
      type: dict
      sample:
        {
          "name": "jabber",
          "port": 8080,
          "priority": 10,
          "proto": "_tcp",
          "service": "_xmpp",
          "target": "jabberhost.sample.com",
          "weight": 5
        }
    id:
      description: The record ID.
      returned: success
      type: str
      sample: f9efb0549e96abcb750de63b38c9576e
    locked:
      description: No documentation available.
      returned: success
      type: bool
      sample: false
    meta:
      description: Extra Cloudflare-specific information about the record.
      returned: success
      type: dict
      sample:
        {
          "auto_added": false
        }
    modified_on:
      description: Record modification date.
      returned: success
      type: str
      sample: "2016-03-25T19:09:42.516553Z"
    name:
      description: The record name as FQDN (including _service and _proto for SRV).
      returned: success
      type: str
      sample: www.sample.com
    priority:
      description: Priority of the MX record.
      returned: success, if type is MX
      type: int
      sample: 10
    proxiable:
      description: Whether this record can be proxied through Cloudflare.
      returned: success
      type: bool
      sample: false
    proxied:
      description: Whether the record is proxied through Cloudflare.
      returned: success
      type: bool
      sample: false
    tags:
      description: Custom tags for the DNS record.
      returned: success
      type: list
      elements: str
      sample: ['production', 'app']
      version_added: 10.1.0
    tags_modified_on:
      description: When the record tags were last modified. Omitted if there are no tags.
      returned: success
      type: str
      sample: "2025-01-01T05:20:00.12345Z"
      version_added: 10.1.0
    ttl:
      description: The time-to-live for the record.
      returned: success
      type: int
      sample: 300
    type:
      description: The record type.
      returned: success
      type: str
      sample: A
    zone_id:
      description: The ID of the zone containing the record.
      returned: success
      type: str
      sample: abcede0bf9f0066f94029d2e6b73856a
    zone_name:
      description: The name of the zone containing the record.
      returned: success
      type: str
      sample: sample.com
N)AnsibleModuleenv_fallback)	urlencode)	to_nativeto_text)	fetch_urlc                 F    t        | t              s| S | j                         S N)
isinstancestrlower)params    t/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/cloudflare_dns.pylowercase_stringr     s    eS!;;=    c                   N    e Zd ZdZdZd ZddZddZddZddZ	dd	Z
d
 Zd Zy)CloudflareAPIz$https://api.cloudflare.com/client/v4Fc                    || _         |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _	        |j                  d	   | _
        |j                  d
   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        t        |j                  d         | _        |j                  d   | _        |j                  d   | _        t        |j                  d         | _        t        |j                  d         | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        t        |j                  d         | _        | j&                  dk(  r| j8                  | _        | j2                  dv r:| j4                  .| j4                  j;                  d      j=                         | _        | j2                  dk(  r+| j4                  | j4                  j=                         | _        | j2                  dk(  rv| j                   /| j                   j?                  d       sd | j                   z   | _        | j(                  /| j(                  j?                  d       sd | j(                  z   | _        | j2                  d!k(  rd| j                   /| j                   j?                  d       sd | j                   z   | _        | j                  d tA        | j                        z   | _        | j&                  jC                  | j8                        s!| j&                  dz   | j8                  z   | _        | j2                  d"k(  r7| j&                  | j8                  k(  r| j                   jE                  d#$       y y y )%N	api_tokenaccount_api_keyaccount_email	algorithm
cert_usagecomment	hash_typeflagtagtagskey_tagportpriorityprotoproxiedselectorrecordservicesolostatetimeoutttltypevalueweightzone@)CNAMENSMXSRV.AAAAr6   _TLSADSz$DS records only apply to subdomains.msg)#moduleparamsr   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r   r%   r&   r'   r(   r)   is_solor+   r,   r-   r.   r/   r0   r1   rstripr   
startswithr   endswith	fail_json)selfr>   s     r   __init__zCloudflareAPI.__init__  sW   {3%}}->?#]]?;{3 --5}}Y/{3MM&)	=='MM&)	}}Y/MM&)	j1%fmmG&<=
}}Y/j1&v}}X'>?'i(@A}}V,]]7+
}}Y/=='MM&)	]]7+
mmH-$V]]6%:;	;;#))DKII55DJJ<R**3/557DJIIdjj&<))+DJII

&1F1Fs1K 4::-
(4<<3J3J33O"T\\1II

&1F1Fs1K 4::-
		%#dii.0	{{##DII.+++dii7DKII{{dii'%%*P%Q ( r   Nc           	         | j                   rd| j                   z   dd}n| j                  | j                  dd}d }|r	 t        j                  |      }t        | j                  | j                  |z   |||| j                        \  }}|d   d	vr?| j                  j                  d
j                  ||d   |j                  d                   d}	|d   dk(  rdj                  |d   ||      }	n|d   dk(  rdj                  |d   ||      }	n{|d   dk(  rdj                  |d   ||      }	n\|d   dk(  rdj                  |d   ||      }	n=|d   dk(  rdj                  |d   ||      }	n|d   dk(  rdj                  |d   ||      }	d }
	 |j                         }|s|d   r|d   }n|	dz  }	|r!	 t        j                   t#        |d            }
|
| j                  j                  |	       d|
vr?|	d j                  |
j                  d!            z  }	| j                  j                  |	       |
d   sp|	d"z  }	|
d#   D ]G  }|	d$j                  |d%   |d&         z  }	d'|v s#|d'   D ]  }|	d$j                  |d%   |d&         z  }	 I | j                  j                  |	       |
|d   fS # t
        $ r3}| j                  j                  dt        |      z         Y d }~d }~ww xY w# t        $ r d }Y _w xY w# t%        t        dt&              $ r)}|	dj                  t        |      |      z  }	Y d }~jd }~ww xY w)(NzBearer zapplication/json)AuthorizationContent-Type)zX-Auth-Emailz
X-Auth-KeyrI   z%Failed to encode payload as JSON: %s r<   )headersdatamethodr,   status)   i0              z6Failed API call {0}; got unexpected HTTP code {1}: {2}r=    rP   zFAPI user does not have permission; Status: {0}; Method: {1}: Call: {2}rQ   zBAPI request not authenticated; Status: {0}; Method: {1}: Call: {2}rR   z?API client is rate limited; Status: {0}; Method: {1}: Call: {2}rS   zGAPI incorrect HTTP method provided; Status: {0}; Method: {1}: Call: {2}rT   zBAPI request is not valid JSON; Status: {0}; Method: {1}: Call: {2}rO   z4API bad request; Status: {0}; Method: {1}: Call: {2}bodyz; The API response was emptysurrogate_or_strict)errorsJSONDecodeErrorz2; Failed to parse API response with error {0}: {1}successz; Unexpected error details: {0}errorz; Error details: rX   zcode: {0}, error: {1}; codemessageerror_chain)r   r   r   jsondumps	Exceptionr>   rD   r	   r   cf_api_endpointr,   formatgetreadAttributeErrorloadsr
   getattr
ValueError)rE   api_callrL   payloadrJ   rK   erespinfo	error_msgresultcontentr[   chain_errors                 r   _cf_simple_api_callz!CloudflareAPI._cf_simple_api_call   s   >>!*T^^!; 2G !% 2 2"22 2G
 bzz'* t{{#33h>'.$(&,'+||5
d >!IIKK!!&^&e&efnptu}p~  AE  AI  AI  JO  AP  'Q!  R	>S `gghlmuhvx~  AI  JI(^s"\ccdhiqdrtz  }E  FI(^s"Y``aefnaoqw  zB  CI(^s"ahhimnviwy  BJ  KI(^s"\ccdhiqdrtz  }E  FI(^s"NUUVZ[cVdflnvwI	iikG F|v,;;	pGG<Q$RS
 >KK!!i!0F":AA&**WBUVVIKK!!i!0i ,,I) s6==eFmUS\M]^^	 E)',]'; s!%>%E%EkRXFY[fgp[q%rr	ss
 KK!!i!0tH~%%I  b%%*QT]^_T`*`%aabH  	G	 D"3Z@ pQXXYbcdYegnoo	psA   J/ K. 2 L  /	K+8(K&&K+.K=<K= ML<<Mc                    | j                  |||      \  }}|d   }d|v r|d   }|d   dkD  rt        |d         dz   }dj                  |      g}	d|v rJ|j                  dd      \  }
}|	|j                  d      D cg c]  }|j	                  d      r| c}z  }	n|}
||d   k  rC|
ddj                  |	      z   z  }
| j                  |
||      \  }}||d   z  }|dz  }||d   k  rC||fS c c}w )	Nrp   result_infototal_pages   pagezpage={0}?&)rs   intrc   splitrB   join)rE   rj   rL   rk   rp   rM   rK   
pagination	next_page
parametersraw_api_callqueryr   s                r   _cf_api_callzCloudflareAPI._cf_api_callV  s.   11(FGLhF".J-(1,
6 23a7	(//	:;
(?*2..a*@'L%ekk#6F"gUeN^N^_eNf5"ggJ#+L:m#<< C#((:*>$>>L%)%=%=lFT[%\NFFF8,,DNI	  :m#<< V| #hs   =C/C/c                 4   |s| j                   }| j                  |      }t        |      dkD  r+| j                  j	                  dj                  |             t        |      dk  r+| j                  j	                  dj                  |             |d   d   S )Nrw   zMore than one zone matches {0}r<   zNo zone found with name {0}r   id)r1   	get_zoneslenr>   rD   rc   )rE   r1   zoness      r   _get_zone_idzCloudflareAPI._get_zone_idn  s    99Dt$u:>KK!!&F&M&Md&S!Tu:>KK!!&C&J&J4&P!QQx~r   c                 x    |s| j                   }d}|rdt        d|i      z   }| j                  d|z         \  }}|S )NrU   ry   namez/zones)r1   r   r   )rE   r   r   r   rM   s        r   r   zCloudflareAPI.get_zones{  sG    99D)VTN33E))(U*:;vr   c                 8   |s| j                   }|s| j                  }|s| j                  }|s|| j                  }| j	                         }dj                  |      }i }|r||d<   |r||d<   |r||d<   |r|dt        |      z   z  }| j                  |      \  }}	|S )N/zones/{0}/dns_recordsr.   r   rq   ry   )r1   r.   r(   r/   r   rc   r   r   )
rE   	zone_namer.   r(   r/   zone_idrj   r   recordsrM   s
             r   get_dns_recordszCloudflareAPI.get_dns_records  s    		I99D[[F E-JJE##%+227; E&M"E&M$E)i...H++H5r   c                    i }dD ]  }||v r	||   ||<   t        | |      ||<     g }|d   }|d   }|d   dk(  rO|d   1|d   dk(  s)t        |d         dz   t        |d	         z   dz   |d   z   }|d
   dz   |d   z   dz   |d   z   }n|d   dk(  rI|d   |d   dk(  st        |d         dz   t        |d         z   dz   t        |d         z   dz   |d   z   }n|d   dk(  rE|d   |d   dk(  st        |d         dz   t        |d         z   dz   |d   j                         z   }ng|d   dk(  r_|d   C|d   dk(  s;t        |d         dz   t        |d         z   dz   t        |d         z   dz   |d   z   }|d	   dz   |d   z   dz   |d   z   }|d   rd }n|}| j                  |d         }| j	                  |d   |d   ||      }|D ]  }	|d   rc|	d   |d   k(  r|	d   |k(  r	|	d   |k(  r$d| _        | j                  j                  rB| j                  dj                  ||	d         d      \  }
}kd| _        | j                  j                  r| j                  dj                  ||	d         d      \  }
} | j
                  S )N)r#   r%   r)   r*   r.   r(   r/   r0   r1   r   r   r   r'   r"   r   r    r/   r(   r.   r6   rU   r0   	r#   r)   r7   r%   r;   r"   r   r   SSHFP r:   r   r'   r*   r1   r   rq   T/zones/{0}/dns_records/{1}r   DELETE)
rh   r   upperr   r   changedr>   
check_moder   rc   )rE   kwargsr?   r   r   rq   search_recordsearch_valuer   rrrp   rn   s               r   delete_dns_recordsz CloudflareAPI.delete_dns_records  sg   d 	5E &uu 'e 4u	5 /x(&>U"7O+vg"/DfX./$6VF^9LLtSV\]dVee"9-3fWoEKfU]N^^MF^t#7O+vg"/DfY/047#f[>Q:RRUYY\_`fgr`s\ttw{{  E  FM  N  NF^w&7O+vg"/Df[12S83vk?R;SSVYY\bcj\k\q\q\ssF^v%7O+vg"/Df\23d:S
AS=TTW[[^abhitbu^vvy}}  AG  HO  AP  P"6NS06'?BSH6RZK[[M&>L"L##F6N3&&vf~vf~}Vbc 		wBf~Fvf~5BvJ-<W^`aj^kov^v#'DL;;11'+'8'89U9\9\]dfhimfn9oqy'z#{{--#'#4#45Q5X5XY`bdeibj5kmu#vLFD		w ||r   c                 f   i }dD ]  }||v r	||   ||<   t        | |      ||<     |d   }|d   }d }|d   |d   | j                  j                  d       |d   dv r>|d   s| j                  j                  d       |d   d	k(  rd }|d   |d   |d   |d
   d}|d   dv r|d   |d<   |d   dk(  rJ|d   |d   fD ]&  }||dk(  s| j                  j                  d       ( |d   |d   |d   |d   |d
   d}|d   dk(  r|d   |d   |d   |d   |d   |d   fD ]&  }||dk(  s| j                  j                  d       ( |d   |d   |d   |d   d}|d   |d   dz   |d   z   dz   |d   z   |d
   |d}t        |d         dz   t        |d         z   dz   |d   z   }|d   dz   |d   z   dz   |d   z   }|d   dk(  r|d   |d   |d    |d   fD ]&  }||dk(  s| j                  j                  d!       ( |d   |d   |d    |d   d"}	|d   |d   |	|d
   d#}t        |d         dz   t        |d         z   dz   t        |d          z   dz   |d   z   }|d   d$k(  r|d   |d    |d   fD ]&  }||dk(  s| j                  j                  d%       ( |d   j	                         |d    |d   d&}
|d   |d   |
|d
   d#}t        |d         d'z   t        |d          z   d'z   |d   z   }|d   d(k(  r|d   |d   |d)   |d*   |d    |d   fD ]&  }||dk(  s| j                  j                  d+       ( |d   dz   |d   z   dz   |d   z   }|d)   |d*   |d    |d   d,}|d   |||d
   d#}t        |d)         dz   t        |d*         z   dz   t        |d          z   dz   |d   z   }|d   d-k(  rX|d.   |d/   |d   fD ]&  }||dk(  s| j                  j                  d0       ( |d.   |d/   |d   d1}|d   |d   ||d
   d#}d }|d2   xs d |d2<   |d3   xs g |d3<   | j                  |d4         }| j                  |d4   |d   ||      }t        |      d5kD  rh|d   d-k(  rD|D ]>  }|d6   d7   d7   k(  s|d6   d/   |d/   k(  s!|d6   d   |d   k(  s0|| j                  fc S  n| j                  j                  d8       t        |      d5k(  r|d9   }d:}|d
   |d
   |d
   k7  rd;}|d   d|v r|d   |d   k7  rd;}d|v rd|v r|d   |d   k7  rd;}d6|v rd6|v r|d6   |d6   k7  rd;}|d   d	k(  r|d<   |d<   k7  rd;}|d2   |d2   k7  rd;}t        |d3         t        |d3         k7  rd;}|rZ| j                  j                  r|}n,| j                  d=j                  ||d9   d>         d?|      \  }}d;| _        || j                  fS || j                  fS | j                  j                  r|}n%| j                  d@j                  |      dA|      \  }}d;| _        || j                  fS )BN)r#   r$   r%   r&   r)   r-   r.   r(   r/   r0   r1   r   r   r   r'   r"   r   r    r!   r   r/   r(   r.   z;You must provide a type and a record to create a new recordr<   )Ar8   r3   TXTr5   r4   z=You must provide a non-empty value to create this record typer3   r-   )r.   r   rq   r-   )r   r8   r3   r&   r5   r$   rU   z@You must provide priority and a value to create this record type)r.   r   rq   r$   r-   r6   r#   r%   r)   r0   z^You must provide port, priority, proto, service, weight and a value to create this record type)targetr#   r0   r$   r7   )r.   r   r-   rK   r   r;   r"   r   r   zUYou must provide key_tag, algorithm, hash_type and a value to create this record type)r"   r   digest_typedigest)r.   r   rK   r-   r   zLYou must provide algorithm, hash_type and a value to create this record type)fingerprintr.   r   r   r:   r   r'   zdYou must provide port, proto, cert_usage, selector, hash_type and a value to create this record type)usager'   matching_typecertificateCAAr   r    zAYou must provide flag, tag and a value to create this record type)flagsr    r/   r   r!   r1   rw   rK   r   znMore than one record already exists for the given attributes. That should be impossible, please open an issue!r   FTrq   r   r   PUTr   POST)rh   r>   rD   r   r   r   r   r   r   sortedr   r   rc   )rE   r   r?   r   r   r   
new_recordattrsrv_datads_data
sshfp_data	tlsa_datacaa_datar   r   r   
cur_record	do_updaterp   rn   s                       r   ensure_dns_recordzCloudflareAPI.ensure_dns_record  si	   w 	5E &uu 'e 4u	5 gx(
6N"x(8(@KK!!&c!d6NGG'?%%*i%j f~(# vx(!'?e}	J 6N44$*9$5Jy!&>T!
+VG_= rLdbjKK)).p)qr vx(!'?":.e}J &>U"
);VG_fU^N_aghpaqsy  {B  tC  D PLdbjKK))  /O)  PP !/v *":.	H vy)C/&/ACG&QYJZZe} 	J vh/047#fVn:MMPTTW]^eWffL"9-3fWoEKfU]N^^M&>T!	*F;,?ATV\]dVef GLdbjKK))  /F)  GG "),#K0%k2 /	G vx(e}	J vi01D83vk?R;SSVZZ]`aghsat]uux||  @F  GN  @O  OL&>W$,f[.A6'?S ~LdbjKK)).|)}~  &g446{+#K0J vx("e}	J vk23c9C{@S<TTWZZ]cdk]llL&>V#&:NPVWaPbdjkvdwy  AH  zI  J VLdbjKK))  /U)  VV #6NS06'?BSH6RZK[[M-":.!'!4%g	I v%!e}	J vl34t;c&BT>UUX\\_bcijucv_wwz~~  BH  IP  BQ  QL&>U"wH sLdbjKK)).q)rs  e}H vx( e}	J  L &y 1 9T
9#F^1r
6##F6N3&&vf~vf~}Vbc w<! f~&! 0B&z'*hw.??BvJuDUYabgYhDhmopvmwx  nA  EM  NU  EV  nV!4<<//0 %%  +[%  \w<1 JIu)
50AVE]0R 	z".Z:5MT^_iTjntu  oA  UA 	Z'i:.EJW`LaekluevLv 	*$6Z+?v&*V*<< $Iv')
90ET]I^0^ 	)$
9(== 	j()VJv4F-GG 	;;))'F#'#4#45Q5X5XY`bijkblmqbr5suz  }G  $HLFD#t||++,,;;!!F,,-E-L-LW-UW]_ijLFDt||##r   )GETNr   )NNNrU   )__name__
__module____qualname__rb   r   rF   rs   r   r   r   r   r   r    r   r   r   r     s:    <OG7RrT&l06,\|$r   r   c                     t        t        dOi dt        dddt        dgf      dt        ddddg	      d
t        dd      dt        d      dt        dg d      dt        d      dt        dddg      dt        dd      dt        d      dt        dddg      dt        dg d      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dg      d*t        d      d+t        d$      d,t        dd-d.d-g/      d0t        dd1!      d2t        dd!      d3t        dg d4      d5t        dd6g7      d8t        dd!      d9t        ddd:g;      dd,d-g d<fd,d.d%gfd3d=d"d*gfd3d>d"dgfd3d?ddgfg@      } | j                  d   s0| j                  d   r| j                  d
   s| j	                  dAB       | j                  d3   d=k(  r| j                  d8   0| j                  d   !| j                  d5   | j                  d5   dCk(  rQ| j                  d8   0| j                  d   !| j                  d5   $| j                  d5   dCk(  s| j	                  dDB       | j                  d3   dEk(  r| j                  d   0| j                  d   !| j                  d5   | j                  d5   dCk(  rQ| j                  d   0| j                  d   !| j                  d5   $| j                  d5   dCk(  s| j	                  dFB       | j                  d3   d>k(  r| j                  d   ?| j                  d)   0| j                  d   !| j                  d5   | j                  d5   dCk(  r`| j                  d   ?| j                  d)   0| j                  d   !| j                  d5   $| j                  d5   dCk(  s| j	                  dGB       | j                  d3   d?k(  r| j                  d   0| j                  d   !| j                  d5   | j                  d5   dCk(  rQ| j                  d   0| j                  d   !| j                  d5   $| j                  d5   dCk(  s| j	                  dHB       | j                  d3   dIk(  r| j                  d   ?| j                  d   0| j                  d   !| j                  d5   | j                  d5   dCk(  r`| j                  d   ?| j                  d   0| j                  d   !| j                  d5   $| j                  d5   dCk(  s| j	                  dJB       d}t        |       }|j                  r!|j                  d.k(  r| j	                  dKB       |j                  d-k(  ry|j                  r|j                  |j                  L      }|j                         \  }}t        |t              r| j                  |d%|d   iM       | j                  |d%|iM       y |j                  dL      }| j                  |N       y )PNr   r   FTCLOUDFLARE_TOKEN)r.   requiredno_logfallbackr   account_api_token)r.   r   r   aliasesr   )r.   r   r   r{   )r.   r   )r   rw         )r.   choicesr   r   rw   r   r"   )r.   r   r#   r   r   r    )issue	issuewildiodefr!   list)r.   elementsr$   )r.   defaultr%   r&   boolr(   r2   r   )r.   r   r   r'   r)   r*   r+   presentabsent)r.   r   r   r,      r-   r.   )r   r8   r3   r;   r5   r4   r6   r   r:   r   r   r/   rq   )r.   r   r0   r1   domain)r.   r   r   )r(   r.   r/   r6   r:   r   )argument_specsupports_check_moderequired_ifzJEither api_token or account_api_key and account_email params are required.r<   rU   zXFor SRV records the params weight, port and value all need to be defined, or not at all.r   zbFor SSHFP records the params algorithm, hash_type and value all need to be defined, or not at all.zlFor TLSA records the params cert_usage, selector, hash_type and value all need to be defined, or not at all.zUFor CAA records the params flag, tag and value all need to be defined, or not at all.r;   zhFor DS records the params key_tag, algorithm, hash_type and value all need to be defined, or not at all.z-solo=true can only be used with state=present)r*   )r   rp   )r   r   )r   dictr   r?   rD   r   r@   r+   r   r   r   r   	exit_json)r>   r   cf_apirp   s       r   mainr     s     
&);(<=	 
 !eeDSfRgh 
 EE: 
 & 
 = 
 e$ 
 1v6 
 eE2 
 5! 
 51a&1 
  %)HI! 
" 6E2# 
$ ua0% 
& E"' 
( fe4) 
* UC&B+ 
, uq!f5- 
. e$/ 
0 6"1 
2 E9x>ST3 
4 eR05 
6 %+7 
8 5*xy9 
: EI;7; 
< UA.= 
> 54(D? 
B !i!<=h
+UWi01Vgv./UVUO,
G*FX ==%v}}=N/OTZTaTabqTrij}}V%x(4v9N9Z --08FMM'<RVX<XMM(+3f8M8Uw/76==;QUW;W!{|}}V'{+7FMM+<V<b --08FMM'<RVX<XMM+.66==;U;]w/76==;QUW;W  "F  G}}V&|,8V]]:=V=bgmgtgt  vA  hB  hN --08FMM'<RVX<XMM,/7FMM*<U<]bhbobop{b|  cEw/76==;QUW;W  "P  Q}}V%v&2v}}U7K7W --08FMM'<RVX<XMM&)1fmmE6J6Rw/76==;QUW;W!xy}}V$y)5&--:T:`ekerers~e  fL --08FMM'<RVX<XMM),4{9S9[`f`m`mny`z  aCw/76==;QUW;W  "L  MG6"F ~~&,,(2LM ||y >>//V^^/DG 224fd#Whq	5JK(F1CD +++7)r   __main__)
__future__r   r   r   r.   __metaclass__DOCUMENTATIONEXAMPLESRETURNr_   ansible.module_utils.basicr   r   +ansible.module_utils.six.moves.urllib.parser   +ansible.module_utils.common.text.convertersr	   r
   ansible.module_utils.urlsr   r   objectr   r   r   r   r   r   <module>r      sn    A @jXCJv
p  B A J /G$F G$Tg*T zF r   