Skip to content

Commit

Permalink
chore: move resource template getter to duty
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Jan 28, 2025
1 parent 24b3793 commit be9e28a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 144 deletions.
28 changes: 0 additions & 28 deletions db/common.go

This file was deleted.

62 changes: 4 additions & 58 deletions notification/cel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"errors"
"fmt"

"github.com/flanksource/commons/logger"
"github.com/flanksource/commons/collections"
"github.com/flanksource/duty"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/types"
"github.com/flanksource/gomplate/v3"
"github.com/flanksource/incident-commander/db"
"github.com/samber/lo"
)

Expand Down Expand Up @@ -102,61 +101,8 @@ func (t *celVariables) AsMap(ctx context.Context) map[string]any {
output["new_state"] = t.NewState
}

// Placeholders for commonly used fields of the resource
// If a resource exists, they'll be filled up below
output["name"] = ""
output["status"] = ""
output["health"] = ""
output["labels"] = map[string]string{}
tags := map[string]string{}

if resource := t.SelectableResource(); resource != nil {
// set the alias name/status/health/labels/tags of the resource
output["name"] = resource.GetName()
if status, err := resource.GetStatus(); err == nil {
output["status"] = status
}
if health, err := resource.GetHealth(); err == nil {
output["health"] = health
}
if table, ok := resource.(models.TaggableModel); ok {
tags = table.GetTags()
}
if table, ok := resource.(models.LabelableModel); ok {
output["labels"] = table.GetLabels()
}
}

if ctx.DB() != nil {
if tags, err := db.GetDistinctTags(ctx); err != nil {
logger.Errorf("failed to get distinct tags for notification cel variable: %w", err)
} else {
for _, tag := range tags {
if _, ok := output[tag]; !ok {
output[tag] = ""
}
}
}
}

output["tags"] = tags

// Inject tags as top level variables
for k, v := range tags {
if !gomplate.IsValidCELIdentifier(k) {
logger.V(9).Infof("skipping tag %s as it is not a valid CEL identifier", k)
continue
}

if _, ok := output[k]; ok {
logger.V(9).Infof("skipping tag %s as it already exists in the playbook template environment", k)
continue
}

output[k] = v
}

return output
resourceContext := duty.GetResourceContext(ctx, t.SelectableResource())
return collections.MergeMap(resourceContext, output)
}

func (t *celVariables) SelectableResource() types.ResourceSelectable {
Expand Down
62 changes: 4 additions & 58 deletions playbook/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"encoding/json"
"io"

"github.com/flanksource/commons/logger"
"github.com/flanksource/commons/collections"
"github.com/flanksource/duty"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/query"
"github.com/flanksource/duty/types"
"github.com/flanksource/gomplate/v3"
"github.com/flanksource/incident-commander/db"
"github.com/labstack/echo/v4"
"github.com/samber/lo"
)
Expand Down Expand Up @@ -88,61 +87,8 @@ func (t *TemplateEnv) AsMap(ctx context.Context) map[string]any {
"request": t.Request,
}

// Placeholders for commonly used fields of the resource
// If a resource exists, they'll be filled up below
output["name"] = ""
output["status"] = ""
output["health"] = ""
output["labels"] = map[string]string{}
tags := map[string]string{}

if resource := t.SelectableResource(); resource != nil {
// set the alias name/status/health/labels/tags of the resource
output["name"] = resource.GetName()
if status, err := resource.GetStatus(); err == nil {
output["status"] = status
}
if health, err := resource.GetHealth(); err == nil {
output["health"] = health
}
if table, ok := resource.(models.TaggableModel); ok {
tags = table.GetTags()
}
if table, ok := resource.(models.LabelableModel); ok {
output["labels"] = table.GetLabels()
}
}

if ctx.DB() != nil {
if distinctTags, err := db.GetDistinctTags(ctx); err != nil {
logger.Errorf("failed to get distinct tags for notification cel variable: %w", err)
} else {
for _, tag := range distinctTags {
if _, ok := tags[tag]; !ok {
tags[tag] = ""
}
}
}
}

output["tags"] = tags

// Inject tags as top level variables
for k, v := range tags {
if !gomplate.IsValidCELIdentifier(k) {
logger.V(9).Infof("skipping tag %s as it is not a valid CEL identifier", k)
continue
}

if _, ok := output[k]; ok {
logger.V(9).Infof("skipping tag %s as it already exists in the playbook template environment", k)
continue
}

output[k] = v
}

return output
resourceContext := duty.GetResourceContext(ctx, t.SelectableResource())
return collections.MergeMap(resourceContext, output)
}

func (t *TemplateEnv) JSON(ctx context.Context) string {
Expand Down

0 comments on commit be9e28a

Please sign in to comment.