예제 #1
0
파일: rest_call.py 프로젝트: luckcyq/eNMS
 def job(self, *args):
     if len(args) == 2:
         device, payload = args
     rest_url = substitute(self.url, locals())
     kwargs = {
         p: getattr(self, p)
         for p in ('headers', 'params', 'timeout')
     }
     if self.call_type in ('GET', 'DELETE'):
         result = self.request_dict[self.call_type](rest_url,
                                                    auth=HTTPBasicAuth(
                                                        self.username,
                                                        self.password),
                                                    **kwargs).json()
     else:
         result = loads(self.request_dict[self.call_type](
             rest_url,
             data=dumps(self.payload),
             auth=HTTPBasicAuth(self.username, self.password),
             **kwargs).content)
     match = substitute(self.content_match, locals())
     return {
         'url': rest_url,
         'expected': match,
         'negative_logic': self.negative_logic,
         'result': result,
         'success': self.match_content(str(result), match)
     }
예제 #2
0
 def job(self, device, _):
     arguments = substitute(self.arguments, locals()).split()
     command = ['ansible-playbook']
     if self.pass_device_properties:
         properties = device.get_properties()
         properties['password'] = device.password
         command.extend(['-e', dumps(properties)])
     if self.inventory_from_selection:
         command.extend(['-i', device.ip_address + ','])
     command.append(substitute(self.playbook_path, locals()))
     info(f"Sending Ansible playbook: {' '.join(command + arguments)}")
     result = check_output(command + arguments)
     info(result)
     try:
         result = result.decode('utf-8')
     except AttributeError:
         pass
     try:
         result = loads(result)
     except JSONDecodeError:
         pass
     match = substitute(self.content_match, locals())
     return {
         'expected': match,
         'negative_logic': self.negative_logic,
         'result': result,
         'success': self.match_content(str(result), match)
     }
예제 #3
0
 def job(self, device, _):
     netmiko_handler = netmiko_connection(self, device)
     command = substitute(self.command, locals())
     result = netmiko_handler.send_command(command)
     match = substitute(self.content_match, locals())
     netmiko_handler.disconnect()
     return {
         'expected': match,
         'negative_logic': self.negative_logic,
         'result': result,
         'success': self.match_content(result, match)
     }
예제 #4
0
 def job(self, device, payload):
     arguments = substitute(self.arguments, locals()).split()
     command = ['ansible-playbook']
     if self.pass_device_properties:
         command.extend(['-e', dumps(device.properties)])
     if self.inventory_from_selection:
         command.extend(['-i', device.ip_address + ','])
     command.append(substitute(self.playbook_path, locals()))
     result = check_output(command + arguments)
     try:
         result = result.decode('utf-8')
     except AttributeError:
         pass
     match = substitute(self.content_match, locals())
     success = (self.content_match_regex and search(match, result)
                or match in result and not self.content_match_regex)
     return {'success': False, 'result': result}
예제 #5
0
 def job(self, device, payload):
     napalm_driver = napalm_connection(self, device)
     napalm_driver.open()
     config = '\n'.join(substitute(self.content, locals()).splitlines())
     getattr(napalm_driver, self.action)(config=config)
     napalm_driver.commit_config()
     napalm_driver.close()
     return {'success': True, 'result': f'Config push ({config})'}
예제 #6
0
 def job(self, *args):
     if len(args) == 2:
         device, payload = args
     rest_url = substitute(self.url, locals())
     if self.call_type in ('GET', 'DELETE'):
         result = self.request_dict[self.call_type](
             rest_url, auth=HTTPBasicAuth(self.username,
                                          self.password)).json()
     else:
         result = loads(self.request_dict[self.call_type](
             rest_url,
             data=dumps(self.payload),
             auth=HTTPBasicAuth(self.username, self.password)).content)
     match = substitute(self.content_match, locals())
     success = (self.content_match_regex and search(match, str(result))
                or match in str(result) and not self.content_match_regex)
     return {'success': success, 'result': result, 'url': rest_url}
예제 #7
0
 def job(self, device, _):
     netmiko_handler = netmiko_connection(self, device)
     if self.enable_mode:
         netmiko_handler.enable()
     config = substitute(self.content, locals())
     netmiko_handler.send_config_set(config.splitlines())
     netmiko_handler.disconnect()
     return {'success': True, 'result': f'configuration OK {config}'}
예제 #8
0
 def job(self, device, _):
     netmiko_handler = netmiko_connection(self, device)
     command = substitute(self.command, locals())
     result = netmiko_handler.send_command_timing(command, delay_factor=2)
     if self.response1 and self.confirmation1 in result:
         result = netmiko_handler.send_command_timing(
             self.response1, delay_factor=self.delay_factor)
         if self.response2 and self.confirmation2 in result:
             result = netmiko_handler.send_command_timing(
                 self.response2, delay_factor=self.delay_factor)
             if self.response3 and self.confirmation3 in result:
                 result = netmiko_handler.send_command_timing(
                     self.response3, delay_factor=self.delay_factor)
     match = substitute(self.content_match, locals())
     netmiko_handler.disconnect()
     return {
         'expected': match,
         'negative_logic': self.negative_logic,
         'result': result,
         'success': self.match_content(result, match)
     }
예제 #9
0
 def job(self, device, payload):
     napalm_driver, result = napalm_connection(self, device), {}
     napalm_driver.open()
     for getter in self.getters:
         try:
             result[getter] = getattr(napalm_driver, getter)()
         except Exception as e:
             result[getter] = f'{getter} failed because of {e}'
     output, match = str(result), substitute(self.content_match, locals())
     success = (self.content_match_regex and search(match, output)
                or match in output and not self.content_match_regex)
     napalm_driver.close()
     return {'success': success, 'result': result}
예제 #10
0
 def job(self, device, _):
     ssh_client = SSHClient()
     if self.missing_host_key_policy:
         ssh_client.set_missing_host_key_policy(AutoAddPolicy())
     if self.load_known_host_keys:
         ssh_client.load_system_host_keys()
     ssh_client.connect(
         device.ip_address,
         username=device.username,
         password=device.password,
         look_for_keys=self.look_for_keys
     )
     self.transfer_file(
         ssh_client,
         substitute(self.source_file, locals()),
         substitute(self.destination_file, locals())
     )
     ssh_client.close()
     return {
         'success': True,
         'result': f'File {self.source_file} transferred successfully'
     }
예제 #11
0
 def job(self, device, payload):
     netmiko_handler = netmiko_connection(self, device)
     command = substitute(self.command, locals())
     output = netmiko_handler.send_command(command)
     success = (
         self.content_match_regex and search(self.content_match, output)
         or self.content_match in output and not self.content_match_regex)
     netmiko_handler.disconnect()
     return {
         'output': output,
         'expected': self.content_match,
         'success': success,
     }
예제 #12
0
 def job(self, *args):
     if self.multiprocessing:
         device, payload = args
     rest_url = substitute(self.url, locals())
     if self.convert_to_xml:
         data = dicttoxml(self.payload)
     else:
         data = dumps(self.payload)
     if self.call_type in ('GET', 'DELETE'):
         result = self.request_dict[self.call_type](
             rest_url,
             headers={
                 'Accept': 'application/json'
             },
             auth=HTTPBasicAuth(self.username, self.password)).json()
     else:
         result = loads(self.request_dict[self.call_type](
             rest_url,
             data=data,
             auth=HTTPBasicAuth(self.username, self.password)).content)
     match = substitute(self.content_match, locals())
     success = (self.content_match_regex and search(match, str(result))
                or match in str(result) and not self.content_match_regex)
     return {'success': success, 'result': result, 'url': rest_url}
예제 #13
0
 def job(self, device, _):
     napalm_driver, result = napalm_connection(self, device), {}
     napalm_driver.open()
     for getter in self.getters:
         try:
             result[getter] = getattr(napalm_driver, getter)()
         except Exception as e:
             result[getter] = f'{getter} failed because of {e}'
     match = substitute(self.content_match, locals())
     napalm_driver.close()
     return {
         'expected': match,
         'negative_logic': self.negative_logic,
         'result': result,
         'success': self.match_content(str(result), match)
     }