o
    #jV                     @   sz   d Z ddlZddlZddlZddlmZmZ ddlmZ ddl	m
Z
mZmZ ddlmZ ddlmZ G dd	 d	ejZdS )
z-
This module provides a client class for AS.
    N)bce_base_clientcompat)bce_v1_signer)handlerbce_http_clienthttp_methods)
as_handler)requiredc                
   @   sF  e Zd ZdZdZdZdZdZd;ddZd;d	d
Z		d<ddZ
d=ddZdd Z		d=ddZdd Z			d>ddZeeeddd Zeeeeeeeed			d?dd Zeeeeeeeeed!			d?d"d#Zeed$d%d& Zeed'		d@d*d+ZdAd,d-Z	.	dBd/d0Zd1d2 Zd;d3d4Zd5d6 Zd7d8 Zd9d: ZdS )CAsClientz
    AS base sdk client
    s   /v1s   content-types   application/json;charset=UTF-8s   x-bce-request-idNc                 C   s   t j| | d S N)r   BceBaseClient__init__)selfconfig r   h/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/baidubce/services/autoscaling/as_client.pyr      s   zAsClient.__init__c                 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AsClient._merge_configc              
   C   sx   |  |}|d u rtj}|d u ri }| j|vr| j|| j< | j|vr)t || j< t	|t
jtj|g|tj| |||S r   )r   r   Z
parse_jsoncontent_type_header_keycontent_type_header_valuerequest_id_header_keyuuiduuid4r   send_requestr   signr   Zparse_errorr
   version)r   Zhttp_methodpathbodyheadersparamsr   Zbody_parserr   r   r   _send_request$   s   


zAsClient._send_request     c           	      C   s(   ||||||d}d}| j tj||dS )af  
            Get autoscaling group list
            :param page_no:
                 page_no
            :type page_no: int

            :param keyword:
                query keyword
            :type keyword: string

            :param keyword_type:
                query keyword type
            :type keyword_type: string

            :param order:
                order-ascending order or descending order
            :type order: string

            :param order_by:
                order by param
            :type order_by:

            :return:
            :rtype baidubce.bce_response.BceResponse
        )   keyword   keywordType   order   orderBy   pageNo   pageSize   /groupr   r    r   GET)	r   page_no	page_sizekeywordkeyword_typeorderorder_byr   r   r   r   r   get_as_group_list4   s   zAsClient.get_as_group_listc                 C   s4   t |dkr
tdt|}d| }| tj|S )z
            Get autoscaling group detail
            :param group_id:
                autoscaling group_id
            :type group_id: string
            :return:
            :rtype baidubce.bce_response.BceResponse
        r   +group_id should not be none or empty string	   /group/%s)len
ValueErrorr   convert_to_bytesr    r   r,   )r   group_idr   r   r   r   get_as_group_detailY   s
   	
zAsClient.get_as_group_detailc           
      C   s>   t |dkr
td|||||||d}d}	| jtj|	|dS )a  
            Get autoscaling group node list

            :param group_id:
                autoscaling group_id
            :type group_id: string

            :param page_no:
                 page_no
            :type page_no: int

            :param keyword:
                query keyword
            :type keyword: string

            :param keyword_type:
                query keyword type
            :type keyword_type: string

            :param order:
                order-ascending order or descending order
            :type order: string

            :param order_by:
                order by param
            :type order_by:

            :return:
            :rtype baidubce.bce_response.BceResponse
        r   r4   )   groupidr#   r$   r%   r&   r'   r(   s   /noder*   )r6   r7   r    r   r,   )
r   r9   r-   r.   r/   r0   r1   r2   r   r   r   r   r   get_as_group_node_listh   s    	zAsClient.get_as_group_node_listc                 C   s8   t |dkr
tdd}d|i}| jtj|t|dS )z
            Get autoscaling group node list

            :param group_ids:
                autoscaling group_ids
            :type group_ids: list of strings
        r   r4   s   /group/deletegroupIdsr   )r6   r7   r    r   POSTjsondumps)r   	group_idsr   r   r   r   r   delete_group   s   zAsClient.delete_groupc                 C   s>   d}|||||||||	|
||||d}| j tj|t|dS )a[  
            Create autoscaling group

            :param group_name:
                autoscaling group_name
            :type group_name: string

            :param config:
                 autoscaling group config
            :type config: dict

            :param health_check:
                autoscaling group health check info
            :type health_check: dict

            :param blb:
                blb info
            :type blb: list of strings

            :param rds:
                rds info
            :type rds: list of strings

            :param scs:
                scs info
            :type scs: list of strings

            :param shrinkage_strategy:
                autoscaling group shrinkage strategy
            :type shrinkage_strategy: string

            :param zone_info:
                autoscaling group zone info
            :type zone_info: dict

            :param assign_tag_info:
                autoscaling group tag info
            :type assign_tag_info: dict

            :param nodes:
                autoscaling group nodes
            :type nodes: dict

            :param eip:
                autoscaling group eip info
            :type eip: dict

            :param billing:
                autoscaling group billing info
            :type billing: dict

            :param cmd_config:
                autoscaling group cmd config
            :type cmd_config: dict

            :param bcc_name_config:
                autoscaling group bcc name config
            :type bcc_name_config: dict

            :return:
            :rtype baidubce.bce_response.BceResponse
        r)   )Z	groupNamer   ZhealthCheckblbrdsscsZshrinkageStrategyZzoneInfoZassignTagInfonodeseipbillingZ	cmdConfigZbccNameConfigr>   r    r   r?   r@   rA   )r   
group_namer   Zhealth_checkrD   rE   rF   Zshrinkage_strategyZ
zone_infosZassign_tag_infoZ	node_listrH   rI   Z
cmd_configZbcc_name_configr   r   r   r   r   create_group   s"   BzAsClient.create_group)r9   rG   c                 C   s8   dt | }ddi}d|i}| jtj|t||dS )z
        Detach nodes from group
        :param group_id: the id of group
        :type group_id: string
        :param nodes: the list of node
        :type nodes: list
        :return: the result of detach node
        :rtype: dict
        r5   Z
detachNode rG   )r   r   r   r8   r    r   r?   r@   rA   r   r9   rG   r   r   r   r   r   r   detach_node   s   zAsClient.detach_node)	rule_namer9   state	rule_typeaction_type
action_numcooldown_in_secrM   0c                 C   s   d}i d|d|d|d|d|d|	d|
d	|d
|d|d|d|d|d|d|d|d|d|i}| j tj|t|dS )a  
        Create rule
        :param rule_name: the name of the rule
        :type rule_name: string
        :param group_id: the id of the group
        :type group_id: string
        :param state: the state of the rule, can be "ENABLE" or "DISABLE"
        :type state: string
        :param rule_type: the type of the rule, can be "ALARM", "CRONTAB", or "PERIOD"
        :type rule_type: string
        :param target_type: the type of the target
        :type target_type: string
        :param target_id: the id of the target
        :type target_id: string
        :param indicator: the indicator of the rule
        :type indicator: string
        :param threshold: the threshold of the rule
        :type threshold: string
        :param unit: the unit of the threshold
        :type unit: string
        :param comparison_operator: the comparison operator of the rule
        :type comparison_operator: string
        :param action_type: the action type of the rule, can be INCREASE, DECREASE, ADJUST
        :type action_type: string
        :param action_num: the number of actions to be performed
        :type action_num: int
        :param cron_time: the cron time of the rule
        :type cron_time: string
        :param cooldown_in_sec: the cooldown time in seconds
        :type cooldown_in_sec: int
        :param period_type: the period type of the rule
        :type period_type: string
        :param period_value: the period value of the rule
        :type period_value: int
        :param period_start_time: the start time of the period
        :type period_start_time: string
        :param period_end_time: the end time of the period
        :type period_end_time: string
           /ruleruleNamegroupIdrR   type
targetTypetargetId	indicator	thresholdunitcomparisonOperator
actionType	actionNumcronTimecooldownInSec
periodTypeperiodValueperiodStartTimeperiodEndTimer>   rJ   )r   rQ   r9   rR   rS   rT   rU   rV   target_type	target_idr^   r_   r`   comparison_operator	cron_timeperiod_typeperiod_valueperiod_start_timeperiod_end_timer   r   r   r   r   create_rule  sN   .	
zAsClient.create_rule)rule_idrQ   r9   rR   rS   rT   rU   rV   c                 C   s   dt | }i d|d|d|d|d|	d|
d|d	|d
|d|d|d|d|d|d|d|d|d|i}| jtj|t|dS )a  
        Update rule
        :param rule_id: the id of the rule
        :type rule_id: string
        :param rule_name: the name of the rule
        :type rule_name: string
        :param group_id: the id of the group
        :type group_id: string
        :param state: the state of the rule, can be "ENABLE" or "DISABLE"
        :type state: string
        :param rule_type: the type of the rule, can be "ALARM", "CRONTAB", or "PERIOD"
        :type rule_type: string
        :param target_type: the type of the target
        :type target_type: string
        :param target_id: the id of the target
        :type target_id: string
        :param indicator: the indicator of the rule
        :type indicator: string
        :param threshold: the threshold of the rule
        :type threshold: string
        :param unit: the unit of the threshold
        :type unit: string
        :param comparison_operator: the comparison operator of the rule
        :type comparison_operator: string
        :param action_type: the action type of the rule, can be INCREASE, DECREASE, ADJUST
        :type action_type: string
        :param action_num: the number of actions to be performed
        :type action_num: int
        :param cron_time: the cron time of the rule
        :type cron_time: string
        :param cooldown_in_sec: the cooldown time in seconds
        :type cooldown_in_sec: int
        :param period_type: the period type of the rule
        :type period_type: string
        :param period_value: the period value of the rule
        :type period_value: int
        :param period_start_time: the start time of the period
        :type period_start_time: string
        :param period_end_time: the end time of the period
        :type period_end_time: string
           /rule/%srY   rZ   rR   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   r>   )r   r8   r    r   PUTr@   rA   )r   rs   rQ   r9   rR   rS   rT   rU   rV   rj   rk   r^   r_   r`   rl   rm   rn   ro   rp   rq   r   r   r   r   r   update_ruleT  sN   /	
zAsClient.update_rule)rs   c                 C   s   dt | }| tj|S )z
        Get rule
        :param rule_id: the id of the rule
        :type rule_id: string
        :return: the result of get rule
        :rtype: dict
        rt   )r   r8   r    r   r,   )r   rs   r   r   r   r   get_rule  s   	zAsClient.get_rule)r9   desc
createTimec           
      C   s*   d}|||||||d}	| j tj||	dS )a4  
        Query rule list
        :param group_id: the id of the group
        :type group_id: string
        :param keyword: the keyword of the rule
        :type keyword: string
        :param keyword_type: the type of the keyword
        :type keyword_type: string
        :param order: the order of the rule
        :type order: string
        :param order_by: the order by of the rule
        :type order_by: string
        :param page_no: the page number
        :type page_no: int
        :param page_size: the page size
        :type page_size: int
        rX   )Zgroupidr/   ZkeywordTyper1   ZorderByZpageNoZpageSizer*   r+   )
r   r9   r/   r0   r1   r2   r-   r.   r   r   r   r   r   	list_rule  s   	zAsClient.list_rulec                 C   s@   |s|st dd}ddi}||d}| jtj||t|dS )z
        Delete rule
        :param rule_ids: the list of rule id
        :type rule_ids: list
        :param group_ids: the list of group id
        :type group_ids: list
        z8rule_ids and group_ids can not be empty at the same timerX   deleterM   )ZruleIdsr=   r   r   )r7   r    r   r?   r@   rA   )r   Zrule_idsrB   r   r   r   r   r   r   delete_rule  s   zAsClient.delete_rule	startTimec           
      C   s*   |||||||d}d}	| j tj|	|dS )a@  
           Get details of a specific group
           :param group_id:
                Group identifier
           :type group_id: str

           :param page_no:
                Page number
           :type page_no: int

           :param page_size:
                Page size
           :type page_size: int

           :param order:
               Order-ascending order or descending order
           :type order: string

           :param order_by:
               Order by parameter
           :type order_by: string

           :param start_time:
               Start time for filtering the results
           :type start_time: string

           :param end_time:
               End time for filtering the results
           :type end_time: string

           :return:
           rtype baidubce.bce_response.BceResponse
       )r;   r%   r&   r'   r(   s	   startTimes   endTimes   /recordr*   r+   )
r   r9   r-   r.   r1   r2   
start_timeZend_timer   r   r   r   r   get_records  s   $	zAsClient.get_recordsc                 C   <   t |}d| }ddi}d|i}| jtj||t|dS )aI  
           Execute a specific rule within a group
           :param group_id:
                Group identifier
           :type group_id: str

           :param rule_id:
                Rule identifier within the group
           :type rule_id: str

           :return:
           rtype baidubce.bce_response.BceResponse
       r5   s   execRulerM   ZruleIdr|   rN   )r   r9   rs   r   r   r   r   r   r   	exec_rule	     
zAsClient.exec_rulec                 C   s@   t |}d| }ddi}|||d}| jtj||t|dS )aK  
            Scale up a specific group

            :param group_id:
                Group identifier
            :type group_id: str

            :param node_count:
                Number of nodes to be added
            :type node_count: int

            :param zone:
                Zone where the nodes will be added
            :type zone: str

            :param expansion_strategy:
                Strategy to be used for the expansion, optional
            :type expansion_strategy: str, optional

            :return:
            rtype baidubce.bce_response.BceResponse
        r5   s	   scalingUprM   )Z	nodeCountzoneZexpansionStrategyr|   rN   )r   r9   Z
node_countr   Zexpansion_strategyr   r   r   r   r   r   
scaling_up"  s   
zAsClient.scaling_upc                 C   r   )a>  
            Scale down a specific group

            :param group_id:
                Group identifier
            :type group_id: str

            :param nodes:
                Nodes to be removed
            :type nodes: list or str

            :return:
            rtype baidubce.bce_response.BceResponse
        r5   s   scalingDownrM   rG   r|   rN   rO   r   r   r   scaling_downE  r   zAsClient.scaling_downc                 C   r   )a  
            Adjust the number of nodes in a specific group

            :param group_id:
                Group identifier
            :type group_id: str

            :param adjust_num:
                Number to adjust the nodes by. This could be positive (for adding nodes) or
                negative (for removing nodes).
            :type adjust_num: int

            :return:
            rtype baidubce.bce_response.BceResponse
        r5   s
   adjustNoderM   Z	adjustNumr|   rN   )r   r9   Z
adjust_numr   r   r   r   r   r   adjust_node^  s   
zAsClient.adjust_nodec                 C   r   )a  
            Attach nodes to a specific group.

            :param group_id: str
                The unique identifier of the group to which the nodes will be attached.
            :param nodes: list or str
                The nodes that will be attached to the group.
                This can be a list of node identifiers or a single node identifier.

            :return: baidubce.bce_response.BceResponse
            rtype baidubce.bce_response.BceResponse
        r5   s
   attachNoderM   rG   r|   rN   rO   r   r   r   attach_nodex  s   
zAsClient.attach_noder   )NNNNN)r!   r"   NNNN)NNNNNNNNNNNNNN)rM   rM   rM   rW   rM   rM   rM   NNNN)rM   rM   rx   ry   r!   r"   )NN)r!   r"   Nr~   NN) __name__
__module____qualname____doc__r   r   r   r   r   r   r    r3   r:   r<   rC   rL   r	   strlistrP   intrr   rv   rw   rz   r}   r   r   r   r   r   r   r   r   r   r   r
      sf    

	

%
.

U
CD


/
#r
   )r   r   r@   r   Zbaidubcer   r   Zbaidubce.authr   Zbaidubce.httpr   r   r   Zbaidubce.services.autoscalingr   Zbaidubce.utilsr	   r   r
   r   r   r   r   <module>   s    