def _validate_period(self, period): if (not 0 < int(period) <= (60 * 60 * 24)): err = "The length of Period is 1~86400. (24 hours)" raise exception.InvalidParameterValue(err) if (not int(period) % 60 == 0): err = "Period is must be multiple of 60." raise exception.InvalidParameterValue(err) return True
def _validate_instanceaction(self, actions, project_id, context): instanceactions = [action for action in actions if utils.validate_instance_action(action)] parsed = [utils.parse_instance_action(a) for a in instanceactions] nc = utils.get_python_novaclient() err = "Server is not found" for action_type, vm_uuid in parsed: try: server = nc.servers.get(vm_uuid) except NotFound: raise exception.InvalidParameterValue(err) if not context.is_admin and server.tenant_id != project_id: raise exception.InvalidParameterValue(err)
def validate_get_metric_statistics(self, start_time, end_time, period): minute = datetime.timedelta(minutes=1) max_query_period = FLAGS.get('max_query_period_minutes') * minute max_query_datapoints = FLAGS.get('max_query_datapoints') period_diff = end_time - start_time if not (datetime.timedelta(0) <= period_diff <= max_query_period): err = "Difference between start_time and end_time should be "\ "lesser than %s" % max_query_period raise exception.InvalidParameterValue(err) queried_datapoints = (period_diff.total_seconds() / (int(period))) if queried_datapoints > max_query_datapoints: err = "Requested too many datapoints (%d). "\ "Limitation is %s datapoints" % (queried_datapoints, max_query_datapoints) raise exception.InvalidParameterValue(err)
def validate_put_metric_alarm(self, period, evaluation_periods): self._validate_period(period) self._validate_evaluation_periods(evaluation_periods) if (int(period) * int(evaluation_periods) > (60 * 60 * 24)): err = "Period * EvaluationPeriods should not exceed 86400(24 hours)" raise exception.InvalidParameterValue(err)
def check_unit(self, unit): unit_sample = utils.UNITS if unit and (unit not in unit_sample): err = "Unsuitable Unit Value" raise exception.InvalidParameterValue(err) return True
def describe_alarms_for_metric(self, project_id, namespace, metric_name, dimensions=None, period=None, statistic=None, unit=None): metric_key = self.get_metric_key(project_id, namespace, metric_name, dimensions) if not metric_key: raise exception.InvalidParameterValue("no metric") expr_list = [create_index_expression("metric_key", metric_key)] if period: expr = create_index_expression("period", int(period)) expr_list.append(expr) if statistic: expr = create_index_expression("statistic", statistic) expr_list.append(expr) if unit: expr = create_index_expression("unit", unit) expr_list.append(expr) LOG.info("expr %s" % expr_list) index_clause = pycassa.create_index_clause(expr_list) items = self.cf_metric_alarm.get_indexed_slices(index_clause) return items
def check_statistic(self, statistic): statistic_sample = db.Cassandra.STATISTICS if statistic and (statistic not in statistic_sample): err_template = "Statistic value should be in %s not %s" err = err_template % (statistic_sample, statistic) raise exception.InvalidParameterValue(err) return True
def check_state_value(self, state_value): state_value_sample = ['OK', 'ALARM', 'INSUFFICIENT_DATA'] if state_value and (state_value not in state_value_sample): err_template = "State value should be one of %s, not %s" err = err_template % (state_value_sample, state_value) raise exception.InvalidParameterValue(err) return True
def check_history_item_type(self, history_item_type): history_item_sample = ['ConfigurationUpdate', 'StateUpdate', 'Action'] if history_item_type and (history_item_type not in history_item_sample): err = "Unsuitable History Item Type Value" raise exception.InvalidParameterValue(err) return True
def _check_ascii(self, v, name, min_length=1, max_length=255): if isinstance(v, str) or isinstance(v, unicode): if not (min_length <= len(v) <= max_length): err = "%s length should be in %d ~ %d" % (name, min_length, max_length) raise exception.InvalidParameterValue(err) if name == u"None": err = "'None' can't be %s" % (name) raise exception.InvalidParameterValue(err) try: str(v) except UnicodeEncodeError: err = "%s should be in ascii characters" % (name) raise exception.InvalidParameterValue(err) elif isinstance(v, int) or isinstance(v, float): err = "%s should not be in numeric form" % (name) raise exception.InvalidParameterValue(err)
def check_statistics(self, statistics): statistic_sample = db.Cassandra.STATISTICS if statistics: if (not (0 < len(statistics) <= 5)): err = "The length of Namespace is 1~5." raise exception.InvalidParameterValue(err) for statistic in statistics: self.check_statistic(statistic) return True
def check_dimensions(self, dimensions): if dimensions and (not (0 <= len(dimensions) <= 10)): err = "The length of Dimensions is 0~10." raise exception.InvalidParameterValue(err) for k, v in dimensions.iteritems(): self._check_ascii(k, "dimension name") self._check_ascii(v, "dimension value") return True
def check_comparison_operator(self, comparison_operator): comparison_operator_sample = ['GreaterThanOrEqualToThreshold', 'GreaterThanThreshold', 'LessThanThreshold', 'LessThanOrEqualToThreshold'] if comparison_operator and (comparison_operator not in comparison_operator_sample): err = "Unsuitable comparison operator value" raise exception.InvalidParameterValue(err) return True
def check_next_token(self, next_token): if not next_token: return True else: next_token = next_token.replace(' ', '') try: uuid.UUID(next_token) except ValueError: err = "badly formed nextToken(%s)" % next_token raise exception.InvalidParameterValue(err) return True
def _validate_evaluation_periods(self, evaluation_periods): if evaluation_periods and (not 0 < int(evaluation_periods) <= 1440): err = "Evaluation Periods should be in range of 1~1440." raise exception.InvalidParameterValue(err)
def _check_dimension_filter(self, dimensions): if dimensions and (not (0 <= len(dimensions) <= 10)): err = "The length of Dimensions is 0~10." raise exception.InvalidParameterValue(err) return True