
    Vhr.                         d Z dZdZddlZ	 ddlZddlmZ ddlm	Z	 ddl
mZ d Zd	 Zd
 Zd Zd Zedk(  r e        yy# e$ r Y 6w xY w)a  
---
module: s3_website
version_added: 1.0.0
short_description: Configure an s3 bucket as a website
description:
  - Configure an s3 bucket as a website
author:
  - Rob White (@wimnat)
options:
  name:
    description:
      - "Name of the s3 bucket"
    required: true
    type: str
  error_key:
    description:
      - "The object key name to use when a 4XX class error occurs. To remove an error key, set to None."
    type: str
  redirect_all_requests:
    description:
      - "Describes the redirect behavior for every request to this s3 bucket website endpoint"
    type: str
  state:
    description:
      - "Add or remove s3 website configuration"
    choices: [ 'present', 'absent' ]
    required: true
    type: str
  suffix:
    description:
      - >
        Suffix that is appended to a request that is for a directory on the website endpoint (e.g. if the suffix is index.html and you make a request to
        samplebucket/images/ the data that is returned will be for the object with the key name images/index.html). The suffix must not include a slash
        character.
    default: index.html
    type: str

extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.boto3
aM  
# Note: These examples do not set authentication details, see the AWS Guide for details.

- name: Configure an s3 bucket to redirect all requests to example.com
  community.aws.s3_website:
    name: mybucket.com
    redirect_all_requests: example.com
    state: present

- name: Remove website configuration from an s3 bucket
  community.aws.s3_website:
    name: mybucket.com
    state: absent

- name: Configure an s3 bucket as a website with index and error pages
  community.aws.s3_website:
    name: mybucket.com
    suffix: home.htm
    error_key: errors/404.htm
    state: present
a  
index_document:
    description: index document
    type: complex
    returned: always
    contains:
        suffix:
            description: suffix that is appended to a request that is for a directory on the website endpoint
            returned: success
            type: str
            sample: index.html
error_document:
    description: error document
    type: complex
    returned: always
    contains:
        key:
            description:  object key name to use when a 4XX class error occurs
            returned: when error_document parameter set
            type: str
            sample: error.html
redirect_all_requests_to:
    description: where to redirect requests
    type: complex
    returned: always
    contains:
        host_name:
            description: name of the host where requests will be redirected.
            returned: when redirect all requests parameter set
            type: str
            sample: ansible.com
        protocol:
            description: protocol to use when redirecting requests.
            returned: when redirect all requests parameter set
            type: str
            sample: https
routing_rules:
    description: routing rules
    type: list
    returned: always
    contains:
        condition:
            type: complex
            description: A container for describing a condition that must be met for the specified redirect to apply.
            contains:
                http_error_code_returned_equals:
                    description: The HTTP error code when the redirect is applied.
                    returned: always
                    type: str
                key_prefix_equals:
                    description: object key name prefix when the redirect is applied. For example, to redirect
                                 requests for ExamplePage.html, the key prefix will be ExamplePage.html
                    returned: when routing rule present
                    type: str
                    sample: docs/
        redirect:
            type: complex
            description: Container for redirect information.
            returned: always
            contains:
                host_name:
                    description: name of the host where requests will be redirected.
                    returned: when host name set as part of redirect rule
                    type: str
                    sample: ansible.com
                http_redirect_code:
                    description: The HTTP redirect code to use on the response.
                    returned: when routing rule present
                    type: str
                protocol:
                    description: Protocol to use when redirecting requests.
                    returned: when routing rule present
                    type: str
                    sample: http
                replace_key_prefix_with:
                    description: object key prefix to use in the redirect request
                    returned: when routing rule present
                    type: str
                    sample: documents/
                replace_key_with:
                    description: object key prefix to use in the redirect request
                    returned: when routing rule present
                    type: str
                    sample: documents/
    N)camel_dict_to_snake_dict)is_boto3_error_code)AnsibleCommunityAWSModulec                     i }| j                  d      }t        |      dk(  r"|d   |d<   |d   j                  dd      |d<   |S t        |      dk(  r
|d   |d<   |S t        d	      )
N:   r   Protocol   z// HostNamezRedirect URL appears invalid)splitlenreplace
ValueError)urlredirect_dict	url_splits      l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/s3_website.py_create_redirect_dictr      s    M		#I 9~$-aLj!$-aL$8$8r$Bj!  
Y1	$-aLj!  788    c                 N    i }|d|i|d<   | d| i|d<   |t        |      |d<   |S )NKeyErrorDocumentSuffixIndexDocumentRedirectAllRequestsTo)r   )suffix	error_keyredirect_all_requestswebsite_configurations       r   _create_website_configurationr!      sR    271Co.2:F1Co.(9NOd9e56  r   c                 F   |j                   j                  d      }|j                   j                  d      }|d }n|j                   j                  d      }|j                   j                  d      }d}	 |j                  |      }	 | j                  |      }

!	 j                  t        |||             d}nc	 ||
d   d   |k7  s ||
d   d   |k7  s|1|
d   t        |      k7  r 	 j                  t        |||             d}t#        j$                  d       | j                  |      }
 |j&                  dd|it)        |
       y # t        j                  j
                  t        j                  j                  f$ r}	|j                  |	d       Y d }	~	d }	~	ww xY w# t        d	      $ r d }
Y !t        j                  j
                  t        j                  j                  f$ r}	|j                  |	d
       Y d }	~	pd }	~	ww xY w# t        j                  j
                  t        j                  j                  f$ r}	|j                  |	d       Y d }	~	Bd }	~	wt        $ r&}	|j                  t        |	             Y d }	~	od }	~	ww xY w# t        j                  j
                  t        j                  j                  f$ r}	|j                  |	d       Y d }	~	d }	~	ww xY w# t         $ r| 	 j                  t        |||             d}nW# t        j                  j
                  t        j                  j                  f$ r}	|j                  |	d       Y d }	~	nd }	~	ww xY wY ht        $ r&}	|j                  t        |	             Y d }	~	d }	~	ww xY w)Nnamer   r   r   FzFailed to get bucketmsgBucketNoSuchWebsiteConfigurationz#Failed to get website configuration)WebsiteConfigurationTz*Failed to set bucket website configurationr   r   r   r   r   z-Failed to update bucket website configuration   changed )paramsgetBucketWebsitebotocore
exceptionsClientErrorBotoCoreErrorfail_json_awsget_bucket_websiter   putr!   r   	fail_jsonstrr   KeyErrortimesleep	exit_jsonr   )client_connectionresource_connectionmodulebucket_namer   r   r   r+   bucket_websiteewebsite_configs              r   "enable_or_update_bucket_as_websiterD      sy   --##F+K"MM--.EF(""8,!!+.IG<,::;GK*==[=Q 	)%B69Vk%l   G	)#(G(QU[([)n_.Me.TXa.a)5&'>?CXYnCooa"&&-J6S\^s-t '  #G 	

1&999MNFQWQ(@(PQo ++X-@-@-N-NO <Q$:;;<
 ;< '')) K 	Q$IJJ	K ##//1D1D1R1RS 	V  (T UU 	)Q((	)" !++779L9L9Z9Z[ a((0_(``a 	]]"")FvyZo)p #  ''33X5H5H5V5VW ]$$Q,[$\\] 	)Q((	)s   5E F' H =-K. +J 7F$FF$'H:6H0H		H7JI!!J-JJ7K+K& K. &K++K. .	N 8LN 7M,M'"N 'M,,N 2N :NN c                 P   d}|j                   j                  d      }	 | j                  |       	 | j                  |       d}|j	                  |       y # t        d      $ r |j	                  |       Y Jt
        j                  j                  t
        j                  j                  f$ r}|j                  |d       Y d }~d }~ww xY w# t
        j                  j                  t
        j                  j                  f$ r}|j                  |d	       Y d }~d }~ww xY w)
NFr#   r&   r(   )r+   zFailed to get bucket websiter$   TzFailed to delete bucket website)r-   r.   r5   r   r<   r0   r1   r2   r3   r4   delete_bucket_website)r=   r?   r+   r@   rB   s        r   disable_bucket_as_websiterG     s   G--##F+KD,,K,@G//{/C W% ;< *)'')) D 	Q$BCC	D ++X-@-@-N-NO GQ$EFFGs4   A C  C;6C1C		C7D%D  D%c            
      6   t        t        dd      t        ddddg      t        ddd	      t        ddd
      t        dd            } t        | ddgddgg      }	 |j                  d      }|j                  d      }|j                  j                  d      }|dk(  rt        |       y |dk(  rt        |       y y # t        j
                  j                  t        j
                  j                  f$ r}|j                  |d       Y d }~d }~ww xY w)Nr8   T)typerequiredpresentabsent)rI   rJ   choicesFz
index.html)rI   rJ   default)rI   rJ   no_log)r#   stater   r   r   r   r   r   )argument_specmutually_exclusives3zFailed to connect to AWSr$   rP   )dictAnsibleAWSModuleclientresourcer0   r1   r2   r3   r4   r-   r.   rD   rG   )rQ   r?   r=   r>   rB   rP   s         r   mainrX   *  s   ut,y(6KLEEE%@">M #$h/$k2
F@"MM$/$ood3 MMg&E	*+<>QSYZ	(	!"3V< 
 ++X-@-@-N-NO @Q$>??@s   !"C 7D;DD__main__)DOCUMENTATIONEXAMPLESRETURNr:   r0   ImportError0ansible.module_utils.common.dict_transformationsr   <ansible_collections.amazon.aws.plugins.module_utils.botocorer   >ansible_collections.community.aws.plugins.module_utils.modulesr   rU   r   r!   rD   rG   rX   __name__r,   r   r   <module>rb      s{   *X,T
l 	 V \ x !DRN&.=> zF M  		s   ? AA