§
    ÎÑßjŸ'  ã                   ó²   — d Z ddlZddlmZmZmZ ddlmZmZm	Z	 ddl
mZmZ ddlmZ d„ Z G d„ d	e¦  «        Z G d
„ de	e¦  «        Z G d„ de¦  «        ZdS )zk
Abiquo Utilities Module for the Abiquo Driver.

Common utilities needed by the :class:`AbiquoNodeDriver`.
é    N)ÚbÚhttplibÚurlparse)ÚXmlResponseÚPollingConnectionÚConnectionUserAndKey)ÚLibcloudErrorÚInvalidCredsError)Ú	NodeStatec                 ó  — |                       d¦  «        }|D ]q}|j        d         |k    r^|j        d         }d}t          j        |¦  «        j        }|                     |¦  «        }||t          |¦  «        z   dz
  d…         }|c S ŒrdS )aH  
    Search a RESTLink element in the :class:`AbiquoResponse`.

    Abiquo, as a REST API, it offers self-discovering functionality.
    That means that you could walk through the whole API only
    navigating from the links offered by the entities.

    This is a basic method to find the 'relations' of an entity searching into
    its links.

    For instance, a Rack entity serialized as XML as the following::

        <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <rack>
         <link href="http://host/api/admin/datacenters/1"
            type="application/vnd.abiquo.datacenter+xml" rel="datacenter"/>
         <link href="http://host/api/admin/datacenters/1/racks/1"
            type="application/vnd.abiquo.rack+xml" rel="edit"/>
         <link href="http://host/api/admin/datacenters/1/racks/1/machines"
            type="application/vnd.abiquo.machines+xml" rel="machines"/>
         <haEnabled>false</haEnabled>
         <id>1</id>
         <longDescription></longDescription>
         <name>racacaca</name>
         <nrsq>10</nrsq>
         <shortDescription></shortDescription>
         <vlanIdMax>4094</vlanIdMax>
         <vlanIdMin>2</vlanIdMin>
         <vlanPerVdcReserved>1</vlanPerVdcReserved>
         <vlansIdAvoided></vlansIdAvoided>
        </rack>

    offers link to datacenters (rel='datacenter'), to itself (rel='edit') and
    to the machines defined in it (rel='machines')

    A call to this method with the 'rack' element using 'datacenter' as 'rel'
    will return:

    'http://10.60.12.7:80/api/admin/datacenters/1'

    :type  element: :class:`xml.etree.ElementTree`
    :param element: Xml Entity returned by Abiquo API (required)
    :type      rel: ``str``
    :param     rel: relation link name
    :rtype:         ``str``
    :return:        the 'href' value according to the 'rel' input parameter
    ÚlinkÚrelÚhrefz/api/é   N)ÚfindallÚattribr   ÚpathÚfindÚlen)	Úelementr   Úlinksr   r   ÚneedleÚurl_pathÚindexÚresults	            úJ/opt/saltstack/salt/lib/python3.11/site-packages/libcloud/common/abiquo.pyÚget_hrefr      s¦   € ð` �OŠO˜FÑ#Ô#€EØð ð ˆØŒ;�uÔ Ò$Ð$Ø”;˜vÔ&ˆDð ˆFÝÔ(¨Ñ.Ô.Ô3ˆHØ—M’M &Ñ)Ô)ˆEØ˜e¥c¨&¡k¤kÑ1°AÑ5Ð7Ð7Ô8ˆFØˆMˆMˆMð %ðð ó    c            	       óŠ   — e Zd ZdZej        ej        ej        ej        ej        ej        ej        ej        dœZ	d„ Z
d„ Zd„ ZdS )ÚAbiquoResponsezt
    Abiquo XML Response.

    Wraps the response in XML bodies or extract the error data in
    case of error.
    )ÚNOT_ALLOCATEDÚ	ALLOCATEDÚ
CONFIGUREDÚONÚPAUSEDÚOFFÚLOCKEDÚUNKNOWNc                 ó  — | j         t          j        k    rt          | j        j        ¬¦  «        ‚| j         t          j        k    rt          | j        j        ¦  «        ‚| j         t          j        k    rt          d¦  «        ‚|  
                    ¦   «         }|�_t          |d¦  «        rO|  
                    ¦   «                              d¦  «        }t          |d                              d¦  «        ¦  «        ‚t          | j        ¦  «        ‚)a  
        Parse the error messages.

        Response body can easily be handled by this class parent
        :class:`XmlResponse`, but there are use cases which Abiquo API
        does not respond an XML but an HTML. So we need to
        handle these special cases.
        )ÚdriverzNot AcceptableNr   Úerrorr   Úmessage)Ústatusr   ÚUNAUTHORIZEDr
   Ú
connectionr*   Ú	FORBIDDENÚForbiddenErrorÚNOT_ACCEPTABLEr	   Ú
parse_bodyÚhasattrr   ÚfindtextÚbody)ÚselfÚ	parsebodyÚerrorss      r   Úparse_errorzAbiquoResponse.parse_errorq   sÝ   € ð Œ;�'Ô.Ò.Ð.Ý#¨4¬?Ô+AÐBÑBÔBÐBØŒ[�GÔ-Ò-Ð-Ý  ¤Ô!7Ñ8Ô8Ð8ØŒ[�GÔ2Ò2Ð2ÝÐ 0Ñ1Ô1Ð1àŸšÑ)Ô)ˆIØÐ$­°¸IÑ)FÔ)FÐ$ØŸšÑ*Ô*×2Ò2°7Ñ;Ô;�å# F¨1¤I×$6Ò$6°yÑ$AÔ$AÑBÔBÐBå# D¤IÑ.Ô.Ð.r   c                 ól   — | j         t          j        t          j        t          j        t          j        fv S )zÑ
        Determine if the request was successful.

        Any of the 2XX HTTP response codes are accepted as successful requests

        :rtype:  ``bool``
        :return: successful request or not.
        )r-   r   ÚOKÚCREATEDÚ
NO_CONTENTÚACCEPTED)r7   s    r   ÚsuccesszAbiquoResponse.success‰   s.   € ð Œ{ÝŒJÝŒOÝÔÝÔð	
ð 
ð 	
r   c                 óˆ   — |                       ¦   «         r-|                      ¦   «         }|                     d¦  «        dk    S dS )aé  
        Determinate if async request was successful.

        An async_request retrieves for a task object that can be successfully
        retrieved (self.status == OK), but the asynchronous task (the body of
        the HTTP response) which we are asking for has finished with an error.
        So this method checks if the status code is 'OK' and if the task
        has finished successfully.

        :rtype:  ``bool``
        :return: successful asynchronous request or not
        ÚstateÚFINISHED_SUCCESSFULLYF)r@   r3   r5   )r7   Útasks     r   Úasync_successzAbiquoResponse.async_success™   s@   € ð �<Š<‰>Œ>ð 	à—?’?Ñ$Ô$ˆDØ—=’= Ñ)Ô)Ð-DÒDÐDà�5r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú
TERMINATEDÚPENDINGÚRUNNINGr(   ÚNODE_STATE_MAPr:   r@   rE   © r   r   r    r    ]   s‰   € € € € € ðð ð #Ô-ØÔ&ØÔ'ØÔØÔ#ØÔ ØÔ#ØÔ$ð	ð 	€Nð/ð /ð /ð0
ð 
ð 
ð ð ð ð ð r   r    c                   óJ   ‡ — e Zd ZdZeZ	 	 	 	 	 	 	 	 dˆ fd„	Zd„ Zd„ Zd„ Z	ˆ xZ
S )	ÚAbiquoConnectionzž
    A Connection to Abiquo API.

    Basic :class:`ConnectionUserAndKey` connection with
    :class:`PollingConnection` features for asynchronous tasks.
    TNc                 ól   •— t          ¦   «                              |||||||||	|
¬¦
  «
         i | _        d S )N)
Úuser_idÚkeyÚsecureÚhostÚportÚurlÚtimeoutÚretry_delayÚbackoffÚ	proxy_url)ÚsuperÚ__init__Úcache)r7   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   Ú	__class__s              €r   r]   zAbiquoConnection.__init__¸   sQ   ø€ õ 	‰Œ×ÒØØØØØØØØ#ØØð 	ñ 	
ô 	
ð 	
ð ˆŒ
ˆ
ˆ
r   c                 óÂ   — t          d                     | j        | j        ¦  «        ¦  «        }t	          j        |¦  «                             d¦  «        }d|z   }||d<   |S )a|  
        Add Basic Authentication header to all the requests.

        It injects the 'Authorization: Basic Base64String===' header
        in each request

        :type  headers: ``dict``
        :param headers: Default input headers

        :rtype:         ``dict``
        :return:        Default input headers with the 'Authorization'
                        header
        z{}:{}zutf-8zBasic ÚAuthorization)r   ÚformatrR   rS   Úbase64Ú	b64encodeÚdecode)r7   ÚheadersÚ	b64stringÚencodedÚauthorizations        r   Úadd_default_headersz$AbiquoConnection.add_default_headersÕ   sZ   € õ �g—n’n T¤\°4´8Ñ<Ô<Ñ=Ô=ˆ	ÝÔ" 9Ñ-Ô-×4Ò4°WÑ=Ô=ˆà  7Ñ*ˆà#0ˆ�Ñ Øˆr   c                 óZ   — |j         }t          |d¦  «        }ddi}||d<   d|d<   ||d<   |S )az  
        Manage polling request arguments.

        Return keyword arguments which are passed to the
        :class:`NodeDriver.request` method when polling for the job status. The
        Abiquo Asynchronous Response returns and 'acceptedrequest' XmlElement
        as the following::

            <acceptedrequest>
                <link href="http://uri/to/task" rel="status"/>
                <message>You can follow the progress in the link</message>
            </acceptedrequest>

        We need to extract the href URI to poll.

        :type    response:       :class:`xml.etree.ElementTree`
        :keyword response:       Object returned by poll request.
        :type    request_kwargs: ``dict``
        :keyword request_kwargs: Default request arguments and headers
        :rtype:                  ``dict``
        :return:                 Modified keyword arguments
        r-   ÚAcceptzapplication/vnd.abiquo.task+xmlÚactionÚGETÚmethodrf   )Úobjectr   )r7   ÚresponseÚcontextÚrequest_kwargsÚaccepted_request_objÚ	link_pollÚhdr_polls          r   Úget_poll_request_kwargsz(AbiquoConnection.get_poll_request_kwargsë   sM   € ð.  (œÐÝÐ1°8Ñ<Ô<ˆ	ØÐ?Ð@ˆð $-ˆ�xÑ Ø#(ˆ�xÑ Ø$,ˆ�yÑ!ØÐr   c                 óB   — |j         }|                     d¦  «        }|dv S )a  
        Decide if the asynchronous job has ended.

        :type  response: :class:`xml.etree.ElementTree`
        :param response: Response object returned by poll request
        :rtype:          ``bool``
        :return:         Whether the job has completed
        rB   )rC   ÚABORTEDÚFINISHED_UNSUCCESSFULLY)rp   r5   )r7   rq   rD   Ú
task_states       r   Úhas_completedzAbiquoConnection.has_completed  s.   € ð ŒˆØ—]’] 7Ñ+Ô+ˆ
Øð 
ð 
ð 	
r   )TNNNNNNN)rF   rG   rH   rI   r    ÚresponseClsr]   rj   rw   r|   Ú__classcell__©r_   s   @r   rP   rP   ®   s–   ø€ € € € € ðð ð !€Kð ØØØØØØØðð ð ð ð ð ð:ð ð ð, ð  ð  ðD
ð 
ð 
ð 
ð 
ð 
ð 
r   rP   c                   ó"   ‡ — e Zd ZdZˆ fd„Zˆ xZS )r1   zN
    Exception used when credentials are ok but user has not permissions.
    c                 óP   •— d}t          ¦   «                              ||¦  «         d S )Nz-User has not permission to perform this task.)r\   r]   )r7   r*   r,   r_   s      €r   r]   zForbiddenError.__init__$  s(   ø€ ØAˆÝ‰Œ×Ò˜ &Ñ)Ô)Ð)Ð)Ð)r   )rF   rG   rH   rI   r]   r~   r   s   @r   r1   r1     sB   ø€ € € € € ðð ð*ð *ð *ð *ð *ð *ð *ð *ð *r   r1   )rI   rc   Úlibcloud.utils.py3r   r   r   Úlibcloud.common.baser   r   r   Úlibcloud.common.typesr	   r
   Úlibcloud.compute.baser   r   r    rP   r1   rN   r   r   ú<module>r†      s.  ððð ð €€€à 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø UÐ UÐ UÐ UÐ UÐ UÐ UÐ UÐ UÐ UØ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BØ +Ð +Ð +Ð +Ð +Ð +ð=ð =ð =ð@Nð Nð Nð Nð N�[ñ Nô Nð Nðbn
ð n
ð n
ð n
ð n
Ð+Ð->ñ n
ô n
ð n
ðb*ð *ð *ð *ð *�]ñ *ô *ð *ð *ð *r   