#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Thomas Dreibholz's PlanetLab/NorNet Script Collection
# Copyright (C) 2019-2023 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: dreibh@simula.no

import sys;

# XML-RPC
if sys.version_info < (3,0,0):
   import xmlrpclib;
else:
   import xmlrpc.client;



# ###### Main program #######################################################
if len(sys.argv) < 4:
   sys.stderr.write('Usage: ' + sys.argv[0] + ' URL user password\n')
   sys.exit(1)

apiURL = sys.argv[1]
if apiURL == 'default':
   apiURL = 'https://www.planet-lab.eu/PLCAPI/'
user     = sys.argv[2]
password = sys.argv[3]


if sys.version_info < (3,0,0):
   plc_server = xmlrpclib.ServerProxy(apiURL, allow_none=True)
else:
   plc_server = xmlrpc.client.ServerProxy(apiURL, allow_none=True)


plc_authentication = {}
plc_authentication['AuthMethod'] = 'password'
plc_authentication['Username']   = user
plc_authentication['AuthString'] = password

nodeList = plc_server.GetNodes(plc_authentication, 
                               { 'boot_state' : 'boot' },
                               ['node_id', 'site_id', 'hostname', 'model', 'boot_state'])
for node in nodeList:
   print node['hostname']
