From 09723c5e17225191d721e2876fe5e99b5a3b8e14 Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Fri, 25 Aug 2023 01:51:37 +0530 Subject: [PATCH 1/4] generate structs Signed-off-by: MUzairS15 --- models/events/events.go | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 models/events/events.go diff --git a/models/events/events.go b/models/events/events.go new file mode 100644 index 00000000..26718f84 --- /dev/null +++ b/models/events/events.go @@ -0,0 +1,58 @@ +// Package events provides primitives to interact with the openapi HTTP API. +// +// Code generated by github.com/deepmap/oapi-codegen version v1.13.4 DO NOT EDIT. +package events + +import ( + "time" + + "github.com/google/uuid" +) + +// Defines values for EventSeverity. +const ( + Alert EventSeverity = "alert" + Critical EventSeverity = "critical" + Debug EventSeverity = "debug" + Emergency EventSeverity = "emergency" + Error EventSeverity = "error" + Informational EventSeverity = "informational" + Warning EventSeverity = "warning" +) + +// Event Defines model for event_trackers +type Event struct { + // ActedUpon UUID of the entity on which the event was performed. + ActedUpon uuid.UUID `json:"acted_upon"` + CreatedAt time.Time `json:"created_at"` + DeletedAt time.Time `json:"deleted_at"` + + // Description A summary/receipt of event that occured. + Description string `json:"description"` + + // EventType Is a composite key of resource name on which the operation is invoked and the action taken on the resource separated by ‘_’ + EventType string `json:"event_type"` + + // Id UUID of the event. + Id uuid.UUID `json:"id"` + + // Metadata Contains meaningful information, specific to the type of event. + // Structure of metadata can be different for different events. + Metadata interface{} `json:"metadata"` + + // OperationId Each Event will have a OperationID. This field is never NULL, which is to say an operation can result in series of events, for eg: Different stages of Pattern Engine / activities of Workflow engine. Each operation (and sub-operation) will have a different operation ID. + OperationId uuid.UUID `json:"operation_id"` + + // Severity A set of seven standard event levels. + Severity EventSeverity `json:"severity"` + + // SystemId The system from which the request is sourced. In the case of Meshery Server, + // the ID is meshery_instance_id of Meshery Server (which can be found in the metadata of`Connections` table). + SystemId uuid.UUID `json:"system_id"` + + // UserId UUID of the user that initiated the event. In most cases this would be present, but not always. + UserId uuid.UUID `json:"user_id,omitempty"` +} + +// EventSeverity A set of seven standard event levels. +type EventSeverity string \ No newline at end of file From 83dcd0952d500438cda2161e305b8ed1d12547a8 Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Fri, 25 Aug 2023 11:49:39 +0530 Subject: [PATCH 2/4] add builder for event Signed-off-by: MUzairS15 --- go.mod | 1 + go.sum | 2 ++ models/events/build.go | 60 +++++++++++++++++++++++++++++++++ models/events/events.go | 74 +++++++++++++++++++++++++++++------------ 4 files changed, 115 insertions(+), 22 deletions(-) create mode 100644 models/events/build.go diff --git a/go.mod b/go.mod index 5501a933..70ed3d5a 100644 --- a/go.mod +++ b/go.mod @@ -133,6 +133,7 @@ require ( github.com/go-openapi/jsonreference v0.20.1 // indirect github.com/go-openapi/swag v0.22.3 // indirect github.com/gobwas/glob v0.2.3 // indirect + github.com/gofrs/uuid v4.4.0+incompatible github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.3 // indirect diff --git a/go.sum b/go.sum index ed3feaf9..0e6608d5 100644 --- a/go.sum +++ b/go.sum @@ -551,6 +551,8 @@ github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw= github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= github.com/gofrs/uuid v3.3.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw= +github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA= +github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/googleapis v1.2.0/go.mod h1:Njal3psf3qN6dwBtQfUmBZh2ybovJ0tlu3o/AC7HYjU= github.com/gogo/googleapis v1.4.0 h1:zgVt4UpGxcqVOw97aRGxT4svlcmdK35fynLNctY32zI= github.com/gogo/googleapis v1.4.0/go.mod h1:5YRNX2z1oM5gXdAkurHa942MDgEJyk02w4OecKY87+c= diff --git a/models/events/build.go b/models/events/build.go new file mode 100644 index 00000000..37266c87 --- /dev/null +++ b/models/events/build.go @@ -0,0 +1,60 @@ +package events + +import ( + "time" + + "github.com/gofrs/uuid" +) + +type EventBuilder struct { + event Event +} + +func NewEvent() *EventBuilder { + operationId, _ := uuid.NewV4() + return &EventBuilder{ + event: Event{ + CreatedAt: time.Now(), + OperationID: operationId, + }, + } +} + +func (e *EventBuilder) ActedOn(resource uuid.UUID) *EventBuilder { + e.event.ActedUpon = resource + return e +} + +func (e *EventBuilder) Description(description string) *EventBuilder { + e.event.Description = description + return e +} + +func (e *EventBuilder) WithEventType(eventType string) *EventBuilder { + e.event.EventType = eventType + return e +} + +func (e *EventBuilder) AddMetadata(metadata interface{}) *EventBuilder { + e.event.Metadata = metadata + return e +} + +func (e *EventBuilder) WithSeverity(severity EventSeverity) *EventBuilder { + e.event.Severity = severity + return e +} + +func (e *EventBuilder) InitiatedBy(id uuid.UUID) *EventBuilder { + e.event.UserID = &id + return e +} + +func (e *EventBuilder) FromSystem(id uuid.UUID) *EventBuilder { + e.event.SystemID = id + return e +} + +func (e *EventBuilder) Build() *Event { + return &e.event +} diff --git a/models/events/events.go b/models/events/events.go index 26718f84..7d12f2dd 100644 --- a/models/events/events.go +++ b/models/events/events.go @@ -6,7 +6,7 @@ package events import ( "time" - "github.com/google/uuid" + "github.com/gofrs/uuid" ) // Defines values for EventSeverity. @@ -20,39 +20,69 @@ const ( Warning EventSeverity = "warning" ) +// CreatedAt Timestamp when the resource was created. +type CreatedAt = time.Time + +// DeletedAt Timestamp when the resource was deleted. +type DeletedAt = time.Time + // Event Defines model for event_trackers type Event struct { // ActedUpon UUID of the entity on which the event was performed. - ActedUpon uuid.UUID `json:"acted_upon"` - CreatedAt time.Time `json:"created_at"` - DeletedAt time.Time `json:"deleted_at"` + ActedUpon uuid.UUID `db:"acted_upon" json:"acted_upon"` - // Description A summary/receipt of event that occured. - Description string `json:"description"` + // CreatedAt Timestamp when the resource was created. + CreatedAt CreatedAt `db:"created_at" json:"created_at"` - // EventType Is a composite key of resource name on which the operation is invoked and the action taken on the resource separated by ‘_’ - EventType string `json:"event_type"` + // DeletedAt Timestamp when the resource was deleted. + DeletedAt *DeletedAt `db:"deleted_at" json:"deleted_at,omitempty"` - // Id UUID of the event. - Id uuid.UUID `json:"id"` + // Description A summary/receipt of event that occured. + Description string `db:"description" json:"description"` + EventType EventType `db:"event_type" json:"event_type"` + ID ID `db:"id" json:"id"` // Metadata Contains meaningful information, specific to the type of event. // Structure of metadata can be different for different events. - Metadata interface{} `json:"metadata"` - - // OperationId Each Event will have a OperationID. This field is never NULL, which is to say an operation can result in series of events, for eg: Different stages of Pattern Engine / activities of Workflow engine. Each operation (and sub-operation) will have a different operation ID. - OperationId uuid.UUID `json:"operation_id"` + Metadata interface{} `db:"metadata" json:"metadata"` + OperationID OperationID `db:"operation_id" json:"operation_id"` // Severity A set of seven standard event levels. - Severity EventSeverity `json:"severity"` - - // SystemId The system from which the request is sourced. In the case of Meshery Server, - // the ID is meshery_instance_id of Meshery Server (which can be found in the metadata of`Connections` table). - SystemId uuid.UUID `json:"system_id"` + Severity EventSeverity `db:"severity" json:"severity"` + SystemID SystemID `db:"system_id" json:"system_id"` - // UserId UUID of the user that initiated the event. In most cases this would be present, but not always. - UserId uuid.UUID `json:"user_id,omitempty"` + // UpdatedAt Timestamp when the resource was updated. + UpdatedAt UpdatedAt `db:"updated_at" json:"updated_at"` + UserID *UserID `db:"user_id" json:"user_id,omitempty"` } // EventSeverity A set of seven standard event levels. -type EventSeverity string \ No newline at end of file +type EventSeverity string + +// EventType defines model for eventType. +type EventType = string + +// ID defines model for id. +type ID = uuid.UUID + +// OperationID defines model for operation_id. +type OperationID = uuid.UUID + +// SystemID defines model for system_id. +type SystemID = uuid.UUID + +// Time defines model for time. +type Time = time.Time + +// UpdatedAt Timestamp when the resource was updated. +type UpdatedAt = time.Time + +// UserID defines model for user_uuid. +type UserID = uuid.UUID + + +// EventsFilter defines model for eventsFilter. +type EventsFilter struct { + EventType []string `json:"event_type"` + Provider []string `json:"provider"` +} \ No newline at end of file From 7991f810333993dfd7459c68aeebd4d24275c9e5 Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Mon, 28 Aug 2023 10:43:51 +0530 Subject: [PATCH 3/4] rename funcs Signed-off-by: MUzairS15 --- models/events/build.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/models/events/build.go b/models/events/build.go index 37266c87..fb8a2343 100644 --- a/models/events/build.go +++ b/models/events/build.go @@ -20,12 +20,12 @@ func NewEvent() *EventBuilder { } } -func (e *EventBuilder) ActedOn(resource uuid.UUID) *EventBuilder { +func (e *EventBuilder) ActedUpon(resource uuid.UUID) *EventBuilder { e.event.ActedUpon = resource return e } -func (e *EventBuilder) Description(description string) *EventBuilder { +func (e *EventBuilder) WithDescription(description string) *EventBuilder { e.event.Description = description return e } @@ -35,7 +35,7 @@ func (e *EventBuilder) WithEventType(eventType string) *EventBuilder { return e } -func (e *EventBuilder) AddMetadata(metadata interface{}) *EventBuilder { +func (e *EventBuilder) WithMetadata(metadata interface{}) *EventBuilder { e.event.Metadata = metadata return e } @@ -45,7 +45,7 @@ func (e *EventBuilder) WithSeverity(severity EventSeverity) *EventBuilder { return e } -func (e *EventBuilder) InitiatedBy(id uuid.UUID) *EventBuilder { +func (e *EventBuilder) FromUser(id uuid.UUID) *EventBuilder { e.event.UserID = &id return e } From ebf5cbf194d914ce441be9ea10b0d70c2f3b6e23 Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Wed, 30 Aug 2023 11:57:10 +0530 Subject: [PATCH 4/4] split event_type Signed-off-by: MUzairS15 --- models/events/build.go | 9 +++++++-- models/events/events.go | 20 +++++++++++--------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/models/events/build.go b/models/events/build.go index fb8a2343..b196938d 100644 --- a/models/events/build.go +++ b/models/events/build.go @@ -30,8 +30,13 @@ func (e *EventBuilder) WithDescription(description string) *EventBuilder { return e } -func (e *EventBuilder) WithEventType(eventType string) *EventBuilder { - e.event.EventType = eventType +func (e *EventBuilder) WithCategory(eventCategory string) *EventBuilder { + e.event.Category = eventCategory + return e +} + +func (e *EventBuilder) WithAction(eventAction string) *EventBuilder { + e.event.Action = eventAction return e } diff --git a/models/events/events.go b/models/events/events.go index 7d12f2dd..fa53cf91 100644 --- a/models/events/events.go +++ b/models/events/events.go @@ -31,6 +31,12 @@ type Event struct { // ActedUpon UUID of the entity on which the event was performed. ActedUpon uuid.UUID `db:"acted_upon" json:"acted_upon"` + // Action Action taken on the resource. + Action string `db:"action" json:"action"` + + // Category Resource name on which the operation is invoked. + Category string `db:"category" json:"category"` + // CreatedAt Timestamp when the resource was created. CreatedAt CreatedAt `db:"created_at" json:"created_at"` @@ -38,9 +44,8 @@ type Event struct { DeletedAt *DeletedAt `db:"deleted_at" json:"deleted_at,omitempty"` // Description A summary/receipt of event that occured. - Description string `db:"description" json:"description"` - EventType EventType `db:"event_type" json:"event_type"` - ID ID `db:"id" json:"id"` + Description string `db:"description" json:"description"` + ID ID `db:"id" json:"id"` // Metadata Contains meaningful information, specific to the type of event. // Structure of metadata can be different for different events. @@ -59,9 +64,6 @@ type Event struct { // EventSeverity A set of seven standard event levels. type EventSeverity string -// EventType defines model for eventType. -type EventType = string - // ID defines model for id. type ID = uuid.UUID @@ -80,9 +82,9 @@ type UpdatedAt = time.Time // UserID defines model for user_uuid. type UserID = uuid.UUID - // EventsFilter defines model for eventsFilter. type EventsFilter struct { - EventType []string `json:"event_type"` - Provider []string `json:"provider"` + Action []string `json:"action"` + Category []string `json:"category"` + Provider []string `json:"provider"` } \ No newline at end of file