def _add_credentials(self, primary, credentials): """ Add credentials to the SUSE Manager database. """ if not credentials: user = cli_ask( msg=("User to add")) pw = cli_ask( msg=("Password to add"), password=True) if not pw == cli_ask(msg=("Confirm password"),password=True): self.log.error("Passwords do not match") print("Passwords do not match") self.exit_with_error = True return else: user = credentials[0] pw = credentials[1] saved_users = self._fetch_credentials() if any(user == saved_user['user'] for saved_user in saved_users): self.log.error("Credentials already exist") print("Credentials already exist") self.exit_with_error = True else: self._execute_xmlrpc_method(self.conn.sync.content, "addCredentials", self.auth.token(), user, pw, primary) self.log.info("Successfully added credentials.") print("Successfully added credentials.")
def _get_credentials_interactive(self): """ Get credentials from CLI interactively. """ print("Please enter the credentials of SUSE Manager Administrator.") self.user = cli_ask("Login") self.password = cli_ask("Password", password=True) self.credentials_prompts += 1
def _delete_credentials(self, credentials): """ Delete credentials from the SUSE Manager database. If the credentials list is empty interactive mode is used. """ interactive = False if not credentials: credentials = self._delete_credentials_interactive_mode() interactive = True saved_credentials = self._fetch_credentials() for user in credentials: if any(user == saved_user['user'] for saved_user in saved_credentials): if interactive: confirm = cli_ask( msg=("Really delete credentials '{0}'? (y/n)".format(user))) if not re.search("[yY]", confirm): return self._execute_xmlrpc_method(self.conn.sync.content, "deleteCredentials", self.auth.token(), user) self.log.info("Successfully deleted credentials: {0}".format( user)) print("Successfully deleted credentials: {0}".format( user)) else: self.log.error("Credentials not found in database: {0}".format(user)) print("Credentials not found in database: {0}".format(user)) self.exit_with_error = True
def _select_product_interactive_mode(self): """Show not installed products prefixing a number, then reads user input and returns the label of the chosen product """ interactive_data = self._list_products( filter=None, expand=False, show_interactive_numbers=True) num_prod = interactive_data['num_prod'] if num_prod: validator = lambda i: re.search("\d+", i) and \ int(i) in range(1, len(list(num_prod.keys())) + 1) choice = cli_ask( msg=("Enter product number (1-{0})".format( len(list(num_prod.keys())))), validator=validator) self.log.info("Selecting product '{0} {1}' from choice '{2}'".format( num_prod[int(choice)].friendly_name, num_prod[int(choice)].arch, choice)) return num_prod[int(choice)] else: self.log.info("All the available products have already been " "installed, nothing to do") print("All the available products have already been installed, " "nothing to do") return None
def _select_product_interactive_mode(self): """Show not installed products prefixing a number, then reads user input and returns the label of the chosen product """ interactive_data = self._list_products( filter=None, expand=False, show_interactive_numbers=True) if interactive_data is not None and 'num_prod' in interactive_data: num_prod = interactive_data['num_prod'] if num_prod: choice = cli_ask( msg=("Enter product number (1-{0})".format(len(num_prod.keys()))), validator=[str(i) for i in list(range(1, len(num_prod.keys()) + 1))]) self.log.info("Selecting product '{0} {1}' from choice '{2}'".format( num_prod[int(choice)].friendly_name, num_prod[int(choice)].arch, choice)) return num_prod[int(choice)] else: self.log.info("All the available products have already been " "installed, nothing to do") print("All the available products have already been installed, " "nothing to do") return None else: self.log.info("Have you run 'mgr-sync refresh'?") print("Have you run 'mgr-sync refresh'?")
def test_cli_ask_without_validator(self): message = "how are you" response = "I'm fine" with FakeStdin(None, response) as mocked_input: value = cli_ask(message) self.assertEqual(response, value) self.assertEqual(2, mocked_input.call_count)
def test_cli_ask_with_custom_validator(self): message = "how old are you?" response = "10" validator = lambda i: re.search("\d+", i) and int(i) in range(1, 19) with FakeStdin("young", "0", "40", "10") as mocked_input: value = cli_ask(message, validator=validator) self.assertEqual(response, value) self.assertEqual(4, mocked_input.call_count)
def test_cli_ask_with_regexp_validator(self): message = "how old are you?" response = "18" validator = "\d+" with FakeStdin("young", response) as mocked_input: value = cli_ask(message, validator=validator) self.assertEqual(response, value) self.assertEqual(2, mocked_input.call_count)
def test_cli_ask_with_list_validator(self): message = "how are you?" response = "happy" validator = ["happy", "blue"] with FakeStdin("angry", response) as mocked_input: value = cli_ask(message, validator=validator) self.assertEqual(response, value) self.assertEqual(2, mocked_input.call_count)
def _delete_credentials_interactive_mode(self): """ Show saved credentials prefixed with a number, read the user input and return the chosen credential. """ saved_credentials = self._fetch_credentials() self._list_credentials(True) number = cli_ask( msg=("Enter credentials number (1-{0})".format(len(saved_credentials))), validator=[str(i) for i in list(range(1, len(saved_credentials)+1))]) self.log.info("Selecting credentials '{0}' from choice '{1}'".format( saved_credentials[int(number)-1]['user'], number)) return [saved_credentials[int(number)-1]['user']]
def _select_channel_interactive_mode(self, no_optionals=False, only_installed=False): """Show not installed channels prefixing a number, then reads user input and returns the label of the chosen channel """ channels = self._list_channels( expand=False, filter=None, compact=False, no_optionals=no_optionals, show_interactive_numbers=True, only_installed=only_installed) choice = cli_ask( msg=("Enter channel number (1-{0})".format(len(channels))), validator=[str(i) for i in list(range(1, len(channels)+1))]) self.log.info("Selecting channel '{0}' from choice '{1}'".format( channels[int(choice)-1], choice)) return channels[int(choice)-1]
def _delete_credentials_interactive_mode(self): """ Show saved credentials prefixed with a number, read the user input and return the chosen credential. """ credentials = []; saved_credentials = self._fetch_credentials() validator = lambda i: re.search("\d+", i) and \ int(i) in range(1, len(saved_credentials)+1) self._list_credentials(True); number = cli_ask( msg=("Enter credentials number (1-{0})".format(len(saved_credentials))), validator=validator) self.log.info("Selecting credentials '{0}' from choice '{1}'".format( saved_credentials[int(number)-1]['user'], number)) return [saved_credentials[int(number)-1]['user']]