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

refactor: receiver behaviour #110

Merged
merged 7 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 10 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ NAME="github.com/odpf/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 := "ef83b9e9248e064a1c366da4fe07b3068266fe59"
PROTON_COMMIT := "4acc1ae4519a6e993f7f3d44ee14f6d4b1ff41f6"

.PHONY: all build test clean dist vet proto install

Expand Down Expand Up @@ -37,16 +37,15 @@ clean: ## Clean the build artifacts
install: ## install required dependencies
@echo "> installing dependencies"
go mod tidy
go install github.com/vektra/mockery/v2@v2.12.2
go get -d google.golang.org/protobuf/cmd/protoc-gen-go@v1.27.1
go get github.com/golang/protobuf/proto@v1.5.2
go get -d github.com/golang/protobuf/protoc-gen-go@v1.5.2
go get google.golang.org/grpc@v1.40.0
go get -d google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1.0
go get -d github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@v2.5.0
go get -d github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@v2.5.0
go get -d github.com/bufbuild/buf/cmd/buf@v0.54.1
go get github.com/envoyproxy/protoc-gen-validate
go get -d github.com/vektra/mockery/v2@v2.13.1
go get -d google.golang.org/protobuf/cmd/protoc-gen-go@v1.28.0
go get google.golang.org/protobuf/proto@v1.28.0
go get google.golang.org/grpc@v1.47.0
go get -d google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2.0
go get -d github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@v2.10.3
go get -d github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@v2.10.3
go get -d github.com/bufbuild/buf/cmd/buf@v1.5.0
go get github.com/envoyproxy/protoc-gen-validate@v0.6.7

help: ## Display this help message
@cat $(MAKEFILE_LIST) | grep -e "^[a-zA-Z_\-]*: *.*## *" | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
7 changes: 6 additions & 1 deletion buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ plugins:
- paths=source_relative
- lang=go
- name: openapiv2
out: third_party/OpenAPI
out: internal/server
opt:
- allow_repeated_fields_in_body=true
- output_format=yaml
- allow_merge=true
- merge_file_name=siren
- openapi_naming_strategy=simple
- json_names_for_fields=false
2 changes: 1 addition & 1 deletion cli/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

"github.com/MakeNowJust/heredoc"
"github.com/odpf/salt/printer"
sirenv1beta1 "github.com/odpf/siren/internal/server/proto/odpf/siren/v1beta1"
"github.com/spf13/cobra"
sirenv1beta1 "go.buf.build/odpf/gw/odpf/proton/odpf/siren/v1beta1"
)

func alertsCmd(c *configuration) *cobra.Command {
Expand Down
5 changes: 3 additions & 2 deletions cli/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"time"

sirenv1beta1 "go.buf.build/odpf/gw/odpf/proton/odpf/siren/v1beta1"
sirenv1beta1 "github.com/odpf/siren/internal/server/proto/odpf/siren/v1beta1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

func createConnection(ctx context.Context, host string) (*grpc.ClientConn, error) {
opts := []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
}

Expand Down
12 changes: 11 additions & 1 deletion cli/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,17 @@ func migrateCmd() *cobra.Command {
)

slackClient := slack.NewClient(slack.ClientWithHTTPClient(httpClient))
receiverService := receiver.NewService(repositories.ReceiverRepository, slackClient, encryptor)
slackReceiverService := receiver.NewSlackService(slackClient, encryptor)
httpReceiverService := receiver.NewHTTPService()
pagerDutyReceiverService := receiver.NewPagerDutyService()
receiverService := receiver.NewService(
repositories.ReceiverRepository,
map[string]receiver.TypeService{
receiver.TypeSlack: slackReceiverService,
receiver.TypeHTTP: httpReceiverService,
receiver.TypePagerDuty: pagerDutyReceiverService,
},
)

subscriptionService := subscription.NewService(repositories.SubscriptionRepository, providerService, namespaceService, receiverService, cortexClient)

Expand Down
41 changes: 25 additions & 16 deletions cli/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package cli

import (
"context"
"errors"
"fmt"
"os"
"strconv"

"github.com/MakeNowJust/heredoc"
"github.com/odpf/salt/printer"
"github.com/odpf/siren/core/namespace"
sirenv1beta1 "github.com/odpf/siren/internal/server/proto/odpf/siren/v1beta1"

"github.com/spf13/cobra"
sirenv1beta1 "go.buf.build/odpf/gw/odpf/proton/odpf/siren/v1beta1"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/structpb"
)

Expand Down Expand Up @@ -56,12 +57,16 @@ func listNamespacesCmd(c *configuration) *cobra.Command {
}
defer cancel()

res, err := client.ListNamespaces(ctx, &emptypb.Empty{})
res, err := client.ListNamespaces(ctx, &sirenv1beta1.ListNamespacesRequest{})
if err != nil {
return err
}

namespaces := res.Namespaces
if res.GetData() == nil {
return errors.New("no response from server")
}

namespaces := res.GetData()
report := [][]string{}

fmt.Printf(" \nShowing %d of %d namespaces\n \n", len(namespaces), len(namespaces))
Expand Down Expand Up @@ -113,7 +118,7 @@ func createNamespaceCmd(c *configuration) *cobra.Command {

res, err := client.CreateNamespace(ctx, &sirenv1beta1.CreateNamespaceRequest{
Provider: namespaceConfig.Provider,
Urn: namespaceConfig.Urn,
Urn: namespaceConfig.URN,
Name: namespaceConfig.Name,
Credentials: grpcCredentials,
Labels: namespaceConfig.Labels,
Expand Down Expand Up @@ -172,17 +177,21 @@ func getNamespaceCmd(c *configuration) *cobra.Command {
return err
}

namespace := &namespace.Namespace{
Id: res.GetId(),
Urn: res.GetUrn(),
Name: res.GetName(),
Credentials: res.GetCredentials().AsMap(),
Labels: res.GetLabels(),
CreatedAt: res.CreatedAt.AsTime(),
UpdatedAt: res.UpdatedAt.AsTime(),
if res.GetData() == nil {
return errors.New("no response from server")
}

nspace := &namespace.Namespace{
ID: res.GetData().GetId(),
URN: res.GetData().GetUrn(),
Name: res.GetData().GetName(),
Credentials: res.GetData().GetCredentials().AsMap(),
Labels: res.GetData().GetLabels(),
CreatedAt: res.GetData().GetCreatedAt().AsTime(),
UpdatedAt: res.GetData().GetUpdatedAt().AsTime(),
}

if err := printer.Text(namespace, format); err != nil {
if err := printer.Text(nspace, format); err != nil {
return fmt.Errorf("failed to format namespace: %v", err)
}
return nil
Expand Down Expand Up @@ -224,7 +233,7 @@ func updateNamespaceCmd(c *configuration) *cobra.Command {
}
defer cancel()

_, err = client.UpdateNamespace(ctx, &sirenv1beta1.UpdateNamespaceRequest{
res, err := client.UpdateNamespace(ctx, &sirenv1beta1.UpdateNamespaceRequest{
Id: id,
Provider: namespaceConfig.Provider,
Name: namespaceConfig.Name,
Expand All @@ -235,7 +244,7 @@ func updateNamespaceCmd(c *configuration) *cobra.Command {
return err
}

fmt.Println("Successfully updated namespace")
fmt.Printf("Successfully updated namespace with id %q", res.GetId())

return nil
},
Expand Down
8 changes: 4 additions & 4 deletions cli/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/MakeNowJust/heredoc"
"github.com/odpf/salt/printer"
"github.com/odpf/siren/core/provider"
sirenv1beta1 "github.com/odpf/siren/internal/server/proto/odpf/siren/v1beta1"
"github.com/spf13/cobra"
sirenv1beta1 "go.buf.build/odpf/gw/odpf/proton/odpf/siren/v1beta1"
"google.golang.org/protobuf/types/known/structpb"
)

Expand Down Expand Up @@ -113,7 +113,7 @@ func createProviderCmd(c *configuration) *cobra.Command {

res, err := client.CreateProvider(ctx, &sirenv1beta1.CreateProviderRequest{
Host: providerConfig.Host,
Urn: providerConfig.Urn,
Urn: providerConfig.URN,
Name: providerConfig.Name,
Type: providerConfig.Type,
Credentials: grpcCredentials,
Expand Down Expand Up @@ -174,9 +174,9 @@ func getProviderCmd(c *configuration) *cobra.Command {
}

provider := &provider.Provider{
Id: res.GetId(),
ID: res.GetId(),
Host: res.GetHost(),
Urn: res.GetUrn(),
URN: res.GetUrn(),
Name: res.GetName(),
Type: res.GetType(),
Credentials: res.GetCredentials().AsMap(),
Expand Down
54 changes: 33 additions & 21 deletions cli/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package cli

import (
"context"
"errors"
"fmt"
"os"
"strconv"

"github.com/MakeNowJust/heredoc"
"github.com/odpf/salt/printer"
"github.com/odpf/siren/core/receiver"
sirenv1beta1 "github.com/odpf/siren/internal/server/proto/odpf/siren/v1beta1"
"github.com/spf13/cobra"
sirenv1beta1 "go.buf.build/odpf/gw/odpf/proton/odpf/siren/v1beta1"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/structpb"
)
Expand All @@ -35,7 +36,7 @@ func receiversCmd(c *configuration) *cobra.Command {
cmd.AddCommand(getReceiverCmd(c))
cmd.AddCommand(updateReceiverCmd(c))
cmd.AddCommand(deleteReceiverCmd(c))
cmd.AddCommand(sendReceiverNotificationCmd(c))
cmd.AddCommand(notifyReceiverCmd(c))
return cmd
}

Expand All @@ -62,7 +63,11 @@ func listReceiversCmd(c *configuration) *cobra.Command {
return err
}

receivers := res.Receivers
if res.GetData() == nil {
return errors.New("no response from server")
}

receivers := res.GetData()
report := [][]string{}

fmt.Printf(" \nShowing %d of %d receivers\n \n", len(receivers), len(receivers))
Expand Down Expand Up @@ -172,15 +177,19 @@ func getReceiverCmd(c *configuration) *cobra.Command {
return err
}

if res.GetData() == nil {
return errors.New("no response from server")
}

receiver := &receiver.Receiver{
Id: res.GetId(),
Name: res.GetName(),
Type: res.GetType(),
Configurations: res.GetConfigurations().AsMap(),
Labels: res.GetLabels(),
Data: res.GetData().AsMap(),
CreatedAt: res.CreatedAt.AsTime(),
UpdatedAt: res.UpdatedAt.AsTime(),
ID: res.GetData().GetId(),
Name: res.GetData().GetName(),
Type: res.GetData().GetType(),
Configurations: res.GetData().GetConfigurations().AsMap(),
Labels: res.GetData().GetLabels(),
Data: res.GetData().GetData().AsMap(),
CreatedAt: res.GetData().GetCreatedAt().AsTime(),
UpdatedAt: res.GetData().GetUpdatedAt().AsTime(),
}

if err := printer.Text(receiver, format); err != nil {
Expand Down Expand Up @@ -236,7 +245,7 @@ func updateReceiverCmd(c *configuration) *cobra.Command {
return err
}

fmt.Println("Successfully updated receiver")
fmt.Printf("Successfully updated receiver with id %q", id)

return nil
},
Expand Down Expand Up @@ -289,7 +298,7 @@ func deleteReceiverCmd(c *configuration) *cobra.Command {
return cmd
}

func sendReceiverNotificationCmd(c *configuration) *cobra.Command {
func notifyReceiverCmd(c *configuration) *cobra.Command {
var id uint64
var filePath string
cmd := &cobra.Command{
Expand All @@ -302,34 +311,37 @@ func sendReceiverNotificationCmd(c *configuration) *cobra.Command {
"group:core": "true",
},
RunE: func(cmd *cobra.Command, args []string) error {
var notificationConfig sirenv1beta1.SendReceiverNotificationRequest

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

receiver, err := client.GetReceiver(ctx, &sirenv1beta1.GetReceiverRequest{
rcv, err := client.GetReceiver(ctx, &sirenv1beta1.GetReceiverRequest{
Id: id,
})
if err != nil {
return err
}

notificationConfig.Id = id
switch receiver.Type {
if rcv.GetData() == nil {
return errors.New("no response from server")
}

notifyReceiverReq := &sirenv1beta1.NotifyReceiverRequest{}
notifyReceiverReq.Id = rcv.GetData().GetId()
switch rcv.GetData().GetType() {
case "slack":
var slackConfig *sirenv1beta1.SendReceiverNotificationRequest_Slack
var slackConfig *structpb.Struct
if err := parseFile(filePath, &slackConfig); err != nil {
return err
}

notificationConfig.Data = slackConfig
notifyReceiverReq.Payload = slackConfig
}

_, err = client.SendReceiverNotification(ctx, &notificationConfig)
_, err = client.NotifyReceiver(ctx, notifyReceiverReq)
if err != nil {
return err
}
Expand Down
16 changes: 9 additions & 7 deletions cli/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ import (
"strconv"
"strings"

"google.golang.org/protobuf/types/known/emptypb"

"gopkg.in/yaml.v3"

"github.com/MakeNowJust/heredoc"
"github.com/odpf/salt/printer"
"github.com/odpf/siren/core/rule"
sirenv1beta1 "github.com/odpf/siren/internal/server/proto/odpf/siren/v1beta1"
"github.com/spf13/cobra"
sirenv1beta1 "go.buf.build/odpf/gw/odpf/proton/odpf/siren/v1beta1"
)

type variables struct {
Expand Down Expand Up @@ -272,15 +270,19 @@ func uploadRule(client sirenv1beta1.SirenServiceClient, yamlFile []byte) ([]*sir
return nil, errors.New("provider not found")
}

data, err := client.ListNamespaces(context.Background(), &emptypb.Empty{})
res, err := client.ListNamespaces(context.Background(), &sirenv1beta1.ListNamespacesRequest{})
if err != nil {
return nil, err
}

if res.GetData() == nil {
return nil, errors.New("no response of getting list of namespaces from server")
}

var providerNamespace *sirenv1beta1.Namespace
for _, namespace := range data.Namespaces {
if namespace.Urn == yamlBody.ProviderNamespace && namespace.Provider == provider.Id {
providerNamespace = namespace
for _, ns := range res.GetData() {
if ns.GetUrn() == yamlBody.ProviderNamespace && ns.Provider == provider.Id {
providerNamespace = ns
break
}
}
Expand Down
Loading