def change_interface_id(self, newid): """ Change the inline interface ID. The current format is nicid='1-2', where '1' is the top level interface ID (first), and '2' is the second interface in the pair. Consider the existing nicid in case this is a VLAN. :param str newid: string defining new pair, i.e. '3-4' :return: None """ try: newleft, newright = newid.split('-') except ValueError: raise EngineCommandFailed( 'You must provide two parts when changing ' 'the interface ID on an inline interface, i.e. 1-2.') first, second = self.nicid.split('-') if '.' in first and '.' in second: firstvlan = first.split('.')[-1] secondvlan = second.split('.')[-1] self.update(nicid='{}.{}-{}.{}'.format(newleft, firstvlan, newright, secondvlan)) else: # Top level interface or no VLANs self.update(nicid=newid)
def get(self, interface_id): """ Get the interface by id, if known. The interface is retrieved from the top level Physical or Tunnel Interface. If interface type is unknown, use engine.interface for retrieving:: engine = Engine('testfw') p = engine.physical_interface.get(0) print(p.name, p.typeof, p.address, p.network_value) ..... :param str|int interface_id: interface ID to retrieve :raises: :py:class:`smc.api.exceptions.EngineCommandFailed` if interface not found :return: interface object by type (Physical, Tunnel, PhysicalVlanInterface) """ interface_id = str(interface_id) for interface in self._get_resource(self.meta.href): intf_type = interface.get('type') #Keep type intf = InterfaceFactory(intf_type)(meta=Meta(**interface), engine=self._engine) if intf.data.get('interface_id') == interface_id: return intf raise EngineCommandFailed( 'Interface id {} not found'.format(interface_id))
def routing_monitoring(self): """ Return route table for the engine, including gateway, networks and type of route (dynamic, static). Calling this can take a few seconds to retrieve routes from the engine. Find all routes for engine resource:: engine = Engine('myengine') for route in engine.routing_monitoring.all(): print route :raises EngineCommandFailed: routes cannot be retrieved :return: list of route elements :rtype: Routes """ try: result = self.make_request( EngineCommandFailed, resource='routing_monitoring') return Routes(result) except SMCConnectionError: raise EngineCommandFailed('Timed out waiting for routes')
def add_ipaddress_to_vlan_interface(self, interface_id, address, network_value, vlan_id, nodeid=1, **kwargs): """ When an existing VLAN exists but has no IP address assigned, use this to add an ip address to the VLAN. This assumes that the engine context exists as the interface specified This is only supported on layer 3 interfaces. :param str interface_id: interface to modify :param str address: ip address for vlan :param str network_value: network for address (i.e. 255.255.255.0) :param int vlan_id: id of vlan :raises: :py:class:`smc.api.exceptions.EngineCommandFailed` for invalid interface :return: :py:class:`smc.api.web.SMCResult` """ if not self.href: raise EngineCommandFailed( 'Adding a vlan to existing interface requires ' 'an engine reference.') if self._engine.type == 'single_fw': intf = SingleNodeInterface.create(interface_id, address, network_value, nodeid, nicid='{}.{}'.format( interface_id, vlan_id)) else: intf = NodeInterface.create(interface_id, address, network_value, nodeid, nicid='{}.{}'.format( interface_id, vlan_id)) p = self.get(interface_id) for vlan in p.sub_interfaces(): if isinstance(vlan, PhysicalVlanInterface): if vlan.interface_id == '{}.{}'.format(interface_id, vlan_id): vlan.data['interfaces'] = [intf()] prepared_request(EngineCommandFailed, href=p.href, json=p.data, etag=p.etag).update()
def get(self, address): """ Get a loopback address by it's address. Find all loopback addresses by iterating at either the node level or the engine:: loopback = engine.loopback_interface.get('127.0.0.10') :param str address: ip address of loopback :rtype: LoopbackInterface """ loopback = super(LoopbackCollection, self).get(address=address) if loopback: return loopback raise EngineCommandFailed('Loopback address specified was not found')
def generate_snapshot(self, filename='snapshot.zip'): """ Generate and retrieve a policy snapshot from the engine This is blocking as file is downloaded :param str filename: name of file to save file to, including directory path :raises: :py:class:`smc.api.exceptions.EngineCommandFailed` :return: None """ try: prepared_request(EngineCommandFailed, href=self._link('generate_snapshot'), filename=filename).read() except IOError as e: raise EngineCommandFailed("Generate snapshot failed: {}".format(e))
def download(self, filename=None): """ Download snapshot to filename :param str filename: fully qualified path including filename .zip :raises: :py:class:`smc.api.exceptions.EngineCommandFailed` :return: None """ if not filename: filename = '{}{}'.format(self.name, '.zip') try: prepared_request(EngineCommandFailed, href=self._link('content'), filename=filename).read() except IOError as e: raise EngineCommandFailed("Snapshot download failed: {}".format(e))
def download(self, filename=None): """ Download snapshot to filename :param str filename: fully qualified path including filename .zip :raises EngineCommandFailed: IOError occurred downloading snapshot :return: None """ if not filename: filename = '{}{}'.format(self.name, '.zip') try: self.make_request(EngineCommandFailed, resource='content', filename=filename) except IOError as e: raise EngineCommandFailed("Snapshot download failed: {}".format(e))
def generate_snapshot(self, filename='snapshot.zip'): """ Generate and retrieve a policy snapshot from the engine This is blocking as file is downloaded :param str filename: name of file to save file to, including directory path :raises EngineCommandFailed: snapshot failed, possibly invalid filename specified :return: None """ try: self.make_request( EngineCommandFailed, resource='generate_snapshot', filename=filename) except IOError as e: raise EngineCommandFailed( 'Generate snapshot failed: {}'.format(e))
def routing_monitoring(self): """ Return route table for the engine, including gateway, networks and type of route (dynamic, static). Calling this can take a few seconds to retrieve routes from the engine. Find all routes for engine resource:: engine = Engine('myengine') for route in engine.routing_monitoring.all(): print route :raises: `smc.api.exceptions.EngineCommandFailed`: routes cannot be retrieved :return: list :py:class:`smc.core.route.Routes` """ try: result = prepared_request( EngineCommandFailed, href=self._link('routing_monitoring')).read() return Routes(result.json) except SMCConnectionError: raise EngineCommandFailed('Timed out waiting for routes')