o
    eUP                     @   sL  d Z ddlZddl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 ddl
mZ ddl
mZ dd	lmZ dd
lmZ eeZejd dkrSeefZneefZG dd dejZdd Z	d+ddZdd Zd,ddZd-ddZd,ddZ d-ddZ!d.ddZ"d.d d!Z#d,d"d#Z$d$d% Z%d&d' Z&ee'ed(d-d)d*Z(dS )/z.
This module provides a client class for BMR.
    N)bce_v1_signer)bce_base_client)compat)bce_http_client)handler)http_methods)required)aes128_encrypt_16char_key   c                   @   sn  e Zd ZdZdZd0ddZeeeed														d1ddZ	d2d	d
Z
eeddd Zeeddd Zeeddd Zeeddd Zeeeddd Zeeedd0ddZeedd2ddZeeeddd Zeeedd d! Zeeed"d#d$ Zeedd%d& Zeedd'd( Zeeed)d*d+ Zd0d,d-Z		d3d.d/ZdS )4	BmrClientz
    Bmr sdk client
    z/v1Nc                 C   s   t j| | d S N)r   BceBaseClient__init__)selfconfig r   PD:\Projects\ConvertPro\env\Lib\site-packages\baidubce/services/bmr/bmr_client.pyr   0   s   zBmrClient.__init__)
image_typeimage_versioninstance_groupsc                 C   s4  d}d}|durd|i}t |t ||d}|dur ||d< |dur(||d< |dur0||d< |dur8||d< |	dur@|	|d	< |
durH|
|d
< |durP||d< |dure| jdure| jjj}t|||d< |durm||d< |duru||d< |dur}||d< |dur||d< |dur||d< | jtj||t	
|dS )a  
        Create cluster

        :param image_type: the type of virtual machine image
        :type image_type: string

        :param image_version: the version of virtual machine image
        :type image_version: string

        :param instance_groups: instance groups for cluster
        :type instance_groups: array

        :return:
        :rtype baidubce.bce_response.BceResponse
        /clusterNclientToken)Z	imageTypeZimageVersioninstanceGroupsapplicationsZautoTerminatenameZlogUristepsZserviceHaEnabledZsafeModeEnabledZadminPasswordZvpcIdZsubnetIdZsecurityGroupZavailabilityZonetemplateTypeparamsbody)r   convert_to_stringr   credentialssecret_access_keyr	   _send_requestr   POSTjsondumps)r   r   r   r   client_tokenr   Zauto_terminateZlog_urir   r   Zservice_ha_enabledZsafe_mode_enabledZ
admin_passZvpc_idZ	subnet_idZsecurity_groupZavailability_zoner   pathr   r   r"   r   r   r   create_cluster3   sJ   $
zBmrClient.create_clusterc                 C   sN   d}d}|dus|duri }|dur||d< |dur||d< | j tj||dS )z
        List clusters

        :param marker:
        :type marker: string

        :param max_keys: max records returned.
        :type max_keys: int

        :return:
        :rtype baidubce.bce_response.BceResponse
        r   NmarkerZmaxKeysr   )r#   r   GET)r   r*   Zmax_keysr(   r   r   r   r   list_clusters   s   zBmrClient.list_clusters)
cluster_idc                 C      dt | }| tj|S )z
        Get cluster

        :param cluster_id: cluster id
        :type cluster_id: string

        :return:
        :rtype baidubce.bce_response.BceResponse
        /cluster/%sr   r    r#   r   r,   r   r.   r(   r   r   r   get_cluster      zBmrClient.get_clusterc                 C   r/   )z
        Get cluster hosts
        :param cluster_id: cluster id
        :type cluster_id: string

        :return:
        :rtype baidubce.bce_response.BceResponse
        z/cluster/%s/hostsr1   r2   r   r   r   list_cluster_hosts   s   
zBmrClient.list_cluster_hostsc                 C   r/   )z
        Get cluster ambari password

        :param cluster_id: cluster id
        :type cluster_id: string

        :return:
        :rtype baidubce.bce_response.BceResponse
        z/cluster/%s/ambaripasswordr1   r2   r   r   r   get_cluster_ambariPassword   r4   z$BmrClient.get_cluster_ambariPasswordc                 C   r/   )z
        Terminate cluster

        :param cluster_id: cluster id
        :type cluster_id: string

        :return:
        :rtype baidubce.bce_response.BceResponse
        r0   )r   r    r#   r   DELETEr2   r   r   r   terminate_cluster   r4   zBmrClient.terminate_cluster)r.   instance_group_configc                 C   s4   dt | }d}d|i}| jtj||t|dS )aW  
        Scale cluster
        :param cluster_id: cluster id
        :type cluster_id: string

        :param instance_group_id: instance group id
        :type instance_group_id: string

        :param instance_count: instance count
        :type instance_count: int

        :return:
        :rtype baidubce.bce_response.BceResponse
        /cluster/%s/instanceGroupNr   r   )r   r    r#   r   PUTr%   r&   )r   r.   r9   r(   r   r   r   r   r   scale_cluster   s
   zBmrClient.scale_cluster)r.   r   c                 C   sD   dt | }d}|durd|i}td|i}| jtj|||dS )z
        Add steps

        :param cluster_id: cluster id
        :type cluster_id: string

        :param steps: steps to be added
        :type steps: Array

        :return:
        :rtype baidubce.bce_response.BceResponse
        /cluster/%s/stepNr   r   r   )r   r    r%   r&   r#   r   r$   )r   r.   r   r'   r(   r   r   r   r   r   	add_steps   s   zBmrClient.add_stepsc                 C   sX   dt | }d}|dus|duri }|dur||d< |dur#||d< | jtj||dS )a'  
        List step

        :param cluster_id: cluster id
        :type cluster_id: string

        :param marker:
        :type marker: string

        :param max_keys: max records returned.
        :type max_keys: int

        :return:
        :rtype baidubce.bce_response.BceResponse
        r=   NpageNopageSizer+   r1   )r   r.   r?   r@   r(   r   r   r   r   
list_steps   s   zBmrClient.list_steps)r.   step_idc                 C   &   dt |t |f }| tj|S )a  
        Get step

        :param cluster_id: cluster id
        :type cluster_id: string

        :param step_id: step id
        :type step_id: string

        :return: baidubce.bce_response.BceResponse
        :rtype baidubce.bce_response.BceResponse
        /cluster/%s/step/%sr1   )r   r.   rB   r(   r   r   r   get_step     
zBmrClient.get_step)	clusterIdstepIdc                 C   s   d||f }|  tj|S )z

        :param clusterId:cluster id
        :param stepId: step id
        :return: baidubce.bce_response.BceResponse
        rD   )r#   r   r7   )r   rG   rH   r(   r   r   r   cancel_step-  s   zBmrClient.cancel_step)r.   instance_group_idc                 C   rC   )a  
        List instances

        :param cluster_id: cluster id
        :type cluster_id: string

        :param instance_group_id: instance group id
        :type instance_group_id: string

        :return:
        :rtype baidubce.bce_response.BceResponse
        z%/cluster/%s/instanceGroup/%s/instancer1   )r   r.   rJ   r(   r   r   r   list_instances;  rF   zBmrClient.list_instancesc                 C   r/   )z
        List instance groups

        :param cluster_id: cluster id
        :type cluster_id: string

        :return:
        :rtype: baidubce.bce_response.BceResponse
        r:   r1   r2   r   r   r   list_instance_groupsM  r4   zBmrClient.list_instance_groupsc                 C   s*   d}d|i}d}| j tj||t|dS )z
        :param cluster_id: clusterId
        :type  cluster_id: string
        :return: baidubce.bce_response.BceResponse
        NrG   z/cluster/detailr   )r#   r   r$   r%   r&   )r   r.   r   r   r(   r   r   r   describe_cluster\  s
   zBmrClient.describe_clusterrG   newNamec                 C   sN   |du st |ddkrtdd}||d}d}| jtj||t|dS )z
        rename a cluster
        :param clusterId: clusterId
        :type  clusterId: string
        :param newName: newName
        :type  newName: string
        :return: baidubce.bce_response.BceResponse
        N r   znewName can not be empty.rN   z/cluster/renamer   )lenstrip
ValueErrorr#   r   r$   r%   r&   )r   rG   rO   r   r   r(   r   r   r   rename_clusterj  s   zBmrClient.rename_clusterc                 C   s(   |d u r| j S t| j }|| |S r   )r   copyZmerge_non_none_values)r   r   Z
new_configr   r   r   _merge_config  s
   
zBmrClient._merge_configc              
   C   sX   |  |}|d u rtj}t|j|_t|tddgtj	|g|tt
j| |||S )Ns   hosts
   x-bce-date)rV   r   Z
parse_jsonr   convert_to_bytesZendpointr   send_requestsign_wrapperZparse_errorr   prefix)r   http_methodr(   r   headersr   r   Zbody_parserr   r   r   r#     s   
zBmrClient._send_requestr   )NNNNNNNNNNNNNNNN)NNNNN)__name__
__module____qualname____doc__rZ   r   r   
value_typelistr)   r-   r3   r5   r6   r8   r<   r>   rA   rE   rI   rK   rL   rM   rT   rV   r#   r   r   r   r   r   )   sz    

J















	r   c                    s    fdd}|S )z!wrapper the bce_v1_signer.sign().c                    s>   t | j| _t | j| _tj| t |t ||| dS )Nheaders_to_sign)r   rW   Zaccess_key_idr"   r   sign)r!   r[   r(   r\   r   rd   r   r   _wrapper  s   zsign_wrapper.<locals>._wrapperr   )re   rg   r   rd   r   rY     s   rY   (   ssdc                 C   s&   | |||||d}|dur||d< |S )an  
    Construct instance group

    :param group_type: instance group type
    :type group_type: ENUM {'Master', 'Core', 'Task'}

    :param instance_type
    :type instance_type: ENUM {'g.small', 'c.large', 'm.medium', 's.medium', 'c.2xlarge'}

    :param instance_count
    :type instance_count: int

    :param name: instance group name
    :type name: string
    )typeinstanceCountZinstanceTypeZrootDiskSizeInGBZrootDiskMediumTypecdsNr   r   )Z
group_typeZinstance_typeZinstance_countr   rl   Zroot_DiskSizeInGBZroot_DiskMediumTypeinstance_groupr   r   r   rm     s   rm   c                 C      | |d}|S )z
    :param sizeInGB: sizeInGB
    :type string
    :param mediumType: mediumType
    :type string ssd, sata, premium_ssd
    :return: cds
    )sizeInGB
mediumTyper   )ro   rp   rl   r   r   r   cdsitem  s   	rq   c                 C      | |d}|dur||d< |S )a6  
    Construct application

    :param name: application type
    :type name: ENUM {'hadoop', 'spark', 'hive', 'pig', 'hbase', 'hue', 'zeppelin', 'kafka', 'mahout'}

    :param version: application version
    :type version: string

    :param properties: application properties
    :type properties: dict
    )r   versionN
propertiesr   )r   rs   rt   applicationr   r   r   ru     s   ru   c                 C   s<   || |d}|dur||d< |durt |dkr||d< |S )a  
    Create step

    :param step_type: the type of step
    :type step_type: Enum {'Java','Streaming','Hive','Pig', 'Spark'}

    :param action_on_failure
    :type actionOnFailure: Enum {'Continue','TerminateCluster','CancelAndWait'}

    :param properties: step properties
    :type properties: dict

    :param name: the name of the step
    :type name: string

    :param additional_files: list of step additional file
    :type additional_files: list
    )ZactionOnFailurerj   rt   Nr   r   ZadditionalFiles)rQ   )Z	step_typeZaction_on_failurert   r   Zadditional_filesstepr   r   r   rv     s   rv   c                 C   rr   )a  
    Create java step properties

    :param jar: the path of .jar file
    :type jar: string

    :param main_class: the package path for main class
    :type main_class: string

    :param arguments: arguments for the step
    :type arguments: string

    :return:
    :rtype map
    )jarZ	mainClassN	argumentsr   )rw   Z
main_classrx   Z	java_stepr   r   r   java_step_properties     ry   c                 C   s2   |d| |d}|dur||d< |dur||d< |S )a  
    Create streaming step properties

    :param input: the input path of step
    :type input: string

    :param output: the output path of step
    :type output: string

    :param mapper: the mapper program of step
    :type mapper: string

    :param reducer: the reducer program of step
    :type reducer: string

    :param arguments: arguments for the step
    :type arguments: string

    :return:
    :rtype map
     )mapperreducerinputoutputNr}   rx   r   )r~   r   r|   r}   rx   Zstreaming_stepr   r   r   streaming_step_properties  s   r   c                 C   <   d| i}|dur||d< |dur||d< |dur||d< |S )aW  
    Create pig step properties

    :param script: the script path of step
    :type script: string

    :param arguments: arguments for the step
    :type arguments: string

    :param input: the input path of step
    :type input: string

    :param output: the output path of step
    :type output: string

    :return:
    :rtype map
    scriptNrx   r~   r   r   )r   rx   r~   r   Zpig_stepr   r   r   pig_step_properties?     r   c                 C   r   )aX  
    Create hive step properties

    :param script: the script path of step
    :type script: string

    :param arguments: arguments for the step
    :type arguments: string

    :param input: the input path of step
    :type input: string

    :param output: the output path of step
    :type output: string

    :return:
    :rtype map
    r   Nrx   r~   r   r   )r   rx   r~   r   Z	hive_stepr   r   r   hive_step_properties^  r   r   c                 C   rr   )a  
    Create spark step properties

    :param jar: the path of .jar file
    :type jar: string

    :param main_class: the package path for main class
    :type main_class: string

    :param arguments: arguments for the step
    :type arguments: string

    :return:
    :rtype map
    )rw   submitOptionsNrx   r   )rw   r   rx   Z
spark_stepr   r   r   spark_step_properties}  rz   r   c                 C   s
   | |dS )z
    Create step additional file

    :param remote: the remote file of the additional file
    :type remote: string

    :param local: the local file of the additional file
    :type local: string

    :return:
    :rtype map
    remotelocalr   r   r   r   r   additional_file  s   r   c                 C   rn   )z
    create instance_group
    :param id: id
    :type id: string
    :param instanceCount: instanceCount
    :type instanceCount: int
    :return: instancegroup_config
    )idrk   r   )r   rk   instancegroup_configr   r   r   r     s   
r   period
periodUnitc                 C   s.   | |d}|dur||d< |dur||d< |S )z
    :param period: period
    :type id: int
    :param periodUnit: periodUnit
    :type id: string
    :param startTime: startTime
    :param endTime: endTime
    :return: schedule
    r   N	startTimeendTimer   )r   r   r   r   Zscheduler   r   r   schedule_properties  s   r   )NNrh   ri   r   r]   )NNN))ra   rU   loggingr%   sysZbaidubce.authr   Zbaidubcer   r   Zbaidubce.httpr   r   r   Zbaidubce.utilsr   r	   	getLoggerr^   Z_loggerversion_infostrunicoderb   bytesr   r   rY   rm   rq   ru   rv   ry   r   r   r   r   r   r   intr   r   r   r   r   <module>   sL   

  n





#

