2015-04-21 13:29:10 +03:00
# -*- coding: utf-8 -*-
#
# Copyright (c) 2014 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
2023-06-22 17:04:11 +03:00
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
2015-04-21 13:29:10 +03:00
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''
@author : Adolfo Gómez , dkmaster at dkmon dot com
'''
2023-02-06 04:39:33 +03:00
import typing
import requests
2015-04-21 13:29:10 +03:00
2023-02-06 04:39:33 +03:00
REST_URL : typing . Final [ str ] = ' http://172.27.0.1:8000/rest/ '
2015-04-21 13:29:10 +03:00
2023-02-06 04:39:33 +03:00
# Global session
session = requests . Session ( )
2015-04-21 13:29:10 +03:00
# Hace login con el root, puede usarse cualquier autenticador y cualquier usuario, pero en la 1.5 solo está implementado poder hacer
# este tipo de login con el usuario "root"
def login ( ) :
2023-02-06 04:39:33 +03:00
2015-04-21 13:29:10 +03:00
# parameters = '{ "auth": "admin", "username": "root", "password": "temporal" }'
2023-02-06 04:39:33 +03:00
parameters = { " auth " : " casa " , " username " : " 172.27.0.1 " , " password " : " " }
2015-04-21 13:29:10 +03:00
2023-02-06 04:39:33 +03:00
response = session . post ( REST_URL + ' auth/login ' , parameters )
2015-04-21 13:29:10 +03:00
2023-02-06 04:39:33 +03:00
if response . status_code / / 100 != 2 : # Authentication error due to incorrect parameters, bad request, etc...
print ( " Authentication error " )
2015-04-21 13:29:10 +03:00
return - 1
# resp contiene las cabeceras, content el contenido de la respuesta (que es json), pero aún está en formato texto
2023-02-06 04:39:33 +03:00
res = response . json ( )
print ( res )
2015-04-21 13:29:10 +03:00
if res [ ' result ' ] != ' ok ' : # Authentication error
2023-02-06 04:39:33 +03:00
print ( " Authentication error " )
2015-04-21 13:29:10 +03:00
return - 1
2023-02-06 04:39:33 +03:00
session . headers [ ' X-Auth-Token ' ] = res [ ' token ' ]
2015-04-21 13:29:10 +03:00
return 0
def logout ( ) :
2023-02-06 04:39:33 +03:00
response = session . get ( REST_URL + ' auth/logout ' )
2015-04-21 13:29:10 +03:00
2023-02-06 04:39:33 +03:00
if response . status_code / / 100 != 2 : # error due to incorrect parameters, bad request, etc...
print ( " Error requesting logout %s " % response . status_code )
2015-04-21 13:29:10 +03:00
return - 1
# Return value of logout method is nonsense (returns always done right now, but it's not important)
return 0
# Sample response from request_pools
# [
# {
# u'initial_srvs': 0,
# u'name': u'WinAdolfo',
# u'max_srvs': 0,
# u'comments': u'',
# u'id': 6,
# u'state': u'A',
# u'user_services_count': 3,
# u'cache_l2_srvs': 0,
# u'service_id': 9,
# u'provider_id': 2,
# u'cache_l1_srvs': 0,
# u'restrained': False}
# ]
2023-02-06 04:39:33 +03:00
def request_services ( ) - > typing . Dict [ str , typing . Any ] :
response = session . get ( REST_URL + ' connection ' )
if response . status_code / / 100 != 2 :
print ( " Error requesting services %s " % response . status_code )
print ( response . text )
2015-04-21 13:29:10 +03:00
return { }
2023-02-06 04:39:33 +03:00
return response . json ( )
2015-04-21 13:29:10 +03:00
if __name__ == ' __main__ ' :
if login ( ) == 0 : # If we can log in, will get the pools correctly
res = request_services ( )
2023-02-06 04:39:33 +03:00
print ( res )
print ( logout ( ) )