# ********************************************************************
# Copyright (c) Omnissa, LLC. All rights reserved.
# This product is protected by copyright and intellectual property laws in the
# United States and other countries as well as by international treaties.
# -- Omnissa Restricted
# ********************************************************************

import getopt
import logging
import requests
import sys

from httptask import *

# Access and Control VADC agent from /vadc/admin path
# We only support ListSession, DisconnectSession and
# killSession now.

if __name__ == "__main__":
   logging.root.level = logging.INFO
   try:
      opts, args = getopt.getopt(sys.argv[1:], "s:c:u:")
   except getopt.GetoptError:
      sys.exit(1)

   host = "127.0.0.1"
   command = ""
   username = ""

   for opt, val in opts:
      if opt == "-s":
         host = val
      elif opt == "-c":
         command = val
      elif opt == "-u":
         username = val
      else:
         logging.error("Invalid parameter. Usage: adminTools -h <ipaddress> -c <command>")
         sys.exit(1)

   if command == "ListSession":
      task = HttpDoListSessionTask(host)
      if not task.processTask():
         logging.error("Failed to execute " + command)
         sys.exit(1)
      else:
         print(task.getResponse())
   elif command == "DisconnectSession":
      task = HttpDoDisconnectSessionTask(username, host)
      if not task.processTask():
         logging.error("Failed to execute " + command)
         sys.exit(1)
      else:
         print("ok")
   elif command == "KillSession":
      task = HttpDoKillSessionTask(username, host)
      if not task.processTask():
         logging.error("Failed to execute " + command)
         sys.exit(1)
      else:
         print("ok")
   else:
      logging.error("Unknown command " + command)
      sys.exit(1)
