def checkAlarm(alarm): try: # check that the meter is available if alarm.meter_name: if alarm.meter_name in METERS: LOG.debug("meter_name \"%s\" is available." % alarm.meter_name) else: raise NotFoundException( "meter_name:\"%s\" is not available. Available meter names:%s" % (alarm.meter_name, METERS)) else: raise NotDefinedException("meter_name:\"%s\" is not defined.") # check that the statistic is available if alarm.statistic in STATISTICS: LOG.debug("statistic \"%s\" is available." % alarm.statistic) else: raise NotFoundException( "statistic:\"%s\" is not available. Available statistics: %s" % (alarm.statistic, STATISTICS)) # check that the evaluation period is an interger greater than 0 if alarm.evaluation_periods: if isinstance(alarm.evaluation_periods, (long, int)): if alarm.evaluation_periods > 0: LOG.debug("evaluation_periods \"%s\" is valid." % alarm.evaluation_periods) else: raise InvalidInputException( "evaluation_periods:\"%s\" is not valid. It must be greater than 0." % alarm.evaluation_periods) else: raise TypeErrorException( "evaluation_periods:\"%s\" is not valid. It must be an integer." % alarm.evaluation_periods) else: raise NotDefinedException("evaluation_periods is not defined.") if alarm.threshold: if isinstance(alarm.threshold, (long, int)): LOG.debug("threshold \"%s\" is valid." % alarm.threshold) else: raise TypeErrorException( "threshold:\"%s\" is not valid. It must be an integer." % alarm.threshold) else: raise NotDefinedException("threshold is not defined.") if alarm.comparison_operator: if alarm.comparison_operator in COMPARISON_OPERATORS: LOG.debug("comparison_operator \"%s\" is available." % alarm.comparison_operator) else: NotFoundException( "comparison_operator:\"%s\" is not available. Available comparison operators: %s" % (alarm.comparison_operator, COMPARISON_OPERATORS)) else: raise NotDefinedException("comparison_operator is not defined.") except Exception, exc: exc.message = 'Alarm->%s' % exc.message raise exc
def checkRequirementsDependencies(requirements=[], service_instances=[]): for requirement in requirements: try: counter = 0 LOG.debug("Check dependencies for requirement \"%s\"." % requirement.name) for service_instance in service_instances: if requirement.source == service_instance.name: LOG.debug("source \"%s\" was found." % requirement.source) if requirement.parameter == 'private_ip' or requirement.parameter == 'public_ip': LOG.debug("parameter \"%s\" is available." % requirement.parameter) for obj in service_instance.networks: if requirement.obj_name == obj.name: LOG.debug("obj_name \"%s\" was found." % requirement.obj_name) counter += 1 else: raise InvalidInputException( "parameter:\"%s\" is not available." % requirement.parameter) if counter == 0: raise NotFoundException( "requirement:\"%s\" was not found (\"source:%s\", \"obj_name:%s\")." % (requirement.name, requirement.source, requirement.obj_name)) elif counter == 1: LOG.debug("requirement \"%s\" is valid." % requirement.name) else: raise InvalidInputException( "is not valid. Found sources or objects several times.") except Exception, exc: exc.message = 'Requirement:\"%s\"->%s' % (requirement.name, exc.message) raise exc
def checkRequirement(requirement): try: if requirement.name: LOG.debug("Check requirement with name \"%s\"" % requirement.name) else: raise NotDefinedException("name of requirement is not defined.") if requirement.parameter: if requirement.parameter in PARAMETERS: LOG.debug("parameter \"%s\" is available" % requirement.parameter) else: raise NotFoundException( "parameter:\"%s\" is not available. Available parameters:%s" % (requirement.parameter, PARAMETERS)) else: raise NotDefinedException("parameter is not defined.") if requirement.source: LOG.debug("source \"%s\" is defined." % requirement.source) else: raise NotDefinedException("source is not defined.") if requirement.obj_name: LOG.debug("obj_name \"%s\" of requirement is defined." % requirement.obj_name) else: raise NotDefinedException("obj_name is not defined.") except Exception, exc: exc.message = 'Requirement:\"%s\"->%s' % (requirement.name, exc.message) raise exc
def checkKey(key): db = DatabaseManager() existing_keys = db.get_all(Key) if key.name in [existing_key.name for existing_key in existing_keys]: LOG.debug("key \"%s\" is available." % key) else: raise NotFoundException( "key:\"%s\" is not available. Available keys: %s" % (key, [existing_key.name for existing_key in existing_keys]))
def get_by_id(self, _class, _id): self.create_session() try: res = self.session.query(_class).filter_by(id=_id).one() except Exception as e: raise NotFoundException('No object was found in table ' + _class.__name__ + " with id " + str(_id)) return res
def checkServiceType(service_type): db = DatabaseManager() services = db.get_all(Service) found = False for service in services: if service.service_type == service_type: found = True LOG.debug("service_type \"%s\" is available." % service_type) if not found: raise NotFoundException( "service_type:\"%s\" is not available. Available service_types:%s" % (service_type, [service.service_type for service in services]))
def checkFlavor(flavor): db = DatabaseManager() existing_flavors = db.get_all(Flavor) if flavor.name in [ existing_flavor.name for existing_flavor in existing_flavors ]: LOG.debug("flavor \"%s\" is available." % flavor) else: raise NotFoundException( "flavor:\"%s\" is not available. Available flavors: %s" % (flavor, [existing_flavor.name for existing_flavor in existing_flavors]))
def checkImage(image): db = DatabaseManager() existing_images = db.get_all(Image) if image.name in [ existing_image.name for existing_image in existing_images ]: LOG.debug("image \"%s\" is available." % image) else: raise NotFoundException( "image:\"%s\" is not available. Available images: %s" % (image, [existing_image.name for existing_image in existing_images]))
def checkAction(action): try: # check adjustment type if action.adjustment_type: if action.adjustment_type in ADJUSTMENT_TYPES: LOG.debug("adjustment_type \"%s\" is available." % action.adjustment_type) else: raise NotFoundException( "adjustment_type:\"%s\" is not available. Available adjustment types: %s" % (action.adjustment_type, ADJUSTMENT_TYPES)) else: raise NotDefinedException("adjustment_type:\"%s\" is not defined.") # check scaling adjustment if action.scaling_adjustment: if isinstance(action.scaling_adjustment, (long, int)): LOG.debug("scaling_adjusment \"%s\" is valid." % action.scaling_adjustment) else: raise TypeErrorException( "scaling_adjusment:\"%s\" is not valid. It must be an integer." % action.scaling_adjustment) else: raise NotDefinedException("scaling_adjusment is not defined.") # check cooldown if action.cooldown: if isinstance(action.cooldown, (long, int)): if action.cooldown > 0: LOG.debug("cooldown \"%s\" is valid." % action.cooldown) else: raise InvalidInputException( "cooldown:\"%s\" must be greater than 0" % action.cooldown) else: raise TypeErrorException( "cooldown:\"%s\" is not valid. It must be an integer." % action.cooldown) else: raise NotDefinedException("cooldown is not defined.") except Exception, exc: exc.message = 'Action->%s' % exc.message raise exc
def checkRule(rule): try: if rule.name: LOG.debug("Check rule \"%s\"." % rule.name) else: raise NotDefinedException("name is not defined.") # check remote_ip_prefix if rule.remote_ip_prefix: a = re.compile( "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/([012345]?[1-9]|[6]?[0-4])$" ) if a.match(rule.remote_ip_prefix): LOG.debug("remote_ip_prefix:\"%s\" for rule %s is valid." % (rule.remote_ip_prefix, rule.name)) else: raise InvalidInputException( "remote_ip_prefix:\"%s\"is not valid. Example: \"0.0.0.0/0\"." % (rule.remote_ip_prefix)) else: raise NotDefinedException("remote_ip_prefix is not defined.") # check protocol if rule.protocol: if rule.protocol in PROTOCOLS: LOG.debug("protocol \"%s\" is available." % rule.protocol) else: raise NotFoundException( "protocol:\"%s\" is not available. Available protocols are: %s." % (rule.protocol, PROTOCOLS)) else: raise NotDefinedException("protocol is not defined.") # check defined ports port_min = 0 port_max = 65535 if rule.port_range_min and rule.port_range_max: if not isinstance(rule.port_range_min, (long, int)): try: rule.port_range_min = int(rule.port_range_min) except Exception: raise TypeErrorException( "port_range_min:\"%s\" is not valid. It must be an integer." % rule.port_range_min) if not isinstance(rule.port_range_max, (long, int)): try: rule.port_range_max = int(rule.port_range_max) except Exception: raise TypeErrorException( "port_range_max:\"%s\" is not valid. It must be an integer." % rule.port_range_max) if rule.port_range_min <= rule.port_range_max: LOG.debug("port_range is valid (%s <= %s)." % (rule.port_range_min, rule.port_range_max)) else: raise InvalidInputException( "port_range is not valid (%s <= %s). port_range_min is bigger than port_range_max" % (rule.port_range_min, rule.port_range_max)) if rule.port_range_min >= port_min and rule.port_range_min <= port_max: LOG.debug( "port_range_min \"%s\" for rule %s is valid (%s >= %s and %s <= %s)." % (rule.port_range_min, rule.name, rule.port_range_min, port_min, rule.port_range_min, port_max)) else: raise InvalidInputException( "port_range_min:\"%s\" is not valid (%s >= %s and %s <= %s). \"port_range_min\" is not in range." % (rule.port_range_min, rule.port_range_min, port_min, rule.port_range_min, port_max)) if rule.port_range_max >= port_min and rule.port_range_max <= port_max: LOG.debug( "port_range_max \"%s\" for rule %s is valid (%s >= %s and %s <= %s)." % (rule.port_range_max, rule.name, rule.port_range_max, port_min, rule.port_range_max, port_max)) else: raise InvalidInputException( "port_range_max:\"%s\" is not valid (%s >= %s and %s <= %s). \"port_range_max\" is not in range." % (rule.port_range_max, rule.port_range_max, port_min, rule.port_range_max, port_max)) elif rule.port_range_min and not rule.port_range_max: raise InvalidInputException( "port range is not valid. Found \"port_range_min\":%s but no \"port_range_max\"." % (rule.port_range_min)) elif not rule.port_range_min and rule.port_range_max: raise InvalidInputException( "port_range is not valid. Found \"port_range_max\":%s but no \"port_range_min\"." % (rule.port_range_max)) else: LOG.debug("port_range is not defined.") except Exception, exc: exc.message = 'Rule:\"%s\"->%s' % (rule.name, exc.message) raise exc
def checkNetwork(network): try: db = DatabaseManager() existing_networks = db.get_all(Network) found_private_net = False found_subnet = False found_public_net = False for existing_network in existing_networks: if network.private_net == existing_network.ext_id and not found_private_net: if existing_network.public == False: LOG.debug("private_network \"%s\" is available." % network.private_net) found_private_net = True else: raise InvalidInputException( "private_network:\"%s\" is available but it is marked as public and not as private as defined." % network.private_net) for subnet in existing_network.subnets: if network.private_subnet == subnet.ext_id and not found_subnet: found_subnet = True if found_subnet: LOG.debug("private_subnet \"%s\" is available." % network.private_subnet) else: raise InvalidInputException( "private_subnet:\"%s\" is not available." % network.private_subnet) if network.public_net == existing_network.ext_id and not found_public_net: if existing_network.public == True: LOG.debug("public_network \"%s\" is available." % network.public_net) found_public_net = True else: raise InvalidInputException( "network:\"%s\" is available but it is marked as private and not as public as defined." % network.public_net) if not network.private_net and not network.private_subnet and not network.public_net: LOG.debug("Networks were not defined.") elif network.private_net and network.private_subnet and network.public_net: if found_private_net and found_subnet and found_public_net: LOG.debug( "All defined networks are available for network: %s" % network) if not found_private_net: raise NotFoundException("Not found private network: %s" % network) if not found_subnet: raise NotFoundException( "Not found private subnet network: %s" % network) if not found_public_net: raise NotFoundException("Not found public network: %s" % network) elif network.private_net and network.private_subnet and not network.public_net: if found_private_net and found_subnet and not found_public_net: LOG.debug( "All defined networks are available for network: %s" % network) if not found_private_net: raise NotFoundException("Not found private network: %s" % network) if not found_subnet: raise NotFoundException( "Not found private subnet network: %s" % network) elif not network.private_net and network.public_net: raise InvalidInputException( "Private net is not defined but the public.") else: raise InvalidInputException("Error while checking networks.") except Exception, exc: exc.message = 'Network:\"%s\"->%s' % (network.name, exc.message) raise exc
def create(self, config): ###Topology arguments top_name = config.get('name') top_state = 'DEFINED' top_service_instances = [] LOG.debug("Parse Topology \"%s\"" % top_name) ###Parse all service instances described in config file sis_config = config.get('service_instances') for si_config in sis_config: si_args = {} si_args['state'] = 'DEFINED' service = None if si_config.get('service_type'): service_type = si_config.get('service_type') LOG.debug('Fetching service of service_type \"%s\"' % service_type) service_list = self.db.get_by_service_type( Service, service_type) if len(service_list) == 1: service = service_list[0] LOG.debug('Service \"%s\" is available.' % service) si_args['service_type'] = service.service_type si_args['image'] = service.image si_args['flavor'] = service.flavor si_args['configuration'] = service.configuration si_args['key'] = service.key si_args['size'] = service.size si_args['adapter'] = service.adapter si_args['requirements'] = [] for requirement in service.requirements: func = Requirement.__init__ needed_parameters = func.func_code.co_varnames[ 1:func.func_code.co_argcount] args = {} for param in needed_parameters: args[param] = requirement.__dict__.get(param) new_requirement = Requirement(**args) si_args['requirements'].append(new_requirement) si_args['networks'] = [] for network in service.networks: func = Network_Instance.__init__ needed_parameters = func.func_code.co_varnames[ 1:func.func_code.co_argcount] args = {} for param in needed_parameters: args[param] = network.__dict__.get(param) new_network = Network_Instance(**args) si_args['networks'].append(new_network) si_args['user_data'] = [] for command in service.user_data: func = Command.__init__ needed_parameters = func.func_code.co_varnames[ 1:func.func_code.co_argcount] args = {} for param in needed_parameters: args[param] = command.__dict__.get(param) new_command = Command(**args) si_args['user_data'].append(new_command) else: raise NotFoundException( 'service_type:\"%s\" is not available.' % service_type) else: raise NotDefinedException("service_type is not defined.") for si_item in si_config: if si_item == "name": si_args['name'] = si_config.get(si_item) LOG.debug("Parsing service instance \"%s\"" % si_args['name']) elif si_item == "service_type": si_args['service_type'] = si_config.get(si_item) elif si_item == "adapter": si_args['adapter'] = si_config.get(si_item) elif si_item == "image": image_name = si_config.get(si_item) image_list = self.db.get_by_name(Image, image_name) if len(image_list) == 1: image = image_list[0] si_args['image'] = image else: raise NotFoundException( 'image:\"%s\" is not available.' % image_name) elif si_item == "flavor": flavor_name = si_config.get(si_item) flavor_list = self.db.get_by_name(Flavor, flavor_name) if len(flavor_list) == 1: flavor = flavor_list[0] si_args['flavor'] = flavor else: raise NotFoundException( 'flavor:\"%s\" is not available.' % flavor_name) elif si_item == "key": key_name = si_config.get(si_item) key_list = self.db.get_by_name(Key, key_name) if len(key_list) == 1: key = key_list[0] si_args['key'] = key else: raise NotFoundException( 'key:\"%s\" is not available.' % key_name) elif si_item == "size": si_args['size'].update(si_config.get(si_item)) elif si_item == "networks": networks = [] _networks = si_config.get(si_item) LOG.debug("Fetch SecurityGroups for networks %s." % _networks) for _net_inst in _networks: secgroups = [] for _secgroup_name in _net_inst.get('security_groups'): lst = self.db.get_by_name(SecurityGroup, _secgroup_name) if len(lst) > 0: _secgroup = lst[0] else: raise NotFoundException( 'SecurityGroup:\"%s\" is not available.' % _secgroup_name) secgroups.append(_secgroup) _net_inst['security_groups'] = secgroups networks.append(Network_Instance(**_net_inst)) si_args['networks'] = networks elif si_item == "configuration": for key in si_config.get(si_item).keys(): if key in service.configuration.keys(): si_args['configuration'][key] = si_config.get( si_item).get(key) elif si_item == "policies": policies = [] _policies = si_config.get(si_item) for _policy in _policies: _new_policy_args = {} for _po_item in _policy: if _po_item == "name": _new_policy_args.update( {'name': _policy.get(_po_item)}) elif _po_item == "period": _new_policy_args.update( {'period': _policy.get(_po_item)}) elif _po_item == "alarm": _new_alarm_args = _policy.get(_po_item) _new_alarm = Alarm(**_new_alarm_args) _new_policy_args.update({'alarm': _new_alarm}) elif _po_item == "action": _new_action_args = _policy.get(_po_item) _new_action = Action(**_new_action_args) _new_policy_args.update( {'action': _new_action}) try: _new_policy = Policy(**_new_policy_args) except TypeError: raise InvalidInputException() policies.append(_new_policy) si_args['policies'] = policies elif si_item == "requirements": requirements = [] for _req in si_config.get(si_item): _req_args = _req requirement = Requirement(**_req_args) requirements.append(requirement) si_args['requirements'] = requirements elif si_item == "user_data": user_data = [] for _user_data_item in si_config.get(si_item): command = Command(_user_data_item) user_data.append(command) si_args['user_data'] = user_data elif si_item == "location": location = [] for _loc in si_config.get(si_item): LOG.debug("found item location %s" % _loc) loc = Location(_loc) location.append(loc) si_args['location'] = location elif si_item == "relation": relation = [] for _rel in si_config.get(si_item): LOG.debug("found item relation %s" % _rel) rel = Relation(_rel) relation.append(rel) si_args['relation'] = relation else: raise InvalidInputException( "parameter \"%s\" is not provided by Services." % si_config.get(si_item)) ###Initialise Units units = [] unit_number = si_args.get('size').get('def') or 1 for i in range(1, unit_number + 1): if i == 1: _hostname = '%s' % si_args.get('name') else: _hostname = '%s-%s' % (si_args.get('name'), i) _new_unit = Unit(hostname=_hostname, state='DEFINED') units.append(_new_unit) si_args['units'] = units ###Initialise Service Instance new_service_instance = ServiceInstance(**si_args) ###Add the new service instance to the topology top_service_instances.append(new_service_instance) ###Initialise Topology ext_name = '' + top_name + '_' + str(random.randint(1000, 9999)) topology = Topology(name=top_name, state=top_state, service_instances=top_service_instances, ext_name=ext_name) LOG.debug(topology) return topology
def create(self, config): ###Service arguments service_type = config.get('service_type') LOG.debug("Parse Service \"%s\"" % service_type) ###Parse each item described in config file new_service_args = {} for service_item in config.keys(): if service_item == "service_type": new_service_args['service_type'] = config.get(service_item) elif service_item == "image": image_name = config.get(service_item) image_list = self.db.get_by_name(Image, image_name) if len(image_list) == 1: image = image_list[0] new_service_args['image'] = image else: raise NotFoundException('image:\"%s\" is not available.' % image_name) elif service_item == "flavor": flavor_name = config.get(service_item) flavor_list = self.db.get_by_name(Flavor, flavor_name) if len(flavor_list) == 1: flavor = flavor_list[0] new_service_args['flavor'] = flavor else: raise NotFoundException('flavor:\"%s\" is not available.' % flavor_name) elif service_item == "key": key_name = config.get(service_item) key_list = self.db.get_by_name(Key, key_name) if len(key_list) == 1: key = key_list[0] new_service_args['key'] = key else: raise NotFoundException('key:\"%s\" is not available.' % key_name) elif service_item == "size": new_service_args['size'] = config.get(service_item) elif service_item == "networks": networks = [] _networks = config.get(service_item) LOG.debug("Fetch SecurityGroups for networks %s." % _networks) for _net_inst in _networks: secgroups = [] for _secgroup_name in _net_inst.get('security_groups'): lst = self.db.get_by_name(SecurityGroup, _secgroup_name) if len(lst) > 0: _secgroup = lst[0] else: raise NotFoundException( 'SecurityGroup:\"%s\" is not available.' % _secgroup_name) secgroups.append(_secgroup) _net_inst['security_groups'] = secgroups networks.append(Network_Instance(**_net_inst)) new_service_args['networks'] = networks elif service_item == "configuration": new_service_args['configuration'] = config.get(service_item) elif service_item == "requirements": requirements = [] for _req in config.get(service_item): _req_args = _req requirement = Requirement(**_req_args) requirements.append(requirement) new_service_args['requirements'] = requirements elif service_item == "user_data": user_data = [] for _user_data_item in config.get(service_item): command = Command(_user_data_item) user_data.append(command) new_service_args['user_data'] = user_data elif service_item == "version": new_service_args['version'] = config.get(service_item) elif service_item == "adapter": new_service_args['adapter'] = config.get(service_item) else: raise InvalidInputException( "paramter \"%s\" is not provided by Services." % config.get(service_item)) ###Create Service print(new_service_args) new_service = Service(**new_service_args) LOG.debug(new_service) return new_service
def get_floating_ip(self, ip): res = self.nova.floating_ips.list() for _fip in res: if _fip.ip == ip: return _fip raise NotFoundException("Floating ip " + ip + " not found")