Skip to content

Commit

Permalink
[Extend Governor APIs] System Extension Resources (#75)
Browse files Browse the repository at this point in the history
* add system extension resources

* Update pkg/api/v1alpha1/sys_extension_resources.go

Co-authored-by: Jacob See <5027680+jacobsee@users.noreply.github.com>

* use `->>` instead of `->`

---------

Co-authored-by: Jacob See <5027680+jacobsee@users.noreply.github.com>
  • Loading branch information
bailinhe and jacobsee authored Oct 19, 2023
1 parent 9cac4a8 commit c7f0dd4
Show file tree
Hide file tree
Showing 9 changed files with 2,933 additions and 9 deletions.
57 changes: 57 additions & 0 deletions internal/dbtools/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -1126,3 +1126,60 @@ func AuditExtensionResourceDefinitionDeleted(ctx context.Context, exec boil.Cont

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

// AuditSystemExtensionResourceCreated inserts an event representing an extension resource being created
func AuditSystemExtensionResourceCreated(ctx context.Context, exec boil.ContextExecutor, pID string, actor *models.User, a *models.SystemExtensionResource) (*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.resource.created",
Changeset: calculateChangeset(&models.SystemExtensionResource{}, a),
}

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

// AuditSystemExtensionResourceUpdated inserts an event representing a extension being created
func AuditSystemExtensionResourceUpdated(ctx context.Context, exec boil.ContextExecutor, pID string, actor *models.User, o, a *models.SystemExtensionResource) (*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.resource.updated",
Changeset: calculateChangeset(o, a),
}

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

// AuditSystemExtensionResourceDeleted inserts an event representing an extension being deleted
func AuditSystemExtensionResourceDeleted(ctx context.Context, exec boil.ContextExecutor, pID string, actor *models.User, a *models.SystemExtensionResource) (*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.resource.deleted",
Changeset: calculateChangeset(a, &models.SystemExtensionResource{}),
}

return &event, event.Insert(ctx, exec, boil.Infer())
}
37 changes: 36 additions & 1 deletion pkg/api/v1alpha1/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,6 @@ func (r *Router) Routes(rg *gin.RouterGroup) {
"/extensions/:eid",
r.AuditMW.AuditWithType("UpdateExtension"),
r.AuthMW.AuthRequired(updateScopesWithOpenID("governor:extensions")),
r.mwUserAuthRequired(AuthRoleAdmin),
r.updateExtension,
)

Expand Down Expand Up @@ -662,6 +661,42 @@ func (r *Router) Routes(rg *gin.RouterGroup) {
r.AuthMW.AuthRequired(deleteScopesWithOpenID("governor:extensions")),
r.deleteExtensionResourceDefinition,
)

// system-wise extension resources
rg.POST(
"/extension-resources/:ex-slug/:erd-slug-plural/:erd-version",
r.AuditMW.AuditWithType("CreateSystemExtensionResource"),
r.AuthMW.AuthRequired(createScopesWithOpenID("governor:extensionresources")),
r.createSystemExtensionResource,
)

rg.GET(
"/extension-resources/:ex-slug/:erd-slug-plural/:erd-version",
r.AuditMW.AuditWithType("ListSystemExtensionResources"),
r.AuthMW.AuthRequired(createScopesWithOpenID("governor:extensionresources")),
r.listSystemExtensionResources,
)

rg.GET(
"/extension-resources/:ex-slug/:erd-slug-plural/:erd-version/:resource-id",
r.AuditMW.AuditWithType("GetSystemExtensionResource"),
r.AuthMW.AuthRequired(createScopesWithOpenID("governor:extensionresources")),
r.getSystemExtensionResource,
)

rg.PATCH(
"/extension-resources/:ex-slug/:erd-slug-plural/:erd-version/:resource-id",
r.AuditMW.AuditWithType("UpdateSystemExtensionResource"),
r.AuthMW.AuthRequired(createScopesWithOpenID("governor:extensionresources")),
r.updateSystemExtensionResource,
)

rg.DELETE(
"/extension-resources/:ex-slug/:erd-slug-plural/:erd-version/:resource-id",
r.AuditMW.AuditWithType("DeleteSystemExtensionResource"),
r.AuthMW.AuthRequired(createScopesWithOpenID("governor:extensionresources")),
r.deleteSystemExtensionResource,
)
}

func contains(list []string, item string) bool {
Expand Down
Loading

0 comments on commit c7f0dd4

Please sign in to comment.