def get_all_objects(self): # x variables are the object while x_data is the OrderedDict virtual_services = [] response = self._get("/listvs") data = get_data(response) virtual_services_data = data.get('VS', []) virtual_services_data = cast_to_list(virtual_services_data) # create vs and rs objects at this point # loop through all vss and attach matching real server objects for service_data in virtual_services_data: master_vs_id = int(service_data.get('MasterVSID', 0)) if master_vs_id != 0: for vs in virtual_services_data: if int(vs.get("Index", 0)) == master_vs_id: virt_serv = self.build_virtual_service(service_data, vs) else: virt_serv = self.build_virtual_service(service_data, response) real_servers = cast_to_list(service_data.get("Rs", [])) for server_data in real_servers: rs = virt_serv.build_real_server(server_data) virt_serv.real_servers.append(rs) virtual_services.append(virt_serv) # replace subvs's with vs's that have RSs in them. for vs in virtual_services: for subvs in vs.subvs_entries: for top_level_vs in virtual_services: if subvs.index == top_level_vs.index: subvs.real_servers = top_level_vs.real_servers return virtual_services
def build_virtual_service(self, service, response=None): """Create a VirtualService instance with populated with API parameters This does not include potentially attached real servers :param service: OrderedDict populated with virtual service data :param response: Optional response of a listvs call. This acts as a cache, if you want to create a lot of VirtualService objects in a row, such as with looping, you can call listvs and pass the response in each time and this will nullify the extra listvs calls. :return: VirtualService object with populated attributes """ is_sub_vs = True if int(service.get('MasterVSID', 0)) != 0 else False if is_sub_vs: # `response` needs to be a dict in here # Add lb properties to the sub vs if response is None: response = self._get("/showvs", {"vs": service.get('MasterVSID')}) parent_vs_data = get_data(response) else: parent_vs_data = response subvs_lb_props = get_sub_vs_list_from_data(parent_vs_data)[1] virt_serv = VirtualService(self.access_info, service.get('Index'), is_sub_vs=True) virt_serv.subvs_data = subvs_lb_props[service.get('Index')] virt_serv.subvs_data['parentvs'] = service.get('MasterVSID') else: # `response` needs to be a raw xml output here # Add any sub VSs to the top level VS if response is None: response = self._get("/listvs") data = get_data(response) virt_serv = VirtualService(self.access_info, service.get('VSAddress'), service.get('VSPort'), service.get('Protocol'), is_sub_vs=False) virt_serv.subvs_entries = [] services = get_dict_entry_as_list("VS", data) this_vs_index = service.get('Index') for vs in services: # add subvs to parent vs if vs['MasterVSID'] == this_vs_index: subvs = VirtualService(self.access_info, vs['Index'], is_sub_vs=True) subvs.populate_default_attributes(vs) subvs_api_entries = service.get("SubVS", []) subvs_api_entries = cast_to_list(subvs_api_entries) for subvs_api in subvs_api_entries: # add the "Rs" part of the subvs to the subvs instance if subvs_api["VSIndex"] == subvs.index: subvs.subvs_data = subvs_api # Have to add a parentvs hook to make life easy subvs.subvs_data['parentvs'] = this_vs_index virt_serv.subvs_entries.append(subvs) virt_serv.populate_default_attributes(service) return virt_serv
def set_user_perms(self, user, perms=None): perms = [] if perms is None else perms perms = cast_to_list(perms) params = { 'user': user, 'perms': ",".join([perm for perm in perms]), } response = self._get('/usersetperms', params) return send_response(response)
def get_rules(self): response = self._get("/showrule") data = get_data(response) rules_list = [] for rule_type, rules in data.items(): rules = cast_to_list(rules) for rule in rules: rule['type'] = rule_type rule_object = build_object(Rule, self.access_info, rule) rules_list.append(rule_object) return rules_list
def get_ranges(self): try: response = self._get("/listips") data = get_data(response) ranges = data.get('IPAddress', []) except KempTechApiException: ranges = [] range_list = [] ranges = cast_to_list(ranges) for r in ranges: range = self.build_range(r) range_list.append(range) return range_list
def get_clusters(self): try: response = self._get("/listclusters") data = get_data(response) clusters = data.get('cluster', []) except KempTechApiException: clusters = [] cluster_list = [] clusters = cast_to_list(clusters) for c in clusters: cluster = self.build_cluster(c) cluster_list.append(cluster) return cluster_list
def get_customlocations(self): try: response = self._get("/listcustomlocation") data = get_data(response) customlocations = data.get('location', []) except KempTechApiException: customlocations = [] customlocation_list = [] customlocations = cast_to_list(customlocations) for c in customlocations: customlocation = self.build_customlocation(c) customlocation_list.append(customlocation) return customlocation_list
def get_virtual_services(self): response = self._get("/listvs") data = get_data(response) virtual_services = [] services = data.get('VS', []) services = cast_to_list(services) for service in services: master_vs_id = int(service.get('MasterVSID', 0)) if master_vs_id != 0: for vs in services: if int(vs.get("Index", 0)) == master_vs_id: virt_serv = self.build_virtual_service(service, vs) else: virt_serv = self.build_virtual_service(service, response) virtual_services.append(virt_serv) return virtual_services
def get_rule(self, name): response = self._get("/showrule", {"name": name}) data = get_data(response) rules_list = [] rule_object = None if len(data) > 1: raise KempTechApiException("Too many rules returned") for rule_type, rules in data.items(): rules = cast_to_list(rules) for rule in rules: rule['type'] = rule_type rule_object = build_object(Rule, self.access_info, rule) rules_list.append(rule_object) return rule_object
def get_interfaces(self): interfaces = [] try: response = self._get('/showiface') data = get_data(response) interfaces_data = data.get('Interface', []) interfaces_data = cast_to_list(interfaces_data) for iface_data in interfaces_data: iface = build_object(Interface, self.access_info, iface_data) # Check for duplicate IPs as there is a bug in LoadMaster showiface # that makes unset interfaces inherit the previous interfaces IP. for interface in interfaces: if iface.addr == interface.addr: break else: interfaces.append(iface) return interfaces except KempTechApiException as e: # If showiface does not support listing of all interfaces (possibly due to # older version loadmasters) do it the hard way by doing it one by one getting # the IDs from /access/stats. # This will cause N+1 API calls to occur, N being the number of interfaces. if hasattr(e, "status_code") and e.status_code == 422: try: response = self._get('/stats') data = get_data(response) xml_object = data.get('Network', {}) except KempTechApiException: xml_object = {} for k, v in xml_object.items(): obj = self.get_interface(v['ifaceID']) obj.name = k interfaces.append(obj) return interfaces else: raise