Skip to content

Commit

Permalink
[Extend Governor APIs] Extensions Management (#76)
Browse files Browse the repository at this point in the history
* add extensions management

* Update pkg/api/v1alpha1/errors.go

Co-authored-by: E Camden Fisher <[email protected]>

* address review issues

---------

Co-authored-by: E Camden Fisher <[email protected]>
  • Loading branch information
bailinhe and fishnix authored Oct 5, 2023
1 parent d82fe47 commit 7f58565
Show file tree
Hide file tree
Showing 11 changed files with 1,966 additions and 20 deletions.
94 changes: 76 additions & 18 deletions internal/dbtools/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/gosimple/slug"
"github.com/volatiletech/null/v8"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/types"

"github.com/metal-toolbox/governor-api/internal/models"
)
Expand Down Expand Up @@ -39,29 +40,29 @@ func SetApplicationTypeSlug(a *models.ApplicationType) {
}

func changesetLine(set []string, key string, old, new interface{}) []string {
if old == new {
if reflect.DeepEqual(old, new) {
return set
}

var str string

if old != new {
switch o := old.(type) {
case string:
str = fmt.Sprintf(`%s: "%s" => "%s"`, key, o, new.(string))
case null.String:
str = fmt.Sprintf(`%s: "%s" => "%s"`, key, o.String, new.(null.String).String)
case int:
str = fmt.Sprintf(`%s: "%d" => "%d"`, key, o, new)
case int64:
str = fmt.Sprintf(`%s: "%d" => "%d"`, key, o, new)
case bool:
str = fmt.Sprintf(`%s: "%t" => "%t"`, key, o, new)
case time.Time:
str = fmt.Sprintf(`%s: "%s" => "%s"`, key, o.UTC().Format(time.RFC3339), new.(time.Time).UTC().Format(time.RFC3339))
default:
str = fmt.Sprintf(`%s: "%s" => "%s"`, key, o, new)
}
switch o := old.(type) {
case string:
str = fmt.Sprintf(`%s: "%s" => "%s"`, key, o, new.(string))
case null.String:
str = fmt.Sprintf(`%s: "%s" => "%s"`, key, o.String, new.(null.String).String)
case int:
str = fmt.Sprintf(`%s: "%d" => "%d"`, key, o, new)
case int64:
str = fmt.Sprintf(`%s: "%d" => "%d"`, key, o, new)
case bool:
str = fmt.Sprintf(`%s: "%t" => "%t"`, key, o, new)
case time.Time:
str = fmt.Sprintf(`%s: "%s" => "%s"`, key, o.UTC().Format(time.RFC3339), new.(time.Time).UTC().Format(time.RFC3339))
case types.JSON:
str = fmt.Sprintf(`%s: "%s" => "%s"`, key, string(o), string(new.(types.JSON)))
default:
str = fmt.Sprintf(`%s: "%s" => "%s"`, key, o, new)
}

return append(set, str)
Expand Down Expand Up @@ -1011,3 +1012,60 @@ func AuditNotificationPreferencesUpdated(ctx context.Context, exec boil.ContextE

return &event, event.Insert(ctx, exec, boil.Infer())
}

// AuditExtensionCreated inserts an event representing a extension being created
func AuditExtensionCreated(ctx context.Context, exec boil.ContextExecutor, pID string, actor *models.User, a *models.Extension) (*models.AuditEvent, error) {
// TODO non-user API actors don't exist in the governor database,
// we need to figure out how to handle that relationship in the audit table
var actorID null.String
if actor != nil {
actorID = null.StringFrom(actor.ID)
}

event := models.AuditEvent{
ParentID: null.StringFrom(pID),
ActorID: actorID,
Action: "extension.created",
Changeset: calculateChangeset(&models.Extension{}, a),
}

return &event, event.Insert(ctx, exec, boil.Infer())
}

// AuditExtensionUpdated inserts an event representing a extension being created
func AuditExtensionUpdated(ctx context.Context, exec boil.ContextExecutor, pID string, actor *models.User, o, a *models.Extension) (*models.AuditEvent, error) {
// TODO non-user API actors don't exist in the governor database,
// we need to figure out how to handle that relationship in the audit table
var actorID null.String
if actor != nil {
actorID = null.StringFrom(actor.ID)
}

event := models.AuditEvent{
ParentID: null.StringFrom(pID),
ActorID: actorID,
Action: "extension.updated",
Changeset: calculateChangeset(o, a),
}

return &event, event.Insert(ctx, exec, boil.Infer())
}

// AuditExtensionDeleted inserts an event representing an extension being deleted
func AuditExtensionDeleted(ctx context.Context, exec boil.ContextExecutor, pID string, actor *models.User, a *models.Extension) (*models.AuditEvent, error) {
// TODO non-user API actors don't exist in the governor database,
// we need to figure out how to handle that relationship in the audit table
var actorID null.String
if actor != nil {
actorID = null.StringFrom(actor.ID)
}

event := models.AuditEvent{
ParentID: null.StringFrom(pID),
ActorID: actorID,
Action: "extension.deleted",
Changeset: calculateChangeset(a, &models.Extension{}),
}

return &event, event.Insert(ctx, exec, boil.Infer())
}
3 changes: 1 addition & 2 deletions internal/eventbus/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"

"github.com/nats-io/nats.go"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
Expand Down Expand Up @@ -52,7 +51,7 @@ func NewClient(opts ...Option) *Client {
}

// WithNATSConn sets the nats connection
func WithNATSConn(nc *nats.Conn) Option {
func WithNATSConn(nc conn) Option {
return func(c *Client) {
c.conn = nc
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/api/v1alpha1/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ var (
ErrEmptyInput = errors.New("name or description cannot be empty")
// ErrUnknownRequestKind is returned a request kind is unknown
ErrUnknownRequestKind = errors.New("request kind is unrecognized")
// ErrGetDeleteResourcedWithSlug is returned when user tries to query a deleted
// resource with slug
ErrGetDeleteResourcedWithSlug = errors.New("unable to get deleted resource by slug, use the id")
// ErrExtensionNotFound is returned when an extension is not found
ErrExtensionNotFound = errors.New("extension does not exist")
// ErrERDNotFound is returned when an extension resource definition is not found
ErrERDNotFound = errors.New("ERD does not exist")
)

func sendError(c *gin.Context, code int, msg string) {
Expand Down
Loading

0 comments on commit 7f58565

Please sign in to comment.