def configAppliance():
    '''interactively configure an appliance'''

    appName = prompt("\nEnter the name of an existing/new appliance: ", default=None)
    assert appName

    oldHost = oldUser = oldPasswd = None
    
    if appName in getAppliances():
        oldHost, oldUser, oldPasswd = getAppInfo(appName)

        ansi.printc('\nwhat do you want to do with the appliance %s' % appName)
        ansi.writec('type '); ansi.writec('c', colors.PARAM_COLOR);     ansi.printc(' to change the appliance configurations')
        ansi.writec('type '); ansi.writec('r', colors.PARAM_COLOR);     ansi.printc(' to remove the appliance from the setup file')
        ansi.writec('type '); ansi.writec('v', colors.PARAM_COLOR);     ansi.printc(' to view info about the appliance\'s configuration')
        ansi.writec('type '); ansi.writec('check', colors.PARAM_COLOR); ansi.printc(' to check the credentials of the appliance')
        response = prompt('\n', choices='options: c, r, v, check', default='v')
        
        if response == 'c':
            print 'Changing an appliance isn\'t working right now \n the setup file must be manually changed'
            pass
        elif response == 'v':
            displayApplianceInfo(appName)
            return
        elif response == 'r':
            delAppliance(appName)
            return
        elif response == 'check':
            checkCredentials(oldHost, oldUser, oldPasswd)
            return
        else:
            ansi.eprintc('not a valid option', colors.ERROR_COLOR)
            return

    host =   prompt('\nwhat is the ip address for "%s"'             % appName, default=oldHost)
    name =   prompt('\nwhat is the username for the appliance "%s"' % appName, default=oldUser)
    passwd = prompt('\nwhat is the password for "%s"'               % appName, default=oldPasswd)

    if prompt_bool('\nWould you like to check the entered credentials for "%s"? (%s must be running):' % (appName, appName)):
        if not checkCredentials(host, name, passwd):
            ansi.printc('failed to set up %s' % appName, colors.WARNING_COLOR)
            return
            
    print '\n%s successfully configured. You can now use the appliance %s ' \
          'to run tests by using the command' % (appName, appName)
    ansi.printc('sb test --psa %s\n' % appName, colors.TITLE_COLOR)

    setAppliance(appName, host, name, passwd)
    return
def configLocation():
    '''interactively configure a location for local tests'''
    while True:
        ansi.writec('\nwhere are you located(')
        ansi.writec('ps-orem, DSR, or other', colors.PARAM_COLOR)
        ansi.writec(') this is for tests like the NetApp connnector tests that require a local machine: ')

        response = raw_input().lower().strip()
        if response == 'ps-orem' or response == 'orem':
            setLocation('ps-orem')  
            break
        if response == 'dsr':
            setLocation('dsr')      
            break
        if response == 'other':
            setLocation('other')       
            break   
        ansi.eprintc('that location does not yet exist', colors.WARNING_COLOR)
def checkCredentials(host, user, passwd):
    '''checks if a login is possible with the credentials entered for an appliance'''
    #print not Working
    try:
        import requests, socket
        session = requests.session(
            verify=False
        )  #the session will store the login cookies once we're logged in

        #check if the PSA is up
        try:
            socket.create_connection((host, '80')).close()
        except:
            raise Exception(
                'could not connect to the PSA at %s. Is the PSA is running?' %
                host)

        #logout if we are logged in
        try:
            session.get('https://%s/account/logout' % host)
        except:
            pass

        #try to login to the PSA
        loginResponse = session.get(
            'https://%s/account/dologin?login=%s&password=%s' %
            (host, user, passwd))
        whoami = session.get('https://%s/appliance/whoami' % host)

        assert loginResponse and whoami, 'bad or no status code(s) recieved.'
        if '<username>%s</username>' % user not in whoami.content:
            raise Exception(
                'After attempting to login, the user %s was not found in the whoami response.'
                % user)

        ansi.printc('successfully logged into %s' % host, colors.CMD_COLOR)
        return True

    except Exception, exc:
        errorMsg = 'Error logging into Existing Appliance object with the credentials\n' \
                   + 'host: %s  username: %s  password: %s\n' % (host, user, passwd) \
                   + str(exc)
        ansi.eprintc(errorMsg, colors.ERROR_COLOR)
        return False
def configLocation():
    '''interactively configure a location for local tests'''
    while True:
        ansi.writec('\nwhere are you located(')
        ansi.writec('ps-orem, DSR, or other', colors.PARAM_COLOR)
        ansi.writec(
            ') this is for tests like the NetApp connnector tests that require a local machine: '
        )

        response = raw_input().lower().strip()
        if response == 'ps-orem' or response == 'orem':
            setLocation('ps-orem')
            break
        if response == 'dsr':
            setLocation('dsr')
            break
        if response == 'other':
            setLocation('other')
            break
        ansi.eprintc('that location does not yet exist', colors.WARNING_COLOR)
def checkCredentials(host, user, passwd):
    '''checks if a login is possible with the credentials entered for an appliance'''
    #print not Working
    try:
        import requests, socket
        session = requests.session(verify=False) #the session will store the login cookies once we're logged in
        
        #check if the PSA is up
        try: 
            socket.create_connection((host, '80')).close()
        except: 
            raise Exception('could not connect to the PSA at %s. Is the PSA is running?' % host)

        #logout if we are logged in
        try: 
            session.get('https://%s/account/logout' % host)
        except: 
            pass
        
        #try to login to the PSA
        loginResponse = session.get('https://%s/account/dologin?login=%s&password=%s' % (host, user, passwd))
        whoami = session.get('https://%s/appliance/whoami' % host) 

        assert loginResponse and whoami, 'bad or no status code(s) recieved.'
        if '<username>%s</username>' % user not in whoami.content:
            raise Exception('After attempting to login, the user %s was not found in the whoami response.' % user)
        
        ansi.printc('successfully logged into %s'%host, colors.CMD_COLOR)
        return True
        
    except Exception, exc:
        errorMsg = 'Error logging into Existing Appliance object with the credentials\n' \
                   + 'host: %s  username: %s  password: %s\n' % (host, user, passwd) \
                   + str(exc)
        ansi.eprintc(errorMsg, colors.ERROR_COLOR)
        return False
def configAppliance():
    '''interactively configure an appliance'''

    appName = prompt("\nEnter the name of an existing/new appliance: ",
                     default=None)
    assert appName

    oldHost = oldUser = oldPasswd = None

    if appName in getAppliances():
        oldHost, oldUser, oldPasswd = getAppInfo(appName)

        ansi.printc('\nwhat do you want to do with the appliance %s' % appName)
        ansi.writec('type ')
        ansi.writec('c', colors.PARAM_COLOR)
        ansi.printc(' to change the appliance configurations')
        ansi.writec('type ')
        ansi.writec('r', colors.PARAM_COLOR)
        ansi.printc(' to remove the appliance from the setup file')
        ansi.writec('type ')
        ansi.writec('v', colors.PARAM_COLOR)
        ansi.printc(' to view info about the appliance\'s configuration')
        ansi.writec('type ')
        ansi.writec('check', colors.PARAM_COLOR)
        ansi.printc(' to check the credentials of the appliance')
        response = prompt('\n', choices='options: c, r, v, check', default='v')

        if response == 'c':
            print 'Changing an appliance isn\'t working right now \n the setup file must be manually changed'
            pass
        elif response == 'v':
            displayApplianceInfo(appName)
            return
        elif response == 'r':
            delAppliance(appName)
            return
        elif response == 'check':
            checkCredentials(oldHost, oldUser, oldPasswd)
            return
        else:
            ansi.eprintc('not a valid option', colors.ERROR_COLOR)
            return

    host = prompt('\nwhat is the ip address for "%s"' % appName,
                  default=oldHost)
    name = prompt('\nwhat is the username for the appliance "%s"' % appName,
                  default=oldUser)
    passwd = prompt('\nwhat is the password for "%s"' % appName,
                    default=oldPasswd)

    if prompt_bool(
            '\nWould you like to check the entered credentials for "%s"? (%s must be running):'
            % (appName, appName)):
        if not checkCredentials(host, name, passwd):
            ansi.printc('failed to set up %s' % appName, colors.WARNING_COLOR)
            return

    print '\n%s successfully configured. You can now use the appliance %s ' \
          'to run tests by using the command' % (appName, appName)
    ansi.printc('sb test --psa %s\n' % appName, colors.TITLE_COLOR)

    setAppliance(appName, host, name, passwd)
    return