
    Vhd                         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 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_functiona8  
module: pulp_repo
author: "Joe Adams (@sysadmind)"
short_description: Add or remove Pulp repos from a remote host
description:
  - Add or remove Pulp repos from a remote host.
  - Note, this is for Pulp 2 only.
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  add_export_distributor:
    description:
      - Whether or not to add the export distributor to new C(rpm) repositories.
    type: bool
    default: false
  feed:
    description:
      - Upstream feed URL to receive updates from.
    type: str
  force_basic_auth:
    description:
      - C(httplib2), the library used by the M(ansible.builtin.uri) module only sends authentication information when a webservice
        responds to an initial request with a 401 status. Since some basic auth services do not properly send a 401, logins
        will fail. This option forces the sending of the Basic authentication header upon initial request.
    type: bool
    default: false
  generate_sqlite:
    description:
      - Boolean flag to indicate whether sqlite files should be generated during a repository publish.
    required: false
    type: bool
    default: false
  feed_ca_cert:
    description:
      - CA certificate string used to validate the feed source SSL certificate. This can be the file content or the path to
        the file.
    type: str
    aliases: [importer_ssl_ca_cert]
  feed_client_cert:
    description:
      - Certificate used as the client certificate when synchronizing the repository. This is used to communicate authentication
        information to the feed source. The value to this option must be the full path to the certificate. The specified file
        may be the certificate itself or a single file containing both the certificate and private key. This can be the file
        content or the path to the file.
    type: str
    aliases: [importer_ssl_client_cert]
  feed_client_key:
    description:
      - Private key to the certificate specified in O(feed_client_cert), assuming it is not included in the certificate file
        itself. This can be the file content or the path to the file.
    type: str
    aliases: [importer_ssl_client_key]
  name:
    description:
      - Name of the repo to add or remove. This correlates to repo-id in Pulp.
    required: true
    type: str
    aliases: [repo]
  proxy_host:
    description:
      - Proxy URL setting for the pulp repository importer. This is in the format V(scheme://host).
    required: false
    default:
    type: str
  proxy_port:
    description:
      - Proxy port setting for the pulp repository importer.
    required: false
    default:
    type: str
  proxy_username:
    description:
      - Proxy username for the pulp repository importer.
    required: false
    default:
    type: str
  proxy_password:
    description:
      - Proxy password for the pulp repository importer.
    required: false
    default:
    type: str
  publish_distributor:
    description:
      - Distributor to use when O(state=publish). The default is to publish all distributors.
    type: str
  pulp_host:
    description:
      - URL of the pulp server to connect to.
    default: https://127.0.0.1
    type: str
  relative_url:
    description:
      - Relative URL for the local repository. It's required when state=present.
    type: str
  repo_type:
    description:
      - Repo plugin type to use (that is, V(rpm), V(docker)).
    default: rpm
    type: str
  repoview:
    description:
      - Whether to generate repoview files for a published repository. Setting this to V(true) automatically activates O(generate_sqlite).
    required: false
    type: bool
    default: false
  serve_http:
    description:
      - Make the repo available over HTTP.
    type: bool
    default: false
  serve_https:
    description:
      - Make the repo available over HTTPS.
    type: bool
    default: true
  state:
    description:
      - The repo state. A state of V(sync) will queue a sync of the repo. This is asynchronous but not delayed like a scheduled
        sync. A state of V(publish) will use the repository's distributor to publish the content.
    default: present
    choices: ["present", "absent", "sync", "publish"]
    type: str
  url_password:
    description:
      - The password for use in HTTP basic authentication to the pulp API. If the O(url_username) parameter is not specified,
        the O(url_password) parameter will not be used.
  url_username:
    description:
      - The username for use in HTTP basic authentication to the pulp API.
  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
  wait_for_completion:
    description:
      - Wait for asynchronous tasks to complete before returning.
    type: bool
    default: false
notes:
  - This module can currently only create distributors and importers on rpm repositories. Contributions to support other repo
    types are welcome.
extends_documentation_fragment:
  - ansible.builtin.url
  - community.general.attributes
aT  
- name: Create a new repo with name 'my_repo'
  community.general.pulp_repo:
    name: my_repo
    relative_url: my/repo
    state: present

- name: Create a repo with a feed and a relative URL
  community.general.pulp_repo:
    name: my_centos_updates
    repo_type: rpm
    feed: http://mirror.centos.org/centos/6/updates/x86_64/
    relative_url: centos/6/updates
    url_username: admin
    url_password: admin
    force_basic_auth: true
    state: present

- name: Remove a repo from the pulp server
  community.general.pulp_repo:
    name: my_old_repo
    repo_type: rpm
    state: absent
z{
repo:
  description: Name of the repo that the action was performed on.
  returned: success
  type: str
  sample: my_repo
N)sleep)AnsibleModule)	fetch_url)url_argument_specc                   |    e Zd ZdZd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y)pulp_serverz.
    Class to interact with a Pulp server
    c                 Z    || _         || _        || _        t               | _        || _        y )N)modulehost	repo_typedict
repo_cachewait_for_completion)selfr   	pulp_hostr   r   s        o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/pulp_repo.py__init__zpulp_server.__init__   s(    	"&#6     c                 F    	 | j                  |       y# t        $ r Y yw xY w)NTF)get_repo_config_by_id
IndexError)r   repo_ids     r   check_repo_existszpulp_server.check_repo_exists   s.    	&&w/   		s    	  c                     | j                  |      }|d   D ]@  }|j                         D ]+  \  }}||d   j                         vr  y|d   |   |k(  r*  y B y)NdistributorsconfigFTr   itemskeys)r   r   kwargsrepo_configdistributorkeyvalues          r   compare_repo_distributor_configz+pulp_server.compare_repo_distributor_config   sr    009&~6 	!K$lln !
Uk(388:: "8,S1U: !	! r   c                     | j                  |      }|d   D ]C  }|j                         D ].  \  }}|	||d   j                         vr  y|d   |   |k(  r-  y E y)N	importersr   FTr    )r   r   r#   r$   importerr&   r'   s          r   compare_repo_importer_configz(pulp_server.compare_repo_importer_config   sx    009#K0 	%H$lln %
U$(8"4"9"9";;$#H-c2e;$%	% r   Nc                    d| j                   z  }t               }||d<   g |d<   | j                  dk(  r7t               }d|d<   d|d<   d|d	<   t               |d
<   ||d
   d<   ||d
   d<   ||d
   d<   ||d
   d<   |xs ||d
   d<   |d   j                  |       |rft               }d|d<   d|d<   d|d	<   t               |d
<   ||d
   d<   ||d
   d<   ||d
   d<   ||d
   d<   |xs ||d
   d<   |d   j                  |       d|d<   t               |d<   |r||d   d<   |r||d   d<   |r||d   d<   |	r|	|d   d<   |
r|
|d   d<   |r||d   d<   |r||d   d<   |r||d   d<   ddi|d<   t	        | j
                  |t        j                  |      d !      \  }}|d"   d#k7  r&| j
                  j                  d$|d"   |d%   |&       y y)'Nz%s/pulp/api/v2/repositories/idr   rpmyum_distributordistributor_iddistributor_type_idTauto_publishdistributor_confighttphttpsrelative_urlrepoviewgenerate_sqliteexport_distributorFyum_importerimporter_type_idimporter_configfeed
proxy_host
proxy_portproxy_usernameproxy_passwordssl_ca_certssl_client_certssl_client_keyz
_repo-typezrpm-reponotesPOSTdatamethodstatus   zFailed to create repo.msgrM   status_coderesponseurl)	r   r   r   appendr   r   jsondumps	fail_json)r   r   r7   r>   r9   
serve_httpserve_httpsr?   r@   rA   rB   r8   rC   rD   rE   add_export_distributorrQ   rI   r0   r:   rP   infos                         r   create_repozpulp_server.create_repo   s   $ -tyy8vT
!^>>U""fO0AO,-5FO12.2ON+48FO01<FO01&9=HO01':DPO01.A@HO01*=GVGbZbO012CD ''8%%)V"7K"#34<P"#895:">2;?6"#78CM"#78@DO"#78AKW"#78HGO"#78DN]Niai"#789JK^$++,>?'5D#$&*fD"#26&'/8B&'58B&'5<J&'(89<J&'(899D&'6=L&'(9:<J&'(89 jDM #KKD!	$ >S KK!!, Ne	 "  r   c                    | j                   d|d}t        | j                  |dd      \  }}|d   dk7  r%| j                  j                  d|d   |d	   |
       | j                  r$| j                  t        j                  |             y)N/pulp/api/v2/repositories// DELETErH   rK      zFailed to delete repo.rM   rN   Tr   r   r   rU   r   verify_tasks_completedrS   loadr   r   rQ   rP   rY   s        r   delete_repozpulp_server.delete_repo^  s    3799gF"4;;"XN$>S KK!!, Ne	 "  ##''		((;<r   c                     || j                   j                         vr4| j                  D cg c]  }|d   |k(  s| }}|d   | j                   |<   | j                   |   S c c}w )Nr.   r   )r   r"   	repo_list)r   r   x
repo_arrays       r   r   z!pulp_server.get_repo_config_by_idn  s`    $//..00%)^^Jqw'7I!JJJ'1!}DOOG$w'' Ks
   A A c           	         | j                   d|d}|| j                  |      }|d   D ]u  }t               }|d   |d<   t        | j                  |t        j                  |      d      \  }}|d   dk7  sM| j                  j                  d	|d   |d
   ||d          w nlt               }||d<   t        | j                  |t        j                  |      d      \  }}|d   dk7  r&| j                  j                  d|d   |d
   ||       | j                  r$| j                  t        j                               y)Nr\   z/actions/publish/r   r.   rG   rH   rK   r`   zFailed to publish the repo.rM   )rM   rO   rP   rQ   r%   zFailed to publish the repoT)r   r   r   r   r   rS   rT   rU   r   rb   rc   )	r   r   publish_distributorrQ   r$   r%   rI   rP   rY   s	            r   publish_repozpulp_server.publish_repou  sS   CG99gV &44W=K*>: 7v(.T
!*KKD)!	"#$ >S(KK))9$(N!%e$/$5 * 77" 6D,DJ&ZZ%	NHd H~$%%4 $X!%[ 3 & 5 ##''		((;<r   c                    | j                   d|d}t        | j                  |dd      \  }}|d   dk7  r%| j                  j                  d|d   |d	   |
       | j                  r$| j                  t        j                  |             y)Nr\   z/actions/sync/r^   rG   rH   rK   r`   z&Failed to schedule a sync of the repo.rM   rN   Tra   rd   s        r   	sync_repozpulp_server.sync_repo  s    @D		7S"4;;"VL$>S KK!!< Ne	 "  ##''		((;<r   c                    | j                   d|d}| j                  |      }|d   D ]  }||d   d}t               }t               |d<   |j                         D ]  \  }}	|	|d   |<    t	        | j
                  |t        j                  |      d      \  }
}|d	   d
k7  s|| j
                  j                  d|d	   |d   |        y )Nr\   z/distributors/r   r.   r]   r4   PUTrH   rK   r`   z2Failed to set the relative url for the repository.rM   rN   )	r   r   r   r!   r   r   rS   rT   rU   )r   r   r#   rQ   r$   r%   distributor_urlrI   r&   r'   rP   rY   s               r   update_repo_distributor_configz*pulp_server.update_repo_distributor_config  s    @D		7S009&~6 	K),k$.?@O6D)-D%&$lln 8
U27)*3/8 'ZZ%	NHd H~$%%L $X!%[	 & 	r   c                 ~   | j                   d|d}t               }t               }|j                         D ]  \  }}|	|||<    ||d<   | j                  dk(  rd|d<   t	        | j
                  |t        j                  |      d      \  }}	|	d	   d
k7  r'| j
                  j                  d|	d	   |	d   ||       y y )Nr\   z/importers/r=   r/   r;   r<   rG   rH   rK   r`   z-Failed to set the repo importer configurationrM   )rM   rO   rP   r=   rQ   )	r   r   r!   r   r   r   rS   rT   rU   )
r   r   r#   rQ   rI   r=   r&   r'   rP   rY   s
             r   update_repo_importer_configz'pulp_server.update_repo_importer_config  s    =AYYPv& ,,. 	-JC ',$	- #2>>U"'5D#$"KKD!	$ >S KK!!C Ne / "  !r   c                     d| j                   z  }t        | j                  |d      \  }}|d   dk7  r%| j                  j                  d|d   |d   |       t	        j
                  |      | _        y )	Nz)%s/pulp/api/v2/repositories/?details=trueGET)rJ   rK      zRequest failedrM   rN   )r   r   r   rU   rS   rc   rg   )r   rQ   rP   rY   s       r   set_repo_listzpulp_server.set_repo_list  so    9DIIE"4;;EB$>S KK!!$ Ne	 "  8,r   c                 v   |d   D ]  }| j                   |d   }	 t        | j                  |dd      \  }}|d   dk7  r%| j                  j                  d	|d   |d
   |       t	        j
                  |      }|d   dk(  r y|d   dk(  r | j                  j                  d|d          t        d        y )Nspawned_tasks_hrefTr^   rv   rH   rK   rw   z"Failed to check async task status.rM   rN   statefinishederrorz%Asynchronous task failed to complete.)rM   r~      )r   r   r   rU   rS   rc   r   )r   response_dicttasktask_urlrP   rY   	task_dicts          r   rb   z"pulp_server.verify_tasks_completed  s    !/2 	D!%DM:H!*KK 	""$ >S(KK))@$(N!%e$	 * & !IIh/	W%3W%0KK)).U]fgn]o)pa- 	r   )F)NFFTNNNNFNNNF)__name__
__module____qualname____doc__r   r   r(   r,   rZ   re   r   rl   rn   rr   rt   rx   rb    r   r   r   r      st    7$ $!]~ (+Z 28-r   r   c                      t               } | j                  t        dd      t               t        dd      t        dg      t        dg      t        dgd	      t        dd
g      t               t               t               t        d      t               t        d      t               t        d      t        dd      t        dd      t        dd      t        dg d      t        dd             t        | 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    }|j                  d!   }|j                  d"   }|j                  d#   }|j                  d$   }|j                  d%   }|j                  d&   }|j                  d'   }|j                  d(   }|dk(  r|s|j                  d)*       |ct        j                  j                  |      }t        j                  j                  |      r%t        |d+      5 }|j                         }d d d        |ct        j                  j                  |      }t        j                  j                  |      r%t        |d+      5 }|j                         }d d d        |ct        j                  j                  |      }t        j                  j                  |      r%t        |d+      5 }|j                         }d d d        t        ||||,      }|j                          |j                  |      }d}|d-k(  r!|r|j                  s|j!                  |       d}|d.k(  r3|s|j                  d/*       |j                  s|j#                  |       d}|d0k(  r4|s|j                  d1*       |j                  s|j%                  ||       d}|dk(  rz|s0|j                  s |j'                  ||||||||	|
||||||2       d}nH|j)                  ||||	|
||||3	      s(|j                  s|j+                  ||||	|
||||3	       d}|4|j-                  ||4      s!|j                  s|j/                  ||4       d}|j-                  ||5      s!|j                  s|j/                  ||5       d}|j-                  ||6      s!|j                  s|j/                  ||6       d}|j-                  ||7      s!|j                  s|j/                  ||7       d}|j-                  ||8      s!|j                  s|j/                  ||8       d}|j1                  ||9       y # 1 sw Y   1xY w# 1 sw Y   xY w# 1 sw Y   xY w):NFbool)defaulttypeimporter_ssl_ca_cert)aliasesimporter_ssl_client_certimporter_ssl_client_keyT)r   no_logrepo)requiredr   )r   zhttps://127.0.0.1)r   r/   present)absentr   syncpublish)r   choices)rX   r>   r9   feed_ca_certfeed_client_certfeed_client_keynamer?   r@   rA   rB   rk   r   r7   r   r8   rV   rW   r|   r   )argument_specsupports_check_moderX   r>   r9   r   r   r   r?   r@   rA   rB   rk   r   r7   r   r   r8   rV   rW   r|   r   z0When state is present, relative_url is required.)rM   r)r   r   r   z;Repository was not found. The repository can not be synced.r   z>Repository was not found. The repository can not be published.)r   r7   r>   r9   rV   rW   r?   r@   rA   rB   r8   rC   rD   rE   rX   )r>   r?   r@   rA   rB   rC   rD   rE   )r7   )r9   )r8   )r5   )r6   )changedr   )r	   updater   r   paramsrU   ospathabspathisfileopenreadr   rx   r   
check_modere   rn   rl   rZ   r,   rt   r(   rr   	exit_json)r   r   rX   r>   r9   r   r   r   r?   r@   rA   rB   rk   r   r7   r   r   r8   rV   rW   r|   r   importer_ssl_ca_cert_file_path importer_ssl_ca_cert_file_object"importer_ssl_client_cert_file_path$importer_ssl_client_cert_file_object!importer_ssl_client_key_file_path#importer_ssl_client_key_file_objectserverrepo_existsr   s                                  r   mainr     s   %'M#E?VU8#9":;'A&BC&?%@N4&266v4( F23Vu%e&1F3F3<> !V<-  >. # "F $]]+CD== Dmm$56O!==8%}}-?@$mm,=>|,J|,J]]#34N]]#34N --(=>k*I==0L== Dk*I}}Z(H|,J--.KMM'"E --(=>\OP ')+9M)N&77>>894c: O>^'G'L'L'N$O  +-/WW__=U-V*77>><=8#> WBf+O+T+T+V(W *,.GGOO<S,T)77>>;<7= UAd*M*R*R*T'U IK^_F
**40KG[  t$!^_  T"	!ab  &9:	$$"" !-$3) +))#1#1% 4$<#:+A # C" G 66%%--0 86 7 
 ((66!#-#-'5'5$8(@'> 7 	@ '==!- >  ",,== )5 > 7 #G99$P_9`((99$P_9`99$9R((99$9R99$Z9P((99$Z9P99$k9R((99$k9R
W40IO OW WU Us$   W>W&#W3W#&W03W=__main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNrS   r   timer   ansible.module_utils.basicr   ansible.module_utils.urlsr   r	   objectr   r   r   r   r   r   <module>r      sj    A @Vp2
  	  5 / 7~& ~B
}1@ zF r   