Skip to content

Commit

Permalink
ADD create and update action by match method.
Browse files Browse the repository at this point in the history
ADD create and update action by match method.
  • Loading branch information
karminski committed Jan 17, 2024
1 parent cb3358f commit db6973d
Show file tree
Hide file tree
Showing 8 changed files with 347 additions and 82 deletions.
160 changes: 79 additions & 81 deletions src/controller/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,61 +52,62 @@ func (controller *Controller) CreateAction(c *gin.Context) {
return
}

// append remote virtual resource (like aiagent, but the transformet is local virtual resource)
if createActionRequest.IsRemoteVirtualAction() {
// the AI_Agent need fetch resource info from resource manager, but illa drive does not need that
if createActionRequest.NeedFetchResourceInfoFromSourceManager() {
api, errInNewAPI := illaresourcemanagersdk.NewIllaResourceManagerRestAPI()
if errInNewAPI != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_CREATE_ACTION, "error in fetch action mapped virtual resource: "+errInNewAPI.Error())
return
}
virtualResource, errInGetVirtualResource := api.GetResource(createActionRequest.ExportActionTypeInInt(), createActionRequest.ExportResourceIDInInt())
if errInGetVirtualResource != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_CREATE_ACTION, "error in fetch action mapped virtual resource: "+errInGetVirtualResource.Error())
return
}
createActionRequest.AppendVirtualResourceToTemplate(virtualResource)
}
// create
newAction, errInCreateRequest := controller.createAction(c, teamID, appID, userID, createActionRequest)
if errInCreateRequest != nil {
return
}

// get action mapped app
app, errInRetrieveApp := controller.Storage.AppStorage.RetrieveAppByTeamIDAndAppID(teamID, appID)
if errInRetrieveApp != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_GET_APP, "get app failed: "+errInRetrieveApp.Error())
// feedback
controller.FeedbackOK(c, response.NewCreateActionResponse(newAction))
}

func (controller *Controller) CreateActionByBatch(c *gin.Context) {
// fetch needed param
teamID, errInGetTeamID := controller.GetMagicIntParamFromRequest(c, PARAM_TEAM_ID)
appID, errInGetAPPID := controller.GetMagicIntParamFromRequest(c, PARAM_APP_ID)
userID, errInGetUserID := controller.GetUserIDFromAuth(c)
userAuthToken, errInGetAuthToken := controller.GetUserAuthTokenFromHeader(c)
if errInGetTeamID != nil || errInGetAPPID != nil || errInGetUserID != nil || errInGetAuthToken != nil {
return
}

// init action instace
action, errorInNewAction := model.NewAcitonByCreateActionRequest(app, userID, createActionRequest)
if errorInNewAction != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_CREATE_ACTION, "error in create action instance: "+errorInNewAction.Error())
// validate
canManage, errInCheckAttr := controller.AttributeGroup.CanManage(
teamID,
userAuthToken,
accesscontrol.UNIT_TYPE_ACTION,
accesscontrol.DEFAULT_UNIT_ID,
accesscontrol.ACTION_MANAGE_CREATE_ACTION,
)
if errInCheckAttr != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_ACCESS_DENIED, "error in check attribute: "+errInCheckAttr.Error())
return
}

// validate action options
errInValidateActionOptions := controller.ValidateActionTemplate(c, action)
if errInValidateActionOptions != nil {
if !canManage {
controller.FeedbackBadRequest(c, ERROR_FLAG_ACCESS_DENIED, "you can not access this attribute due to access control policy.")
return
}

// create action
_, errInCreateAction := controller.Storage.ActionStorage.Create(action)
if errInCreateAction != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_CREATE_ACTION, "create action error: "+errInCreateAction.Error())
// fetch payload
createActionByBatchRequest := request.NewCreateActionByBatchRequest()
if err := json.NewDecoder(c.Request.Body).Decode(&createActionByBatchRequest); err != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_PARSE_REQUEST_BODY_FAILED, "parse request body error: "+err.Error())
return
}

// update app updatedAt, updatedBy, editedBy field
app.Modify(userID)
errInUpdateApp := controller.Storage.AppStorage.UpdateWholeApp(app)
if errInUpdateApp != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_UPDATE_APP, "update app modify info error: "+errInUpdateApp.Error())
return
// create by batch
newActions := make([]*model.Action, 0)
for _, createActionRequest := range createActionByBatchRequest.ExportActions() {
newAction, errInCreateRequest := controller.createAction(c, teamID, appID, userID, createActionRequest)
if errInCreateRequest != nil {
return
}
newActions = append(newActions, newAction)
}

// feedback
controller.FeedbackOK(c, response.NewCreateActionResponse(action))
controller.FeedbackOK(c, response.NewCreateActionByBatchResponse(newActions))
}

func (controller *Controller) UpdateAction(c *gin.Context) {
Expand Down Expand Up @@ -144,64 +145,61 @@ func (controller *Controller) UpdateAction(c *gin.Context) {
return
}

// append remote virtual resource (like aiagent, but the transformet is local virtual resource)
if updateActionRequest.IsRemoteVirtualAction() {
// the AI_Agent need fetch resource info from resource manager, but illa drive does not need that
if updateActionRequest.NeedFetchResourceInfoFromSourceManager() {
api, errInNewAPI := illaresourcemanagersdk.NewIllaResourceManagerRestAPI()
if errInNewAPI != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_UPDATE_ACTION, "error in fetch action mapped virtual resource: "+errInNewAPI.Error())
return
}
virtualResource, errInGetVirtualResource := api.GetResource(updateActionRequest.ExportActionTypeInInt(), updateActionRequest.ExportResourceIDInInt())
if errInGetVirtualResource != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_UPDATE_ACTION, "error in fetch action mapped virtual resource: "+errInGetVirtualResource.Error())
return
}
updateActionRequest.AppendVirtualResourceToTemplate(virtualResource)
}
// update
newInDatabaseAction, errInUpdateAction := controller.updateAction(c, teamID, appID, userID, actionID, updateActionRequest)
if errInUpdateAction != nil {
return
}

// get action mapped app
app, errInRetrieveApp := controller.Storage.AppStorage.RetrieveAppByTeamIDAndAppID(teamID, appID)
if errInRetrieveApp != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_GET_APP, "get app failed: "+errInRetrieveApp.Error())
// feedback
controller.FeedbackOK(c, response.NewUpdateActionResponse(newInDatabaseAction))
}

func (controller *Controller) UpdateActionByBatch(c *gin.Context) {
// fetch needed param
teamID, errInGetTeamID := controller.GetMagicIntParamFromRequest(c, PARAM_TEAM_ID)
appID, errInGetAPPID := controller.GetMagicIntParamFromRequest(c, PARAM_APP_ID)
userID, errInGetUserID := controller.GetUserIDFromAuth(c)
userAuthToken, errInGetAuthToken := controller.GetUserAuthTokenFromHeader(c)
if errInGetTeamID != nil || errInGetAPPID != nil || errInGetUserID != nil || errInGetAuthToken != nil {
return
}

// get action
inDatabaseAction, errInRetrieveAction := controller.Storage.ActionStorage.RetrieveActionByTeamIDActionID(teamID, actionID)
if errInRetrieveAction != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_GET_APP, "get app failed: "+errInRetrieveAction.Error())
// validate
canManage, errInCheckAttr := controller.AttributeGroup.CanManage(
teamID,
userAuthToken,
accesscontrol.UNIT_TYPE_ACTION,
0,
accesscontrol.ACTION_MANAGE_EDIT_ACTION,
)
if errInCheckAttr != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_ACCESS_DENIED, "error in check attribute: "+errInCheckAttr.Error())
return
}

// update inDatabaseAction instance
inDatabaseAction.UpdateAcitonByUpdateActionRequest(app, userID, updateActionRequest)

// validate action options
errInValidateActionOptions := controller.ValidateActionTemplate(c, inDatabaseAction)
if errInValidateActionOptions != nil {
if !canManage {
controller.FeedbackBadRequest(c, ERROR_FLAG_ACCESS_DENIED, "you can not access this attribute due to access control policy.")
return
}

// update action
errInUpdateAction := controller.Storage.ActionStorage.UpdateWholeAction(inDatabaseAction)
if errInUpdateAction != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_UPDATE_ACTION, "update action error: "+errInUpdateAction.Error())
// fetch payload
updateActionByBatchRequest := request.NewUpdateActionByBatchRequest()
if err := json.NewDecoder(c.Request.Body).Decode(&updateActionByBatchRequest); err != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_PARSE_REQUEST_BODY_FAILED, "parse request body error: "+err.Error())
return
}

// update app updatedAt, updatedBy, editedBy field
app.Modify(userID)
errInUpdateApp := controller.Storage.AppStorage.UpdateWholeApp(app)
if errInUpdateApp != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_UPDATE_APP, "update app modify info error: "+errInUpdateApp.Error())
return
inDatabaseActions := make([]*model.Action, 0)
for _, updateActionRequest := range updateActionByBatchRequest.ExportActions() {
newInDatabaseAction, errInUpdateAction := controller.updateAction(c, teamID, appID, userID, updateActionRequest.ExportActionIDInInt(), updateActionRequest)
if errInUpdateAction != nil {
return
}
inDatabaseActions = append(inDatabaseActions, newInDatabaseAction)
}

// feedback
controller.FeedbackOK(c, response.NewUpdateActionResponse(inDatabaseAction))
controller.FeedbackOK(c, response.NewUpdateActionByBatchResponse(inDatabaseActions))
}

func (controller *Controller) DeleteAction(c *gin.Context) {
Expand Down
120 changes: 120 additions & 0 deletions src/controller/action_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package controller
import (
"github.com/gin-gonic/gin"
"github.com/illacloud/builder-backend/src/model"
"github.com/illacloud/builder-backend/src/request"
"github.com/illacloud/builder-backend/src/utils/illaresourcemanagersdk"
"github.com/illacloud/builder-backend/src/utils/resourcelist"
)

Expand Down Expand Up @@ -49,3 +51,121 @@ func (controller *Controller) ValidateFlowActionTemplate(c *gin.Context, flowAct
}
return nil
}

func (controller *Controller) createAction(c *gin.Context, teamID int, appID int, userID int, createActionRequest *request.CreateActionRequest) (*model.Action, error) {
// append remote virtual resource (like aiagent, but the transformet is local virtual resource)
if createActionRequest.IsRemoteVirtualAction() {
// the AI_Agent need fetch resource info from resource manager, but illa drive does not need that
if createActionRequest.NeedFetchResourceInfoFromSourceManager() {
api, errInNewAPI := illaresourcemanagersdk.NewIllaResourceManagerRestAPI()
if errInNewAPI != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_CREATE_ACTION, "error in fetch action mapped virtual resource: "+errInNewAPI.Error())
return nil, errInNewAPI
}
virtualResource, errInGetVirtualResource := api.GetResource(createActionRequest.ExportActionTypeInInt(), createActionRequest.ExportResourceIDInInt())
if errInGetVirtualResource != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_CREATE_ACTION, "error in fetch action mapped virtual resource: "+errInGetVirtualResource.Error())
return nil, errInGetVirtualResource
}
createActionRequest.AppendVirtualResourceToTemplate(virtualResource)
}
}

// get action mapped app
app, errInRetrieveApp := controller.Storage.AppStorage.RetrieveAppByTeamIDAndAppID(teamID, appID)
if errInRetrieveApp != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_GET_APP, "get app failed: "+errInRetrieveApp.Error())
return nil, errInRetrieveApp
}

// init action instace
action, errorInNewAction := model.NewAcitonByCreateActionRequest(app, userID, createActionRequest)
if errorInNewAction != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_CREATE_ACTION, "error in create action instance: "+errorInNewAction.Error())
return nil, errorInNewAction
}

// validate action options
errInValidateActionOptions := controller.ValidateActionTemplate(c, action)
if errInValidateActionOptions != nil {
return nil, errInValidateActionOptions
}

// create action
_, errInCreateAction := controller.Storage.ActionStorage.Create(action)
if errInCreateAction != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_CREATE_ACTION, "create action error: "+errInCreateAction.Error())
return nil, errInCreateAction
}

// update app updatedAt, updatedBy, editedBy field
app.Modify(userID)
errInUpdateApp := controller.Storage.AppStorage.UpdateWholeApp(app)
if errInUpdateApp != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_UPDATE_APP, "update app modify info error: "+errInUpdateApp.Error())
return nil, errInUpdateApp
}

return action, nil
}

func (controller *Controller) updateAction(c *gin.Context, teamID int, appID int, userID int, actionID int, updateActionRequest *request.UpdateActionRequest) (*model.Action, error) {
// append remote virtual resource (like aiagent, but the transformet is local virtual resource)
if updateActionRequest.IsRemoteVirtualAction() {
// the AI_Agent need fetch resource info from resource manager, but illa drive does not need that
if updateActionRequest.NeedFetchResourceInfoFromSourceManager() {
api, errInNewAPI := illaresourcemanagersdk.NewIllaResourceManagerRestAPI()
if errInNewAPI != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_UPDATE_ACTION, "error in fetch action mapped virtual resource: "+errInNewAPI.Error())
return nil, errInNewAPI
}
virtualResource, errInGetVirtualResource := api.GetResource(updateActionRequest.ExportActionTypeInInt(), updateActionRequest.ExportResourceIDInInt())
if errInGetVirtualResource != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_UPDATE_ACTION, "error in fetch action mapped virtual resource: "+errInGetVirtualResource.Error())
return nil, errInGetVirtualResource
}
updateActionRequest.AppendVirtualResourceToTemplate(virtualResource)
}
}

// get action mapped app
app, errInRetrieveApp := controller.Storage.AppStorage.RetrieveAppByTeamIDAndAppID(teamID, appID)
if errInRetrieveApp != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_GET_APP, "get app failed: "+errInRetrieveApp.Error())
return nil, errInRetrieveApp
}

// get action
inDatabaseAction, errInRetrieveAction := controller.Storage.ActionStorage.RetrieveActionByTeamIDActionID(teamID, actionID)
if errInRetrieveAction != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_GET_APP, "get app failed: "+errInRetrieveAction.Error())
return nil, errInRetrieveAction
}

// update inDatabaseAction instance
inDatabaseAction.UpdateAcitonByUpdateActionRequest(app, userID, updateActionRequest)

// validate action options
errInValidateActionOptions := controller.ValidateActionTemplate(c, inDatabaseAction)
if errInValidateActionOptions != nil {
return nil, errInValidateActionOptions
}

// update action
errInUpdateAction := controller.Storage.ActionStorage.UpdateWholeAction(inDatabaseAction)
if errInUpdateAction != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_UPDATE_ACTION, "update action error: "+errInUpdateAction.Error())
return nil, errInUpdateAction
}

// update app updatedAt, updatedBy, editedBy field
app.Modify(userID)
errInUpdateApp := controller.Storage.AppStorage.UpdateWholeApp(app)
if errInUpdateApp != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_UPDATE_APP, "update app modify info error: "+errInUpdateApp.Error())
return nil, errInUpdateApp
}

// ok
return inDatabaseAction, nil
}
49 changes: 49 additions & 0 deletions src/request/create_action_by_batch_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package request

// The create action HTTP request body like:
// ```json
//
// {
// "actions":[
// {
// "actionType": "postgresql",
// "displayName": "postgresql1",
// "resourceID": "ILAfx4p1C7cd",
// "content": {
// "mode": "sql",
// "query": ""
// },
// "isVirtualResource": true,
// "transformer": {
// "rawData": "",
// "enable": false
// },
// "triggerMode": "manually",
// "config": {
// "public": false,
// "advancedConfig": {
// "runtime": "none",
// "pages": [],
// "delayWhenLoaded": "",
// "displayLoadingPage": false,
// "isPeriodically": false,
// "periodInterval": ""
// }
// }
// },
// ...
// ]
// }
//
// ```
type CreateActionByBatchRequest struct {
Actions []*CreateActionRequest `json:"actions"`
}

func NewCreateActionByBatchRequest() *CreateActionByBatchRequest {
return &CreateActionByBatchRequest{}
}

func (req *CreateActionByBatchRequest) ExportActions() []*CreateActionRequest {
return req.Actions
}
Loading

0 comments on commit db6973d

Please sign in to comment.