def test_invalid_ranges(self): range_list = [ '10.10.[181.9', '10.10.128.[a:25]', '10.10.[1-20].25', 'my_rhel[a:d].company.com', 'my-rhel[a:400].company.com' ] with self.assertRaises(SystemExit): utilities.read_ranges(range_list)
def test_valid_ranges(self): range_list = [ '10.10.181.9', '10.10.128.[1:25]', '10.10.[1:20].25', '10.10.[1:20].[1:25]', 'localhost', 'mycentos.com', 'my-rhel[a:d].company.com', 'my-rhel[120:400].company.com' ] result = utilities.read_ranges(range_list) self.assertEqual(range_list, result)
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: range_list = read_ranges(self.options.hosts) 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(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))
def _do_command(self): vault = get_vault(self.options.vaultfile) profiles_list = [] ssh_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 = read_ranges(self.options.hosts) 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) print(_('Profile "%s" was added' % self.options.name))
def test_cidr_range(self): range_list = ['192.168.124.0/25'] self.assertEqual(utilities.read_ranges(range_list), ['192.168.124.[0:127]'])
def test_issue404(self): range_list = ['10.1.1.1-10.1.1.254'] with self.assertRaises(SystemExit): utilities.read_ranges(range_list)
def test_invalid_file(self): range_list = ['/im/an/invalid/file'] with self.assertRaises(SystemExit): utilities.read_ranges(range_list)