diff --git a/fixtures/dummy/all.go b/fixtures/dummy/all.go index 7523987a..b763b789 100644 --- a/fixtures/dummy/all.go +++ b/fixtures/dummy/all.go @@ -28,6 +28,16 @@ func PopulateDBWithDummyModels(gormDB *gorm.DB) error { return err } } + for _, c := range AllDummyTeams { + if err := gormDB.Create(&c).Error; err != nil { + return err + } + } + for _, c := range AllTeamComponents { + if err := gormDB.Create(&c).Error; err != nil { + return err + } + } for _, c := range AllDummyComponentRelationships { c.UpdatedAt = createTime err = gormDB.Create(&c).Error @@ -155,6 +165,18 @@ func DeleteDummyModelsFromDB(gormDB *gorm.DB) error { return err } } + for _, c := range AllTeamComponents { + err = gormDB.Delete(&c).Error + if err != nil { + return err + } + } + for _, c := range AllDummyTeams { + err = gormDB.Delete(&c).Error + if err != nil { + return err + } + } for _, c := range AllDummyCheckComponentRelationships { err = gormDB.Where("component_id = ?", c.ComponentID).Delete(&c).Error if err != nil { diff --git a/fixtures/dummy/common.go b/fixtures/dummy/common.go index 3f5f7506..9143ddc6 100644 --- a/fixtures/dummy/common.go +++ b/fixtures/dummy/common.go @@ -7,3 +7,7 @@ var ( DummyYearOldDate = time.Now().AddDate(-1, 0, 0) ) + +func ptr[T any](t T) *T { + return &t +} diff --git a/fixtures/dummy/team_components.go b/fixtures/dummy/team_components.go new file mode 100644 index 00000000..7f94d8a6 --- /dev/null +++ b/fixtures/dummy/team_components.go @@ -0,0 +1,17 @@ +package dummy + +import "github.com/flanksource/duty/models" + +var LogisticBackendTeamComponent = models.TeamComponent{ + TeamID: Backend.ID, + ComponentID: Logistics.ID, + SelectorID: ptr("366d4ecb71d8ce12cf253e55d541f987"), +} + +var LogisticUIFrontendTeamComponent = models.TeamComponent{ + TeamID: Frontend.ID, + ComponentID: LogisticsUI.ID, + SelectorID: ptr("7fbaeebb537818e8b334fd336613f8d4 "), +} + +var AllTeamComponents = []models.TeamComponent{LogisticBackendTeamComponent, LogisticUIFrontendTeamComponent} diff --git a/fixtures/dummy/teams.go b/fixtures/dummy/teams.go new file mode 100644 index 00000000..cc7cef22 --- /dev/null +++ b/fixtures/dummy/teams.go @@ -0,0 +1,22 @@ +package dummy + +import ( + "github.com/flanksource/duty/models" + "github.com/google/uuid" +) + +var Backend = models.Team{ + ID: uuid.MustParse("3d3f49ba-93d6-4058-8acc-96233f7c5c80"), + Name: "Backend", + Spec: []byte(`{"components": [{ "name": "logistics" }]}`), + CreatedBy: JohnDoe.ID, +} + +var Frontend = models.Team{ + ID: uuid.MustParse("72d965e2-b58b-4a23-ba73-2cae0daf5981"), + Name: "Frontend", + Spec: []byte(`{"components": [{ "name": "logistics-ui" }]}`), + CreatedBy: JohnDoe.ID, +} + +var AllDummyTeams = []models.Team{Backend, Frontend} diff --git a/fixtures/expectations/topology_depth_1_root_tree.json b/fixtures/expectations/topology_depth_1_root_tree.json index fefd817c..85cceb76 100644 --- a/fixtures/expectations/topology_depth_1_root_tree.json +++ b/fixtures/expectations/topology_depth_1_root_tree.json @@ -67,7 +67,7 @@ "healthy", "unhealthy" ], - "teams": [], + "teams": ["Backend", "Frontend"], "tags": { "telemetry": [ "enabled" diff --git a/fixtures/expectations/topology_depth_2_root_tree.json b/fixtures/expectations/topology_depth_2_root_tree.json index bd0879ef..2f6ff53c 100644 --- a/fixtures/expectations/topology_depth_2_root_tree.json +++ b/fixtures/expectations/topology_depth_2_root_tree.json @@ -153,15 +153,10 @@ "updated_at": "2023-01-01T05:29:00+05:30" } ], - "healthStatuses": [ - "healthy", - "unhealthy" - ], - "teams": [], + "healthStatuses": ["healthy", "unhealthy"], + "teams": ["Backend", "Frontend"], "tags": { - "telemetry": [ - "enabled" - ] + "telemetry": ["enabled"] }, "types": [ "Application", diff --git a/fixtures/expectations/topology_root_tree.json b/fixtures/expectations/topology_root_tree.json index 05a71863..393d9b6d 100644 --- a/fixtures/expectations/topology_root_tree.json +++ b/fixtures/expectations/topology_root_tree.json @@ -362,7 +362,7 @@ "healthy", "unhealthy" ], - "teams": [], + "teams": ["Backend", "Frontend"], "tags": { "telemetry": [ "enabled" diff --git a/fixtures/expectations/topology_tree_with_label_filter.json b/fixtures/expectations/topology_tree_with_label_filter.json index 5e9a59cb..c0fb7cec 100644 --- a/fixtures/expectations/topology_tree_with_label_filter.json +++ b/fixtures/expectations/topology_tree_with_label_filter.json @@ -47,7 +47,7 @@ "healthStatuses": [ "healthy" ], - "teams": [], + "teams": ["Backend"], "tags": { "telemetry": [ "enabled" diff --git a/fixtures/expectations/topology_tree_with_owner_filter.json b/fixtures/expectations/topology_tree_with_owner_filter.json index 37ecfa4c..8c108abf 100644 --- a/fixtures/expectations/topology_tree_with_owner_filter.json +++ b/fixtures/expectations/topology_tree_with_owner_filter.json @@ -64,7 +64,7 @@ "healthStatuses": [ "healthy" ], - "teams": [], + "teams": ["Backend", "Frontend"], "tags": { "telemetry": [ "enabled" diff --git a/fixtures/expectations/topology_tree_with_status_filter.json b/fixtures/expectations/topology_tree_with_status_filter.json index 90af922d..ff054407 100644 --- a/fixtures/expectations/topology_tree_with_status_filter.json +++ b/fixtures/expectations/topology_tree_with_status_filter.json @@ -117,7 +117,7 @@ "healthy", "unhealthy" ], - "teams": [], + "teams": ["Backend", "Frontend"], "tags": { "telemetry": [ "enabled" diff --git a/fixtures/expectations/topology_tree_with_team_filter.json b/fixtures/expectations/topology_tree_with_team_filter.json new file mode 100644 index 00000000..c0fb7cec --- /dev/null +++ b/fixtures/expectations/topology_tree_with_team_filter.json @@ -0,0 +1,60 @@ +{ + "components": [ + { + "id": "018681fc-e54f-bd4f-42be-068a9a69eeb5", + "agent_id": "00000000-0000-0000-0000-000000000000", + "external_id": "dummy/logistics", + "name": "logistics", + "labels": { + "telemetry": "enabled" + }, + "status": "healthy", + "type": "Entity", + "owner": "logistics-team", + "summary": { + "healthy": 1 + }, + "is_leaf": false, + "created_at": "2023-01-01T05:29:00+05:30", + "updated_at": "2023-01-01T05:29:00+05:30", + "components": [ + { + "id": "018681fd-5770-336f-227c-259435d7fc6b", + "agent_id": "00000000-0000-0000-0000-000000000000", + "external_id": "dummy/logistics-api", + "parent_id": "018681fc-e54f-bd4f-42be-068a9a69eeb5", + "name": "logistics-api", + "labels": { + "telemetry": "enabled" + }, + "status": "healthy", + "type": "Application", + "owner": "logistics-team", + "path": "018681fc-e54f-bd4f-42be-068a9a69eeb5", + "summary": { + "healthy": 1 + }, + "is_leaf": false, + "created_at": "2023-01-01T05:29:00+05:30", + "updated_at": "2023-01-01T05:29:00+05:30", + "checks": { + "healthy": 2 + } + } + ] + } + ], + "healthStatuses": [ + "healthy" + ], + "teams": ["Backend"], + "tags": { + "telemetry": [ + "enabled" + ] + }, + "types": [ + "Application", + "Entity" + ] +} diff --git a/fixtures/expectations/topology_tree_with_type_filter.json b/fixtures/expectations/topology_tree_with_type_filter.json index 99265a8e..feaa954d 100644 --- a/fixtures/expectations/topology_tree_with_type_filter.json +++ b/fixtures/expectations/topology_tree_with_type_filter.json @@ -23,7 +23,7 @@ "healthy", "unhealthy" ], - "teams": [], + "teams": ["Backend", "Frontend"], "tags": { "telemetry": [ "enabled" diff --git a/models/teams.go b/models/teams.go new file mode 100644 index 00000000..8e1d5938 --- /dev/null +++ b/models/teams.go @@ -0,0 +1,27 @@ +package models + +import ( + "encoding/json" + "time" + + "github.com/google/uuid" +) + +type Team struct { + ID uuid.UUID `json:"id" gorm:"primaryKey"` + Name string `json:"name"` + Icon string `json:"icon,omitempty"` + Spec json.RawMessage `json:"spec,omitempty"` + Source string `json:"source,omitempty"` + CreatedBy uuid.UUID `json:"created_by"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + DeletedAt *time.Time `json:"deleted_at,omitempty"` +} + +type TeamComponent struct { + TeamID uuid.UUID `json:"team_id" gorm:"primaryKey"` + ComponentID uuid.UUID `json:"component_id" gorm:"primaryKey"` + Role *string `json:"role,omitempty"` + SelectorID *string `json:"selector_id,omitempty"` +} diff --git a/topology_test.go b/topology_test.go index 0095aa04..72c567ba 100644 --- a/topology_test.go +++ b/topology_test.go @@ -11,6 +11,7 @@ import ( "github.com/flanksource/duty/types" ginkgo "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "github.com/onsi/gomega/format" ) // For debugging @@ -44,6 +45,7 @@ func testTopologyJSON(opts TopologyOptions, path string) { } var _ = ginkgo.Describe("Topology behavior", func() { + format.MaxLength = 0 // So the diff is not truncated. ginkgo.It("Should create root tree", func() { testTopologyJSON(TopologyOptions{}, "fixtures/expectations/topology_root_tree.json") @@ -103,4 +105,8 @@ var _ = ginkgo.Describe("Topology behavior", func() { ginkgo.It("Should test tree with agent ID filter", func() { testTopologyJSON(TopologyOptions{AgentID: dummy.GCPAgent.ID.String()}, "fixtures/expectations/topology_tree_with_agent_id.json") }) + + // ginkgo.It("Should test tree with team filter", func() { + // testTopologyJSON(TopologyOptions{Team: dummy.Backend.Name}, "fixtures/expectations/topology_tree_with_team_filter.json") + // }) })