U
    d/e(                     @   s2   d Z ddlmZ ddlZddlZG dd dZdS )1MinIO Admin wrapper using MinIO Client (mc) tool.    )absolute_importNc                   @   s  e Zd ZdZd[ddZd\ddZdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd]d"d#Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd^d0d1Zd_d2d3Zd`d4d5Zd6d7 Zdad8d9Zd:d; Zd<d= Z d>d? Z!dbdAdBZ"dCdD Z#dEdF Z$dGdH Z%dcdIdJZ&dddKdLZ'dedMdNZ(dOdP Z)dfdQdRZ*dSdT Z+dgdUdVZ,dWdX Z-dYdZ Z.dS )h
MinioAdminr   NFc                 C   sV   || _ || _|| _|pddg| _|r6|  jd|g7  _|rF| jd | jd d S )NZmcz--jsonz--config-dirz
--insecureZadmin)_target_timeout_env
_base_argsappend)selftargetZbinary_pathZ
config_dirZignore_cert_checktimeoutenv r   4/tmp/pip-unpacked-wheel-xery97c7/minio/minioadmin.py__init__   s    zMinioAdmin.__init__c                 C   sV   t j| j| d| j| jddd}|js2|r.g S i S |rJdd |j D S t|jS )z*Execute mc command and return JSON output.T)capture_outputr   r   checktextc                 S   s   g | ]}t |qS r   )jsonloads).0liner   r   r   
<listcomp>:   s     z#MinioAdmin._run.<locals>.<listcomp>)	
subprocessrunr   r   r   stdout
splitlinesr   r   )r
   args	multilineprocr   r   r   _run-   s    zMinioAdmin._runc                 C   s   |  dd| jgS )zRestart MinIO service.serviceZrestartr    r   r
   r   r   r   service_restart=   s    zMinioAdmin.service_restartc                 C   s   |  dd| jgS )zStop MinIO service.r!   stopr"   r#   r   r   r   service_stopA   s    zMinioAdmin.service_stopc                 C   s   |  d| jgS )zUpdate MinIO.updater"   r#   r   r   r   r'   E   s    zMinioAdmin.updatec                 C   s   |  d| jgS )zGet MinIO server information.infor"   r#   r   r   r   r(   I   s    zMinioAdmin.infoc                 C   s   |  dd| j||gS )zAdd a new user.useraddr"   )r
   
access_keyZ
secret_keyr   r   r   user_addM   s    zMinioAdmin.user_addc                 C   s   |  dd| j|gS )zDisable user.r)   disabler"   r
   r+   r   r   r   user_disableQ   s    zMinioAdmin.user_disablec                 C   s   |  dd| j|gS )zEnable user.r)   enabler"   r.   r   r   r   user_enableU   s    zMinioAdmin.user_enablec                 C   s   |  dd| j|gS )zRemove user.r)   remover"   r.   r   r   r   user_removeY   s    zMinioAdmin.user_removec                 C   s   |  dd| j|gS )zGet user information.r)   r(   r"   r.   r   r   r   	user_info]   s    zMinioAdmin.user_infoc                 C   s   | j dd| jgddS )zList users.r)   listTr   r"   r#   r   r   r   	user_lista   s    zMinioAdmin.user_listc                 C   s   |  dd| j|g| S )z"Add users a new or existing group.groupr*   r"   r
   
group_namemembersr   r   r   	group_adde   s    zMinioAdmin.group_addc                 C   s   |  dd| j|gS )zDisable group.r8   r-   r"   r
   r:   r   r   r   group_disablei   s    zMinioAdmin.group_disablec                 C   s   |  dd| j|gS )zEnable group.r8   r0   r"   r=   r   r   r   group_enablem   s    zMinioAdmin.group_enablec                 C   s   |  dd| j|g|pg  S )z%Remove group or members from a group.r8   r2   r"   r9   r   r   r   group_removeq   s    zMinioAdmin.group_removec                 C   s   |  dd| j|gS )zGet group information.r8   r(   r"   r=   r   r   r   
group_infow   s    zMinioAdmin.group_infoc                 C   s   | j dd| jgddS )zList groups.r8   r5   Tr6   r"   r#   r   r   r   
group_list{   s    zMinioAdmin.group_listc                 C   s   |  dd| j||gS )zAdd new policy.policycreater"   )r
   policy_nameZpolicy_filer   r   r   
policy_add   s    zMinioAdmin.policy_addc                 C   s   |  dd| j|gS )zRemove policy.rC   r2   r"   r
   rE   r   r   r   policy_remove   s    zMinioAdmin.policy_removec                 C   s   |  dd| j|gS )zGet policy information.rC   r(   r"   rG   r   r   r   policy_info   s    zMinioAdmin.policy_infoc                 C   s   | j dd| jgddS )zList policies.rC   r5   Tr6   r"   r#   r   r   r   policy_list   s    zMinioAdmin.policy_listc                 C   s@   |dk	|dk	A r4|  dd| j||r&dnd|p.|gS tddS )z"Set IAM policy on a user or group.NrC   attach--user--group either user or group must be setr    r   
ValueErrorr
   rE   r)   r8   r   r   r   
policy_set   s       
 zMinioAdmin.policy_setc                 C   s@   |dk	|dk	A r4|  dd| j||r&dnd|p.|gS tddS )z(Unset an IAM policy for a user or group.NrC   detachrL   rM   rN   rO   rQ   r   r   r   policy_unset   s       
 zMinioAdmin.policy_unsetc                 C   s"   |  |rdd| jg|g ng |S )zGet configuration parameters.configgetr"   r
   keyr   r   r   
config_get   s    zMinioAdmin.config_getc                 C   s*   dd |  D }| dd| j|g| S )zSet configuration parameters.c                 S   s   g | ]\}}|d  | qS )=r   )r   namevaluer   r   r   r      s     z)MinioAdmin.config_set.<locals>.<listcomp>rU   set)itemsr    r   )r
   rX   rU   r   r   r   r   
config_set   s    zMinioAdmin.config_setc                 C   s$   |r|d| 7 }|  dd| j|gS )zReset configuration parameters.:rU   resetr"   )r
   rX   r[   r   r   r   config_reset   s    zMinioAdmin.config_resetc                 C   s   |  dd| j|gS )zRemove config.rU   r2   r"   r.   r   r   r   config_remove   s    zMinioAdmin.config_removec                 C   s   | j dd| jgddS )z#Get historic configuration changes.rU   historyTr6   r"   r#   r   r   r   config_history   s    zMinioAdmin.config_historyc                 C   s   |  dd| j|gS )z,Restore to a specific configuration history.rU   restorer"   )r
   Z
restore_idr   r   r   config_restore   s    zMinioAdmin.config_restorer   c                 C   s4   ddg}|r|dd |g7 }|| j | |S )zStart recording profile data.profilestartz--type,)joinr	   r   r    )r
   Z	profilersr   r   r   r   profile_start   s
    zMinioAdmin.profile_startc                 C   s   |  dd| jgS )zStop and download profile data.rh   r%   r"   r#   r   r   r   profile_stop   s    zMinioAdmin.profile_stopc                 C   s   | j dd| jgddS )z5Get a list of the 10 oldest locks on a MinIO cluster.toplocksTr6   r"   r#   r   r   r   	top_locks   s    zMinioAdmin.top_locksc                 C   s   |  dd| jgS )z"Generate prometheus configuration.Z
prometheusgenerater"   r#   r   r   r   prometheus_generate   s    zMinioAdmin.prometheus_generatec                 C   s$   |  ddd| j|g|r|gng  S )zCreate a new KMS master key.kmsrX   rD   r"   rW   r   r   r   kms_key_create   s        zMinioAdmin.kms_key_createc                 C   s$   |  ddd| j|g|r|gng  S )z+Get status information of a KMS master key.rs   rX   statusr"   rW   r   r   r   kms_key_status   s        zMinioAdmin.kms_key_statusc                 C   sZ   ddd| j d | |d|pdg}|r0|d|g7 }|r@|d|g7 }|rP|d	|g7 }| |S )
zAdd a new remote target.bucketremoter*   /	--servicereplicationz--pathz--regionz--bandwidth)r   r    )r
   
src_bucketdest_urlpathZregion	bandwidthr!   r   r   r   r   bucket_remote_add   s         zMinioAdmin.bucket_remote_addc              	   C   s"   |  ddd| jd | |d|gS )z"Edit credentials of remote target.rw   rx   Zeditry   --arnr"   )r
   r|   r}   arnr   r   r   bucket_remote_edit   s         zMinioAdmin.bucket_remote_editc                 C   s,   |  ddd| j|rd| nd d|p&dgS )zList remote targets.rw   rx   Zlsry    rz   r{   r"   )r
   r|   r!   r   r   r   bucket_remote_list  s       zMinioAdmin.bucket_remote_listc                 C   s    |  ddd| jd | d|gS )z Remove configured remote target.rw   rx   Zrmry   r   r"   )r
   r|   r   r   r   r   bucket_remote_remove  s        zMinioAdmin.bucket_remote_removec                 C   sV   |dkr|dkrt ddd| jd | g}|r<|d|g7 }|rL|d|g7 }| |S )zSet bucket quota configuration.Nzfifo or hard must be setrw   quotary   z--fifoz--hard)rP   r   r    )r
   rw   Zfifohardr   r   r   r   bucket_quota_set  s    zMinioAdmin.bucket_quota_setc                 C   s   |  dd| jd | dgS )z!Clear bucket quota configuration.rw   r   ry   z--clearr"   r
   rw   r   r   r   bucket_quota_clear!  s    zMinioAdmin.bucket_quota_clearc                 C   s   |  dd| jd | gS )zGet bucket quota configuration.rw   r   ry   r"   r   r   r   r   bucket_quota_get'  s    zMinioAdmin.bucket_quota_get)NNFNN)F)N)NN)NN)N)N)r   )N)N)NNNN)NN)NN)/__name__
__module____qualname____doc__r   r    r$   r&   r'   r(   r,   r/   r1   r3   r4   r7   r<   r>   r?   r@   rA   rB   rF   rH   rI   rJ   rR   rT   rY   r_   rb   rc   re   rg   rl   rm   rp   rr   rt   rv   r   r   r   r   r   r   r   r   r   r   r   r      sj           










       
	

	
r   )r   
__future__r   r   r   r   r   r   r   r   <module>   s   