Skip to content

Commit

Permalink
source: Add on_prem flag to source store model
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
tupyy committed Jan 16, 2025
1 parent 5381dfc commit f631700
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 77 deletions.
44 changes: 22 additions & 22 deletions api/v1alpha1/agent/spec.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions api/v1alpha1/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ components:
format: date-time
name:
type: string
onPremises:
type: boolean
agents:
type: array
items:
Expand All @@ -369,6 +371,7 @@ components:
- id
- createdAt
- updatedAt
- onPremises

SourceCreate:
type: object
Expand Down
60 changes: 30 additions & 30 deletions api/v1alpha1/spec.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions api/v1alpha1/types.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/service/agent/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions internal/service/mappers/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions internal/service/mappers/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 9 additions & 1 deletion internal/service/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
62 changes: 62 additions & 0 deletions internal/service/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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;")
Expand Down Expand Up @@ -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;")
})
})
})

Expand Down
17 changes: 9 additions & 8 deletions internal/store/model/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions internal/store/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
18 changes: 18 additions & 0 deletions internal/store/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;")
Expand Down

0 comments on commit f631700

Please sign in to comment.