
    Vh*                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZ d dlmZ d dlmZ dd
ZddZddZddZddZd Zedk(  r e        y	y	)    )absolute_importdivisionprint_functiona  
module: memset_zone
author: "Simon Weald (@glitchcrab)"
short_description: Creates and deletes Memset DNS zones
notes:
  - Zones can be thought of as a logical group of domains, all of which share the same DNS records (in other words they point
    to the same IP). An API key generated using the Memset customer control panel is needed with the following minimum scope
    - C(dns.zone_create), C(dns.zone_delete), C(dns.zone_list).
description:
  - Manage DNS zones in a Memset account.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  state:
    required: true
    description:
      - Indicates desired state of resource.
    type: str
    choices: [absent, present]
  api_key:
    required: true
    description:
      - The API key obtained from the Memset control panel.
    type: str
  name:
    required: true
    description:
      - The zone nickname; usually the same as the main domain. Ensure this value has at most 250 characters.
    type: str
    aliases: [nickname]
  ttl:
    description:
      - The default TTL for all records created in the zone. This must be a valid int from U(https://www.memset.com/apidocs/methods_dns.html#dns.zone_create).
    type: int
    default: 0
    choices: [0, 300, 600, 900, 1800, 3600, 7200, 10800, 21600, 43200, 86400]
  force:
    required: false
    default: false
    type: bool
    description:
      - Forces deletion of a zone and all zone domains/zone records it contains.
a  
# Create the zone 'test'
- name: Create zone
  community.general.memset_zone:
    name: test
    state: present
    api_key: 5eb86c9196ab03919abcf03857163741
    ttl: 300
  delegate_to: localhost

# Force zone deletion
- name: Force delete zone
  community.general.memset_zone:
    name: test
    state: absent
    api_key: 5eb86c9196ab03919abcf03857163741
    force: true
  delegate_to: localhost
a  
memset_api:
  description: Zone info from the Memset API.
  returned: when state == present
  type: complex
  contains:
    domains:
      description: List of domains in this zone.
      returned: always
      type: list
      sample: []
    id:
      description: Zone ID.
      returned: always
      type: str
      sample: "b0bb1ce851aeea6feeb2dc32fe83bf9c"
    nickname:
      description: Zone name.
      returned: always
      type: str
      sample: "example.com"
    records:
      description: List of DNS records for domains in this zone.
      returned: always
      type: list
      sample: []
    ttl:
      description: Default TTL for domains in this zone.
      returned: always
      type: int
      sample: 300
)AnsibleModule)
check_zone)get_zone_id)memset_api_callNc                 \    t        | d         dkD  rd}t        j                  d||       yy)z
    Perform some validation which will be enforced by Memset's API (see:
    https://www.memset.com/apidocs/methods_dns.html#dns.zone_record_create)
    name   z5Zone name must be less than 250 characters in length.T)failedmsgstderrN)lenmodule	fail_json)argsr   s     q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/memset_zone.pyapi_validationr   w   s4     4<3H&@     c                     t               }d}t        | d   |      \  }}}t        || d         \  }}|xr | d   dk(  xs | xr | d   dk(  }||d	<   ||d
<   |S )z.
    Support for running with check mode.
    dns.zone_listapi_keyr   
api_methodr   )datar   stateabsentpresentchangedr   )dictr	   r   )	r   retvalsr   
has_failed_msgresponsezone_existscounterhas_changeds	            r   checkr)      s     fG J!0iU_!`Jh%8$v,GK  =DMX$=r{?CqW[\cWdhqWqK$GI"GHNr   c                 <   d\  }}d\  }}|s,| d   |d<   | d   |d<   d}t        | d   ||      \  }}}|sqd	}nnd
}t        | d   |      \  }	}
}|j                         D ]  }|d   | d   k(  s n d   | d   k7  r+|d   |d<   | d   |d<   d}t        | d   ||      \  }}}|sd	}d
}t        | d   |      \  }	}
}t        | d   |j                               \  }}}}|r6t               }||d<   d}t        | d   ||      \  }	}
}|j                         }||||fS )z
    At this point we already know whether the zone exists, so we
    just need to make the API reflect the desired state.
    FFNNttlr   nicknamezdns.zone_creater   r   r   payloadTr   r   idzdns.zone_update	zone_namecurrent_zoneszdns.zone_info)r	   jsonr   r!   )r   r&   r0   r(   r#   r   
memset_apir   r%   _has_failedr$   zoner'   zone_ids                 r   create_zoner:      s   
 +K OCe"6l
&
$3DOXbls$t!
CK$
&5d9oZd&e#T8MMO 	DJ4</	 ;$u+% JGDM!%[GEN*J(7Y\fpw(x%JX" !J"1$y/V`"aKx)4tF|[c[h[h[j)k&Kgw&$
&5d9oZdnu&v#T8]]_
{J33r   c                    d\  }}d\  }}|rd}t        | d   ||      \  }}	}
d}|
j                         D ]  }|d   | d   k(  s|d	z  } |d	k(  r|
j                         D ]/  }|d   | d   k(  s|d
   }t        |d         }t        |d         }1 dkD  sdkD  r'| d   du r d}d}d}t        j	                  ||||d	       d}|d
<   t        | d   ||      \  }}}
|sd}|}d}nd}d}d}nd\  }}||||fS )z
    Deletion requires extra sanity checking as the zone cannot be
    deleted if it contains domains or records. Setting force=true
    will override this behaviour.
    r+   r,   r   r   r/   r   r.   r      r1   domainsrecordsforceFz8Zone contains domains or records and force was not used.T)r   r    r   r   rczdns.zone_deleteNzAUnable to delete zone as multiple zones with the same name exist.)r	   r5   r   r   r   )r   r&   r0   r(   r#   r   r6   r   r7   r$   r%   r'   r8   r9   domain_countrecord_countr   s                    r   delete_zonerC      sm    +K OC$
&5d9oZdnu&v#T8MMO 	DJ4</1	 a<  8
#tF|3"4jG#&tI#7L#&tI#7L	8
 q L1$4$w-5:PS!
#  
KSY_de f*J#GDM(7Y\fpw(x%JX" 
 JKUC".
K{J33r   c                    t               t               }}d\  }}d\  }}}d}t        | d   |      \  }	}
}|	r'|	|d<   |
|d<   |j                  |j                  |d<   |S t        | d	   |j	                         
      \  }}
}}| d   dk(  rt        | ||      \  }}}}n| d   dk(  rt        | ||      \  }}}}||d<   ||d<   dD ]  }|t        |      ||<    |S )z
    We need to perform some initial sanity checking and also look
    up required info before handing it off to create or delete.
    r+   NNNr   r   r   r   r   r   r   r2   r   r   )r   r&   r0   r   r    )r   r   r6   )r!   r	   r   r   r5   r:   rC   eval)r   r"   r0   r#   r(   r   r6   r   r   r7   r$   r%   r&   r'   _zone_idvals                   r   create_or_deleterI      s$   
 vtvWG*J.CV !J"1$y/V`"aKx (??& (GH+6f]e]j]j]l+m(KwG}	!3>DVakr3s0
KS	g(	"3>DVakr3s0
KS"GH$GI. %?9GCL% Nr   c                     t        t        t        dddgd      t        ddd      t        ddgd      t        d	d
g dd      t        d	d	d            d      at        t        j                        } t        j                  | d<   t        |        t        j                  rt        |       }nt        |       }|d   rt        j                  di | y t        j                  di | y )NTr   r   str)requiredchoicestype)rL   rN   no_logr.   )rL   aliasesrN   Fr   )r   i,  iX  i  i  i  i   i0*  i`T  i  iQ int)rL   defaultrM   rN   bool)rL   rR   rN   )r   r   r   r-   r?   )argument_specsupports_check_mode
check_mode)r   r    )
r   r!   r   paramsrV   r   r)   rI   r   	exit_json)r   r"   s     r   mainrZ     s    y(.C%P$U4@tj\FeQ8x  @E  Fu6B
 !	F D**D +"4(x#7##7#r   __main__)NrE   )
__future__r   r   r   rN   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Aansible_collections.community.general.plugins.module_utils.memsetr   r   r	   r   r)   r:   rC   rI   rZ   __name__rW   r   r   <module>rd      sn    C B/b(
B 5 X Y ]A(+4\-4`&R$> zF r   