def _validate_options(self): CliCommand._validate_options(self) if not self.options.hosts: self.parser.print_help() sys.exit(1) if not self.options.auth: self.parser.print_help() sys.exit(1) if not self.options.name: self.parser.print_help() sys.exit(1) if hasattr(self.options, 'sshport') \ and self.options.sshport is not None: ssh_port = self.options.sshport try: ssh_port = utilities.validate_port(ssh_port) self.options.sshport = ssh_port except ValueError as port_error: print(str(port_error)) self.parser.print_help() sys.exit(1) else: self.options.sshport = 22
def _do_command(self): vault = get_vault(self.options.vaultfile) hosts_list = self.options.hosts profiles_list = [] ssh_port = 22 if hasattr(self.options, 'sshport') \ and self.options.sshport is not None: ssh_port = utilities.validate_port(self.options.sshport) if os.path.isfile(utilities.PROFILES_PATH): profiles_list = vault.load_as_json(utilities.PROFILES_PATH) profile_found = profile_exists(profiles_list, self.options.name) if profile_found: print(_("Profile '%s' already exists.") % self.options.name) sys.exit(1) range_list = hosts_list # pylint: disable=len-as-condition if len(hosts_list) > 0 and os.path.isfile(hosts_list[0]): range_list = _read_in_file(hosts_list[0]) _check_range_validity(range_list) if not os.path.isfile(utilities.CREDENTIALS_PATH): print(_('No credentials exist yet.')) sys.exit(1) creds = [] cred_list = vault.load_as_json(utilities.CREDENTIALS_PATH) for auth in self.options.auth: for auth_item in auth.strip().split(","): valid = False for cred in cred_list: if cred.get('name') == auth: valid = True # add the uuids of credentials store_cred = {'id': cred.get('id'), 'name': cred.get('name')} creds.append(store_cred) if not valid: print("Auth " + auth_item + " does not exist") sys.exit(1) new_profile = OrderedDict([("name", self.options.name), ("hosts", range_list), ("ssh_port", str(ssh_port)), ("auth", creds)]) _save_profile(vault, new_profile, profiles_list)
def _validate_options(self): CliCommand._validate_options(self) if not self.options.name: self.parser.print_help() sys.exit(1) if not self.options.hosts and not self.options.auth \ and not self.options.sshport: print(_("Specify either hosts, sshport, or auths to update.")) self.parser.print_help() sys.exit(1) if hasattr(self.options, 'sshport') \ and self.options.sshport is not None: ssh_port = self.options.sshport try: ssh_port = utilities.validate_port(ssh_port) self.options.sshport = ssh_port except ValueError as port_error: print(str(port_error)) self.parser.print_help() sys.exit(1)
def test_valid_int_port(self): self.assertEqual(utilities.validate_port(123), 123)
def test_valid_string_port(self): self.assertEqual(utilities.validate_port("123"), 123)
def test_too_large_port(self): with self.assertRaises(ValueError): utilities.validate_port(65536)
def test_negative_port(self): with self.assertRaises(ValueError): utilities.validate_port(-1)
def test_unparseable_string(self): with self.assertRaises(ValueError): utilities.validate_port("abc")
def test_wrong_type(self): with self.assertRaises(ValueError): utilities.validate_port([1, 2, 3])
def _do_command(self): # pylint: disable=too-many-locals, too-many-branches # pylint: disable=too-many-statements, too-many-nested-blocks vault = get_vault(self.options.vaultfile) cred_list = [] profiles_list = [] range_list = [] profile_found = False auth_found = False if not os.path.isfile(utilities.CREDENTIALS_PATH): print(_('No credentials exist yet.')) sys.exit(1) if not os.path.isfile(utilities.PROFILES_PATH): print(_('No profiles exist yet.')) sys.exit(1) cred_list = vault.load_as_json(utilities.CREDENTIALS_PATH) profiles_list = vault.load_as_json(utilities.PROFILES_PATH) if self.options.hosts: hosts_list = self.options.hosts range_list = hosts_list # pylint: disable=len-as-condition if len(hosts_list) > 0 and os.path.isfile(hosts_list[0]): range_list = _read_in_file(hosts_list[0]) # makes sure the hosts passed in are in a format Ansible # understands. _check_range_validity(range_list) for curr_profile in profiles_list: if curr_profile.get('name') == self.options.name: profile_found = True if self.options.hosts: curr_profile['hosts'] = range_list if self.options.sshport: curr_profile['ssh_port'] = str( utilities.validate_port(self.options.sshport)) if self.options.auth: new_auths = [] auth_list = self.options.auth for auth in auth_list: for cred in cred_list: if auth == cred.get('name'): auth_found = True store_cred = {'id': cred.get('id'), 'name': cred.get('name')} new_auths.append(store_cred) if not auth_found: print(_("Auths do not exist.")) sys.exit(1) curr_profile['auth'] = new_auths break if not profile_found: print(_("Profile '%s' does not exist.") % self.options.name) sys.exit(1) vault.dump_as_json_to_file(profiles_list, utilities.PROFILES_PATH) print(_("Profile '%s' edited" % self.options.name))