Skip to content

Commit

Permalink
fix: handle deletion of configs
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed May 20, 2024
1 parent a306fdd commit 75da805
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
13 changes: 9 additions & 4 deletions api/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,14 @@ func (e ExternalID) WhereClause(db *gorm.DB) *gorm.DB {
type ConfigDeleteReason string

var (
// DeletedReasonStale is used when a config item doesn't get updated on a scrape run
DeletedReasonStale ConfigDeleteReason = "STALE"
DeletedReasonFromAttribute ConfigDeleteReason = "FROM_ATTRIBUTE"
DeletedReasonFromEvent ConfigDeleteReason = "FROM_EVENT"
// DeletedReasonStale is used when a config item doesn't get updated on a scrape run.
DeletedReasonStale ConfigDeleteReason = "STALE"

// DeletedReasonFromAttribute is used when a deletion field (& reason)
// is picked up from the scraped config itself.
DeletedReasonFromAttribute ConfigDeleteReason = "FROM_ATTRIBUTE"

// DeletedReasonFromDeleteField is used when a deletion field (& reason)
// is picked up from the JSONPath expression provided in the scraper config.
DeletedReasonFromDeleteField ConfigDeleteReason = "FROM_DELETE_FIELD"
)
1 change: 0 additions & 1 deletion db/models/config_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ type ConfigItem struct {

ParentExternalID string `gorm:"-" json:"-"`
ParentType string `gorm:"-" json:"-"`
TouchDeletedAt bool `gorm:"-" json:"-"`
}

func (ci ConfigItem) String() string {
Expand Down
9 changes: 1 addition & 8 deletions db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,9 @@ func updateCI(ctx api.ScrapeContext, result v1.ScrapeResult, ci, existing *model
updates := make(map[string]interface{})
changes := make([]*models.ConfigChange, 0)

// In case a resource was marked as deleted but is un-deleted now
// we set an update flag as gorm ignores nil pointers
if ci.DeletedAt != existing.DeletedAt {
ci.TouchDeletedAt = true
updates["deleted_at"] = ci.DeletedAt
updates["delete_reason"] = ci.DeleteReason
}

changeResult, err := generateConfigChange(*ci, *existing)
Expand Down Expand Up @@ -178,11 +176,6 @@ func updateCI(ctx api.ScrapeContext, result v1.ScrapeResult, ci, existing *model
updates["properties"] = *ci.Properties
}

if ci.TouchDeletedAt && ci.DeleteReason != v1.DeletedReasonFromEvent {
updates["deleted_at"] = nil
updates["delete_reason"] = nil
}

if len(updates) == 0 {
return changes, nil
}
Expand Down

0 comments on commit 75da805

Please sign in to comment.