2021-09-29 00:14:02 +02:00
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021 Virtual Cable S.L.U.
# 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 16:04:11 +02:00
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
2021-09-29 00:14:02 +02: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 02:39:33 +01:00
import typing
import asyncio
import aiohttp
2021-09-29 00:14:02 +02:00
import typing
2023-02-06 02:39:33 +01:00
REST_URL : typing . Final [ str ] = ' http://172.27.0.1:8000/uds/rest/ '
2021-09-29 00:14:02 +02:00
class RESTException ( Exception ) :
pass
class AuthException ( RESTException ) :
pass
class LogoutException ( RESTException ) :
pass
# 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"
2023-02-06 02:39:33 +01:00
async def login ( session : aiohttp . ClientSession ) - > None :
2021-09-29 00:14:02 +02:00
# parameters = '{ "auth": "admin", "username": "root", "password": "temporal" }'
# parameters = '{ "auth": "interna", "username": "admin", "password": "temporal" }'
parameters = { ' auth ' : ' interna ' , ' username ' : ' admin ' , ' password ' : ' temporal ' }
2023-02-06 02:39:33 +01:00
response = await session . post ( REST_URL + ' auth/login ' , json = parameters )
2021-09-29 00:14:02 +02:00
if not response . ok :
raise AuthException ( ' Error logging in ' )
# resp contiene las cabeceras, content el contenido de la respuesta (que es json), pero aún está en formato texto
2023-02-06 02:39:33 +01:00
res = await response . json ( )
2021-09-29 00:14:02 +02:00
print ( res )
if res [ ' result ' ] != ' ok ' : # Authentication error
raise AuthException ( ' Authentication error ' )
session . headers . update ( { ' X-Auth-Token ' : res [ ' token ' ] } )
2023-02-06 02:39:33 +01:00
async def logout ( session : aiohttp . ClientSession ) - > None :
response = await session . get ( REST_URL + ' auth/logout ' )
2021-09-29 00:14:02 +02:00
if not response . ok :
raise LogoutException ( ' Error logging out ' )
# 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 02:39:33 +01:00
async def request_pools ( session : aiohttp . ClientSession ) - > typing . List [ typing . MutableMapping [ str , typing . Any ] ] :
response = await session . get ( REST_URL + ' servicespools/overview ' )
2021-09-29 00:14:02 +02:00
if not response . ok :
raise RESTException ( ' Error requesting pools ' )
2023-02-06 02:39:33 +01:00
return await response . json ( )
2021-09-29 00:14:02 +02:00
2023-02-06 02:39:33 +01:00
async def request_ticket (
session : aiohttp . ClientSession ,
2021-09-29 00:14:02 +02:00
username : str ,
authSmallName : str ,
groups : typing . Union [ typing . List [ str ] , str ] ,
servicePool : str ,
realName : typing . Optional [ str ] = None ,
transport : typing . Optional [ str ] = None ,
force : bool = False
) - > typing . MutableMapping [ str , typing . Any ] :
data = {
' username ' : username ,
' authSmallName ' : authSmallName ,
' groups ' : groups ,
' servicePool ' : servicePool ,
' force ' : ' true ' if force else ' false '
}
if realName :
data [ ' realname ' ] = realName
if transport :
data [ ' transport ' ] = transport
2023-02-06 02:39:33 +01:00
response = await session . put (
REST_URL + ' tickets/create ' ,
2021-09-29 00:14:02 +02:00
json = data
)
if not response . ok :
2023-02-06 02:39:33 +01:00
raise RESTException ( ' Error requesting ticket: %s ( %s ) ' % ( response . status , response . reason ) )
2021-09-29 00:14:02 +02:00
2023-02-06 02:39:33 +01:00
return await response . json ( )
async def main ( ) :
async with aiohttp . ClientSession ( ) as session :
# request_pools() # Not logged in, this will generate an error
await login ( session ) # Will raise an exception if error
#pools = request_pools()
#for i in pools:
# print(i['id'], i['name'])
ticket = await request_ticket (
session = session ,
username = ' adolfo ' ,
authSmallName = ' 172.27.0.1:8000 ' ,
groups = [ ' adolfo ' , ' dkmaster ' ] ,
servicePool = ' 6201b357-c4cd-5463-891e-71441a25faee ' ,
realName = ' Adolfo Gómez ' ,
force = True
)
print ( ticket )
await logout ( session )
if __name__ == " __main__ " :
loop = asyncio . new_event_loop ( )
loop . run_until_complete ( main ( ) )