Skip to content

Commit

Permalink
PC-11843 budget adjustment kind
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaceg committed Feb 6, 2024
1 parent efbd0d9 commit 7636972
Show file tree
Hide file tree
Showing 16 changed files with 470 additions and 40 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.32.0
github.com/stretchr/testify v1.8.4
github.com/teambition/rrule-go v1.8.2
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a
golang.org/x/text v0.14.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/teambition/rrule-go v1.8.2 h1:lIjpjvWTj9fFUZCmuoVDrKVOtdiyzbzc93qTmRVe/J8=
github.com/teambition/rrule-go v1.8.2/go.mod h1:Ieq5AbrKGciP1V//Wq8ktsTXwSwJHDD5mD/wLBGl3p4=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand Down
1 change: 1 addition & 0 deletions manifest/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ DataExport
RoleBinding
Annotation
UserGroup
BudgetAdjustment
)*/
type Kind int

Expand Down
87 changes: 47 additions & 40 deletions manifest/kind_enum.go

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

52 changes: 52 additions & 0 deletions manifest/v1alpha/budgetAdjustment/budget_adjustment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package budgetadjustment

import (
"time"

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

//go:generate go run ../../../scripts/generate-object-impl.go BudgetAdjustment

func New(metadata Metadata, spec Spec) BudgetAdjustment {
return BudgetAdjustment{
APIVersion: manifest.VersionV1alpha,
Kind: manifest.KindBudgetAdjustment,
Metadata: metadata,
Spec: spec,
}
}

// BudgetAdjustment represents a object for manipulating budget adjustments.
type BudgetAdjustment struct {
APIVersion manifest.Version `json:"apiVersion"`
Kind manifest.Kind `json:"kind"`
Metadata Metadata `json:"metadata"`
Spec Spec `json:"spec"`

Organization string `json:"organization,omitempty"`
ManifestSource string `json:"manifestSrc,omitempty"`
}

type Metadata struct {
Name string `json:"name"`
DisplayName string `json:"displayName,omitempty"`
}

// Spec represents content of BudgetAdjustment's Spec.
type Spec struct {
Description string `json:"description,omitempty"`
FirstEventStart time.Time `json:"firstEventStart"`
Duration time.Duration `json:"duration"`
Rrule string `json:"rrule,omitempty"`
Filters Filters `json:"filters"`
}

type Filters struct {
Slos []Slo `json:"slos"`
}

type Slo struct {
Name string `json:"name"`
Project string `json:"project"`
}
49 changes: 49 additions & 0 deletions manifest/v1alpha/budgetAdjustment/budget_adjustment_object.go

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Validation for BudgetAdjustment 'MY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENST' has failed for the following fields:
- 'metadata.name' with value 'MY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUSTMENSTMY BUDGET ADJUST...':
- length must be between 1 and 63
- string does not match regular expression: '^[a-z0-9]([-a-z0-9]*[a-z0-9])?$' (e.g. 'my-name', '123-abc'); a DNS-1123 compliant name must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character
- 'metadata.displayName' with value 'my-budgetadjustmentmy-budgetadjustmentmy-budgetadjustmentmy-budgetadjustmentmy-budgetadjustmentmy-bu...':
- length must be between 0 and 63
- 'spec.firstEventStart':
- property is required but was empty
- 'spec.duration':
- property is required but was empty
- 'spec.filters.slos':
- length must be greater than or equal to 1
Manifest source: /home/me/budgetadjustment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Validation for BudgetAdjustment 'my-budget-adjustement' has failed for the following fields:
- 'spec.duration' with value '1m1s':
- duration must be in whole minutes without seconds
Manifest source: /home/me/budgetadjustment.yaml
4 changes: 4 additions & 0 deletions manifest/v1alpha/budgetAdjustment/test_data/invalid-rrule.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Validation for BudgetAdjustment 'my-budget-adjustement' has failed for the following fields:
- 'spec.rrule' with value 'FREQ=WEKLY;INTERVAL=2':
- invalid rrule: undefined frequency: WEKLY
Manifest source: /home/me/budgetadjustment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Validation for BudgetAdjustment 'my-budget-adjustement' has failed for the following fields:
- 'spec.filters.slos':
- length must be greater than or equal to 1
Manifest source: /home/me/budgetadjustment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Validation for BudgetAdjustment 'my-budget-adjustement' has failed for the following fields:
- 'spec.filters.slos[0].name':
- property is required but was empty
Manifest source: /home/me/budgetadjustment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Validation for BudgetAdjustment 'my-budget-adjustement' has failed for the following fields:
- 'spec.filters.slos[0].project':
- property is required but was empty
Manifest source: /home/me/budgetadjustment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Validation for BudgetAdjustment 'my-budget-adjustement' has failed for the following fields:
- 'spec.duration' with value '1s':
- duration must be in whole minutes without seconds
Manifest source: /home/me/budgetadjustment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Validation for BudgetAdjustment 'my-budget-adjustement' has failed for the following fields:
- 'spec.rrule' with value 'some test':
- invalid rrule: wrong format
Manifest source: /home/me/budgetadjustment.yaml
89 changes: 89 additions & 0 deletions manifest/v1alpha/budgetAdjustment/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package budgetadjustment

import (
"time"

"github.com/pkg/errors"
"github.com/teambition/rrule-go"

validationV1Alpha "github.com/nobl9/nobl9-go/internal/manifest/v1alpha"
"github.com/nobl9/nobl9-go/internal/validation"
"github.com/nobl9/nobl9-go/manifest/v1alpha"
)

var budgetAdjustmentValidation = validation.New[BudgetAdjustment](
validation.For(func(b BudgetAdjustment) Metadata { return b.Metadata }).
Include(metadataValidation),
validation.For(func(b BudgetAdjustment) Spec { return b.Spec }).
WithName("spec").
Include(specValidation),
)

var metadataValidation = validation.New[Metadata](
validationV1Alpha.FieldRuleMetadataName(func(m Metadata) string { return m.Name }),
validationV1Alpha.FieldRuleMetadataDisplayName(func(m Metadata) string { return m.DisplayName }),
)

var specValidation = validation.New[Spec](
validation.For(func(s Spec) string { return s.Description }).
WithName("description").
Rules(validation.StringDescription()),
validation.For(func(s Spec) time.Time { return s.FirstEventStart }).
WithName("firstEventStart").
Required(),
validation.For(func(s Spec) time.Duration { return s.Duration }).
WithName("duration").
Required().
Rules(durationValidationRule()),
validation.For(func(s Spec) string { return s.Rrule }).
WithName("rrule").
Rules(rruleValidationRule()),
validation.For(func(s Spec) Filters { return s.Filters }).
WithName("filters").
Include(filtersValidationRule),
)

var filtersValidationRule = validation.New[Filters](
validation.ForEach(func(f Filters) []Slo { return f.Slos }).
WithName("slos").
Rules(validation.SliceMinLength[[]Slo](1)).
IncludeForEach(sloValidationRule),
)

var sloValidationRule = validation.New[Slo](
validation.For(func(s Slo) string { return s.Project }).
WithName("project").
Required(),
validation.For(func(s Slo) string { return s.Name }).
WithName("name").
Required(),
)

func durationValidationRule() validation.SingleRule[time.Duration] {
return validation.NewSingleRule(func(v time.Duration) error {
if v.Truncate(time.Minute) != v {
return errors.New("duration must be in whole minutes without seconds")
}

return nil
})
}

func rruleValidationRule() validation.SingleRule[string] {
return validation.NewSingleRule(func(v string) error {
if len(v) == 0 {
return nil
}

_, err := rrule.StrToRRule(v)
if err != nil {
return errors.Wrap(err, "invalid rrule")
}

return nil
})
}

func validate(b BudgetAdjustment) *v1alpha.ObjectError {
return v1alpha.ValidateObject(budgetAdjustmentValidation, b)
}
Loading

0 comments on commit 7636972

Please sign in to comment.