def _get_nitro_response(self, service, response): """converts nitro response into object and returns the object array in case of get request. :param service: :param response: """ try: result = service.payload_formatter.string_to_resource( auditnslogpolicy_vpnvserver_binding_response, response, self.__class__.__name__) if (result.errorcode != 0): if (result.errorcode == 444): service.clear_session(self) if result.severity: if (result.severity == "ERROR"): raise nitro_exception(result.errorcode, str(result.message), str(result.severity)) else: raise nitro_exception(result.errorcode, str(result.message), str(result.severity)) return result.auditnslogpolicy_vpnvserver_binding except Exception as e: raise e
def _get_nitro_response(self, service, response): """converts nitro response into object and returns the object array in case of get request. :param service: :param response: """ try: result = service.payload_formatter.string_to_resource( protocolhttp_response, response, self.__class__.__name__.replace('_stats', '')) if (result.errorcode != 0): if (result.errorcode == 444): service.clear_session(self) if result.severity: if (result.severity == "ERROR"): raise nitro_exception(result.errorcode, str(result.message), str(result.severity)) else: raise nitro_exception(result.errorcode, str(result.message), str(result.severity)) return result.protocolhttp except Exception as e: raise e
def get_request(self, service, opt): """This method, forms the http GET request, applies on the netscaler. Reads the response from the netscaler and converts it to corresponding resource type. :param service: nitro_service object :param opt: Options class object :returns: Array of requested resources. """ try: ipaddress = service.ipaddress version = service.version sessionid = service.sessionid objtype = self.__class__.get_object_type() protocol = service.protocol # build URL urlstr = protocol + "://" + ipaddress + "/nitro/" + version + "/config/" + objtype name = self._get_object_name() if (name and len(name) > 0): urlstr = urlstr + "/" + str( nitro_util.encode(nitro_util.encode(name))) if (opt or (service.warning)): optionstr = "" if (opt): optionstr = opt.to_string() if (len(optionstr) > 0): urlstr = urlstr + "?" urlstr = urlstr + optionstr if (service.warning): if (opt and len(optionstr) > 0): urlstr = urlstr + "&" else: urlstr = urlstr + "?" urlstr = urlstr + "warning=yes" headers = { 'Content-type': 'application/vnd.com.citrix.netscaler.' + objtype + '+json', 'Connection': 'keep-alive', 'Set-Cookie': 'NITRO_AUTH_TOKEN=' + sessionid } response = requests.get(urlstr, headers=headers, timeout=service.timeout, verify=service.certvalidation) if not response.ok: response = json.loads(response.text) raise nitro_exception((response['errorcode']), str(response['message']), str(response['severity'])) else: result = self._get_nitro_response(service, response.text) return result except Exception as e: raise e
def rename_resource(self, service, newname): """Use this method to perform a rename operation on netscaler resource. :param service: nitro_service object :param newname: new name to be set to the specified resource :returns: status of the operation performed. Throws: """ try: if not service.isLogin(): service.login() if '_newname' in self.__dict__: self.newname = newname else: raise nitro_exception( -1, str("Rename is not supported for this resource"), ) opt = options() opt.action = "rename" response = self.post_request(service, opt) return response except Exception as e: raise e
def protocol(self, protocol): """Sets the protocol. :param protocol: The protocol to be set. """ try: if protocol or protocol.lower!="http" or protocol.lower!="https": raise nitro_exception("error: protocol value " + protocol + " is not supported") self._protocol = protocol except Exception as e: raise e
def __init__(self, ip, protocol="http", payload_format=Json()): """ nitro_service class constructor specifying ip, format and protocol. @param ip Ipadress of the netscaler on which configuration is to be run. @param format format wire language to be used. eg: Json,XML @param protocol Protocol. """ self._ipaddress = ip self._version = "v1" self._protocol = protocol self._format = payload_format if protocol.lower()!="http" and protocol.lower()!="https": raise nitro_exception("error: protocol value " + protocol + " is not supported")
def _get_nitro_response(self, service, response): """ :param service: :param response: """ try: resources = [statobjects() for _ in range(1)] result = service.get_payload_formatter().string_to_resource(statobjects_response, response) if(result.errorcode != 0): if (result.errorcode == 444): service.clear_session() if(result.severity is not None): if (result.severity == "ERROR"): raise nitro_exception(result.message,result.errorcode) else: raise nitro_exception(result.message,result.errorcode) resources[0] = result.statobjects return resources except Exception as e: raise e
def delete_request(self, service, req_args): """This method, forms the http DELETE request, applies on the netscaler. Reads the response from the netscaler and converts it to base response. :param service: nitro_service object :param req_args: :returns: Array of requested resources. Throws: """ try: ipaddress = service.ipaddress version = service.version sessionid = service.sessionid objtype = self.__class__.get_object_type() protocol = service.protocol # build URL urlstr = protocol + "://" + ipaddress + "/nitro/" + version + "/config/" + objtype name = self._get_object_name() if (name and len(name) > 0): urlstr = urlstr + "/" + nitro_util.encode( nitro_util.encode(name)) if (req_args or service.warning): urlstr = urlstr + "?" if (req_args): urlstr = urlstr + "args=" + req_args if (service.warning): if (req_args): urlstr = urlstr + "&" urlstr = urlstr + "warning=yes" headers = { 'Content-type': 'application/vnd.com.citrix.netscaler.' + objtype + '+json', 'Connection': 'keep-alive', 'Set-Cookie': 'NITRO_AUTH_TOKEN=' + sessionid } response = requests.delete(urlstr, headers=headers, timeout=service.timeout, verify=service.certvalidation) if not response.ok: response = json.loads(response.text) raise nitro_exception((response['errorcode']), str(response['message']), str(response['severity'])) result = service.payload_formatter.string_to_resource( base_response, response.text, self.__class__.__name__) if (result.errorcode != 0): if (result.errorcode == 444): service.clear_session(self) if result.severity: if (result.severity == "ERROR"): raise nitro_exception(result.errorcode, str(result.message), str(result.severity)) else: raise nitro_exception(result.errorcode, str(result.message), str(result.severity)) return result except Exception as e: raise e
def _post(resourcetype, service, request, opt, bulk=""): """This method, forms the http POST request, applies on the netscaler. Reads the response from the netscaler and converts it to base response. :param service: nitro_service object :param request: Json request :param resourcetype: :param opt: :param bulk: (Default value = "") :returns: status of the operation performed. Throws: """ try: ipaddress = service.ipaddress version = service.version protocol = service.protocol if inspect.isclass(resourcetype): resrc_type = resourcetype.get_object_type() else: resrc_type = resourcetype.__class__.get_object_type() urlstr = protocol + "://" + ipaddress + "/nitro/" + version + "/config/" + resrc_type warning = service.warning onerror = service.onerror if (opt or (warning and warning == True) or onerror): if opt: if opt.action: urlstr = urlstr + "?action=" + opt.action if warning and warning == True: if opt: urlstr = urlstr + "&warning=yes" else: urlstr = urlstr + "?warning=yes" if onerror: if opt or (warning and warning == True): urlstr = urlstr + "&onerror=" + onerror else: urlstr = urlstr + "?onerror=" + onerror if service.sessionid: if bulk: headers = { 'Content-type': 'application/vnd.com.citrix.netscaler.' + resrc_type + '_list+json', 'Connection': 'keep-alive', 'Set-Cookie': 'NITRO_AUTH_TOKEN=' + service.sessionid } else: headers = { 'Content-type': 'application/vnd.com.citrix.netscaler.' + resrc_type + '+json', 'Connection': 'keep-alive', 'Set-Cookie': 'NITRO_AUTH_TOKEN=' + service.sessionid } else: headers = { 'Content-type': 'application/vnd.com.citrix.netscaler.' + resrc_type + '+json', 'Connection': 'keep-alive' } response = requests.post(urlstr, data=request, headers=headers, timeout=service.timeout, verify=service.certvalidation) if not response.ok: response = json.loads(response.text) raise nitro_exception((response['errorcode']), str(response['message']), str(response['severity'])) elif bulk and response.text: return response.text else: nitro_response = {} if resrc_type == "login": nitro_response['sessionid'] = nitro_util.decode( response.cookies['NITRO_AUTH_TOKEN']) nitro_response['errorcode'] = 0 nitro_response['message'] = "Done" nitro_response['severity'] = "" return json.dumps(nitro_response) except Exception as e: raise e