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

feat: support non-command for web create Push API #1289

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion manifests/bucketeer/charts/web/values.yaml

Large diffs are not rendered by default.

184 changes: 176 additions & 8 deletions pkg/push/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"strconv"

"github.com/jinzhu/copier"
"go.uber.org/zap"
"golang.org/x/oauth2/google"
"google.golang.org/genproto/googleapis/rpc/errdetails"
Expand All @@ -29,6 +30,7 @@ import (
"google.golang.org/grpc/status"

accountclient "github.com/bucketeer-io/bucketeer/pkg/account/client"
domainevent "github.com/bucketeer-io/bucketeer/pkg/domainevent/domain"
experimentclient "github.com/bucketeer-io/bucketeer/pkg/experiment/client"
featureclient "github.com/bucketeer-io/bucketeer/pkg/feature/client"
"github.com/bucketeer-io/bucketeer/pkg/locale"
Expand Down Expand Up @@ -108,6 +110,10 @@ func (s *PushService) CreatePush(
if err != nil {
return nil, err
}
if req.Command == nil {
return s.createPushNoCommand(ctx, req, localizer, editor)
}

if err := s.validateCreatePushRequest(req, localizer); err != nil {
return nil, err
}
Expand Down Expand Up @@ -207,7 +213,6 @@ func (s *PushService) CreatePush(
return err
}
return nil

})
if err != nil {
if err == v2ps.ErrPushAlreadyExists {
Expand Down Expand Up @@ -236,20 +241,149 @@ func (s *PushService) CreatePush(
}
return nil, dt.Err()
}
return &pushproto.CreatePushResponse{}, nil
return &pushproto.CreatePushResponse{
Push: push.Push,
hvn2k1 marked this conversation as resolved.
Show resolved Hide resolved
}, nil
}

func (s *PushService) validateCreatePushRequest(req *pushproto.CreatePushRequest, localizer locale.Localizer) error {
if req.Command == nil {
dt, err := statusNoCommand.WithDetails(&errdetails.LocalizedMessage{
// createPushNoCommand implement logic without command
func (s *PushService) createPushNoCommand(
ctx context.Context,
req *pushproto.CreatePushRequest,
localizer locale.Localizer,
editor *eventproto.Editor,
) (*pushproto.CreatePushResponse, error) {
if err := s.validateCreatePushRequestV2(req, localizer); err != nil {
return nil, err
}
push, err := domain.NewPush(
req.Name,
string(req.FcmServiceAccount),
req.Tags,
)
if err != nil {
s.logger.Error(
"Failed to create a new push",
log.FieldsFromImcomingContext(ctx).AddFields(
zap.Error(err),
zap.String("environmentNamespace", req.EnvironmentNamespace),
zap.Strings("tags", req.Tags),
hvn2k1 marked this conversation as resolved.
Show resolved Hide resolved
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalizeWithTemplate(locale.RequiredFieldTemplate, "command"),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return statusInternal.Err()
return nil, statusInternal.Err()
}
return dt.Err()
return nil, dt.Err()
}
pushes, err := s.listAllPushes(ctx, req.EnvironmentNamespace, localizer)
if err != nil {
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
}
if err := s.checkFCMServiceAccount(ctx, pushes, req.FcmServiceAccount, localizer); err != nil {
return nil, err
}
err = s.containsTags(pushes, req.Tags, localizer)
if err != nil {
if status.Code(err) == codes.AlreadyExists {
dt, err := statusTagAlreadyExists.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.AlreadyExistsError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
}
s.logger.Error(
"Failed to validate tag existence",
log.FieldsFromImcomingContext(ctx).AddFields(
zap.Error(err),
zap.String("environmentNamespace", req.EnvironmentNamespace),
zap.Strings("tags", req.Tags),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
}

var event *eventproto.Event
pushStorage := v2ps.NewPushStorage(s.mysqlClient)
err = pushStorage.CreatePush(ctx, push, req.EnvironmentNamespace)
hvn2k1 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
if err == v2ps.ErrPushAlreadyExists {
dt, err := statusAlreadyExists.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.AlreadyExistsError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
}
s.logger.Error(
"Failed to create push",
log.FieldsFromImcomingContext(ctx).AddFields(
zap.Error(err),
zap.String("environmentNamespace", req.EnvironmentNamespace),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
}
prev := &domain.Push{}
if err = copier.Copy(prev, push); err != nil {
return nil, err
}
event, err = domainevent.NewEvent(
editor,
eventproto.Event_PUSH,
push.Id,
eventproto.Event_PUSH_CREATED,
&eventproto.PushCreatedEvent{
FcmServiceAccount: push.FcmServiceAccount,
Tags: push.Tags,
Name: push.Name,
},
req.EnvironmentNamespace,
push,
prev,
)
if err != nil {
return nil, err
}
if err = s.publisher.Publish(ctx, event); err != nil {
return nil, err
}

return &pushproto.CreatePushResponse{
Push: push.Push,
hvn2k1 marked this conversation as resolved.
Show resolved Hide resolved
}, nil
}

func (s *PushService) validateCreatePushRequest(req *pushproto.CreatePushRequest, localizer locale.Localizer) error {
if string(req.Command.FcmServiceAccount) == "" {
dt, err := statusFCMServiceAccountRequired.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Expand Down Expand Up @@ -283,6 +417,40 @@ func (s *PushService) validateCreatePushRequest(req *pushproto.CreatePushRequest
return nil
}

func (s *PushService) validateCreatePushRequestV2(req *pushproto.CreatePushRequest, localizer locale.Localizer) error {
hvn2k1 marked this conversation as resolved.
Show resolved Hide resolved
if string(req.FcmServiceAccount) == "" {
dt, err := statusFCMServiceAccountRequired.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalizeWithTemplate(locale.RequiredFieldTemplate, "fcm_service_account"),
})
if err != nil {
return statusInternal.Err()
}
return dt.Err()
}
if len(req.Tags) == 0 {
dt, err := statusTagsRequired.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalizeWithTemplate(locale.RequiredFieldTemplate, "tag"),
})
if err != nil {
return statusInternal.Err()
}
return dt.Err()
}
if req.Name == "" {
dt, err := statusNameRequired.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalizeWithTemplate(locale.RequiredFieldTemplate, "name"),
})
if err != nil {
return statusInternal.Err()
}
return dt.Err()
}
return nil
}

func (s *PushService) UpdatePush(
ctx context.Context,
req *pushproto.UpdatePushRequest,
Expand Down
138 changes: 130 additions & 8 deletions pkg/push/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,15 @@ func TestCreatePushMySQL(t *testing.T) {
req *pushproto.CreatePushRequest
expectedErr error
}{
{
desc: "err: ErrNoCommand",
setup: nil,
req: &pushproto.CreatePushRequest{
Command: nil,
},
expectedErr: createError(statusNoCommand, localizer.MustLocalizeWithTemplate(locale.RequiredFieldTemplate, "command")),
},
// command is deprecating
//{
// desc: "err: ErrNoCommand",
// setup: nil,
// req: &pushproto.CreatePushRequest{
// Command: nil,
// },
// expectedErr: createError(statusNoCommand, localizer.MustLocalizeWithTemplate(locale.RequiredFieldTemplate, "command")),
//},
{
desc: "err: ErrFCMServiceAccountRequired",
setup: nil,
Expand Down Expand Up @@ -215,6 +216,127 @@ func TestCreatePushMySQL(t *testing.T) {
}
}

func TestCreatePushV2MySQL(t *testing.T) {
hvn2k1 marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()
mockController := gomock.NewController(t)
defer mockController.Finish()

ctx := createContextWithToken(t, true)
ctx = metadata.NewIncomingContext(ctx, metadata.MD{
"accept-language": []string{"ja"},
})
localizer := locale.NewLocalizer(ctx)
createError := func(status *gstatus.Status, msg string) error {
st, err := status.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: msg,
})
require.NoError(t, err)
return st.Err()
}

patterns := []struct {
desc string
setup func(*PushService)
req *pushproto.CreatePushRequest
expectedErr error
}{
{
desc: "err: ErrFCMServiceAccountRequired",
setup: nil,
req: &pushproto.CreatePushRequest{
FcmServiceAccount: nil,
},
expectedErr: createError(statusFCMServiceAccountRequired, localizer.MustLocalizeWithTemplate(locale.RequiredFieldTemplate, "fcm_service_account")),
},
{
desc: "err: ErrTagsRequired",
setup: nil,
req: &pushproto.CreatePushRequest{
FcmServiceAccount: fcmServiceAccountDummy,
},
expectedErr: createError(statusTagsRequired, localizer.MustLocalizeWithTemplate(locale.RequiredFieldTemplate, "tag")),
},
{
desc: "err: ErrNameRequired",
setup: nil,
req: &pushproto.CreatePushRequest{
FcmServiceAccount: fcmServiceAccountDummy,
Tags: []string{"tag-0"},
},
expectedErr: createError(statusNameRequired, localizer.MustLocalizeWithTemplate(locale.RequiredFieldTemplate, "name")),
},
{
desc: "err: ErrAlreadyExists",
setup: func(s *PushService) {
rows := mysqlmock.NewMockRows(mockController)
rows.EXPECT().Close().Return(nil)
rows.EXPECT().Next().Return(false)
rows.EXPECT().Err().Return(nil)
s.mysqlClient.(*mysqlmock.MockClient).EXPECT().QueryContext(
gomock.Any(), gomock.Any(), gomock.Any(),
).Return(rows, nil)
row := mysqlmock.NewMockRow(mockController)
row.EXPECT().Scan(gomock.Any()).Return(nil)
s.mysqlClient.(*mysqlmock.MockClient).EXPECT().QueryRowContext(
gomock.Any(), gomock.Any(), gomock.Any(),
).Return(row)
s.mysqlClient.(*mysqlmock.MockClient).EXPECT().ExecContext(
gomock.Any(), gomock.Any(), gomock.Any(),
).Return(nil, v2ps.ErrPushAlreadyExists)
},
req: &pushproto.CreatePushRequest{
EnvironmentNamespace: "ns0",
FcmServiceAccount: fcmServiceAccountDummy,
Tags: []string{"tag-0"},
Name: "name-1",
},
expectedErr: createError(statusAlreadyExists, localizer.MustLocalize(locale.AlreadyExistsError)),
},
{
desc: "success",
setup: func(s *PushService) {
rows := mysqlmock.NewMockRows(mockController)
rows.EXPECT().Close().Return(nil)
rows.EXPECT().Next().Return(false)
rows.EXPECT().Err().Return(nil)
s.mysqlClient.(*mysqlmock.MockClient).EXPECT().QueryContext(
gomock.Any(), gomock.Any(), gomock.Any(),
).Return(rows, nil)
row := mysqlmock.NewMockRow(mockController)
row.EXPECT().Scan(gomock.Any()).Return(nil)
s.mysqlClient.(*mysqlmock.MockClient).EXPECT().QueryRowContext(
gomock.Any(), gomock.Any(), gomock.Any(),
).Return(row)
s.mysqlClient.(*mysqlmock.MockClient).EXPECT().ExecContext(
gomock.Any(), gomock.Any(), gomock.Any(),
).Return(nil, nil)
s.publisher.(*publishermock.MockPublisher).EXPECT().Publish(
gomock.Any(), gomock.Any(),
).Return(nil)
},
req: &pushproto.CreatePushRequest{
EnvironmentNamespace: "ns0",
FcmServiceAccount: fcmServiceAccountDummy,
Tags: []string{"tag-0"},
Name: "name-1",
},
expectedErr: nil,
},
}

for _, p := range patterns {
t.Run(p.desc, func(t *testing.T) {
service := newPushServiceWithMock(t, mockController)
if p.setup != nil {
p.setup(service)
}
_, err := service.CreatePush(ctx, p.req)
assert.Equal(t, p.expectedErr, err)
})
}
}

func TestUpdatePushMySQL(t *testing.T) {
t.Parallel()
mockController := gomock.NewController(t)
Expand Down
Loading
Loading