def from_conf(cls, conf: SNMPv3Conf): return cls( UserName=conf.user_name, AuthenticationProtocol=convert_or_none(conf.authentication_protocol, enum_to_str), AuthenticationPassphrase=conf.authentication_passphrase, PrivacyProtocol=convert_or_none(conf.privacy_protocol, enum_to_str), PrivacyPassphrase=conf.privacy_passphrase, )
def to_conf(self) -> SNMPv3Conf: return SNMPv3Conf( user_name=self.UserName, authentication_protocol=convert_or_none(self.AuthenticationProtocol, AuthenticationProtocol), authentication_passphrase=self.AuthenticationPassphrase, privacy_protocol=convert_or_none(self.PrivacyProtocol, PrivacyProtocol), privacy_passphrase=self.PrivacyPassphrase, )
def to_vrf_attributes(self) -> VRFAttributes: return VRFAttributes( name=self.name, description=self.description, route_target=self.route_target, route_distinguisher=self.route_distinguisher, ext_route_distinguisher=self.ext_route_distinguisher, id=convert_or_none(self.id, ID), company_id=convert_or_none(self.company_id, ID), device_id=convert_or_none(self.device_id, ID), )
def from_interface(cls, interface: Interface): # prepare POST/PUT request payload: fill only the user-provided fields return cls( snmp_id=convert_or_none(interface.snmp_id, str), snmp_speed=interface.snmp_speed, interface_description=interface.interface_description, snmp_alias=interface.snmp_alias, interface_ip=interface.interface_ip, interface_ip_netmask=interface.interface_ip_netmask, vrf_id=convert_or_none(interface.vrf_id, str), vrf=convert_or_none(interface.vrf, VRFAttributesPayload.from_vrf_attributes), secondary_ips=convert_list_or_none(interface.secondary_ips, SecondaryIPPayload.from_secondary_ip), )
def from_dict(cls, dic: Dict[str, Any]): required_fields = [ "id", "company_id", "device_name", "device_type", "device_subtype", "plan", "device_sample_rate", "created_date", "updated_date", ] validate_fields(class_name=cls.__name__, required_fields=required_fields, dic=dic) # recreate GET/POST/PUT response payload: fill all available fields return cls( # always returned fields id=dic["id"], company_id=dic["company_id"], device_name=dic["device_name"], device_type=dic["device_type"], device_subtype=dic["device_subtype"], plan=PlanPayload.from_dict(dic["plan"]), device_sample_rate=dic["device_sample_rate"], created_date=dic["created_date"], updated_date=dic["updated_date"], # optional fields sending_ips=dic.get("sending_ips"), site=convert_or_none(dic.get("site"), SitePayload.from_dict), labels=convert_list_or_none(dic.get("labels"), LabelPayload.from_dict), all_interfaces=convert_list_or_none(dic.get("all_interfaces"), AllInterfacesPayload.from_dict), cdn_attr=dic.get("cdn_attr"), device_description=dic.get("device_description"), device_snmp_ip=dic.get("device_snmp_ip"), device_snmp_community=dic.get("device_snmp_community"), device_snmp_v3_conf=convert_or_none(dic.get("device_snmp_v3_conf"), SNMPv3ConfPayload.from_dict), minimize_snmp=dic.get("minimize_snmp"), device_bgp_type=dic.get("device_bgp_type"), device_bgp_neighbor_ip=dic.get("device_bgp_neighbor_ip"), device_bgp_neighbor_ip6=dic.get("device_bgp_neighbor_ip6"), device_bgp_neighbor_asn=dic.get("device_bgp_neighbor_asn"), device_bgp_flowspec=dic.get("device_bgp_flowspec"), device_bgp_password=dic.get("device_bgp_password"), use_bgp_device_id=dic.get("use_bgp_device_id"), device_status=dic.get("device_status"), device_flow_type=dic.get("device_flow_type"), snmp_last_updated=dic.get("snmp_last_updated"), bgpPeerIP4=dic.get("bgpPeerIP4"), bgpPeerIP6=dic.get("bgpPeerIP6"), )
def to_all_interfaces(self) -> AllInterfaces: return AllInterfaces( device_id=convert(self.device_id, ID), snmp_speed=convert(self.snmp_speed, int), interface_description=self.interface_description, initial_snmp_speed=convert_or_none(self.initial_snmp_speed, int), )
def _to_filter(self, dic) -> Filter: return Filter( filterField=dic["filterField"], filterValue=dic["filterValue"], operator=dic["operator"], id=convert_or_none(dic.get("id"), ID), )
def to_plan(self) -> Plan: plan_dict = self.plan device_types = [PlanDeviceType(**i) for i in plan_dict["deviceTypes"]] devices = [ PlanDevice( device_name=i["device_name"], device_type=i["device_type"], id=convert(i["id"], ID), ) for i in plan_dict["devices"] ] return Plan( id=convert(plan_dict["id"], ID), company_id=convert_or_none(plan_dict["company_id"], ID), name=plan_dict["name"], description=plan_dict["description"], active=plan_dict["active"], max_devices=plan_dict["max_devices"], max_fps=plan_dict["max_fps"], bgp_enabled=plan_dict["bgp_enabled"], fast_retention=plan_dict["fast_retention"], full_retention=plan_dict["full_retention"], created_date=plan_dict["cdate"], updated_date=plan_dict.get("edate"), max_bigdata_fps=plan_dict["max_bigdata_fps"], device_types=device_types, devices=devices, metadata=plan_dict["metadata"], )
def test_convert_or_none_provided_invalid_data_format_raises_error() -> None: # given attr = "0x18" # cant convert 0x18 to int with base 10 convert_func = int # when - then with pytest.raises(DataFormatError): _ = convert_or_none(attr, convert_func)
def to_site(self) -> Site: return Site( site_name=self.site_name, latitude=self.lat, longitude=self.lon, id=convert(self.id, ID), company_id=convert_or_none(self.company_id, ID), )
def to_device(self) -> Device: return Device( id=convert_or_none(self.id, ID), plan=convert_or_none(self.plan, PlanPayload.to_plan), site=convert_or_none(self.site, SitePayload.to_site), device_name=self.device_name, device_type=DeviceType(self.device_type), device_subtype=DeviceSubtype(self.device_subtype), device_sample_rate=self.device_sample_rate, sending_ips=self.sending_ips, device_description=self.device_description, device_snmp_ip=self.device_snmp_ip, device_snmp_community=self.device_snmp_community, minimize_snmp=self.minimize_snmp, device_bgp_type=DeviceBGPType(self.device_bgp_type), device_bgp_neighbor_ip=self.device_bgp_neighbor_ip, device_bgp_neighbor_ip6=self.device_bgp_neighbor_ip6, device_bgp_neighbor_asn=self.device_bgp_neighbor_asn, device_bgp_flowspec=self.device_bgp_flowspec, device_bgp_password=self.device_bgp_password, use_bgp_device_id=convert_or_none(self.use_bgp_device_id, ID), device_status=self.device_status, device_flow_type=self.device_flow_type, company_id=convert_or_none(self.company_id, ID), snmp_last_updated=self.snmp_last_updated, created_date=self.created_date, updated_date=self.updated_date, bgp_peer_ip4=self.bgpPeerIP4, bgp_peer_ip6=self.bgpPeerIP6, labels=convert_list_or_none(self.labels, LabelPayload.to_device_label), all_interfaces=convert_list_or_none(self.all_interfaces, AllInterfacesPayload.to_all_interfaces), device_snmp_v3_conf=convert_or_none(self.device_snmp_v3_conf, SNMPv3ConfPayload.to_conf), cdn_attr=convert_or_none(self.cdn_attr, CDNAttribute), )
def test_convert_or_none_provided_empty_returns_none() -> None: # given attr: Dict[str, Any] = {} convert_func = dict # when result = convert_or_none(attr, convert_func) # then assert result is None
def test_convert_or_none_provided_none_returns_none() -> None: # given attr = None convert_func = int # when result = convert_or_none(attr, convert_func) # then assert result is None
def test_convert_or_none_provided_value_returns_value() -> None: # given attr = "128" convert_func = int # when result = convert_or_none(attr, convert_func) # then assert result == 128
def _to_filtergroups(self, dic) -> FilterGroups: filters = [self._to_filter(ftr) for ftr in dic["filters"]] return FilterGroups( connector=dic["connector"], filterString=dic.get("filterString"), filters=filters, id=convert_or_none(dic.get("id"), ID), metric=dic.get("metric"), not_=bool(dic["not"]), )
def to_interface(self) -> Interface: return Interface( id=convert_or_none(self.id, ID), snmp_id=convert_or_none(self.snmp_id, ID), snmp_speed=self.snmp_speed, snmp_alias=self.snmp_alias, interface_ip=self.interface_ip, interface_ip_netmask=self.interface_ip_netmask, interface_description=self.interface_description, vrf_id=convert_or_none(self.vrf_id, ID), vrf=convert_or_none(self.vrf, VRFAttributesPayload.to_vrf_attributes), secondary_ips=convert_list_or_none(self.secondary_ips, SecondaryIPPayload.to_secondary_ip), company_id=convert_or_none(self.company_id, ID), device_id=convert_or_none(self.device_id, ID), created_date=self.cdate, updated_date=self.edate, initial_snmp_id=convert_or_none(self.initial_snmp_id, ID), initial_snmp_alias=self.initial_snmp_alias, initial_interface_description=self.initial_interface_description, initial_snmp_speed=convert_or_none(self.initial_snmp_speed, int), provider=self.provider, top_nexthop_asns=convert_list_or_none(self.top_nexthop_asns, TopNextHopASNPayload.to_top_next_hop_asn), )
def from_dict(cls, dic: Dict[str, Any]): required_fields = [ "id", "snmp_id", "snmp_speed", "interface_description", "company_id", "device_id", "cdate", "edate", ] validate_fields(class_name=cls.__name__, required_fields=required_fields, dic=dic) # recreate GET/POST/PUT response payload: fill all available fields # warning: snmp_speed comes back as str for GET, but as int for POST/PUT # warning: initial_snmp_id comes back as empty string instead of null when not set return cls( # always returned fields id=dic["id"], snmp_id=dic["snmp_id"], snmp_speed=convert(dic["snmp_speed"], int), interface_description=dic["interface_description"], company_id=dic["company_id"], device_id=dic["device_id"], cdate=dic["cdate"], edate=dic["edate"], # optional fields snmp_alias=dic.get("snmp_alias"), interface_ip=dic.get("interface_ip"), interface_ip_netmask=dic.get("interface_ip_netmask"), vrf_id=dic.get("vrf_id"), vrf=convert_or_none(dic.get("vrf"), VRFAttributesPayload.from_dict), secondary_ips=convert_list_or_none(dic.get("secondary_ips"), SecondaryIPPayload.from_dict), initial_snmp_id=dic.get("initial_snmp_id") if dic.get("initial_snmp_id") != "" else None, initial_snmp_alias=dic.get("initial_snmp_alias"), initial_interface_description=dic.get("initial_interface_description"), initial_snmp_speed=dic.get("initial_snmp_speed"), provider=dic.get("provider"), top_nexthop_asns=convert_list_or_none(dic.get("top_nexthop_asns"), TopNextHopASNPayload.from_dict), )
def from_device(cls, device: Device): # prepare POST/PUT request payload: fill only the user-provided fields return cls( plan_id=convert_or_none(device.plan_id, int), site_id=convert_or_none(device.site_id, int), device_name=device.device_name, device_type=convert_or_none(device.device_type, enum_to_str), device_subtype=convert_or_none(device.device_subtype, enum_to_str), device_description=device.device_description, device_sample_rate=device.device_sample_rate, sending_ips=device.sending_ips, device_snmp_ip=device.device_snmp_ip, device_snmp_community=device.device_snmp_community, minimize_snmp=device.minimize_snmp, device_bgp_type=convert_or_none(device.device_bgp_type, enum_to_str), device_bgp_neighbor_ip=device.device_bgp_neighbor_ip, device_bgp_neighbor_ip6=device.device_bgp_neighbor_ip6, device_bgp_neighbor_asn=device.device_bgp_neighbor_asn, device_bgp_flowspec=device.device_bgp_flowspec, device_bgp_password=device.device_bgp_password, use_bgp_device_id=convert_or_none(device.use_bgp_device_id, int), device_snmp_v3_conf=convert_or_none(device.device_snmp_v3_conf, SNMPv3ConfPayload.from_conf), cdn_attr=convert_or_none(device.cdn_attr, enum_to_str), )