From 8860d72f705feaaabce89a9befe56da9dec6fc01 Mon Sep 17 00:00:00 2001 From: Marcelo Rydel Date: Mon, 28 Mar 2022 12:02:09 -0300 Subject: [PATCH] fix(edge/jobs): fix get edge job file content [EE-2702] (#6622) --- api/dataservices/edgejob/edgejob.go | 14 +++++++------- api/dataservices/interface.go | 2 +- api/http/handler/edgejobs/edgejob_create.go | 2 +- api/http/handler/edgejobs/edgejob_file.go | 2 +- .../handler/endpoints/endpoint_status_inspect.go | 2 +- api/internal/testhelpers/datastore.go | 4 +++- .../views/edge-jobs/edgeJob/edgeJobController.js | 4 ++-- 7 files changed, 16 insertions(+), 14 deletions(-) diff --git a/api/dataservices/edgejob/edgejob.go b/api/dataservices/edgejob/edgejob.go index edbb27736..ab048102d 100644 --- a/api/dataservices/edgejob/edgejob.go +++ b/api/dataservices/edgejob/edgejob.go @@ -67,14 +67,14 @@ func (service *Service) EdgeJob(ID portainer.EdgeJobID) (*portainer.EdgeJob, err return &edgeJob, nil } -// CreateEdgeJob creates a new Edge job -func (service *Service) Create(edgeJob *portainer.EdgeJob) error { - return service.connection.CreateObject( +// Create creates a new EdgeJob +func (service *Service) Create(ID portainer.EdgeJobID, edgeJob *portainer.EdgeJob) error { + edgeJob.ID = ID + + return service.connection.CreateObjectWithId( BucketName, - func(id uint64) (int, interface{}) { - edgeJob.ID = portainer.EdgeJobID(id) - return int(edgeJob.ID), edgeJob - }, + int(edgeJob.ID), + edgeJob, ) } diff --git a/api/dataservices/interface.go b/api/dataservices/interface.go index 24dcaf71b..948c946d6 100644 --- a/api/dataservices/interface.go +++ b/api/dataservices/interface.go @@ -74,7 +74,7 @@ type ( EdgeJobService interface { EdgeJobs() ([]portainer.EdgeJob, error) EdgeJob(ID portainer.EdgeJobID) (*portainer.EdgeJob, error) - Create(edgeJob *portainer.EdgeJob) error + Create(ID portainer.EdgeJobID, edgeJob *portainer.EdgeJob) error UpdateEdgeJob(ID portainer.EdgeJobID, edgeJob *portainer.EdgeJob) error DeleteEdgeJob(ID portainer.EdgeJobID) error GetNextIdentifier() int diff --git a/api/http/handler/edgejobs/edgejob_create.go b/api/http/handler/edgejobs/edgejob_create.go index bc4116b0c..392a930e5 100644 --- a/api/http/handler/edgejobs/edgejob_create.go +++ b/api/http/handler/edgejobs/edgejob_create.go @@ -219,7 +219,7 @@ func (handler *Handler) addAndPersistEdgeJob(edgeJob *portainer.EdgeJob, file [] handler.ReverseTunnelService.AddEdgeJob(endpointID, edgeJob) } - return handler.DataStore.EdgeJob().Create(edgeJob) + return handler.DataStore.EdgeJob().Create(edgeJob.ID, edgeJob) } func convertEndpointsToMetaObject(endpoints []portainer.EndpointID) map[portainer.EndpointID]portainer.EdgeJobEndpointMeta { diff --git a/api/http/handler/edgejobs/edgejob_file.go b/api/http/handler/edgejobs/edgejob_file.go index 402b7dfc0..8141aca0a 100644 --- a/api/http/handler/edgejobs/edgejob_file.go +++ b/api/http/handler/edgejobs/edgejob_file.go @@ -39,7 +39,7 @@ func (handler *Handler) edgeJobFile(w http.ResponseWriter, r *http.Request) *htt return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an Edge job with the specified identifier inside the database", err} } - edgeJobFileContent, err := handler.FileService.GetFileContent("", edgeJob.ScriptPath) + edgeJobFileContent, err := handler.FileService.GetFileContent(edgeJob.ScriptPath, "") if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve Edge job script file from disk", err} } diff --git a/api/http/handler/endpoints/endpoint_status_inspect.go b/api/http/handler/endpoints/endpoint_status_inspect.go index afefd9f25..1d32a6e15 100644 --- a/api/http/handler/endpoints/endpoint_status_inspect.go +++ b/api/http/handler/endpoints/endpoint_status_inspect.go @@ -130,7 +130,7 @@ func (handler *Handler) endpointStatusInspect(w http.ResponseWriter, r *http.Req Version: job.Version, } - file, err := handler.FileService.GetFileContent("", job.ScriptPath) + file, err := handler.FileService.GetFileContent(job.ScriptPath, "") if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve Edge job script file", err} diff --git a/api/internal/testhelpers/datastore.go b/api/internal/testhelpers/datastore.go index ff9f9e68a..95f670604 100644 --- a/api/internal/testhelpers/datastore.go +++ b/api/internal/testhelpers/datastore.go @@ -153,7 +153,9 @@ func (s *stubEdgeJobService) EdgeJobs() ([]portainer.EdgeJob, error) { return s. func (s *stubEdgeJobService) EdgeJob(ID portainer.EdgeJobID) (*portainer.EdgeJob, error) { return nil, nil } -func (s *stubEdgeJobService) Create(edgeJob *portainer.EdgeJob) error { return nil } +func (s *stubEdgeJobService) Create(ID portainer.EdgeJobID, edgeJob *portainer.EdgeJob) error { + return nil +} func (s *stubEdgeJobService) UpdateEdgeJob(ID portainer.EdgeJobID, edgeJob *portainer.EdgeJob) error { return nil } diff --git a/app/edge/views/edge-jobs/edgeJob/edgeJobController.js b/app/edge/views/edge-jobs/edgeJob/edgeJobController.js index 190f45fb4..1dd35e2f7 100644 --- a/app/edge/views/edge-jobs/edgeJob/edgeJobController.js +++ b/app/edge/views/edge-jobs/edgeJob/edgeJobController.js @@ -126,7 +126,7 @@ export class EdgeJobController { } async uiCanExit() { - if (this.edgeJob.FileContent !== this.oldFileContent && this.state.isEditorDirty) { + if (this.edgeJob && this.edgeJob.FileContent !== this.oldFileContent && this.state.isEditorDirty) { return this.ModalService.confirmWebEditorDiscard(); } } @@ -165,7 +165,7 @@ export class EdgeJobController { } this.$window.onbeforeunload = () => { - if (this.edgeJob.FileContent !== this.oldFileContent && this.state.isEditorDirty) { + if (this.edgeJob && this.edgeJob.FileContent !== this.oldFileContent && this.state.isEditorDirty) { return ''; } };