Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PC-9234: Merge Kind definitions #75

Merged
merged 3 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions manifest/kind.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package manifest

//go:generate ../bin/go-enum --nocase --lower --names --marshal --values

import "strings"

// Kind represents all the object kinds available in the API to perform operations on.
/* ENUM(
SLO = 1
Service
Agent
AlertPolicy
AlertSilence
Alert
Project
AlertMethod
Direct
DataExport
RoleBinding
Annotation
UserGroup
)*/
type Kind int

// ToLower converts the Kind to a lower case string.
func (k Kind) ToLower() string {
return strings.ToLower(k.String())
}

// Applicable returns true if the Kind can be applied or deleted by the user.
// In other words, it informs whether the Kind's lifecycle is managed by the user.
func (k Kind) Applicable() bool {
return k != KindAlert
}

// ApplicableKinds returns all the Kind instances which can be applied or deleted by the user.
func ApplicableKinds() []Kind {
allValues := KindValues()
applicable := make([]Kind, 0, len(allValues)-1)
for _, value := range allValues {
if value.Applicable() {
applicable = append(applicable, value)
}
}
return applicable
}
175 changes: 175 additions & 0 deletions manifest/kind_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions manifest/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type RoleBindingMetadata struct {
// ObjectHeader represents Header which is common for all available Objects
type ObjectHeader struct {
APIVersion string `json:"apiVersion" validate:"required" example:"n9/v1alpha"`
Kind string `json:"kind" validate:"required" example:"kind"`
Kind Kind `json:"kind" validate:"required" example:"kind"`
MetadataHolder
ObjectInternal
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func JSONToGenericObjects(jsonPayload []byte) ([]ObjectGeneric, error) {
// for not empty field kind returns always that is not supported for this apiVersion
// so have to be validated before
func UnsupportedKindErr(o ObjectGeneric) error {
if strings.TrimSpace(o.Kind) == "" {
if strings.TrimSpace(o.Kind.String()) == "" {
return EnhanceError(o, errors.New("missing or empty field kind for an Object"))
}
return EnhanceError(o, fmt.Errorf("invalid Object kind: %s for apiVersion: %s", o.Kind, o.APIVersion))
Expand Down
2 changes: 1 addition & 1 deletion manifest/v1alpha/alertsilence.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type AlertSilence struct {
manifest.ObjectInternal
APIVersion string `json:"apiVersion" validate:"required" example:"n9/v1alpha"`
Kind string `json:"kind" validate:"required" example:"kind"`
Kind manifest.Kind `json:"kind" validate:"required" example:"kind"`
Metadata manifest.AlertSilenceMetadata `json:"metadata"`
Spec AlertSilenceSpec `json:"spec"`
Status AlertSilenceStatus `json:"status,omitempty"`
Expand Down
9 changes: 6 additions & 3 deletions manifest/v1alpha/datasources.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/pkg/errors"
"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/nobl9/nobl9-go/manifest"
)

type DataSourceType int
Expand Down Expand Up @@ -335,13 +337,14 @@ var directDataRetrievalMaxDuration = map[string]HistoricalRetrievalDuration{
AppDynamics.String(): {Value: ptr(30), Unit: HRDDay},
}

func GetDataRetrievalMaxDuration(kind Kind, typeName string) (HistoricalRetrievalDuration, error) {
func GetDataRetrievalMaxDuration(kind manifest.Kind, typeName string) (HistoricalRetrievalDuration, error) {
//nolint: exhaustive
switch kind {
case KindAgent:
case manifest.KindAgent:
if hrd, ok := agentDataRetrievalMaxDuration[typeName]; ok {
return hrd, nil
}
case KindDirect:
case manifest.KindDirect:
if hrd, ok := directDataRetrievalMaxDuration[typeName]; ok {
return hrd, nil
}
Expand Down
Loading
Loading