def createSharedIpGroup():
    """
    Creates a shared IP group with entered name and single server id.

    Shows:
        how to poll while waiting for a server to be created.
    """
    print "Shared IP Group Name to Create: "
    name = stdin.readline().strip()

    print "Id of first server in group: "
    server = None
    found = False
    id = 0
    while not found and id != -1:
        id = getServerId()
        server = serverManager.find(id)
        found = (server != None)

    if found:
        ipg = SharedIpGroup(name, server.id )
        # Create doesn't return anything, but fills in the ipgroup with info
        sharedIpGroupManager.create(ipg)
        print "IP group is now:"
        pprint(ipg)
def resizeServer():
    """
    Resizes a server and asks you to confirm the resize.
    """
    id = getServerId()

    # Find is guaranteed not to throw a not-found exception
    server = serverManager.find(id)
    if server:
        print "Server: ", server
    else:
        print "Server not found"
        
    flavorId = 2    
    if server.flavorId == 2:
        flavorId = 1
    
    print "Resizing to Flavor ID ", flavorId
    serverManager.resize(server, flavorId)
    serverManager.wait(server)
    
    print "Done!  Ready to confirm or revert?\
           Type confirm or revert or press enter to do nothing:"
    action = stdin.readline().strip()
    
    if action == 'confirm':
        serverManager.confirmResize(server)
        serverManager.wait(server)
    elif action == 'revert':
        serverManager.revertResize(server)
        serverManager.wait(server)
        
    print "Done!"
    print "Server: ", server
def rebootServer():
    """
    Reboot a server, prompting for `id`
    """
    id = getServerId()
    serverToReboot = serverManager.find(id)
    if not serverToReboot:  # find() returns None on failure to find server
        print "Server not found %s" % id
        return

    print "Hard or Soft (h/S): "
    hard_soft = stdin.readline().strip()
    if hard_soft in "Hh":
        rType  = rebootType.hard
    else:
        rType = rebootType.soft

    sleepTime = getSleepTime()  # Get sleep time to avoid overlimit fault
    serverManager.reboot(serverToReboot, rType)
    status = serverToReboot.status
    while status != u"ACTIVE":
        status = serverToReboot.status
        print "Status   : ", serverToReboot.status
        print "Progress : ", serverToReboot.progress
        print "Sleeping : ", sleepTime
        sleep(sleepTime)        # pacing to avoid overlimit fault

    print "Rebooted!"
def addServerToIpGroup():
    """
    Add server to IP Group by id
    """
    serverId = getServerId()
    server = serverManager.find(serverId)
    print "server: ", server
    sharedIpGroupId = getSharedIpGroupId()
    sharedIpGroup = sharedIpGroupManager.find(sharedIpGroupId)
    print "shared ip group: ", sharedIpGroup
    ipAddress = getIpAddress()
    serverManager.shareIp(server, ipAddress, sharedIpGroupId, True)
    pass
def deleteServer():
    """
    Get user input of server ID and delete it
    """
    id = getServerId()
    serverToDelete = serverManager.find(id)

    if not serverToDelete:  # find() returns None on failure to find server
        print "Server not found %s" % id
    else:
        pprint(serverToDelete)
        status = serverManager.remove(serverToDelete)
        pprint(status)
def showDetails():
    """
    Get user input of server ID just show raw status object for that id

    Shows:
        Catching the 404 error by hand instead of using find()
    """
    id = getServerId()
    try:
        server = serverManager.find(id)
    except CloudServersFault, cf:
        if cf.code == 404:
            print "Server not found"
            return
def showBackupSchedule():
    """
    Show server's backup schedule.
    """
    id = getServerId()

    # Find is guaranteed not to throw a not-found exception
    server = serverManager.find(id)

    if server:
        schedule = serverManager.getSchedule(server)
        print "Backup schedule of server: ", id
        print schedule
    else:
        print "Server not found"
def showStatus():
    """
    Get user input of server ID just show raw status object for that id

    Shows:
        Using find() to check an ID since it will just return None and not
        raise an exception.
    """
    id = getServerId()

    # Find is guaranteed not to throw a not-found exception
    server = serverManager.find(id)

    if server:
        status = serverManager.status(id)
        print "Status of server: ", id
        pprint(status)
    else:
        print "Server not found"
def setBackupSchedule():
    """
    Set server's backup schedule
    """
    id = getServerId()

    # Find is guaranteed not to throw a not-found exception
    server = serverManager.find(id)

    if server:
        backupSchedule = serverManager.getSchedule(server)
        print "Backup schedule of server: ", id
        print backupSchedule
        newbs = BackupSchedule(True, daily="H_0000_0200", weekly="SUNDAY")
        serverManager.setSchedule(server, newbs)
        backupSchedule = serverManager.getSchedule(server)
        print "Backup schedule of server: ", id
        print backupSchedule
    else:
        print "Server not found"