Skip to content

Commit 138f396

Browse files
Arta AsadiArta Asadi
Arta Asadi
authored and
Arta Asadi
committed
fix: update migrator
1 parent b55edb3 commit 138f396

File tree

1 file changed

+1
-234
lines changed

1 file changed

+1
-234
lines changed

services/core/http_routes.go

Lines changed: 1 addition & 234 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
api3 "github.com/opengovern/og-util/pkg/api"
2323
"github.com/opengovern/og-util/pkg/httpclient"
2424
"github.com/opengovern/og-util/pkg/httpserver"
25-
model2 "github.com/opengovern/opensecurity/jobs/demo-importer-job/db/model"
2625
"github.com/opengovern/opensecurity/jobs/post-install-job/db/model"
26+
model2 "github.com/opengovern/opensecurity/jobs/post-install-job/db/model"
2727
complianceapi "github.com/opengovern/opensecurity/services/compliance/api"
2828
integrationApi "github.com/opengovern/opensecurity/services/integration/api/models"
2929
integrationClient "github.com/opengovern/opensecurity/services/integration/client"
@@ -79,9 +79,7 @@ func (h *HttpHandler) Register(r *echo.Echo) {
7979
v3 := r.Group("/api/v3")
8080
// metadata
8181
v3.PUT("/sample/purge", httpserver.AuthorizeHandler(h.PurgeSampleData, api3.ViewerRole))
82-
v3.PUT("/sample/sync", httpserver.AuthorizeHandler(h.SyncDemo, api3.ViewerRole))
8382
v3.PUT("/sample/loaded", httpserver.AuthorizeHandler(h.WorkspaceLoadedSampleData, api3.ViewerRole))
84-
v3.GET("/sample/sync/status", httpserver.AuthorizeHandler(h.GetSampleSyncStatus, api3.ViewerRole))
8583
v3.GET("/migration/status", httpserver.AuthorizeHandler(h.GetMigrationStatus, api3.ViewerRole))
8684
v3.GET("/configured/status", httpserver.AuthorizeHandler(h.GetConfiguredStatus, api3.ViewerRole))
8785
v3.PUT("/configured/set", httpserver.AuthorizeHandler(h.SetConfiguredStatus, api3.AdminRole))
@@ -721,203 +719,6 @@ func (h *HttpHandler) PurgeSampleData(c echo.Context) error {
721719
return c.NoContent(http.StatusOK)
722720
}
723721

724-
// SyncDemo godoc
725-
//
726-
// @Summary Sync demo
727-
//
728-
// @Description Syncs demo with the git backend.
729-
//
730-
// @Security BearerToken
731-
// @Tags compliance
732-
// @Param demo_data_s3_url query string false "Demo Data S3 URL"
733-
// @Accept json
734-
// @Produce json
735-
// @Success 200
736-
// @Router /metadata/api/v3/sample/sync [put]
737-
func (h *HttpHandler) SyncDemo(echoCtx echo.Context) error {
738-
ctx := echoCtx.Request().Context()
739-
740-
var mig *model.Migration
741-
tx := h.migratorDb.ORM.Model(&model.Migration{}).Where("id = ?", model2.MigrationJobName).Find(&mig)
742-
if tx.Error != nil && !errors.Is(tx.Error, gorm.ErrRecordNotFound) {
743-
h.logger.Error("failed to get migration", zap.Error(tx.Error))
744-
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get migration")
745-
}
746-
747-
if mig != nil && mig.ID == model2.MigrationJobName {
748-
h.logger.Info("last migration job", zap.Any("job", *mig))
749-
if mig.Status != "COMPLETED" && mig.UpdatedAt.After(time.Now().Add(-1*10*time.Minute)) {
750-
return echo.NewHTTPError(http.StatusBadRequest, "sync sample data already in progress")
751-
}
752-
}
753-
754-
metadata, err := coreUtils.GetConfigMetadata(h.db, string(models.MetadataKeyCustomizationEnabled))
755-
if err != nil {
756-
if errors.Is(err, gorm.ErrRecordNotFound) {
757-
return echo.NewHTTPError(http.StatusNotFound, "config not found")
758-
}
759-
return err
760-
}
761-
762-
cnf := metadata.GetCore()
763-
764-
var enabled models.IConfigMetadata
765-
switch cnf.Type {
766-
case models.ConfigMetadataTypeString:
767-
enabled = &models.StringConfigMetadata{
768-
ConfigMetadata: cnf,
769-
}
770-
case models.ConfigMetadataTypeInt:
771-
intValue, err := strconv.ParseInt(cnf.Value, 10, 64)
772-
if err != nil {
773-
return echo.NewHTTPError(http.StatusBadRequest, "failed to parse int value")
774-
}
775-
enabled = &models.IntConfigMetadata{
776-
ConfigMetadata: cnf,
777-
Value: int(intValue),
778-
}
779-
case models.ConfigMetadataTypeBool:
780-
boolValue, err := strconv.ParseBool(cnf.Value)
781-
if err != nil {
782-
return echo.NewHTTPError(http.StatusInternalServerError, "failed to convert bool to int")
783-
}
784-
enabled = &models.BoolConfigMetadata{
785-
ConfigMetadata: cnf,
786-
Value: boolValue,
787-
}
788-
case models.ConfigMetadataTypeJSON:
789-
enabled = &models.JSONConfigMetadata{
790-
ConfigMetadata: cnf,
791-
Value: cnf.Value,
792-
}
793-
}
794-
795-
if !enabled.GetValue().(bool) {
796-
return echo.NewHTTPError(http.StatusForbidden, "customization is not allowed")
797-
}
798-
799-
demoDataS3URL := echoCtx.QueryParam("demo_data_s3_url")
800-
if demoDataS3URL != "" {
801-
// validate url
802-
_, err := url.ParseRequestURI(demoDataS3URL)
803-
if err != nil {
804-
return echo.NewHTTPError(http.StatusBadRequest, "invalid url")
805-
}
806-
err = coreUtils.SetConfigMetadata(h.db, models.DemoDataS3URL, demoDataS3URL)
807-
if err != nil {
808-
h.logger.Error("set config metadata", zap.Error(err))
809-
return err
810-
}
811-
}
812-
813-
var importDemoJob batchv1.Job
814-
err = h.kubeClient.Get(ctx, k8sclient.ObjectKey{
815-
Namespace: h.cfg.OpengovernanceNamespace,
816-
Name: "import-es-demo-data",
817-
}, &importDemoJob)
818-
if err != nil {
819-
return err
820-
}
821-
822-
err = h.kubeClient.Delete(ctx, &importDemoJob)
823-
if err != nil {
824-
return err
825-
}
826-
827-
for {
828-
err = h.kubeClient.Get(ctx, k8sclient.ObjectKey{
829-
Namespace: h.cfg.OpengovernanceNamespace,
830-
Name: "import-es-demo-data",
831-
}, &importDemoJob)
832-
if err != nil {
833-
if k8sclient.IgnoreNotFound(err) == nil {
834-
break
835-
}
836-
return err
837-
}
838-
839-
time.Sleep(1 * time.Second)
840-
}
841-
842-
importDemoJob.ObjectMeta = metav1.ObjectMeta{
843-
Name: "import-es-demo-data",
844-
Namespace: h.cfg.OpengovernanceNamespace,
845-
Annotations: map[string]string{
846-
"helm.sh/hook": "post-install,post-upgrade",
847-
"helm.sh/hook-weight": "0",
848-
},
849-
}
850-
importDemoJob.Spec.Selector = nil
851-
importDemoJob.Spec.Suspend = aws.Bool(false)
852-
importDemoJob.Spec.Template.ObjectMeta = metav1.ObjectMeta{}
853-
importDemoJob.Status = batchv1.JobStatus{}
854-
855-
err = h.kubeClient.Create(ctx, &importDemoJob)
856-
if err != nil {
857-
return err
858-
}
859-
860-
var importDemoDbJob batchv1.Job
861-
err = h.kubeClient.Get(ctx, k8sclient.ObjectKey{
862-
Namespace: h.cfg.OpengovernanceNamespace,
863-
Name: "import-psql-demo-data",
864-
}, &importDemoDbJob)
865-
if err != nil {
866-
return err
867-
}
868-
869-
err = h.kubeClient.Delete(ctx, &importDemoDbJob)
870-
if err != nil {
871-
return err
872-
}
873-
874-
for {
875-
err = h.kubeClient.Get(ctx, k8sclient.ObjectKey{
876-
Namespace: h.cfg.OpengovernanceNamespace,
877-
Name: "import-psql-demo-data",
878-
}, &importDemoDbJob)
879-
if err != nil {
880-
if k8sclient.IgnoreNotFound(err) == nil {
881-
break
882-
}
883-
return err
884-
}
885-
886-
time.Sleep(1 * time.Second)
887-
}
888-
889-
importDemoDbJob.ObjectMeta = metav1.ObjectMeta{
890-
Name: "import-psql-demo-data",
891-
Namespace: h.cfg.OpengovernanceNamespace,
892-
Annotations: map[string]string{
893-
"helm.sh/hook": "post-install,post-upgrade",
894-
"helm.sh/hook-weight": "0",
895-
},
896-
}
897-
importDemoDbJob.Spec.Selector = nil
898-
importDemoDbJob.Spec.Suspend = aws.Bool(false)
899-
importDemoDbJob.Spec.Template.ObjectMeta = metav1.ObjectMeta{}
900-
importDemoDbJob.Status = batchv1.JobStatus{}
901-
902-
err = h.kubeClient.Create(ctx, &importDemoDbJob)
903-
if err != nil {
904-
return err
905-
}
906-
907-
jp := pgtype.JSONB{}
908-
err = jp.Set([]byte(""))
909-
if err != nil {
910-
return err
911-
}
912-
tx = h.migratorDb.ORM.Model(&model.Migration{}).Where("id = ?", model2.MigrationJobName).Update("status", "Started").Update("jobs_status", jp)
913-
if tx.Error != nil && !errors.Is(tx.Error, gorm.ErrRecordNotFound) {
914-
h.logger.Error("failed to update migration", zap.Error(tx.Error))
915-
return echo.NewHTTPError(http.StatusInternalServerError, "failed to update migration")
916-
}
917-
918-
return echoCtx.JSON(http.StatusOK, struct{}{})
919-
}
920-
921722
// WorkspaceLoadedSampleData godoc
922723
//
923724
// @Summary Sync demo
@@ -1003,40 +804,6 @@ func (h *HttpHandler) GetMigrationStatus(echoCtx echo.Context) error {
1003804
})
1004805
}
1005806

1006-
// GetSampleSyncStatus godoc
1007-
//
1008-
// @Summary Sync demo
1009-
//
1010-
// @Description Syncs demo with the git backend.
1011-
//
1012-
// @Security BearerToken
1013-
// @Tags compliance
1014-
// @Param demo_data_s3_url query string false "Demo Data S3 URL"
1015-
// @Accept json
1016-
// @Produce json
1017-
// @Success 200 {object} api.GetSampleSyncStatusResponse
1018-
// @Router /workspace/api/v3/sample/sync/status [get]
1019-
func (h *HttpHandler) GetSampleSyncStatus(echoCtx echo.Context) error {
1020-
var mig *model.Migration
1021-
tx := h.migratorDb.ORM.Model(&model.Migration{}).Where("id = ?", model2.MigrationJobName).First(&mig)
1022-
if tx.Error != nil && !errors.Is(tx.Error, gorm.ErrRecordNotFound) {
1023-
h.logger.Error("failed to get migration", zap.Error(tx.Error))
1024-
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get migration")
1025-
}
1026-
var jobsStatus model2.ESImportProgress
1027-
1028-
if len(mig.JobsStatus.Bytes) > 0 {
1029-
err := json.Unmarshal(mig.JobsStatus.Bytes, &jobsStatus)
1030-
if err != nil {
1031-
return err
1032-
}
1033-
}
1034-
return echoCtx.JSON(http.StatusOK, api.GetSampleSyncStatusResponse{
1035-
Status: mig.Status,
1036-
Progress: jobsStatus.Progress,
1037-
})
1038-
}
1039-
1040807
// GetConfiguredStatus godoc
1041808
//
1042809
// @Summary Sync demo

0 commit comments

Comments
 (0)