Skip to content

Commit

Permalink
feat: add subscription receiver API
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdh committed May 31, 2024
1 parent 94c684d commit 9ffa7c2
Show file tree
Hide file tree
Showing 29 changed files with 6,353 additions and 3,198 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ NAME="github.com/goto/siren"
LAST_COMMIT := $(shell git rev-parse --short HEAD)
LAST_TAG := "$(shell git rev-list --tags --max-count=1)"
APP_VERSION := "$(shell git describe --tags ${LAST_TAG})-next"
PROTON_COMMIT := "ebb3a512fc98f28b3b2933da022d04c093673106"
PROTON_COMMIT := "d7e0d46f46320edb5501ffa51d97ca4f982c9997"

.PHONY: all build test clean dist vet proto install

Expand Down
19 changes: 10 additions & 9 deletions cli/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,15 @@ func InitDeps(
)

return &api.Deps{
TemplateService: templateService,
RuleService: ruleService,
AlertService: alertService,
ProviderService: providerService,
NamespaceService: namespaceService,
ReceiverService: receiverService,
SubscriptionService: subscriptionService,
NotificationService: notificationService,
SilenceService: silenceService,
TemplateService: templateService,
RuleService: ruleService,
AlertService: alertService,
ProviderService: providerService,
NamespaceService: namespaceService,
ReceiverService: receiverService,
SubscriptionService: subscriptionService,
SubscriptionReceiverService: subscriptionReceiverService,
NotificationService: notificationService,
SilenceService: silenceService,
}, pgClient, notifierRegistry, providersPluginManager, nil
}
2 changes: 1 addition & 1 deletion cli/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func jobCmd(cmdxConfig *cmdx.Config) *cobra.Command {
return cmd
}

func jobRunCommand(cmdxConfig *cmdx.Config) *cobra.Command {
func jobRunCommand(_ *cmdx.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Short: "Trigger a job",
Expand Down
1 change: 1 addition & 0 deletions cli/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func subscriptionsCmd(cmdxConfig *cmdx.Config) *cobra.Command {
createSubscriptionCmd(cmdxConfig),
updateSubscriptionCmd(cmdxConfig),
deleteSubscriptionCmd(cmdxConfig),
subscriptionsReceiversCmd(cmdxConfig),
)

return cmd
Expand Down
214 changes: 214 additions & 0 deletions cli/subscriptionreceiver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package cli

import (
"github.com/MakeNowJust/heredoc"
"github.com/goto/salt/cmdx"
"github.com/goto/salt/printer"
"github.com/goto/siren/core/subscriptionreceiver"
sirenv1beta1 "github.com/goto/siren/proto/gotocompany/siren/v1beta1"
"github.com/spf13/cobra"
)

func subscriptionsReceiversCmd(cmdxConfig *cmdx.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "receiver",
Aliases: []string{"subscription_receiver"},
Short: "Manage subscriptions receiver",
Long: heredoc.Doc(`
Work with subscriptions receiver.
Add receiver to a subscription.
`),
Annotations: map[string]string{
"group": "core",
"client": "true",
},
}

cmd.AddCommand(
addSubscriptionReceiverCmd(cmdxConfig),
editSubscriptionReceiverCmd(cmdxConfig),
removeSubscriptionReceiverCmd(cmdxConfig),
)

return cmd
}

func addSubscriptionReceiverCmd(cmdxConfig *cmdx.Config) *cobra.Command {
var filePath string
cmd := &cobra.Command{
Use: "add",
Short: "Add a receiver to a subscription",
Long: heredoc.Doc(`
Add a receiver to a subscription.
`),
Annotations: map[string]string{
"group": "core",
},
RunE: func(cmd *cobra.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()

ctx := cmd.Context()

c, err := loadClientConfig(cmd, cmdxConfig)
if err != nil {
return err
}

var srRelation subscriptionreceiver.Relation
if err := parseFile(filePath, &srRelation); err != nil {
return err
}

client, cancel, err := createClient(ctx, c.Host)
if err != nil {
return err
}
defer cancel()

res, err := client.AddSubscriptionReceiver(ctx, &sirenv1beta1.AddSubscriptionReceiverRequest{
SubscriptionId: srRelation.SubscriptionID,
ReceiverId: srRelation.ReceiverID,
Labels: srRelation.Labels,
})

if err != nil {
return err
}

spinner.Stop()
printer.Successf("Receiver id %d is added to subscription id %d", res.GetReceiverId(), res.GetSubscriptionId())
printer.Space()
printer.SuccessIcon()

return nil
},
}

cmd.Flags().StringVarP(&filePath, "file", "f", "", "path to the subscription config")
cmd.MarkFlagRequired("file")

return cmd
}

func editSubscriptionReceiverCmd(cmdxConfig *cmdx.Config) *cobra.Command {
var (
subscriptionID uint64
receiverID uint64
filePath string
)

cmd := &cobra.Command{
Use: "edit",
Short: "Edit a subscription receiver",
Long: heredoc.Doc(`
Edit an existing subscription receiver relation detail.
`),
Annotations: map[string]string{
"group": "core",
},
RunE: func(cmd *cobra.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()

ctx := cmd.Context()

c, err := loadClientConfig(cmd, cmdxConfig)
if err != nil {
return err
}

var srRelation subscriptionreceiver.Relation
if err := parseFile(filePath, &srRelation); err != nil {
return err
}

client, cancel, err := createClient(ctx, c.Host)
if err != nil {
return err
}
defer cancel()

res, err := client.UpdateSubscriptionReceiver(ctx, &sirenv1beta1.UpdateSubscriptionReceiverRequest{
SubscriptionId: srRelation.SubscriptionID,
ReceiverId: srRelation.ReceiverID,
Labels: srRelation.Labels,
})
if err != nil {
return err
}

spinner.Stop()
printer.Successf("Successfully updated receiver id %d detail of asubscription with id %d", res.GetReceiverId(), res.GetSubscriptionId())
printer.Space()
printer.SuccessIcon()

return nil
},
}

cmd.Flags().Uint64VarP(&subscriptionID, "subscription_id", "s", 0, "subscription id")
cmd.MarkFlagRequired("subscription_id")
cmd.Flags().Uint64VarP(&receiverID, "receiver_id", "r", 0, "receiver id")
cmd.MarkFlagRequired("receiver_id")
cmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the subscription config")
cmd.MarkFlagRequired("file")

return cmd
}

func removeSubscriptionReceiverCmd(cmdxConfig *cmdx.Config) *cobra.Command {
var (
subscriptionID uint64
receiverID uint64
)
cmd := &cobra.Command{
Use: "remove",
Short: "Remove receiver of a subscription",
Example: heredoc.Doc(`
$ siren subscription receiver remove --subscription_id 1 --receiver_id 2
`),
Annotations: map[string]string{
"group": "core",
},
RunE: func(cmd *cobra.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()

ctx := cmd.Context()

c, err := loadClientConfig(cmd, cmdxConfig)
if err != nil {
return err
}

client, cancel, err := createClient(ctx, c.Host)
if err != nil {
return err
}
defer cancel()

_, err = client.DeleteSubscriptionReceiver(ctx, &sirenv1beta1.DeleteSubscriptionReceiverRequest{
SubscriptionId: subscriptionID,
ReceiverId: receiverID,
})
if err != nil {
return err
}

spinner.Stop()
printer.Successf("Successfully removed receiver id %d from subscription id %d", receiverID, subscriptionID)
printer.Space()
printer.SuccessIcon()

return nil
},
}

cmd.Flags().Uint64VarP(&subscriptionID, "subscription_id", "s", 0, "subscription id")
cmd.MarkFlagRequired("subscription_id")
cmd.Flags().Uint64VarP(&receiverID, "receiver_id", "r", 0, "receiver id")
cmd.MarkFlagRequired("receiver_id")
return cmd
}
Loading

0 comments on commit 9ffa7c2

Please sign in to comment.