def enable_api(self, health_check_api=False): """ Enable LoadMaster RESTfull API This method will attempt to enable the LoadMaster's REST API the 'right' way by initially trying to set it with enableapi parameter. If this fails it will attempt to set it the old way using the progs URL. :param health_check_api: If True, an extra call to the API will be made to verify operability, only works for machines older than 7.2.36. :return: True if successfully enabled. """ # Can't use the HttpClient methods for this as the endpoint is different # (or has strict stipulations) when attempting to enable the API. try: status_code = self._do_request_no_api('access/set?param=enableapi&value=yes') if status_code == 404: self._do_request_no_api('progs/doconfig/enableapi/set/yes') status_code = self._do_request_no_api('progs/status/logout') if status_code != 200: raise KempTechApiException(code=status_code) if health_check_api: # Health check to see if API was actually enabled # if it failed its usually due to auth error so raise a 401 status_code = self._do_request_no_api('access/get?param=version') if status_code != 200: raise KempTechApiException(code=401) elif status_code != 200: raise KempTechApiException(code=status_code) return True except exceptions.RequestException as e: raise KempTechApiException(msg="Enable API failed because of: {}".format( e.__class__.__name__), is_xml_msg=False)
def set_acl_settings(self, autoupdate=None, autoinstall=None, installtime=None): if autoupdate is not None: command = "setautoupdate" key = "enable" value = autoupdate elif autoinstall is not None: command = "setautoinstall" key = "enable" value = autoinstall elif installtime is not None: command = "setinstalltime" key = "hour" value = autoinstall if value in [True, "yes", "y", "1"]: value = "yes" if value in [False, "no", "n", "0"]: value = "no" parameters = { key: value } response = self._get("/geoacl/{}".format(command), parameters) if is_successful(response): pass else: raise KempTechApiException(get_error_msg(response))
def get_acl_settings(self): response = self._get("/geoacl/getsettings") if is_successful(response): data = get_data(response) data = data['GeoAcl'] else: raise KempTechApiException(get_error_msg(response)) acl_settings = {} for k, v in data.items(): if v == "yes": v = True elif v == "no": v = False elif v == "Never": v = None else: try: v = int(v) # pylint: disable=redefined-variable-type except ValueError: pass acl_settings[k.lower()] = v return acl_settings
def acl_install(self): response = self._get("/geoacl/installnow") if is_successful(response): pass else: raise KempTechApiException(get_error_msg(response))
def refresh_dns(self): api = "/resolvenow" response = self._get(api) if is_successful(response): pass else: raise KempTechApiException(get_error_msg(response))
def acl_changes(self): response = self._get("/geoacl/downloadchanges") if is_successful(response): pass else: raise KempTechApiException(get_error_msg(response)) return response
def get_eula(self): api = "/readeula" response = self._get(api) if is_successful(response): data = get_data(response) else: raise KempTechApiException(get_error_msg(response)) self.magic = data['Magic'] return data['Eula']
def remove_acl(self, type, value): parameters = { "type": type, "addr": value } response = self._get("/geoacl/removecustom", parameters) if is_successful(response): pass else: raise KempTechApiException(get_error_msg(response))
def initial_password(self, password="******"): api = "/set_initial_passwd" parameters = { "passwd": password } response = self._get(api, parameters=parameters) if not is_successful(response): raise KempTechApiException(get_error_msg(response)) self.password = password
def initial_license(self, license_type=None, callhome=None, new_password=None, kempid=None): self.get_eula() self.accept_eula(license_type) self.set_callhome(callhome) if kempid is not None: self.alsi_license(kempid['username'], kempid['password']) self.initial_password(new_password) else: raise KempTechApiException("Please license before proceeding.")
def accept_eula(self, license_type="trial"): api = "/accepteula" parameters = { "type": license_type, "magic": self.magic } response = self._get(api, parameters=parameters) if is_successful(response): data = get_data(response) else: raise KempTechApiException(get_error_msg(response)) self.magic = data['Magic']
def set_callhome(self, enabled=True): api = "/accepteula2" if enabled is True: enabled = "yes" else: enabled = "no" parameters = { "accept": enabled, "magic": self.magic } response = self._get(api, parameters=parameters) if not is_successful(response): raise KempTechApiException(get_error_msg(response))
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 apply_template(self, virtual_ip, port, protocol, template_name, nickname=None): params = { 'vs': virtual_ip, 'port': port, 'prot': protocol, 'name': template_name, } existing = self.vs.keys() if nickname is not None: params['nickname'] = nickname response = self._get("/addvs", parameters=params) if is_successful(response): vs = {k: v for k, v in self.vs.items() if k not in existing} else: raise KempTechApiException(get_error_msg(response)) return vs
def license(self): """Current license on the LoadMaster :return: License information """ response = self._get("/licenseinfo") if is_successful(response): data = get_data(response) license_info = {} # Fix annoying API weirdness for k, v in data.items(): k = k.lower() try: if v[0] == '"': v = v[1:] if v[-1] == '"': v = v[:-1] if v[-1] == "\n": v = v[:-1] except (IndexError, KeyError): # Catch scenarios where there is no data pass if isinstance(v, str): v = v.replace(" ", " ") license_info[k] = v else: raise KempTechApiException(get_error_msg(response)) return license_info
def get_acl(self, type): parameters = { "type": type } response = self._get("/geoacl/listcustom", parameters) if is_successful(response): data = get_data(response) else: raise KempTechApiException(get_error_msg(response)) list_tag = "{}list".format(type).capitalize() acl_list = data[list_tag] if acl_list is None: acl_list = [] elif isinstance(acl_list, dict): acl_list = acl_list.get('addr') if not isinstance(acl_list, list): acl_list = [acl_list] return acl_list
def send_response(response): parsed_resp = parse_to_dict(response) if is_successful(parsed_resp): return parsed_resp else: raise KempTechApiException(get_error_msg(parsed_resp))