icinga2-matrix-notification/internal/icinga2/parameters.go

104 lines
2.5 KiB
Go

// 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 <https://www.gnu.org/licenses/>.
package icinga2
import (
"errors"
"fmt"
"net/url"
)
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 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
IcingaWeb2URL URLValue
LongDateTime string
Hostname string
HostDisplayName string
HostAddress string
HostAddress6 string
}
func (p BaseParameters) HasRequired() bool {
return p.LongDateTime != "" && p.Hostname != "" && p.HostDisplayName != ""
}
type HostParameters struct {
HostOutput string
HostState string
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 string
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
}