def _state_overridden(self, want, have, diff): """ The command generator when state is overridden :param want: the desired configuration as a dictionary :param obj_in_have: the current configuration as a dictionary :rtype: A list :returns: the commands necessary to migrate the current configuration to the desired configuration """ commands = [] commands_del = self.filter_comands_to_change(want, have) requests = self.get_delete_interface_requests(commands_del, have) del_req_count = len(requests) if commands_del and del_req_count > 0: commands_del = update_states(commands_del, "deleted") commands.extend(commands_del) commands_over = diff requests.extend(self.get_modify_interface_requests( commands_over, have)) if commands_over and len(requests) > del_req_count: commands_over = update_states(commands_over, "overridden") commands.extend(commands_over) return commands, requests
def _state_overridden(self, want, have, diff): """ The command generator when state is overridden :rtype: A list :returns: the commands necessary to migrate the current configuration to the desired configuration """ commands = [] requests = [] commands_del = get_diff(have, want, TEST_KEYS) requests_del = self.get_delete_all_switchport_requests(commands_del) if len(requests_del): requests.extend(requests_del) commands_del = update_states(commands_del, "deleted") commands.extend(commands_del) commands_over = diff requests_over = self.get_create_l2_interface_request(commands_over) if requests_over: requests.extend(requests_over) commands_over = update_states(commands_over, "overridden") commands.extend(commands_over) return commands, requests
def multiqueries(self, index, bodies): request_header = json.dumps({'index': index}) requests = [] for b in bodies: b["track_total_hits"] = True requests.extend([request_header, b]) resp = self.es_db.msearch(body=requests) # check if there are failed queries for result in resp["responses"]: if "error" in result.keys(): return [False, result["error"]["root_cause"][0]["reason"]] return [True, resp['responses']]
def get_delete_specifig_switchport_requests(self, configs, have): requests = [] if not configs: return requests for conf in configs: name = conf['name'] matched = next((cnf for cnf in have if cnf['name'] == name), None) if matched: keys = conf.keys() # if both access and trunk not mention in delete if not ('access' in keys) and not ('trunk' in keys): requests.extend(self.get_delete_all_switchport_requests([conf])) else: # if access or trnuk is mentioned with value if conf.get('access') or conf.get('trunk'): # if access is mentioned with value if conf.get('access'): vlan = conf.get('access').get('vlan') if vlan: request = self.get_access_delete_switchport_request(conf, matched) if request: requests.append(request) else: if matched.get('access') and matched.get('access').get('vlan'): conf['access']['vlan'] = matched.get('access').get('vlan') request = self.get_access_delete_switchport_request(conf, matched) if request: requests.append(request) # if trunk is mentioned with value if conf.get('trunk'): allowed_vlans = conf['trunk'].get('allowed_vlans') if allowed_vlans: requests.extend(self.get_trunk_delete_switchport_request(conf, matched)) # allowed vlans mentinoed without value else: if matched.get('trunk') and matched.get('trunk').get('allowed_vlans'): conf['trunk']['allowed_vlans'] = matched.get('trunk') and matched.get('trunk').get('allowed_vlans').copy() requests.extend(self.get_trunk_delete_switchport_request(conf, matched)) # check for access or trunk is mentioned without value else: # access mentioned wothout value if ('access' in keys) and conf.get('access', None) is None: # get the existing values and delete it if matched.get('access'): conf['access'] = matched.get('access').copy() request = self.get_access_delete_switchport_request(conf, matched) if request: requests.append(request) # trunk mentioned wothout value if ('trunk' in keys) and conf.get('trunk', None) is None: # get the existing values and delete it if matched.get('trunk'): conf['trunk'] = matched.get('trunk').copy() requests.extend(self.get_trunk_delete_switchport_request(conf, matched)) return requests
def _state_replaced(self, want, have, diff): """ The command generator when state is replaced :param want: the desired configuration as a dictionary :param have: the current configuration as a dictionary :param interface_type: interface type :rtype: A list :returns: the commands necessary to migrate the current configuration to the desired configuration """ commands = self.filter_comands_to_change(diff, have) requests = self.get_delete_interface_requests(commands, have) requests.extend(self.get_modify_interface_requests(commands, have)) if commands and len(requests) > 0: commands = update_states(commands, "replaced") else: commands = [] return commands, requests
def get_new_requests(self): """Retrieve all the new request that were found in this request. Returns: list(obj): A list of request objects. """ content_type = self.__queue_item.response.headers.get('content-type') scrapers = self.__get_all_scrapers() requests = [] for scraper in scrapers: instance = scraper(self.__options, self.__queue_item) if self.__content_type_matches(content_type, instance.content_types): requests.extend(instance.get_requests()) return requests
def addText(text, endIndex, namedStyleType, requests): """Adds requests to add text at endIndex with the desired nameStyleType. Returns new endIndex. Args: text (String): text to add endIndex (int): Current endIndex of document. Location to add text to namedStyleType (String): desired namedStyleType of text requests (list): list of requests to append requests to Returns: int: new endIndex after adding text """ # Finds new end index by incrementing endIndex by the length of the text # plus 1 for the automatically appended '\n' newEndIndex = endIndex + len(text) + 1 # Appends requests to add text, and then request to change namedStyleType # as desired requests.extend([ # Add text, including a '\n' at the previous line to create a new paragraph # Note that a '\n' will automatically be appended to the end of the text { 'insertText': { 'location': { 'index': endIndex - 1 }, 'text': '\n' + text } }, { 'updateParagraphStyle': { 'range': { 'startIndex': endIndex, 'endIndex': newEndIndex }, 'paragraphStyle': { 'namedStyleType': namedStyleType }, 'fields': 'namedStyleType' } } ]) return newEndIndex
def do_it(): args = parser.parse_args() url = args.url token = args.token if not args.http and not url.startswith('https://'): print("URL is not HTTPS but --http was not specified!", file=sys.stderr) sys.exit(-1) requests = [] for path in args.paths: if args.recursive: requests.extend(scan_and_push(url, ENDPOINT, token, path)) else: task_name, submission_id = do_path(url, ENDPOINT, token, path) requests.append((path, task_name, submission_id)) print("Success! List of submitted tasks:") for path, task_name, submission_id in requests: print( f'{task_name}({path}): {submission_id if submission_id else "none"}' )
def _state_replaced(self, want, have, diff): """ The command generator when state is replaced :rtype: A list :returns: the commands necessary to migrate the current configuration to the desired configuration """ requests = [] commands = diff if commands: requests_del = self.get_delete_all_switchport_requests(commands) if requests_del: requests.extend(requests_del) requests_rep = self.get_create_l2_interface_request(commands) if len(requests_del) or len(requests_rep): requests.extend(requests_rep) commands = update_states(commands, "replaced") else: commands = [] return commands, requests
def get_messages(q): while True: msg = q.get() masterip = msg[0] requests = json.loads(msg[1]) put_rand = requests[0]['random'] threads = requests[0]['threads'] ID = requests[0]['id'] master = (masterip, requests[0]['port']) registry = requests[0]['registry'] wait = requests[0]['wait'] print master, ID, threads processes = [] process_requests = [] delayed = [] for i in range(threads): process_requests.append([]) delayed.append(0) for r in requests[1:]: i = 0 for j in range(len(delayed)): if delayed[j] < delayed[i]: i = j if delayed[i] < r['delay']: delayed[i] = r['delay'] + r['duration'] else: delayed[i] += r['duration'] process_requests[i].append(r) requests = [{'id': ID}] startTime = time.time() rq = Queue() for i in range(threads): first = registry.pop(0) registry.append(first) p = Process(target=send_requests, args=(registry, wait, put_rand, process_requests[i], startTime, rq)) p.start() processes.append(p) for i in range(threads): requests.extend(rq.get()) for p in processes: p.join() print 'processes joined, sending response' sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) err = False try: sock.connect(master) sock.sendall(json.dumps(requests)) except Exception as inst: print inst print "Error sending info, writing to file" err = True if err is True: with open('error_output', 'w') as f: f.write(json.dumps(requests)) sock.close() print 'finished'