// Copyright Jan Dittberner // SPDX-License-Identifier: GPL-3.0-or-later // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . package icinga2 import ( "errors" "fmt" "net/url" "strings" ) type URLValue struct { URL *url.URL } func (u *URLValue) Set(value string) error { var err error u.URL, err = url.Parse(value) if err != nil { return fmt.Errorf("could not parse URL: %w", err) } return nil } func (u *URLValue) String() string { if u.URL != nil { return u.URL.String() } return "" } type HostStateValue string func (v *HostStateValue) Set(value string) error { if strings.EqualFold(value, "Up") || strings.EqualFold(value, "Down") { *v = HostStateValue(strings.ToUpper(value)) return nil } return fmt.Errorf("invalid host state value: %s", value) } func (v *HostStateValue) String() string { return string(*v) } type ServiceStateValue string func (v *ServiceStateValue) Set(value string) error { allowedValues := []string{"OK", "Warning", "Critical", "Unknown"} for _, allowed := range allowedValues { if strings.EqualFold(value, allowed) { *v = ServiceStateValue(strings.ToUpper(value)) return nil } } return fmt.Errorf("invalid service state value: %s", value) } func (v *ServiceStateValue) String() string { return string(*v) } type NotificationTypeValue string func (v *NotificationTypeValue) Set(value string) error { allowedValues := []string{ "DowntimeStart", "DowntimeEnd", "DownTimeRemoved", "Custom", "Acknowledgement", "Problem", "Recovery", "FlappingStart", "FlappingEnd", } for _, allowed := range allowedValues { if strings.EqualFold(value, allowed) { *v = NotificationTypeValue(strings.ToUpper(value)) return nil } } return fmt.Errorf("invalid notification type value: %s", value) } func (v *NotificationTypeValue) String() string { return string(*v) } type MatrixParameters struct { MatrixRoom string MatrixServer URLValue MatrixToken string } func (m *MatrixParameters) HasRequired() bool { return !(m.MatrixRoom == "" || m.MatrixServer.URL == nil || m.MatrixToken == "") } type BaseParameters struct { NotificationAuthorName string NotificationComment string NotificationType NotificationTypeValue IcingaWeb2URL URLValue LongDateTime string Hostname string HostDisplayName string HostAddress string HostAddress6 string } func (p BaseParameters) HasRequired() bool { return !(p.LongDateTime == "" || p.Hostname == "" || p.HostDisplayName == "" || p.NotificationType == "") } type HostParameters struct { HostOutput string HostState HostStateValue BaseParameters MatrixParameters } func (p *HostParameters) ValidateRequired() error { if !p.BaseParameters.HasRequired() || !p.MatrixParameters.HasRequired() || p.HostOutput == "" || p.HostState == "" { return errors.New("missing required fields") } return nil } type ServiceParameters struct { ServiceName string ServiceDisplayName string ServiceOutput string ServiceState ServiceStateValue BaseParameters MatrixParameters } func (p *ServiceParameters) ValidateRequired() error { if !p.BaseParameters.HasRequired() || !p.MatrixParameters.HasRequired() || p.ServiceName == "" || p.ServiceDisplayName == "" || p.ServiceState == "" || p.ServiceOutput == "" { return errors.New("missing required fields") } return nil }