Skip to content

Commit

Permalink
Fixed variable naming style
Browse files Browse the repository at this point in the history
  • Loading branch information
MaartendeKruijf committed Aug 16, 2024
1 parent 8ea7d52 commit b56ddf8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
27 changes: 13 additions & 14 deletions routes/trigger/trigger_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ func init() {
}

type TriggerApi struct {
controller decomposer_controller.IController
database database.IController
Executionsch chan decomposer.ExecutionDetails
controller decomposer_controller.IController
database database.IController
ExecutionsChannel chan decomposer.ExecutionDetails
}

func New(controller decomposer_controller.IController, database database.IController) *TriggerApi {
instance := TriggerApi{}
instance.controller = controller
instance.database = database
// Channel to get back execution details
instance.Executionsch = make(chan decomposer.ExecutionDetails)
instance.ExecutionsChannel = make(chan decomposer.ExecutionDetails)
return &instance
}

Expand Down Expand Up @@ -121,7 +121,6 @@ func (trigger *TriggerApi) ExecuteById(context *gin.Context) {
return
}
}
fmt.Println(playbook)
trigger.execute(&playbook, context)
}

Expand All @@ -139,8 +138,8 @@ func (trigger *TriggerApi) ExecuteById(context *gin.Context) {
// @Router /trigger/playbook [POST]
func (trigger *TriggerApi) Execute(context *gin.Context) {

jsonData, errIo := io.ReadAll(context.Request.Body)
if errIo != nil {
jsonData, err := io.ReadAll(context.Request.Body)
if err != nil {
log.Error("failed")
soarca_http_error.SendErrorResponse(context, http.StatusBadRequest,
"Failed to marshall json on server side",
Expand All @@ -161,7 +160,7 @@ func (trigger *TriggerApi) Execute(context *gin.Context) {

func (trigger *TriggerApi) execute(playbook *cacao.Playbook, context *gin.Context) {
decomposer := trigger.controller.NewDecomposer()
go decomposer.ExecuteAsync(*playbook, trigger.Executionsch)
go decomposer.ExecuteAsync(*playbook, trigger.ExecutionsChannel)
timer := time.NewTimer(time.Duration(3) * time.Second)
for {
select {
Expand All @@ -172,13 +171,13 @@ func (trigger *TriggerApi) execute(playbook *cacao.Playbook, context *gin.Contex
}
context.JSON(http.StatusRequestTimeout, msg)
log.Error("async execution timed out for playbook ", playbook.ID)
case exec_details := <-trigger.Executionsch:
playbook_id := exec_details.PlaybookId
exec_id := exec_details.ExecutionId
if playbook_id == playbook.ID {
case executionsDetail := <-trigger.ExecutionsChannel:
playbookId := executionsDetail.PlaybookId
executionId := executionsDetail.ExecutionId
if playbookId == playbook.ID {
msg := gin.H{
"execution_id": exec_id,
"payload": playbook_id,
"execution_id": executionId,
"payload": playbookId,
}
context.JSON(http.StatusOK, msg)
return
Expand Down
6 changes: 3 additions & 3 deletions test/unittest/routes/trigger_api/tigger_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestTriggerExecutionOfPlaybook(t *testing.T) {
trigger.Routes(app, trigger_api)

executionId, _ := uuid.Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
mock_decomposer.On("ExecuteAsync", *playbook, trigger_api.Executionsch).Return(&decomposer.ExecutionDetails{}, nil, executionId)
mock_decomposer.On("ExecuteAsync", *playbook, trigger_api.ExecutionsChannel).Return(&decomposer.ExecutionDetails{}, nil, executionId)

request, err := http.NewRequest("POST", "/trigger/playbook", bytes.NewBuffer(byteValue))
if err != nil {
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestExecutionOfPlaybookById(t *testing.T) {
recorder := httptest.NewRecorder()
trigger_api := trigger.New(mock_controller, mock_database_controller)
trigger.Routes(app, trigger_api)
mock_decomposer.On("ExecuteAsync", *playbook, trigger_api.Executionsch).Return(&decomposer.ExecutionDetails{}, nil, executionId)
mock_decomposer.On("ExecuteAsync", *playbook, trigger_api.ExecutionsChannel).Return(&decomposer.ExecutionDetails{}, nil, executionId)

request, err := http.NewRequest("POST", "/trigger/playbook/1", nil)
if err != nil {
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestExecutionOfPlaybookByIdWithPayloadValidVariables(t *testing.T) {
trigger_api := trigger.New(mock_controller, mock_database_controller)
trigger.Routes(app, trigger_api)

mock_decomposer.On("ExecuteAsync", *playbook, trigger_api.Executionsch).Return(&decomposer.ExecutionDetails{}, nil, executionId)
mock_decomposer.On("ExecuteAsync", *playbook, trigger_api.ExecutionsChannel).Return(&decomposer.ExecutionDetails{}, nil, executionId)

request, err := http.NewRequest("POST", "/trigger/playbook/1", bytes.NewReader(json))
if err != nil {
Expand Down

0 comments on commit b56ddf8

Please sign in to comment.