
    VhvA                     (   d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlmZ  G d d      Zg Zd	Z	 d d
lmZmZ d dlmZ d dlmZ d dlmZmZ dZd dlmZm Z m!Z! d Z"e#dk(  r e"        yy# e$ r# ej9                   e	j:                                Y Bw xY w)    )absolute_importdivisionprint_functiona
  
module: dnsimple
short_description: Interface with dnsimple.com (a DNS hosting service)
description:
  - 'Manages domains and records using the DNSimple API, see the docs: U(http://developer.dnsimple.com/).'
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  account_email:
    description:
      - Account email. If omitted, the environment variables E(DNSIMPLE_EMAIL) and E(DNSIMPLE_API_TOKEN) will be looked for.
      - 'If those variables are not found, a C(.dnsimple) file will be looked for, see: U(https://github.com/mikemaccana/dnsimple-python#getting-started).'
      - C(.dnsimple) config files are only supported in dnsimple-python<2.0.0.
    type: str
  account_api_token:
    description:
      - Account API token. See O(account_email) for more information.
    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 DNSimple.
      - If omitted, a list of domains will be returned.
      - If domain is present but the domain does not exist, it will be created.
    type: str
  record:
    description:
      - Record to add, if blank a record for the domain will be created, supports the wildcard (*).
    type: str
  record_ids:
    description:
      - List of records to ensure they either exist or do not exist.
    type: list
    elements: str
  type:
    description:
      - The type of DNS record to create.
    choices:
      - A
      - ALIAS
      - CNAME
      - MX
      - SPF
      - URL
      - TXT
      - NS
      - SRV
      - NAPTR
      - PTR
      - AAAA
      - SSHFP
      - HINFO
      - POOL
      - CAA
    type: str
  ttl:
    description:
      - The TTL to give the new record in seconds.
    default: 3600
    type: int
  value:
    description:
      - Record value.
      - Must be specified when trying to ensure a record exists.
    type: str
  priority:
    description:
      - Record priority.
    type: int
  state:
    description:
      - Whether the record should exist or not.
    choices: ['present', 'absent']
    default: present
    type: str
  solo:
    description:
      - Whether the record should be the only one for that record type and record name.
      - Only use with O(state) is set to V(present) on a record.
    type: 'bool'
    default: false
  sandbox:
    description:
      - Use the DNSimple sandbox environment.
      - Requires a dedicated account in the dnsimple sandbox environment.
      - Check U(https://developer.dnsimple.com/sandbox/) for more information.
    type: 'bool'
    default: false
    version_added: 3.5.0
requirements:
  - "dnsimple >= 2.0.0"
author: "Alex Coomans (@drcapulet)"
a  
- name: Authenticate using email and API token and fetch all domains
  community.general.dnsimple:
    account_email: test@example.com
    account_api_token: dummyapitoken
  delegate_to: localhost

- name: Delete a domain
  community.general.dnsimple:
    domain: my.com
    state: absent
  delegate_to: localhost

- name: Create a test.my.com A record to point to 127.0.0.1
  community.general.dnsimple:
    domain: my.com
    record: test
    type: A
    value: 127.0.0.1
  delegate_to: localhost
  register: record

- name: Delete record using record_ids
  community.general.dnsimple:
    domain: my.com
    record_ids: '{{ record["id"] }}'
    state: absent
  delegate_to: localhost

- name: Create a my.com CNAME record to example.com
  community.general.dnsimple:
    domain: my.com
    record: ''
    type: CNAME
    value: example.com
    state: present
  delegate_to: localhost

- name: Change TTL value for a record
  community.general.dnsimple:
    domain: my.com
    record: ''
    type: CNAME
    value: example.com
    ttl: 600
    state: present
  delegate_to: localhost

- name: Delete the record
  community.general.dnsimple:
    domain: my.com
    record: ''
    type: CNAME
    value: example.com
    state: absent
  delegate_to: localhost
#N)LooseVersionc                   ^    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
dd
Zd ZddZddZd Zy	)
DNSimpleV2z%class which uses dnsimple-python >= 2c                     || _         || _        || _        || _        d| _        | j                          | j                          y)init   N)moduleaccount_emailaccount_api_tokensandboxpagination_per_pagednsimple_clientdnsimple_account)selfr   r   r   r   s        n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/dnsimple.py__init__zDNSimpleV2.__init__   s@    *!2#%     c                     | j                   r:| j                  r.t        | j                  | j                   | j                  d      }nd}t	        |      |j
                  j                          || _        y)z creates a dnsimple client objectzansible/community.general)r   emailaccess_token
user_agentzOption account_email or account_api_token not provided. Dnsimple authentication with a .dnsimple config file is not supported with dnsimple-python>=2.0.0N)r   r   Clientr   DNSimpleExceptionidentitywhoamiclient)r   r    msgs      r   r   zDNSimpleV2.dnsimple_client   sg    $"8"8DLL8J8JY]YoYo  }X  YF:C $C(( r   c                     | j                   j                  j                         j                  j                  }|sMt        | j                         j                         j                  }t        |      dk7  rd}t        |      |d   }|| _        y)zselect a dnsimple account. If a user token is used for authentication,
        this user must only have access to a single account   zThe provided dnsimple token is a user token with multiple accounts.Use an account token or a user token with access to a single account.See https://support.dnsimple.com/articles/api-access-token/r   N)	r    r   r   dataaccountAccountslist_accountslenr   )r   r%   accountsr!   s       r   r   zDNSimpleV2.dnsimple_account   sy     ++&&--/44<<,::<AAH8}!R (,,qkGr   c                     | j                  | j                  j                  j                  | j                  j
                        }|D cg c]  }|j                   c}S c c}w )zreturns a list of all domains)
account_id)_get_paginated_resultr    domainslist_domainsr%   id__dict__)r   domain_listds      r   get_all_domainszDNSimpleV2.get_all_domains   sL    001D1D1Q1Q^b^j^j^m^m0n$/0q

000s   
A c                 0   	 | j                   j                  j                  | j                  j                  |      j
                  j                  }|S # t        $ r:}t        |j                        }t        j                  d|      rd}n Y d}~|S d}~ww xY w)z%returns a single domain by name or idz^Domain .+ not found$N)r    r-   
get_domainr%   r/   r$   r0   r   strmessagerematch)r   domaindreexception_strings        r   r5   zDNSimpleV2.get_domain   s    	$$//HMMVVB 	 ! 	"199~xx02BC  		s   AA 	B/BBc                     | j                   j                  j                  | j                  j                  |      j
                  j                  S )zcreate a single domain)r    r-   create_domainr%   r/   r$   r0   r   r:   s     r   r?   zDNSimpleV2.create_domain   s3    {{""00&INNWWWr   c                 x    | j                   j                  j                  | j                  j                  |       y)zdelete a single domainN)r    r-   delete_domainr%   r/   r@   s     r   rB   zDNSimpleV2.delete_domain   s$    ))$,,//6Br   Nc                     | j                  | j                  j                  j                  | j                  j
                  ||      }|D cg c]  }|j                   c}S c c}w )z:return dns resource records which match a specified filter)r+   zonefilter)r,   r    zoneslist_recordsr%   r/   r0   )r   rD   dnsimple_filterrecords_listr2   s        r   get_recordszDNSimpleV2.get_records   sT    11$++2C2C2P2P=A\\__7;O 2 U %11q

111s   A"c                 z    | j                   j                  j                  | j                  j                  ||       y)z#delete a single dns resource recordN)r    rF   delete_recordr%   r/   )r   r:   rids      r   rL   zDNSimpleV2.delete_record   s&    ''Er   c                     t        ||      }| j                  j                  j                  | j                  j
                  t        |      t        |      |      j                  j                  }|S )z#update a single dns resource record)ttlpriority)	ZoneRecordUpdateInputr    rF   update_recordr%   r/   r6   r$   r0   )r   r:   rM   rO   rP   zrresults          r   rR   zDNSimpleV2.update_record   sQ    "sX>""00#f+sSVxY[\aajjr   c                     t        |||||      }| j                  j                  j                  | j                  j
                  t        |      |      j                  j                  S )z#create a single dns resource record)nametypecontentrO   rP   )	ZoneRecordInputr    rF   create_recordr%   r/   r6   r$   r0   )r   r:   rV   record_typerX   rO   rP   rS   s           r   rZ   zDNSimpleV2.create_record  sL    $['s]ef{{  ..t||FRPUU^^^r   c                      |dd| j                   i|j                  }g }t        d|j                  dz         D ]2  } |d| j                   |d|j                  }|j                  |       4 |S )z.return all results of a paginated api responseper_pager#   )r]   page )r   
paginationrangetotal_pagesr$   extend)r   	operationoptionsrecords_paginationresult_listr^   	page_datas          r   r,   z DNSimpleV2._get_paginated_result  s    &T0H0HTGT__!/;;a?@ 	*D!Z4+C+C$ZRYZ__Iy)	* r   )N)NN)__name__
__module____qualname____doc__r   r   r   r3   r5   r?   rB   rJ   rL   rR   rZ   r,   r_   r   r   r	   r	      sE    / 
1

XC2F_
r   r	   F)r   r   )r&   )version)rQ   rY   T)AnsibleModulemissing_required_libenv_fallbackc                     t        t        t        dt        dgf      t        ddt        dgf      t        d      t        d      t        dd	      t        dg d
      t        dd      t        d      t        d      t        dddgd      t        dd      t        dd            ddggd      } t        s#| j	                  t        d      t        d          | j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      | j                  j                  d      }| j                  j                  d      | j                  j                  d       }| j                  j                  d      | j                  j                  d!      }| j                  j                  d"      }| j                  j                  d#      }| j                  j                  d$      }	t        t              j                  d   }
	 |
d%k  r| j	                  d&'       t        |||	|       }|s#|j                         }| j                  d|(       |s|j                         rt!        |      }nt#        |      }|j%                  |      }|dk(  rZ|r| j                  d|(       n| j&                  r| j                  d)       ni|j)                  |      }| j                  d|(       nD|r0| j&                  s|j+                  |       | j                  d)       n| j                  d)       Ɖs| j	                  d*'       s| j	                  d+'       |j-                  |d,i-      }t/        fd.|D        d       }|dk(  r$d}|rg|D cg c]  }|d,   k(  s|d   k(  s|d/    }}|r|D cg c]  }||d/   k7  s| }}|r'| j&                  s|D ]  }|j1                  ||        d}|rn|d    |k7  s|d!   |k7  rJ| j&                  r| j                  d)       n|j3                  ||d/   ||      }| j                  d|(       n| j                  ||(       n| j&                  r| j                  d)       nr|j5                  |||      }| j                  d|(       nH|r4| j&                  s|j1                  ||d/          | j                  d)       n| j                  d)       |r|j-                  |d -      }|D cg c]  }t#        |d/          }}|D cg c]  }t#        |       }}|dk(  rKt7        t9        |      t9        |      z
        }|r| j	                  d0|z  '       n| j                  d)       nlt7        t9        |      t9        |      z        }|r8| j&                  s|D ]  }|j1                  ||        | j                  d)       n| j                  d)       | j	                  d4'       y c c}w c c}w c c}w c c}w # t:        $ r]}|
d1kD  r | j	                  d2|j<                  z  '       n.| j	                  d2t#        |j>                  d   d3         z  '       Y d }~d }~ww xY w)5Nr6   DNSIMPLE_EMAIL)rW   fallbackTDNSIMPLE_API_TOKEN)rW   no_logrs   )rW   list)rW   elements)AALIASCNAMEMXSPFURLTXTNSSRVNAPTRPTRAAAASSHFPHINFOPOOLCAA)rW   choicesinti  )rW   defaultpresentabsent)rW   r   r   boolF)r   r   r:   record
record_idsrW   rO   valuerP   statesolor   r   r   )argument_specrequired_togethersupports_check_modednsimpler   )r!   	exceptionr   r   r:   r   rW   rO   rP   r   r   r      zxSupport for python-dnsimple < 2 has been removed in community.general 5.0.0. Update python-dnsimple to version >= 2.0.0.)r!   )changedrT   )r   zMissing the record typezMissing the record valuerV   )rH   c              3   X   K   | ]!  }|d    k(  s|d   k(  s|d   k(  s| # yw)rV   rW   rX   Nr_   ).0rr   r[   r   s     r   	<genexpr>zmain.<locals>.<genexpr>{  s7     }Q!F)v2E!F)WbJbghirgsw|g|q}s   ****r/   z!Missing the following records: %sr#   zDNSimple exception: %sr7   z Unknown what you wanted me to do) rn   dictrp   HAS_DNSIMPLE	fail_jsonro   DNSIMPLE_IMP_ERRparamsgetr   dnsimple_versionrm   r	   r3   	exit_jsonisdigitr   r6   r5   
check_moder?   rB   rJ   nextrL   rR   rZ   rv   setr   r7   args)r   r   r   r:   r   rO   rP   r   is_solor   DNSIMPLE_MAJOR_VERSIONdsall_domainstyped_domainr;   responserI   rrr   r   	same_typerM   current_recordsr2   current_record_idswanted_record_ids
differencer<   r   r[   r   s                               @@@r   mainr   !  s   E\DTCU4VW"*.-9<P;Q,RT U#U#%85 +: ; %.E"u%EIx+@)T651fe4#
( w
 !/F4 1*=IYZ[I\]MM%%o6M))*=>]]x(F]]x(F""<0J--##F+K
--

E
"CMMg&E}}  ,HMMg&Emm'Gmm	*G)*:;CCAFnW!A% O  P'8'6J ,,.KU;? >*~~"6{"6{|,B	!$$U2$>(((((6#%#3#3F#;((h(G !,,((0$$T$2$$U$3   %> ?  %? @>>&66BR>SL},}  @D  EB	!2> sQ!F)vBUZ[\bZcgrZr4 sI s4=$QS4S$Q	$Q %00'0 > " 0 0 =>"&%yC'2j>X+E!,,",,T,:')'7'74#x'XH",,T(,K(((D (((((6#%#3#3FFKQVX[]e#f((h(G !,,((D:$$T$2$$U$3  nnVTnJO8G!H1#ag,!H!H1; <AQ < <	!!#&7"83?Q;R"RS
$$)Lz)Y$Z$$U$3 "#&7"83?Q;R"RS
!,,#- :C,,VS9:$$T$2$$U$3 ;<{ !t$QF "I <$  W!A%!9AII!EF!9Cq	)@T<U!UV	Wsd   FY& Y$Y-Y4Y& <Y
YEY& Y)Y& /Y!B>Y& Y& &	[/A[[__main__)$
__future__r   r   r   rW   __metaclass__DOCUMENTATIONEXAMPLESRETURN	tracebackr8   Bansible_collections.community.general.plugins.module_utils.versionr   r	   r   r   r   r   r   dnsimple.servicer&   dnsimple.versionrm   r   dnsimple.struct.zone_recordrQ   rY   ImportErrorappend
format_excansible.module_utils.basicrn   ro   rp   r   ri   r_   r   r   <module>r      s    A @`D8t 
  	 [^ ^B  42)<RL Y X\=~ zF K  40I00234s   A) )%BB