
from psphere.client import Client
from psphere.scripting import BaseScript

class Connect(BaseScript):
    def connect(self):
        """A simple connection test to login and print the server time."""
        print(self.client.si.CurrentTime())

def main():
    client = Client()
    print('Successfully connected to %s' % client.server)
    c = Connect(client)
    c.connect()
    client.logout()

if __name__ == '__main__':
    main()


from psphere import config
from psphere.client import Client

def main(options):
    """A simple connection test to login and print the server time."""
    server = config._config_value("general", "server", options.server)
    if server is None:
        raise ValueError("server must be supplied on command line"
                         " or in configuration file.")
    username = config._config_value("general", "username", options.username)
    if username is None:
        raise ValueError("username must be supplied on command line"
                         " or in configuration file.")
    password = config._config_value("general", "password", options.password)
    if password is None:
        raise ValueError("password must be supplied on command line"
                         " or in configuration file.")

    client = Client(server=server, username=username, password=password)
    print('Successfully connected to %s' % client.server)
    print(client.si.CurrentTime())
    client.logout()

if __name__ == "__main__":
    from optparse import OptionParser
    usage = "Usage: %prog [options]"
    parser = OptionParser(usage=usage)
    parser.add_option("--server", dest="server",
                      help="The server to connect to for provisioning")
    parser.add_option("--username", dest="username",
                      help="The username used to connect to the server")
    parser.add_option("--password", dest="password",
                      help="The password used to connect to the server")

    (options, args) = parser.parse_args()
    main(options)

Update the script to accept arguments
Update the script to accept arguments

  
+ from psphere import config
  from psphere.client import Client
- from psphere.scripting import BaseScript
  
+ def main(options):
- class Connect(BaseScript):
-     def connect(self):
-         """A simple connection test to login and print the server time."""
+     """A simple connection test to login and print the server time."""
-         print(self.client.si.CurrentTime())
+     server = config._config_value("general", "server", options.server)
+     if server is None:
+         raise ValueError("server must be supplied on command line"
+                          " or in configuration file.")
+     username = config._config_value("general", "username", options.username)
+     if username is None:
+         raise ValueError("username must be supplied on command line"
+                          " or in configuration file.")
+     password = config._config_value("general", "password", options.password)
+     if password is None:
+         raise ValueError("password must be supplied on command line"
+                          " or in configuration file.")
  
+     client = Client(server=server, username=username, password=password)
- def main():
-     client = Client()
      print('Successfully connected to %s' % client.server)
+     print(client.si.CurrentTime())
-     c = Connect(client)
-     c.connect()
      client.logout()
  
- if __name__ == '__main__':
+ if __name__ == "__main__":
-     main()
+     from optparse import OptionParser
+     usage = "Usage: %prog [options]"
+     parser = OptionParser(usage=usage)
+     parser.add_option("--server", dest="server",
+                       help="The server to connect to for provisioning")
+     parser.add_option("--username", dest="username",
+                       help="The username used to connect to the server")
+     parser.add_option("--password", dest="password",
+                       help="The password used to connect to the server")
  
+     (options, args) = parser.parse_args()
+     main(options)
+ 
Update the script to accept arguments
## Code Before:

from psphere.client import Client
from psphere.scripting import BaseScript

class Connect(BaseScript):
    def connect(self):
        """A simple connection test to login and print the server time."""
        print(self.client.si.CurrentTime())

def main():
    client = Client()
    print('Successfully connected to %s' % client.server)
    c = Connect(client)
    c.connect()
    client.logout()

if __name__ == '__main__':
    main()

## Instruction:
Update the script to accept arguments
## Code After:

from psphere import config
from psphere.client import Client

def main(options):
    """A simple connection test to login and print the server time."""
    server = config._config_value("general", "server", options.server)
    if server is None:
        raise ValueError("server must be supplied on command line"
                         " or in configuration file.")
    username = config._config_value("general", "username", options.username)
    if username is None:
        raise ValueError("username must be supplied on command line"
                         " or in configuration file.")
    password = config._config_value("general", "password", options.password)
    if password is None:
        raise ValueError("password must be supplied on command line"
                         " or in configuration file.")

    client = Client(server=server, username=username, password=password)
    print('Successfully connected to %s' % client.server)
    print(client.si.CurrentTime())
    client.logout()

if __name__ == "__main__":
    from optparse import OptionParser
    usage = "Usage: %prog [options]"
    parser = OptionParser(usage=usage)
    parser.add_option("--server", dest="server",
                      help="The server to connect to for provisioning")
    parser.add_option("--username", dest="username",
                      help="The username used to connect to the server")
    parser.add_option("--password", dest="password",
                      help="The password used to connect to the server")

    (options, args) = parser.parse_args()
    main(options)
