Пример #1
0
func silenceSetTimeRange(sil *pb.Silence, now, start, end *timestamp.Timestamp) (*pb.Silence, error) {
	if protoBefore(end, start) {
		return nil, fmt.Errorf("end time must not be before start time")
	}
	// Validate modification based on current silence state.
	switch getState(sil, now) {
	case StateActive:
		if *start != *sil.StartsAt {
			return nil, fmt.Errorf("start time of active silence cannot be modified")
		}
		if protoBefore(end, now) {
			return nil, fmt.Errorf("end time cannot be set into the past")
		}
	case StatePending:
		if protoBefore(start, now) {
			return nil, fmt.Errorf("start time cannot be set into the past")
		}
	case StateExpired:
		return nil, fmt.Errorf("expired silence must not be modified")
	default:
		panic("unknown silence state")
	}

	sil = cloneSilence(sil)
	sil.StartsAt = start
	sil.EndsAt = end
	sil.UpdatedAt = now

	return sil, nil
}
Пример #2
0
// Create adds a new silence and returns its ID.
func (s *Silences) Create(sil *pb.Silence) (id string, err error) {
	if sil.Id != "" {
		return "", fmt.Errorf("unexpected ID in new silence")
	}
	sil.Id = uuid.NewV4().String()

	now, err := s.nowProto()
	if err != nil {
		return "", err
	}
	if sil.StartsAt == nil {
		sil.StartsAt = now
	} else if protoBefore(sil.StartsAt, now) {
		return "", fmt.Errorf("new silence must not start in the past")
	}
	sil.UpdatedAt = now

	if err := validateSilence(sil); err != nil {
		return "", fmt.Errorf("invalid silence: %s", err)
	}

	s.mtx.Lock()
	defer s.mtx.Unlock()

	if err := s.setSilence(sil); err != nil {
		return "", err
	}
	return sil.Id, nil
}