From f6317000cb76b6e4e8269b3cc63af5a1ad06203b Mon Sep 17 00:00:00 2001 From: Cosmin Tupangiu Date: Thu, 16 Jan 2025 14:47:16 +0100 Subject: [PATCH] source: Add on_prem flag to source store model Add `on_premises` flag on store model to know if the source is created from an inventory file or not. Also, add filter for this flat at store level. Signed-off-by: Cosmin Tupangiu --- api/v1alpha1/agent/spec.gen.go | 44 ++++++++++---------- api/v1alpha1/openapi.yaml | 3 ++ api/v1alpha1/spec.gen.go | 60 +++++++++++++-------------- api/v1alpha1/types.gen.go | 13 +++--- internal/service/agent/handler.go | 4 +- internal/service/mappers/inbound.go | 9 ++-- internal/service/mappers/outbound.go | 9 ++-- internal/service/source.go | 10 ++++- internal/service/source_test.go | 62 ++++++++++++++++++++++++++++ internal/store/model/source.go | 17 ++++---- internal/store/options.go | 10 +++++ internal/store/source_test.go | 18 ++++++++ 12 files changed, 182 insertions(+), 77 deletions(-) diff --git a/api/v1alpha1/agent/spec.gen.go b/api/v1alpha1/agent/spec.gen.go index 0bb67f3..2ca0cb4 100644 --- a/api/v1alpha1/agent/spec.gen.go +++ b/api/v1alpha1/agent/spec.gen.go @@ -19,28 +19,28 @@ import ( // Base64 encoded, gzipped, json marshaled Swagger object var swaggerSpec = []string{ - "H4sIAAAAAAAC/+xYzXLbNhB+FQ7aIyPKjnvRzVbzo2mTeuxJcsjoABErETEJoMDSGtejd+8AICRKhCjG", - "tTPtTG8SsNj9sD/fLvhIclkpKUCgIZNHYvICKup+Xq5A4C1SrM0nxSiCXVRaKtDIwYnkGhgI5LT8pEu7", - "gA8KyIQY1FysyCYlnEWXjVPbszUTSxndvgdtuBSRvU1KNPxZcw2MTL4GE3sK0wPEDt9O5zwNOuXiG+Ro", - "7b3RWuruzSswhq6cSxiYXHOFDpWXT8J2egJkkJtvUjITS027lhhFalBq/48jVKYrtNQAU6pozvHh3VXL", - "OVwgrEDbm6BEWp4UciunfOt2uxrTQxwxhzYLVGv6YP8X0uC1XIO2qeZvQxnj1p20vN675TG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StWAXP0gx1VBws8NzcylzoMLul64Bf8dF9jLmLrGNRlPhjvFQ+vbj", + "HMKbTLlVF3a+V0hDDjcUH3ase0JxmJq8X0733d7sxBvTIddr4mjTVA9C5wd0Xt+CcSG50EAfmVyJtv6M", + "G5RLTYvwoPGd/bLg4gvNSwhLGwTVoz9slFQnPMuHmcR2j46WdCW1p0k6z6Gv3FeO2VeqBRdL033mRmK3", + "+r2bbYNdux7086hThzwIoyDQEVJVjusJsrtDtSG0diPK47juGq887wfOVxwu6r7XzFGXnv1GaXm3GbZb", + "oEaK16iR/3Q6VW8252pavDqix6qoVwn1r5/QOEjapuItSuvrbZDThKBLw24oDyQ4hJ12yawd8/vXZs5T", + "EMZd27dqcq5omkF0OhjapmkfuSRDVGaUJKvVakDd9kDqZVKdNcnvk/Hlzd3lh9PBcJBhkbuQcbTR3A5x", + "0TSnQoCOzqeT6EPkekUEginJ3R03z1xSCgYLLoA5BCoQVHEyIh8Hw8GJjQPFzIU9oYonTyeJn6KSF87W", + "yfaZrUpsv1X9EBB5qUguIszAu0KcqQr/jIyI76+NTwLOtKYF+FfCw77uya872rhds77WL5GRb/FbZKAu", + "Ia6+P/SZF2b+MBi8kMy17FQKrIZeqlTOU+d+8s34zwVb1V010/7qsV57CBslRTWgnQ6H7Wj+8ZvN0Onw", + "5NDWmT/1Jm76zxLOs11TF5RFtz4u3ubJ+9u8F7TETGr+l0fp2fDj+xu9knrOGQNHaWcnPyC0n6QAa+yX", + "H5HHiZ0PBc2jO9BPoKNaMCZILc8++PmSzOxSXfue7PsVvwaV03S/+o2ClC84sMjrahHBrT/WnL17UEGt", + "vdb5n6GDwBtjvdvRrKMH+OENPQhB5H9WeW9WGZ69v8UbiVeyFOxfxCxVmXpqyYDm6D53LiFAI347SjNI", + "H1tk8dmf7ds9Gy5UVmfOf+Mc9dTiZ7CErGfrvwMAAP//zYIt9LwYAAA=", } // GetSwagger returns the content of the embedded swagger specification file diff --git a/api/v1alpha1/openapi.yaml b/api/v1alpha1/openapi.yaml index fb56382..d5c1c8e 100644 --- a/api/v1alpha1/openapi.yaml +++ b/api/v1alpha1/openapi.yaml @@ -361,6 +361,8 @@ components: format: date-time name: type: string + onPremises: + type: boolean agents: type: array items: @@ -369,6 +371,7 @@ components: - id - createdAt - updatedAt + - onPremises SourceCreate: type: object diff --git a/api/v1alpha1/spec.gen.go b/api/v1alpha1/spec.gen.go index 2a3d913..dac0e34 100644 --- a/api/v1alpha1/spec.gen.go +++ b/api/v1alpha1/spec.gen.go @@ -19,37 +19,37 @@ import ( var swaggerSpec = []string{ "H4sIAAAAAAAC/+xZUW/bOBL+KwT3gHuR7bTbAw5+c7Pt1thtGzS72Yc2D2NxbHEjkSo5spEr/N8PJCVb", - "tmhbSdOid8lbLJLD4TffcL5hvvBUF6VWqMjy8Rdu0wwL8H9OFqjI/VEaXaIhif4zWKtTCYTC/aLbEvmY", + "tmhbSdOid8lbLJIzw2/mI79hvvBUF6VWqMjy8Rdu0wwL8H9OFqjI/VEaXaIhif4zWKtTCYTC/aLbEvmY", "z7TOERRfJzw16AYnfulcmwKIj7kAwgHJAnnSrLFkpFrUSwQqkpD/afKW1e0MgTne0agUUUtWVybF6YFB", - "Aqr8KVFVBR9/5ErTINVKYeoOnPAVSJJqMZhrM9i6bXnC0RhteMIXQBk6gwOppBscSLVERdrc8oRX5YD0", - "wDnOG18GC62QXyeH3JmquY56W5Xirkgv0VipVcTcOuEGP1fSuLh+dOht4NhxZD9a7YC3XUraRNluvD2m", - "nv2NKTmnPNF+l9afQxIWPgL/MDjnY/7TaEvQUc3OUaDmemMLjIFb9/uVD0KHswVaCwt0fwq0qZEleRTC", - "fNYMJydAaeZdrxM+VXMD3Z0EEFjSJvzaHGZ30twgnkMJqaTbX1+2giEV4QKNP5kmyE9O8l9OxdKPdi0m", - "+37EYrMPcKYtXegVmksCqi8EIaSDE/KLnVMecrdl3VmzF2jO88oSmh3IDi7f+KKQVtrcHENaLO1KUppF", - "80dBgdGBBtbmErAESoBxPBbSTZ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kJprje2wBi4db9f+SR0arZAa2GB7k+BNjWyJI9C", + "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", } // GetSwagger returns the content of the embedded swagger specification file diff --git a/api/v1alpha1/types.gen.go b/api/v1alpha1/types.gen.go index 850ac2e..6f48ec0 100644 --- a/api/v1alpha1/types.gen.go +++ b/api/v1alpha1/types.gen.go @@ -91,12 +91,13 @@ type MigrationIssues = []struct { // Source defines model for Source. type Source struct { - Agents *[]SourceAgentItem `json:"agents,omitempty"` - CreatedAt time.Time `json:"createdAt"` - Id openapi_types.UUID `json:"id"` - Inventory *Inventory `json:"inventory,omitempty"` - Name *string `json:"name,omitempty"` - UpdatedAt time.Time `json:"updatedAt"` + Agents *[]SourceAgentItem `json:"agents,omitempty"` + CreatedAt time.Time `json:"createdAt"` + Id openapi_types.UUID `json:"id"` + Inventory *Inventory `json:"inventory,omitempty"` + Name *string `json:"name,omitempty"` + OnPremises bool `json:"onPremises"` + UpdatedAt time.Time `json:"updatedAt"` } // SourceAgentItem defines model for SourceAgentItem. diff --git a/internal/service/agent/handler.go b/internal/service/agent/handler.go index 69c4c3c..a0f0c76 100644 --- a/internal/service/agent/handler.go +++ b/internal/service/agent/handler.go @@ -64,7 +64,7 @@ func (h *AgentServiceHandler) ReplaceSourceStatus(ctx context.Context, request a associated := false if source == nil { - source, err = h.store.Source().Create(ctx, mappers.SourceFromApi(request.Id, username, orgID, nil)) + source, err = h.store.Source().Create(ctx, mappers.SourceFromApi(request.Id, username, orgID, nil, false)) if err != nil { return agentServer.ReplaceSourceStatus400JSONResponse{}, nil } @@ -90,7 +90,7 @@ func (h *AgentServiceHandler) ReplaceSourceStatus(ctx context.Context, request a return agentServer.ReplaceSourceStatus400JSONResponse{}, nil } - newSource := mappers.SourceFromApi(request.Id, username, "", &request.Body.Inventory) + newSource := mappers.SourceFromApi(request.Id, username, "", &request.Body.Inventory, false) result, err := h.store.Source().Update(ctx, newSource) if err != nil { _, _ = store.Rollback(ctx) diff --git a/internal/service/mappers/inbound.go b/internal/service/mappers/inbound.go index 56f3102..4a02a41 100644 --- a/internal/service/mappers/inbound.go +++ b/internal/service/mappers/inbound.go @@ -19,11 +19,12 @@ func AgentFromApi(username string, orgID string, resource *apiAgent.AgentStatusU } } -func SourceFromApi(id uuid.UUID, username string, orgID string, inventory *api.Inventory) model.Source { +func SourceFromApi(id uuid.UUID, username string, orgID string, inventory *api.Inventory, onPremises bool) model.Source { source := model.Source{ - ID: id, - Username: username, - OrgID: orgID, + ID: id, + Username: username, + OrgID: orgID, + OnPremises: onPremises, } if inventory != nil { diff --git a/internal/service/mappers/outbound.go b/internal/service/mappers/outbound.go index f9659f0..8cf8cff 100644 --- a/internal/service/mappers/outbound.go +++ b/internal/service/mappers/outbound.go @@ -8,10 +8,11 @@ import ( func SourceToApi(s model.Source) api.Source { source := api.Source{ - Id: s.ID, - Inventory: nil, - CreatedAt: s.CreatedAt, - UpdatedAt: s.UpdatedAt, + Id: s.ID, + Inventory: nil, + CreatedAt: s.CreatedAt, + UpdatedAt: s.UpdatedAt, + OnPremises: s.OnPremises, } if len(s.Agents) > 0 { diff --git a/internal/service/source.go b/internal/service/source.go index e4d5269..d961855 100644 --- a/internal/service/source.go +++ b/internal/service/source.go @@ -107,7 +107,15 @@ func (h *ServiceHandler) CreateSource(ctx context.Context, request server.Create return server.CreateSource500JSONResponse{}, nil } - if _, err = h.store.Source().Create(ctx, mappers.SourceFromApi(id, username, orgID, &inventory)); err != nil { + source, err := h.store.Source().Get(ctx, id) + if err == nil && source != nil { + if _, err = h.store.Source().Update(ctx, mappers.SourceFromApi(id, username, orgID, &inventory, true)); err != nil { + return server.CreateSource500JSONResponse{}, nil + } + return server.CreateSource201JSONResponse{}, nil + } + + if _, err = h.store.Source().Create(ctx, mappers.SourceFromApi(id, username, orgID, &inventory, true)); err != nil { return server.CreateSource500JSONResponse{}, nil } diff --git a/internal/service/source_test.go b/internal/service/source_test.go index 6351641..2aa669a 100644 --- a/internal/service/source_test.go +++ b/internal/service/source_test.go @@ -7,6 +7,7 @@ import ( cloudevents "github.com/cloudevents/sdk-go/v2" "github.com/google/uuid" + "github.com/kubev2v/migration-planner/api/v1alpha1" "github.com/kubev2v/migration-planner/internal/api/server" "github.com/kubev2v/migration-planner/internal/auth" "github.com/kubev2v/migration-planner/internal/config" @@ -22,6 +23,7 @@ const ( insertAgentWithSourceStm = "INSERT INTO agents (id, source_id,associated) VALUES ('%s', '%s', TRUE);" insertSourceStm = "INSERT INTO sources (id) VALUES ('%s');" insertSourceWithUsernameStm = "INSERT INTO sources (id,username, org_id) VALUES ('%s', '%s', '%s');" + insertSourceOnPremisesStm = "INSERT INTO sources (id,username, org_id, on_premises) VALUES ('%s', '%s', '%s', TRUE);" ) var _ = Describe("source handler", Ordered, func() { @@ -79,6 +81,32 @@ var _ = Describe("source handler", Ordered, func() { Expect(resp).To(HaveLen(1)) }) + It("successfully list all the sources -- on premises", func() { + tx := gormdb.Exec(fmt.Sprintf(insertSourceWithUsernameStm, uuid.NewString(), "admin", "admin")) + Expect(tx.Error).To(BeNil()) + tx = gormdb.Exec(fmt.Sprintf(insertSourceOnPremisesStm, uuid.NewString(), "admin", "admin")) + Expect(tx.Error).To(BeNil()) + + eventWriter := newTestWriter() + + user := auth.User{ + Username: "admin", + Organization: "admin", + } + ctx := auth.NewUserContext(context.TODO(), user) + + srv := service.NewServiceHandler(s, events.NewEventProducer(eventWriter)) + resp, err := srv.ListSources(ctx, server.ListSourcesRequestObject{}) + Expect(err).To(BeNil()) + Expect(reflect.TypeOf(resp)).To(Equal(reflect.TypeOf(server.ListSources200JSONResponse{}))) + Expect(resp).To(HaveLen(2)) + + count := 0 + tx = gormdb.Raw("SELECT count(*) from sources where on_premises IS TRUE;").Scan(&count) + Expect(tx.Error).To(BeNil()) + Expect(count).To(Equal(1)) + }) + AfterEach(func() { gormdb.Exec("DELETE FROM agents;") gormdb.Exec("DELETE FROM sources;") @@ -232,6 +260,40 @@ var _ = Describe("source handler", Ordered, func() { gormdb.Exec("DELETE FROM agents;") gormdb.Exec("DELETE FROM sources;") }) + + Context("create", func() { + It("successfully creates a source on prem", func() { + eventWriter := newTestWriter() + srv := service.NewServiceHandler(s, events.NewEventProducer(eventWriter)) + + resp, err := srv.CreateSource(context.TODO(), server.CreateSourceRequestObject{ + Body: &v1alpha1.SourceCreate{ + Inventory: v1alpha1.Inventory{ + Vcenter: v1alpha1.VCenter{ + Id: uuid.NewString(), + }, + }, + }, + }) + Expect(err).To(BeNil()) + Expect(reflect.TypeOf(resp)).To(Equal(reflect.TypeOf(server.CreateSource201JSONResponse{}))) + + count := 0 + tx := gormdb.Raw("SELECT count(*) from sources;").Scan(&count) + Expect(tx.Error).To(BeNil()) + Expect(count).To(Equal(1)) + + onPrem := false + tx = gormdb.Raw("SELECT on_premises from sources LIMIT 1;").Scan(&onPrem) + Expect(tx.Error).To(BeNil()) + Expect(onPrem).To(BeTrue()) + }) + }) + + AfterEach(func() { + gormdb.Exec("DELETE FROM agents;") + gormdb.Exec("DELETE FROM sources;") + }) }) }) diff --git a/internal/store/model/source.go b/internal/store/model/source.go index 14331ca..60bd347 100644 --- a/internal/store/model/source.go +++ b/internal/store/model/source.go @@ -10,14 +10,15 @@ import ( ) type Source struct { - ID openapi_types.UUID `json:"id" gorm:"primaryKey"` - Username string - OrgID string - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt gorm.DeletedAt `gorm:"index"` - Inventory *JSONField[api.Inventory] `gorm:"type:jsonb"` - Agents []Agent `gorm:"constraint:OnDelete:SET NULL;"` + ID openapi_types.UUID `json:"id" gorm:"primaryKey"` + Username string + OrgID string + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt gorm.DeletedAt `gorm:"index"` + Inventory *JSONField[api.Inventory] `gorm:"type:jsonb"` + OnPremises bool + Agents []Agent `gorm:"constraint:OnDelete:SET NULL;"` } type SourceList []Source diff --git a/internal/store/options.go b/internal/store/options.go index f28d54d..1a546ca 100644 --- a/internal/store/options.go +++ b/internal/store/options.go @@ -118,3 +118,13 @@ func (sf *SourceQueryFilter) ByDefaultInventory() *SourceQueryFilter { }) return sf } + +func (qf *SourceQueryFilter) ByOnPremises(isOnPremises bool) *SourceQueryFilter { + qf.QueryFn = append(qf.QueryFn, func(tx *gorm.DB) *gorm.DB { + if isOnPremises { + return tx.Where("on_premises IS TRUE") + } + return tx.Where("on_premises IS NOT TRUE") + }) + return qf +} diff --git a/internal/store/source_test.go b/internal/store/source_test.go index b27fd6f..7f0132b 100644 --- a/internal/store/source_test.go +++ b/internal/store/source_test.go @@ -16,6 +16,7 @@ import ( const ( insertSourceStm = "INSERT INTO sources (id) VALUES ('%s');" insertSourceWithUsernameStm = "INSERT INTO sources (id, username, org_id) VALUES ('%s', '%s', '%s');" + insertSourceOnPremisesStm = "INSERT INTO sources (id,username, org_id, on_premises) VALUES ('%s', '%s', '%s', TRUE);" ) var _ = Describe("source store", Ordered, func() { @@ -84,6 +85,23 @@ var _ = Describe("source store", Ordered, func() { Expect(sources[0].Username).To(Equal("admin")) }) + It("successfully list the source on prem", func() { + tx := gormdb.Exec(fmt.Sprintf(insertSourceWithUsernameStm, uuid.NewString(), "admin", "admin")) + Expect(tx.Error).To(BeNil()) + tx = gormdb.Exec(fmt.Sprintf(insertSourceWithUsernameStm, uuid.NewString(), "admin", "admin")) + Expect(tx.Error).To(BeNil()) + tx = gormdb.Exec(fmt.Sprintf(insertSourceOnPremisesStm, uuid.NewString(), "admin", "admin")) + Expect(tx.Error).To(BeNil()) + + sources, err := s.Source().List(context.TODO(), store.NewSourceQueryFilter().ByUsername("admin").ByOnPremises(false)) + Expect(err).To(BeNil()) + Expect(sources).To(HaveLen(2)) + + sources, err = s.Source().List(context.TODO(), store.NewSourceQueryFilter().ByUsername("admin").ByOnPremises(true)) + Expect(err).To(BeNil()) + Expect(sources).To(HaveLen(1)) + }) + AfterEach(func() { gormdb.Exec("DELETE from agents;") gormdb.Exec("DELETE from sources;")