Skip to content

Commit

Permalink
add new interfaces for models that have tags and labels
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Jan 23, 2025
1 parent c279682 commit c544cb1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
10 changes: 9 additions & 1 deletion models/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ var CheckHealthStatuses = []CheckHealthStatus{
CheckStatusUnhealthy,
}

var _ types.ResourceSelectable = Check{}
// Ensure interface compliance
var (
_ types.ResourceSelectable = Check{}
_ LabelableModel = Check{}
)

type Check struct {
ID uuid.UUID `json:"id" gorm:"default:generate_ulid()"`
Expand Down Expand Up @@ -94,6 +98,10 @@ func (c Check) TableName() string {
return "checks"
}

func (t Check) GetLabels() map[string]string {
return t.Labels
}

func (c Check) ToString() string {
return fmt.Sprintf("%s-%s-%s", c.Name, c.Type, c.Description)
}
Expand Down
8 changes: 8 additions & 0 deletions models/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,11 @@ type ABACAttribute struct {
Config ConfigItem
Check Check
}

type TaggableModel interface {
GetTags() map[string]string
}

type LabelableModel interface {
GetLabels() map[string]string
}
10 changes: 10 additions & 0 deletions models/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ var AllowedColumnFieldsInComponents = []string{
"type", // Deprecated. Use resource_selector.types instead
}

// Ensure interface compliance
var (
_ types.ResourceSelectable = Component{}
_ LabelableModel = Component{}
)

type Component struct {
ID uuid.UUID `json:"id,omitempty" gorm:"default:generate_ulid()"` //nolint
TopologyID *uuid.UUID `json:"topology_id,omitempty"`
Expand Down Expand Up @@ -137,6 +143,10 @@ func (c Component) TableName() string {
return "components"
}

func (t Component) GetLabels() map[string]string {
return t.Labels
}

func DeleteAllComponents(db *gorm.DB, components ...Component) error {
ids := lo.Map(components, func(c Component, _ int) string { return c.ID.String() })
if err := db.Exec("DELETE from component_relationships WHERE component_id in (?) or relationship_id in (?)", ids, ids).Error; err != nil {
Expand Down
10 changes: 10 additions & 0 deletions models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ const (
var (
_ types.ResourceSelectable = ConfigItem{}
_ types.TagsMatchable = ConfigItem{}
_ TaggableModel = ConfigItem{}
_ LabelableModel = ConfigItem{}
)

// ConfigItem represents the config item database table
Expand Down Expand Up @@ -170,6 +172,14 @@ func (ConfigItem) TableName() string {
return "config_items"
}

func (t ConfigItem) GetTags() map[string]string {
return t.Tags
}

func (t ConfigItem) GetLabels() map[string]string {
return lo.FromPtr(t.Labels)
}

func (ci *ConfigItem) SetParent(parent *ConfigItem) {
ci.ParentID = &parent.ID
ci.Path = parent.Path + "." + ci.ID.String()
Expand Down

0 comments on commit c544cb1

Please sign in to comment.