def get_loadbalancer_object_from_dictionary(self, dict_obj): if not isinstance(dict_obj, dict): self.logger.debug( "Bad Request: We are expecting a dictionary to be passed as a parameter" ) raise BadRequestException( "Validation fault", "the object is not valid", "malformed request body. expecting a loadBalancer object") loadBalancer = Utils.get_object_from_dictionary( "loadBalancer", dict_obj, self) return loadBalancer
def get_lbnode_object_from_request(self, req): dict_obj = Utils.get_dictionary_from_request(req, self.request_format) if not isinstance(dict_obj, dict): self.logger.debug( "Bad Request: The root element is not a dictionary") raise BadRequestException("Validation fault", "the object is not valid", "malformed request body.") dict_keys = dict_obj.keys() if "node" in dict_keys: state = dict_obj["node"] lbnode = self.get_lbnode_object_from_dictionary(state) return lbnode else: self.logger.debug( "Bad Request: The root element in the request is neither \"nodes\", nor \"node\"" ) raise BadRequestException( "Validation fault", "the object is not valid", "malformed request body. expecting a node object")
def get_lbnode_object_from_dictionary(self, dict_obj): if not isinstance(dict_obj, dict): self.logger.debug( "Bad Request: We are expecting a dictionary to be passed as a parameter" ) raise BadRequestException( "Validation fault", "the object is not valid", "malformed request body: expecting a node object") lbnode = Utils.get_object_from_dictionary("node", dict_obj, self) self.logger.debug("Exiting get_lbnode_object_from_dictionary()") return lbnode
def validate_loadbalancer_object_for_add(self, loadBalancer): error_list = self._validate_loadbalancer_attributes_for_add_operation( loadBalancer) if error_list: badReq = BadRequestException("Validation fault", "object is not valid", error_list) self.logger.debug( "About to raise a BadRequest Exception with following attribute:\r\n \ message: %s \r\n \ detail: %s \r\n \ validationErrors: %s \r\n" % (badReq.message, badReq.detail, str(badReq.validationErrors))) raise badReq
def _allocate_loadBalancer_vips(self, loadBalancer, tenant_id, loadBalancerId): if not loadBalancer.virtualIps: raise BadRequestException("validation fault", "object is not valid", "No virtual IP field specified.") if len(loadBalancer.virtualIps) > 1: raise NotImplementedException( "Not Implemented. " + "Sorry, this loadBalancer service currently allow only for one virtual Ip per loadBalancer" ) for virtualIP in loadBalancer.virtualIps: self._allocate_loadBalancer_vip(virtualIP, tenant_id, loadBalancerId)
def get_loadbalancer_objects_from_dictionary(self, dict_list): if not isinstance(dict_list, list): self.logger.debug( "Bad Request: In Add(), loadBalancers dict obj is expected to contain a list" ) raise BadRequestException( "Validation fault", "the object is not valid", "malformed request: expecting a list of loadBalancer objects") loadbalancers = [] for item in dict_list: loadbalancer = self.get_loadbalancer_object_from_dictionary(item) loadbalancers.append(loadbalancer) return loadbalancers
def _nitro_check_for_errors(self, response_object): if not ('errorcode' in response_object.keys()) or not('message' in response_object.keys()): self.logger.error("Nitro Response body doesn't contain errorcode and/or message") raise ImplementationErrorException("programming error.") errorcode = response_object["errorcode"] errormessage = response_object["message"] self.logger.debug("errorcode=%s" % errorcode) self.logger.debug("message=%s" % errormessage) if errorcode == 444: return "SESSION_EXPIRED" if errorcode == 258 or errorcode == 344: self.logger.debug("Netscaler entity not found: %s " % errormessage) raise ItemNotFoundException("resource not found") if errorcode == 273 or errorcode == 2305 or errorcode == 304: self.logger.debug("Netscaler entity already exists: %s" % errormessage) raise BadRequestException("Logic fault", "resource already exists with these settings") if errorcode == 257: self.logger.debug("Very likely to be the wrong session persistence setting for this lb protocol: %s" % errormessage) raise BadRequestException("Logic fault", "session persistence persistenceType not compatible with protocol") if errorcode == 1097: self.logger.debug("Invalid argument for Netscaler entity: %s" % errormessage) raise BadRequestException("Validation fault", "invalid value for attribute", errormessage) if errorcode == 1075: self.logger.debug("Invalid argument for Netscaler entity: %s" % errormessage) """ chop-off the name portion of the message, so as not to reveal how we are mapping names. """ index = errormessage.find('[') if index != -1: errormessage = errormessage[:index] raise BadRequestException("Validation fault", "invalid value for attribute", errormessage) if errorcode == 1110: self.logger.debug("Invalid IP address for Netscaler entity: %s" % errormessage) raise BadRequestException("Validation fault", "invalid value for attribute", "Invalid IP address") """ Catch all check """ if errorcode != 0: self.logger.debug("Nitro Response error code unknown: %d" % errorcode) raise BadRequestException("Unknown Error", "Error with request", "This request caused an error and cannot be fulfilled.") return "SUCCESS"
def _adjust_healthmonitor_state(self, newHealthMonitor, existingHealthMonitor): if not "type" in newHealthMonitor and "type" in existingHealthMonitor: newHealthMonitor.type = existingHealthMonitor.type if not "delay" in newHealthMonitor and "delay" in existingHealthMonitor: newHealthMonitor.delay = existingHealthMonitor.delay if not "timeout" in newHealthMonitor and "timeout" in existingHealthMonitor: newHealthMonitor.timeout = existingHealthMonitor.timeout if (not "attemptsBeforeDeactivation" in newHealthMonitor and "attemptsBeforeDeactivation" in existingHealthMonitor): newHealthMonitor.attemptsBeforeDeactivation = existingHealthMonitor.attemptsBeforeDeactivation if newHealthMonitor.type.upper() == "CONNECT": return if not existingHealthMonitor.type.upper().startswith("HTTP"): return if not "path" in newHealthMonitor and "path" in existingHealthMonitor: newHealthMonitor.path = existingHealthMonitor.path if not "statusRegex" in newHealthMonitor and "statusRegex" in existingHealthMonitor: newHealthMonitor.statusRegex = existingHealthMonitor.statusRegex if not "bodyRegex" in newHealthMonitor and "bodyRegex" in existingHealthMonitor: newHealthMonitor.bodyRegex = existingHealthMonitor.bodyRegex if "delay" in newHealthMonitor and "timeout" in newHealthMonitor: delay = int(newHealthMonitor.delay) timeout = int(newHealthMonitor.timeout) if delay <= timeout: self.logger.debug( "Validation error: healthMonitor delay must be greater than timeout" ) raise BadRequestException( "Validation fault", "invalid object", "healthMonitor delay attribute must have a value greater than that of timeout attribute" ) self.logger.debug("adjusted health monitor: %s" % repr(newHealthMonitor))
def _get_netscaler_servicebinding_from_node(self, loadBalancerId, loadBalancer, nodeId, node): service_binding_obj = {} lbvserver_name = NitroUtils.get_lbvservername_from_loadbalancerid(loadBalancerId) service_name = NitroUtils.get_servicename_from_nodeid(loadBalancerId, nodeId) service_binding_obj["name"] = lbvserver_name service_binding_obj["servicename"] = service_name if "weight" in node and node.weight != 1 : if not loadBalancer.algorithm.startswith("WEIGHTED_"): raise BadRequestException("validation fault", "invalid object", "LB algorithm in use doesn't allow for weighted nodes") service_binding_obj["weight"] = node["weight"] return service_binding_obj
def _check_loadbalancer_object_for_update(self, loadBalancer): error_list = self.validate_loadbalancer_object_for_update(loadBalancer) if error_list: raise BadRequestException("Validation fault", "invalid object", error_list)
def get_object_from_dictionary(nodename, dict_obj, context, parentnode=None): stateobject = context.get_element_stateobject(nodename) if not stateobject: valid_lists = context.get_valid_listnames() if nodename in valid_lists: if Utils.lbservice.plurals != None: singulars = dict( zip(Utils.lbservice.plurals.values(), Utils.lbservice.plurals.keys())) singlename = singulars[nodename] else: singlename = singulars[:-1] raise BadRequestException( "Validation fault", "invalid object", "\"%s\" property is expected to be a list of \"%s\" elements" % (nodename, singlename)) else: raise BadRequestException( "Validation fault", "invalid object", "\"%s\" is not a valid property of \"%s\"" % (nodename, parentnode)) if not isinstance(dict_obj, dict): return None for key, val in dict_obj.items(): if isinstance(val, basestring): try: setattr(stateobject, str(key), str(val)) except AttributeError: Utils.logger.debug( "Bad Request: Attribute %s is invalid for the obj %s" % (key, stateobject.__class__.__name__)) raise BadRequestException( "Validation fault", "The object is not valid", "%s is not a property for object %s" % (key, nodename)) elif isinstance(val, list): if Utils.lbservice.plurals != None: singulars = dict( zip(Utils.lbservice.plurals.values(), Utils.lbservice.plurals.keys())) if key in singulars.keys(): itemname = singulars[key] else: raise BadRequestException( "Validation fault", "The object is not valid", "%s is not a recognized property in object %s" % (key, nodename)) else: raise ImplementationErrorException( "We need to have a plurals dictionary") try: setattr(stateobject, str(key), []) except AttributeError: Utils.logger.debug( "Bad Request: %s is invalid for the obj %s" % (key, stateobject.__class__.__name__)) raise BadRequestException( "Validation fault", "The object is not valid", "%s is a not a recognized property of object %s" % (key, nodename)) key_attr = getattr(stateobject, str(key)) for item in val: item_obj = Utils.get_object_from_dictionary( itemname, item, context, nodename) key_attr.append(item_obj) elif isinstance(val, dict): item_obj = Utils.get_object_from_dictionary( key, val, context, nodename) try: setattr(stateobject, str(key), item_obj) except AttributeError: Utils.logger.debug( "Bad Request: Attribute %s is invalid for the obj %s" % (key, stateobject.__class__.__name__)) raise BadRequestException( "Validation fault", "The object is not valid", "%s is not a recognized property of object %s" % (key, nodename)) continue return stateobject
def _get_netscaler_monitor_from_healthmonitor(self, loadBalancerId, loadBalancer, healthMonitor): self.logger.debug( "obtaining a netscaler monitor from an OpenStack healthmonitor: %s" % str(healthMonitor)) if not "type" in healthMonitor or not healthMonitor.type: self.logger.debug("healthMonitor type attribute missing") raise BadRequestException("validation fault", "invalid object", "healthMonitor type attribute missing") if not healthMonitor.type.upper() in ["CONNECT", "HTTP", "HTTPS"]: self.logger.debug( "healthMonitor type attribute has an invalid value") raise BadRequestException( "validation fault", "invalid object", "healthMonitor type attribute has an invalid value") monitor = {} monitor[ "monitorname"] = NitroUtils.get_monitorname_from_loadBalancerId( loadBalancerId) if healthMonitor.type.upper() in ["HTTP", "HTTPS"]: monitor["type"] = "USER" monitor["scriptname"] = self.usermonitor else: monitor["type"] = "TCP" if "delay" in healthMonitor: monitor["interval"] = int(healthMonitor.delay) if "timeout" in healthMonitor: monitor["resptimeout"] = int(healthMonitor.timeout) if "attemptsBeforeDeactivation" in healthMonitor: monitor["retries"] = int(healthMonitor.attemptsBeforeDeactivation) if monitor["type"] == "TCP": return monitor if not "path" in healthMonitor or not healthMonitor.path: self.logger.debug( "healthMonitor of type HTTP or HTTPS has the path attribute missing" ) raise BadRequestException( "validation fault", "invalid object", "healthMonitor of type HTTP or HTTPS has the path attribute missing" ) type_expr = '='.join(["type", healthMonitor.type]) scriptargs = type_expr path = '\'' + healthMonitor.path + '\'' path_expr = '='.join(["path", path]) scriptargs = ';'.join([scriptargs, path_expr]) if not "statusRegex" in healthMonitor or not healthMonitor.statusRegex: healthMonitor.statusRegex = "[1-5]{3}" statusRegex = '\'' + healthMonitor.statusRegex + "\'" status_expr = '='.join(["statusRegex", statusRegex]) scriptargs = ';'.join([scriptargs, status_expr]) if not "bodyRegex" in healthMonitor or not healthMonitor.bodyRegex: healthMonitor.bodyRegex = ".*" bodyRegex = '\'' + healthMonitor.bodyRegex + "\'" body_expr = '='.join(["bodyRegex", bodyRegex]) scriptargs = ';'.join([scriptargs, body_expr]) monitor["scriptargs"] = scriptargs self.logger.debug("Netscaler monitor built from healthmonitor: %s" % str(monitor)) return monitor
def _check_lbnode_object_for_update(self, lbnode): error_list = self.validate_node_object_for_update(lbnode) if error_list: raise BadRequestException("Validation fault", "invalid object", error_list)