def device(self, primary_name="", secondary_name=""): """ Add primary and secondary devices to HA Pair. :param primary_name: (str) 1st Device. :param secondary_name: (str) 2nd Device. :return: None """ logging.debug("In device() for FTDDeviceHAPairs class.") primary = DeviceRecords(fmc=self.fmc) primary.get(name=primary_name) secondary = DeviceRecords(fmc=self.fmc) secondary.get(name=secondary_name) if "id" in primary.__dict__: self.primary_id = primary.id else: logging.warning( f"Device {primary_name} not found. Cannot set up device for FTDDeviceHAPairs." ) if "id" in secondary.__dict__: self.secondary_id = secondary.id else: logging.warning( f"Device {secondary_name} not found. Cannot set up device for FTDDeviceHAPairs." )
def accesspolicy(self, name, devices): logging.debug("In accesspolicy() for PolicyAssignments class.") targets = [] pol1 = AccessPolicies(fmc=self.fmc) pol1.get(name=name) if "id" in pol1.__dict__: self.policy = {"type": pol1.type, "name": pol1.name, "id": pol1.id} else: logging.warning( f"Access Control Policy {name} not found. Cannot set up PolicyAssignment." ) for device in devices: if device["type"] == "device": dev1 = DeviceRecords(fmc=self.fmc) dev1.get(name=device["name"]) elif device["type"] == "deviceHAPair": dev1 = FTDDeviceHAPairs(fmc=self.fmc) dev1.get(name=device["name"]) if "id" in dev1.__dict__: logging.info( f'Adding "{dev1.name}" to targets for this Access Control Policy PolicyAssignment.' ) targets.append({ "type": dev1.type, "id": dev1.id, "name": dev1.name }) else: logging.warning( f"Device/DeviceHA {device['name']} not found. Cannot add to PolicyAssignment." ) self.targets = targets
def devices(self, devices): """ List of devices. :param devices: (list) List of device names. :return: None """ logging.debug("In devices() for Upgrades class.") for device in devices: device1 = DeviceRecords(fmc=self.fmc) device1.get(name=device) if "id" in device1.__dict__ and "targets" in self.__dict__: self.targets.append({ "id": device1.id, "type": device1.type, "name": device1.name }) elif "id" in device1.__dict__: self.targets = [{ "id": device1.id, "type": device1.type, "name": device1.name }] else: logging.warning( f'Device "{device}" not found. Cannot prepare devices for Upgrades.' )
def device(self, name="", device_id=""): """ Associate a device with this HitCount. Either the 'name' or the 'device_id' is required, not both. :param name: (str) Name of device. :param device_id: (str) UUID of device. :return: None """ logging.debug("In device() for HitCounts class") if device_id != "": self.device_id = device_id elif name != "": device1 = DeviceRecords(fmc=self.fmc) device1.get(name=name) if "id" in device1.__dict__: self.device_id = device1.id else: logging.warning( f'Device "{name}" not found. Cannot configure device for HitCounts.' ) else: logging.error("No device name or id was provided.") # Rebuild the URL with possible new information self.URL = self.URL.split("?")[0] self.URL = f"{self.URL}{self.URL_SUFFIX}"
def device(self, device_name): logging.debug("In device() for PhysicalInterface class.") device1 = DeviceRecords(fmc=self.fmc) device1.get(name=device_name) if "id" in device1.__dict__: self.device_id = device1.id self.URL = f"{self.fmc.configuration_url}{self.PREFIX_URL}/{self.device_id}/physicalinterfaces" self.device_added_to_url = True else: logging.warning( f"Device {device_name} not found. Cannot set up device for physicalInterface." )
def secondary(self, name): """ Identify secondary device. :param name: (str) Name of secondary device. :return: None """ logging.debug("In secondary() for DeviceHAPairs class.") secondary = DeviceRecords(fmc=self.fmc) secondary.get(name=name) if "id" in secondary.__dict__: self.secondary = {"id": secondary.id} else: logging.warning( f"Device {secondary.name} not found. Cannot set up device for FTDDeviceHAPairs." )
def device(self, device_name): """ Associate device to this subinterface. :param device_name: (str) Name of device. :return: None """ logging.debug("In device() for SubInterfaces class.") device1 = DeviceRecords(fmc=self.fmc) device1.get(name=device_name) if "id" in device1.__dict__: self.device_id = device1.id self.URL = f"{self.fmc.configuration_url}{self.PREFIX_URL}/{self.device_id}/subinterfaces" self.device_added_to_url = True else: logging.warning( f'Device "{device_name}" not found. Cannot set up device for SubInterfaces.' )
def device(self, name="", device_id=""): logging.debug("In device() for HitCounts class") if device_id != "": self.device_id = device_id elif name != "": device1 = DeviceRecords(fmc=self.fmc) device1.get(name=name) if "id" in device1.__dict__: self.device_id = device1.id else: logging.warning( f'Device "{name}" not found. Cannot configure device for HitCounts.' ) else: logging.error("No device name or id was provided.") # Rebuild the URL with possible new information self.URL = self.URL.split("?")[0] self.URL = f"{self.URL}{self.URL_SUFFIX}"
def ftd_natpolicy(self, name, devices): """ Associate FTD NAT Policy with a list of devices. :param name: (str) Name of FTD NAT Policy. :param devices: (list) List of device names. """ logging.debug("In ftd_natpolicy() for PolicyAssignments class.") targets = [] pol1 = FTDNatPolicies(fmc=self.fmc) pol1.get(name=name) if "id" in pol1.__dict__: self.policy = {"type": pol1.type, "name": pol1.name, "id": pol1.id} else: logging.warning( f"FTD NAT Policy {name} not found. Cannot set up PolicyAssignment." ) for device in devices: if device["type"] == "device": dev1 = DeviceRecords(fmc=self.fmc) dev1.get(name=device["name"]) elif device["type"] == "deviceHAPair": dev1 = FTDDeviceHAPairs(fmc=self.fmc) dev1.get(name=device["name"]) if "id" in dev1.__dict__: logging.info( f'Adding "{dev1.name}" to targets for this FTDNat PolicyAssignment.' ) targets.append({ "type": dev1.type, "id": dev1.id, "name": dev1.name }) else: logging.warning( f"Device/DeviceHA {device['name']} not found. Cannot add to PolicyAssignment." ) self.targets = targets
def endpoint(self, action, device_name): """ Associate an endpoint. :param action: (str) 'add', 'remove', or 'clear' :param device_name: (str) Name of device. """ logging.debug("In endpoint() for Endpoints class.") device_json = DeviceRecords(fmc=self.fmc).get() device_ha_json = FTDDeviceHAPairs(fmc=self.fmc).get() items = device_json.get("items", []) + device_ha_json.get("items", []) new_device = None if action == "add": for item in items: if item["name"] == device_name: new_device = { "name": item["name"], "id": item["id"], "type": item["type"], } break if new_device is None: logging.warning( f'Device/DeviceHA "{device_name}" is not found in FMC. Cannot add to Endpoints.' ) else: if "device" in self.__dict__: self.device.append(new_device) logging.info(f'Adding "{device_name}" to Endpoints.') else: self.device = new_device elif action == "remove": if "device" in self.__dict__: self.device = list( filter(lambda i: i["name"] != device_name, self.device) ) else: logging.warning("Endpoints has no members. Cannot remove device.") elif action == "clear": if "device" in self.__dict__: del self.device
def devices(self, action, members=[]): """ Add/modify name to members field of DeviceGroupRecords object. :param action: (str) 'add', 'remove', or 'clear' :param membres: (list) List of members in group. :return: None """ logging.debug("In devices() for DeviceGroupRecords class.") if action == "add": for member in members: if member["type"] == "device": dev1 = DeviceRecords(fmc=self.fmc) dev1.get(name=member["name"]) elif member["type"] == "deviceHAPair": dev1 = FTDDeviceHAPairs(fmc=self.fmc) dev1.get(name=member["name"]) if "id" in dev1.__dict__: if "members" in self.__dict__: self.members.append({ "id": dev1.id, "type": dev1.type, "name": dev1.name }) else: self.members = [{ "id": dev1.id, "type": dev1.type, "name": dev1.name }] logging.info( f'DeviceRecord "{dev1.name}" added to this DeviceGroupRecords object.' ) else: logging.warning( f"{member} not found. Cannot add DeviceRecord to DeviceGroupRecords." ) elif action == "remove": if "members" in self.__dict__: for member in members: if member["type"] == "device": dev1 = DeviceRecords(fmc=self.fmc) dev1.get(name=member["name"]) elif member["type"] == "deviceHAPair": dev1 = FTDDeviceHAPairs(fmc=self.fmc) dev1.get(name=member["name"]) if "id" in dev1.__dict__: if member["type"] == "device": self.members = list( filter(lambda i: i["id"] != dev1.id, self.members)) elif member["type"] == "deviceHAPair": devHA1 = FTDDeviceHAPairs(fmc=self.fmc) devHA1.get(name=member["name"]) self.members = list( filter( lambda i: i["id"] != devHA1.primary["id"], self.members, )) self.members = list( filter( lambda i: i["id"] != devHA1.secondary["id" ], self.members, )) else: logging.warning( f"DeviceRecord {member} not registered. Cannot remove DeviceRecord" f" from DeviceGroupRecords.") else: logging.warning( "DeviceGroupRecords has no members. Cannot remove DeviceRecord." ) elif action == "clear": if "members" in self.__dict__: del self.members logging.info( "All device records removed from this DeviceGroupRecords object." )