Skip to content

Commit 4296366

Browse files
feat: plugin creation support (#5630)
* wip: new plugin creation api and min plugin api with only shared plugin list * wip: create new plugin version code * wip:plugin type SHARED by default * wip:find plugin either by identifier or by id while creating a new version of existing plugin * wip: create new plugin tag logic improved * wip: optimize GetAllFilteredPluginParentMetadata query * wip: create plugin tag new flow * wip: minor fix * wip: minor fix * wip: minor fix * wip: newTagsPresent -> areNewTagsPresent * wip: icon is not mandatory code incorporated * wip:minor refactoring * wip: prevent duplicate version from being created and save tags relation only when * wip: minor fix * wip: details api, get all plugin data or non * wip: code review incorp part -1 * wip: code review incorp part -2 * wip: code review incorp part -3 * wip: remove code duplication * wip: hardcode isExposed to true * wip: hardcode StepType= inline * wip: set default VariableStepIndex= 1
1 parent c66ccf5 commit 4296366

File tree

8 files changed

+757
-103
lines changed

8 files changed

+757
-103
lines changed

api/restHandler/GlobalPluginRestHandler.go

+67
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535

3636
type GlobalPluginRestHandler interface {
3737
PatchPlugin(w http.ResponseWriter, r *http.Request)
38+
CreatePlugin(w http.ResponseWriter, r *http.Request)
3839

3940
GetAllGlobalVariables(w http.ResponseWriter, r *http.Request)
4041
ListAllPlugins(w http.ResponseWriter, r *http.Request)
@@ -46,6 +47,7 @@ type GlobalPluginRestHandler interface {
4647
GetPluginDetailByIds(w http.ResponseWriter, r *http.Request)
4748
GetAllUniqueTags(w http.ResponseWriter, r *http.Request)
4849
MigratePluginData(w http.ResponseWriter, r *http.Request)
50+
GetAllPluginMinData(w http.ResponseWriter, r *http.Request)
4951
}
5052

5153
func NewGlobalPluginRestHandler(logger *zap.SugaredLogger, globalPluginService plugin.GlobalPluginService,
@@ -420,3 +422,68 @@ func (handler *GlobalPluginRestHandlerImpl) MigratePluginData(w http.ResponseWri
420422
}
421423
common.WriteJsonResp(w, nil, nil, http.StatusOK)
422424
}
425+
426+
func (handler *GlobalPluginRestHandlerImpl) CreatePlugin(w http.ResponseWriter, r *http.Request) {
427+
userId, err := handler.userService.GetLoggedInUser(r)
428+
if userId == 0 || err != nil {
429+
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
430+
return
431+
}
432+
token := r.Header.Get("token")
433+
appId, err := common.ExtractIntQueryParam(w, r, "appId", 0)
434+
if err != nil {
435+
return
436+
}
437+
ok, err := handler.IsUserAuthorized(token, appId)
438+
if err != nil {
439+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
440+
return
441+
}
442+
if !ok {
443+
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), "Unauthorized User", http.StatusForbidden)
444+
return
445+
}
446+
decoder := json.NewDecoder(r.Body)
447+
var pluginDataDto bean.PluginParentMetadataDto
448+
err = decoder.Decode(&pluginDataDto)
449+
if err != nil {
450+
handler.logger.Errorw("request err, CreatePlugin", "error", err, "payload", pluginDataDto)
451+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
452+
return
453+
}
454+
handler.logger.Infow("request payload received for creating plugins", pluginDataDto, "userId", userId)
455+
456+
pluginVersionId, err := handler.globalPluginService.CreatePluginOrVersions(&pluginDataDto, userId)
457+
if err != nil {
458+
handler.logger.Errorw("service error, error in creating plugin", "pluginCreateRequestDto", pluginDataDto, "err", err)
459+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
460+
return
461+
}
462+
463+
common.WriteJsonResp(w, nil, bean.NewPluginMinDto().WithPluginVersionId(pluginVersionId), http.StatusOK)
464+
}
465+
466+
func (handler *GlobalPluginRestHandlerImpl) GetAllPluginMinData(w http.ResponseWriter, r *http.Request) {
467+
token := r.Header.Get("token")
468+
appId, err := common.ExtractIntQueryParam(w, r, "appId", 0)
469+
if err != nil {
470+
return
471+
}
472+
ok, err := handler.IsUserAuthorized(token, appId)
473+
if err != nil {
474+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
475+
return
476+
}
477+
if !ok {
478+
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), "Unauthorized User", http.StatusForbidden)
479+
return
480+
}
481+
482+
pluginDetail, err := handler.globalPluginService.GetAllPluginMinData()
483+
if err != nil {
484+
handler.logger.Errorw("error in getting all unique tags", "err", err)
485+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
486+
return
487+
}
488+
common.WriteJsonResp(w, nil, pluginDetail, http.StatusOK)
489+
}

api/router/GlobalPluginRouter.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ type GlobalPluginRouterImpl struct {
4141
func (impl *GlobalPluginRouterImpl) initGlobalPluginRouter(globalPluginRouter *mux.Router) {
4242
globalPluginRouter.Path("/migrate").
4343
HandlerFunc(impl.globalPluginRestHandler.MigratePluginData).Methods("PUT")
44-
44+
globalPluginRouter.Path("/create").
45+
HandlerFunc(impl.globalPluginRestHandler.CreatePlugin).Methods("POST")
4546
// versioning impact handling to be done for below apis,
4647
globalPluginRouter.Path("").
4748
HandlerFunc(impl.globalPluginRestHandler.PatchPlugin).Methods("POST")
@@ -68,5 +69,7 @@ func (impl *GlobalPluginRouterImpl) initGlobalPluginRouter(globalPluginRouter *m
6869

6970
globalPluginRouter.Path("/list/tags").
7071
HandlerFunc(impl.globalPluginRestHandler.GetAllUniqueTags).Methods("GET")
72+
globalPluginRouter.Path("/list/v2/min").
73+
HandlerFunc(impl.globalPluginRestHandler.GetAllPluginMinData).Methods("GET")
7174

7275
}

internal/util/ErrorUtil.go

+9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"google.golang.org/grpc/codes"
2727
"google.golang.org/grpc/status"
2828
"net/http"
29+
"strconv"
2930
)
3031

3132
type ApiError struct {
@@ -36,6 +37,14 @@ type ApiError struct {
3637
UserDetailMessage string `json:"userDetailMessage,omitempty"`
3738
}
3839

40+
func GetApiError(code int, userMessage, internalMessage string) *ApiError {
41+
return &ApiError{
42+
HttpStatusCode: code,
43+
Code: strconv.Itoa(code),
44+
InternalMessage: internalMessage,
45+
UserMessage: userMessage,
46+
}
47+
}
3948
func NewApiError() *ApiError {
4049
return &ApiError{}
4150
}

0 commit comments

Comments
 (0)