-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [PC-13433]: add get Adjustment Events (#228)
## Motivation This change is a part of new Error Budget Adjustments management feature. We need to have a way to get/update/delete Events related to an Adjustments. ## Summary Add new command to `sloctl` to - get Adjustment Events for adjustment and optional SLO filter - delete Adjustment Events for adjustment - update Adjustment Events for adjustment ## Testing See added examples of usage ## Release Notes New Past Adjustment functionality. Allow users to manage past events. --------- Co-authored-by: dawidwisn <[email protected]> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Mateusz Hawrus <[email protected]>
- Loading branch information
1 parent
2af304a
commit 6e938bd
Showing
28 changed files
with
727 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.8.0 | ||
0.9.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package events | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/nobl9/go-yaml" | ||
"github.com/nobl9/nobl9-go/sdk" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/nobl9/sloctl/internal/budgetadjustments/sdkclient" | ||
) | ||
|
||
type DeleteCmd struct { | ||
client *sdk.Client | ||
filepath string | ||
adjustment string | ||
} | ||
|
||
//go:embed examples/delete_example.sh | ||
var deleteExample string | ||
|
||
func NewDeleteCmd(clientProvider sdkclient.SdkClientProvider) *cobra.Command { | ||
deleteCmd := &DeleteCmd{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "delete", | ||
Short: "Delete existing past events.", | ||
Example: deleteExample, | ||
PersistentPreRun: func(cmd *cobra.Command, args []string) { | ||
deleteCmd.client = clientProvider.GetClient() | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { return deleteCmd.run(cmd) }, | ||
} | ||
|
||
mustRegisterFileFlag(cmd, &deleteCmd.filepath) | ||
mustRegisterAdjustmentFlag(cmd, &deleteCmd.adjustment) | ||
|
||
return cmd | ||
} | ||
|
||
func (g *DeleteCmd) run(cmd *cobra.Command) error { | ||
data, err := readFile(g.filepath) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to read input data") | ||
} | ||
body, err := yaml.YAMLToJSON(data) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to convert input data to JSON") | ||
} | ||
|
||
if _, err = DoRequest( | ||
g.client, | ||
cmd.Context(), | ||
http.MethodPost, | ||
fmt.Sprintf("%s/%s/events/delete", BudgetAdjustmentAPI, g.adjustment), | ||
nil, | ||
bytes.NewReader(body), | ||
); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package events | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/nobl9/sloctl/internal/budgetadjustments/sdkclient" | ||
) | ||
|
||
func NewRootCmd(clientProvider sdkclient.SdkClientProvider) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "events", | ||
Short: "Budget adjustments events management", | ||
Long: "The 'events' command allows you to manage events related to SLO error budget adjustments", | ||
} | ||
cmd.PersistentFlags().BoolP("help", "h", false, fmt.Sprintf("Help for %s.", cmd.Name())) | ||
cmd.AddCommand(NewGetCmd(clientProvider)) | ||
cmd.AddCommand(NewDeleteCmd(clientProvider)) | ||
cmd.AddCommand(NewUpdateCmd(clientProvider)) | ||
|
||
return cmd | ||
} |
17 changes: 17 additions & 0 deletions
17
internal/budgetadjustments/events/examples/delete_example.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Delete Adjustment Events using a file: | ||
cat <<EOF > ./events.yaml | ||
- eventStart: 2024-10-24T04:07:04Z | ||
eventEnd: 2024-10-24T05:27:04Z | ||
slos: | ||
- project: test-project | ||
name: sample-slo-1 | ||
- eventStart: 2024-10-25T04:07:04Z | ||
eventEnd: 2024-10-25T05:27:04Z | ||
slos: | ||
- project: test-project | ||
name: sample-slo-2 | ||
EOF | ||
sloctl budgetadjustments events delete --adjustment-name=sample-adjustment-name -f ./events.yaml | ||
|
||
# Delete Adjustment Events using stdin: | ||
sloctl budgetadjustments events delete --adjustment-name=sample-adjustment-name -f - <./events.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Get Adjustment Events for 'sample-adjustment-name' from 2024-09-23T00:45:00 UTC to 2024-09-23T20:46:00 UTC. | ||
sloctl budgetadjustments events get --adjustment-name=sample-adjustment-name --from=2024-09-23T00:45:00Z --to=2024-09-23T20:46:00Z | ||
|
||
|
||
# Get Adjustment Events for 'sample-adjustment-name' from 2024-09-23T00:45:00 UTC to 2024-09-23T20:46:00 UTC | ||
# only for one slo with sloName and project filters. | ||
sloctl budgetadjustments events get \ | ||
--adjustment-name=sample-adjustment-name \ | ||
--from=2024-09-23T00:45:00Z \ | ||
--to=2024-09-23T20:46:00Z \ | ||
--slo-project=sample-project-name \ | ||
--slo-name=sample-slo-name |
23 changes: 23 additions & 0 deletions
23
internal/budgetadjustments/events/examples/update_example.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Update Adjustment Events using a file: | ||
cat <<EOF > ./events.yaml | ||
- eventStart: 2024-10-24T04:07:04Z | ||
eventEnd: 2024-10-24T05:27:04Z | ||
slos: | ||
- project: test-project | ||
name: sample-slo-1 | ||
update: | ||
eventStart: 2024-10-24T03:07:04Z | ||
eventEnd: 2024-10-24T04:27:04Z | ||
- eventStart: 2024-10-25T04:07:04Z | ||
eventEnd: 2024-10-25T05:27:04Z | ||
slos: | ||
- project: test-project | ||
name: sample-slo-2 | ||
update: | ||
eventStart: 2024-10-25T03:07:04Z | ||
eventEnd: 2024-10-25T04:27:04Z | ||
EOF | ||
sloctl budgetadjustments events update --adjustment-name=sample-adjustment-name -f ./events.yaml | ||
|
||
# Update Adjustment Events using stdin: | ||
sloctl budgetadjustments events update --adjustment-name=sample-adjustment-name -f - <./events.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package events | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/nobl9/sloctl/internal/flags" | ||
) | ||
|
||
const ( | ||
FlagFile = "file" | ||
FlagAdjustment = "adjustment-name" | ||
FlagFrom = "from" | ||
FlagTo = "to" | ||
FlagSloProject = "slo-project" | ||
FlagSloName = "slo-name" | ||
) | ||
|
||
func mustRegisterFileFlag(cmd *cobra.Command, storeIn *string) { | ||
cmd.Flags().StringVarP(storeIn, FlagFile, "f", "", | ||
"File path to events definitions in YAML.") | ||
if err := cmd.MarkFlagRequired(FlagFile); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func mustRegisterAdjustmentFlag(cmd *cobra.Command, storeIn *string) { | ||
cmd.Flags().StringVar(storeIn, FlagAdjustment, "", "Name of the Adjustment.") | ||
if err := cmd.MarkFlagRequired(FlagAdjustment); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func registerProjectFlag(cmd *cobra.Command, storeIn *string) { | ||
cmd.Flags().StringVarP(storeIn, FlagSloProject, "", "", | ||
"Name of the project. Required when sloName is defined.") | ||
} | ||
|
||
func registerSloNameFlag(cmd *cobra.Command, storeIn *string) { | ||
cmd.Flags().StringVarP(storeIn, FlagSloName, "", "", | ||
"Name of the SLO. Required when sloName is defined.") | ||
} | ||
|
||
func mustRegisterFromFlag( | ||
cmd *cobra.Command, | ||
storeIn *flags.TimeValue, | ||
) { | ||
cmd.Flags(). | ||
Var(storeIn, FlagFrom, "Specifies the start date and time for the data range (in UTC).") | ||
if err := cmd.MarkFlagRequired(FlagFrom); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func mustRegisterToFlag( | ||
cmd *cobra.Command, | ||
storeIn *flags.TimeValue, | ||
) { | ||
cmd.Flags().Var(storeIn, FlagTo, "Specifies the end date and time for the data range (in UTC).") | ||
if err := cmd.MarkFlagRequired(FlagTo); err != nil { | ||
panic(err) | ||
} | ||
} |
Oops, something went wrong.