def translated_destination_port(self, name): """ Associate Port to be used as Destination port. :param name: (str) Name of Port. :return: None """ logging.debug( "In translated_destination_port() for ManualNatRules class.") ports_json = ProtocolPortObjects(fmc=self.fmc).get() portgroup_json = PortObjectGroups(fmc=self.fmc).get() items = ports_json.get("items", []) + portgroup_json.get("items", []) new_port = None for item in items: if item["name"] == name: new_port = {"id": item["id"], "type": item["type"]} break if new_port is None: logging.warning( f'Port "{name}" is not found in FMC. Cannot add to translated_destination_port.' ) else: self.translatedDestinationPort = new_port logging.info( f'Adding "{name}" to translated_destination_port for this ManualNatRule.' )
def _find_port_object(self, name): """ Find port object or port group object and return dictionary. :param name: (str) Name of port object/port group object :return: (dict) Port object """ protocol_port = ProtocolPortObjects(fmc=self.fmc, name=name) resp = protocol_port.get() if "id" in resp.keys(): return {"name": name, "id": resp["id"], "type": resp["type"]} protocol_port_group = PortObjectGroups(fmc=self.fmc, name=name) resp = protocol_port_group.get() if "id" in resp.keys(): return {"name": name, "id": resp["id"], "type": resp["type"]} logging.warning(f'Unable to find port object "{name}"') return None
def translated_source_port(self, name): logging.debug("In translated_source_port() for ManualNatRules class.") ports_json = ProtocolPortObjects(fmc=self.fmc).get() portgroup_json = PortObjectGroups(fmc=self.fmc).get() items = ports_json.get("items", []) + portgroup_json.get("items", []) new_port = None for item in items: if item["name"] == name: new_port = {"id": item["id"], "type": item["type"]} break if new_port is None: logging.warning( f'Port "{name}" is not found in FMC. Cannot add to translated_source_port.' ) else: self.translatedSourcePort = new_port logging.info( f'Adding "{name}" to translated_source_port for this ManualNatRule.' )
def destination_port(self, action, name=""): """ Add/modify name to destinationPorts field of AccessRules object. :param action: (str) 'add', 'remove', or 'clear' :param name: (str) Name of Port in FMC. :return: None """ logging.debug("In destination_port() for AccessRules class.") if action == "add": pport_json = ProtocolPortObjects(fmc=self.fmc) pport_json.get(name=name) if "id" in pport_json.__dict__: item = pport_json else: item = PortObjectGroups(fmc=self.fmc) item.get(name=name) if "id" in item.__dict__: if "destinationPorts" in self.__dict__: new_port = {"name": item.name, "id": item.id, "type": item.type} duplicate = False if "objects" not in self.destinationPorts: self.__dict__["destinationPorts"]["objects"] = [] for obj in self.destinationPorts["objects"]: if obj["name"] == new_port["name"]: duplicate = True break if not duplicate: self.destinationPorts["objects"].append(new_port) logging.info( f'Adding "{name}" to destinationPorts for this AccessRules.' ) else: self.destinationPorts = { "objects": [ {"name": item.name, "id": item.id, "type": item.type} ] } logging.info( f'Adding "{name}" to destinationPorts for this AccessRules.' ) else: logging.warning( f'Protocol Port or Protocol Port Group: "{name}", ' f"not found. Cannot add to AccessRules." ) elif action == "remove": pport_json = ProtocolPortObjects(fmc=self.fmc) pport_json.get(name=name) if "id" in pport_json.__dict__: item = pport_json else: item = PortObjectGroups(fmc=self.fmc) item.get(name=name) if "id" in item.__dict__: if "destinationPorts" in self.__dict__: objects = [] for obj in self.destinationPorts["objects"]: if obj["name"] != name: objects.append(obj) self.destinationPorts["objects"] = objects logging.info( f'Removed "{name}" from destinationPorts for this AccessRules.' ) else: logging.info( "destinationPorts doesn't exist for this AccessRules. Nothing to remove." ) else: logging.warning( f'Protocol Port or Protocol Port Group: "{name}", ' f"not found. Cannot add to AccessRules." ) elif action == "clear": if "destinationPorts" in self.__dict__: del self.destinationPorts logging.info( "All Destination Ports removed from this AccessRules object." )
def source_port(self, action, name=""): logging.debug("In source_port() for AccessRules class.") if action == "add": pport_json = ProtocolPortObjects(fmc=self.fmc) pport_json.get(name=name) if "id" in pport_json.__dict__: item = pport_json else: item = PortObjectGroups(fmc=self.fmc) item.get(name=name) if "id" in item.__dict__: if "sourcePorts" in self.__dict__: new_port = {"name": item.name, "id": item.id, "type": item.type} duplicate = False if "objects" not in self.sourcePorts: self.__dict__["sourcePorts"]["objects"] = [] for obj in self.sourcePorts["objects"]: if obj["name"] == new_port["name"]: duplicate = True break if not duplicate: self.sourcePorts["objects"].append(new_port) logging.info( f'Adding "{name}" to sourcePorts for this AccessRules.' ) else: self.sourcePorts = { "objects": [ {"name": item.name, "id": item.id, "type": item.type} ] } logging.info( f'Adding "{name}" to sourcePorts for this AccessRules.' ) else: logging.warning( f'Protocol Port or Protocol Port Group: "{name}", ' f"not found. Cannot add to AccessRules." ) elif action == "remove": pport_json = ProtocolPortObjects(fmc=self.fmc) pport_json.get(name=name) if "id" in pport_json.__dict__: item = pport_json else: item = PortObjectGroups(fmc=self.fmc) item.get(name=name) if "id" in item.__dict__: if "sourcePorts" in self.__dict__: objects = [] for obj in self.sourcePorts["objects"]: if obj["name"] != name: objects.append(obj) self.sourcePorts["objects"] = objects logging.info( f'Removed "{name}" from sourcePorts for this AccessRules.' ) else: logging.info( "sourcePorts doesn't exist for this AccessRules. Nothing to remove." ) else: logging.warning( f'Protocol Port or Protocol Port Group: "{name}", ' f"not found. Cannot add to AccessRules." ) elif action == "clear": if "sourcePorts" in self.__dict__: del self.sourcePorts logging.info("All Source Ports removed from this AccessRules object.")