2014-06-26 23:14:36 +04: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.
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
# 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
'''
from __future__ import unicode_literals
from httplib2 import Http
import json
2021-03-22 13:42:11 +03:00
import sys
2014-06-26 23:14:36 +04:00
2021-03-22 13:42:11 +03:00
rest_url = ' http://172.27.0.1:8000/uds/rest/ '
2014-06-26 23:14:36 +04:00
headers = { }
# 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 ( ) :
global headers
h = Http ( )
2014-06-30 16:10:20 +04:00
# parameters = '{ "auth": "admin", "username": "root", "password": "temporal" }'
2021-03-22 13:42:11 +03:00
# parameters = '{ "auth": "interna", "username": "admin", "password": "temporal" }'
parameters = ' { " auth " : " interna " , " username " : " admin.1 " , " password " : " temporal " } '
2014-06-26 23:14:36 +04:00
resp , content = h . request ( rest_url + ' auth/login ' , method = ' POST ' , body = parameters )
if resp [ ' status ' ] != ' 200 ' : # Authentication error due to incorrect parameters, bad request, etc...
2021-03-22 13:42:11 +03:00
print ( " Authentication error " )
2014-06-26 23:14:36 +04:00
return - 1
# resp contiene las cabeceras, content el contenido de la respuesta (que es json), pero aún está en formato texto
res = json . loads ( content )
2021-03-22 13:42:11 +03:00
print ( res )
2014-06-26 23:14:36 +04:00
if res [ ' result ' ] != ' ok ' : # Authentication error
2021-03-22 13:42:11 +03:00
print ( " Authentication error " )
2014-06-26 23:14:36 +04:00
return - 1
headers [ ' X-Auth-Token ' ] = res [ ' token ' ]
return 0
2017-11-06 12:56:35 +03:00
2014-06-26 23:14:36 +04:00
def logout ( ) :
global headers
h = Http ( )
resp , content = h . request ( rest_url + ' auth/logout ' , headers = headers )
if resp [ ' status ' ] != ' 200 ' : # Logout error due to incorrect parameters, bad request, etc...
2021-03-22 13:42:11 +03:00
print ( " Error requesting logout " )
2014-06-26 23:14:36 +04: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}
# ]
def request_pools ( ) :
h = Http ( )
resp , content = h . request ( rest_url + ' servicespools/overview ' , headers = headers )
if resp [ ' status ' ] != ' 200 ' : # error due to incorrect parameters, bad request, etc...
2021-03-22 13:42:11 +03:00
print ( " Error requesting pools " )
2014-06-26 23:14:36 +04:00
return { }
return json . loads ( content )
# PATH: /rest/providers/[provider_id]/services/[service_id]
def request_service_info ( provider_id , service_id ) :
h = Http ( )
resp , content = h . request ( rest_url + ' providers/ {0} /services/ {1} ' . format ( provider_id , service_id ) , headers = headers )
if resp [ ' status ' ] != ' 200 ' : # error due to incorrect parameters, bad request, etc...
2021-03-22 13:42:11 +03:00
print ( " Error requesting pools: response: {} , content: {} " . format ( resp , content ) )
2017-06-29 11:36:36 +03:00
return None
2014-06-26 23:14:36 +04:00
return json . loads ( content )
if __name__ == ' __main__ ' :
2014-06-30 16:10:20 +04:00
# request_pools() # Not logged in, this will generate an error
2014-06-26 23:14:36 +04:00
if login ( ) == 0 : # If we can log in, will get the pools correctly
res = request_pools ( )
2021-03-22 13:42:11 +03:00
print ( res )
sys . exit ( 0 )
2014-06-26 23:14:36 +04:00
for r in res :
res2 = request_service_info ( r [ ' provider_id ' ] , r [ ' service_id ' ] )
2017-06-29 11:36:36 +03:00
if res2 is not None :
2021-03-22 13:42:11 +03:00
print ( " Base Service info por pool {0} : {1} " . format ( r [ ' name ' ] , res2 [ ' type ' ] ) )
2017-06-29 11:36:36 +03:00
else :
2021-03-22 13:42:11 +03:00
print ( " Base service {} is not accesible " . format ( r [ ' name ' ] ) )
print ( " First logout " )
print ( logout ( ) ) # This will success
print ( " Second logout " )
print ( logout ( ) ) # This will fail (already logged out)
2014-06-26 23:14:36 +04:00
# Also new requests will fail
2021-03-22 13:42:11 +03:00
print ( request_pools ( ) )
2017-06-29 11:36:36 +03:00
# Until we do log in again
2014-06-26 23:14:36 +04:00
login ( )
2021-03-22 13:42:11 +03:00
print ( request_pools ( ) )
2014-06-26 23:14:36 +04:00