Add Events HTTP API

This commit is contained in:
braginini
2022-12-11 20:01:17 +01:00
parent cf27bd8951
commit f684baa665
3 changed files with 147 additions and 1 deletions

View File

@@ -18,6 +18,8 @@ tags:
description: Interact with and view information about routes.
- name: DNS
description: Interact with and view information about DNS configuration.
- name: Events
description: View information about the account and network events.
components:
schemas:
User:
@@ -501,7 +503,40 @@ components:
enum: [ "name", "description", "enabled", "groups", "nameservers", "primary", "domains" ]
required:
- path
Event:
type: object
properties:
id:
description: Event unique identifier
type: string
timestamp:
description: The date and time when the event occurred
type: string
format: date-time
operation:
description: The operation (or action) that occurred during the event
type: string
operation_code:
description: The numeric code of the operation (or action) that occurred during the event
type: integer
type:
description: The type of the event that occurred. Indicates whether it was a management or device event
type: string
enum: ["device", "management"]
initiator_id:
description: The ID of the initiator of the event. E.g., an ID of a user that triggered the event.
type: string
target_id:
description: The ID of the target of the event. E.g., an ID of the peer that a user removed.
type: string
required:
- id
- timestamp
- operation
- operation_code
- type
- initiator_id
- target_id
responses:
not_found:
description: Resource not found
@@ -1573,5 +1608,28 @@ paths:
"$ref": "#/components/responses/requires_authentication"
'403':
"$ref": "#/components/responses/forbidden"
'500':
"$ref": "#/components/responses/internal_error"
/api/events:
get:
summary: Returns a list of all events
tags: [ Events ]
security:
- BearerAuth: [ ]
responses:
'200':
description: A JSON Array of Events
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Event'
'400':
"$ref": "#/components/responses/bad_request"
'401':
"$ref": "#/components/responses/requires_authentication"
'403':
"$ref": "#/components/responses/forbidden"
'500':
"$ref": "#/components/responses/internal_error"

View File

@@ -11,6 +11,12 @@ const (
BearerAuthScopes = "BearerAuth.Scopes"
)
// Defines values for EventType.
const (
EventTypeDevice EventType = "device"
EventTypeManagement EventType = "management"
)
// Defines values for GroupPatchOperationOp.
const (
GroupPatchOperationOpAdd GroupPatchOperationOp = "add"
@@ -97,6 +103,33 @@ const (
UserStatusInvited UserStatus = "invited"
)
// Event defines model for Event.
type Event struct {
// Id Event unique identifier
Id string `json:"id"`
// InitiatorId The ID of the initiator of the event. E.g., an ID of a user that triggered the event.
InitiatorId string `json:"initiator_id"`
// Operation The operation (or action) that occurred during the event
Operation string `json:"operation"`
// OperationCode The numeric code of the operation (or action) that occurred during the event
OperationCode int `json:"operation_code"`
// TargetId The ID of the target of the event. E.g., an ID of the peer that a user removed.
TargetId string `json:"target_id"`
// Timestamp The date and time when the event occurred
Timestamp time.Time `json:"timestamp"`
// Type The type of the event that occurred. Indicates whether it was a management or device event
Type EventType `json:"type"`
}
// EventType The type of the event that occurred. Indicates whether it was a management or device event
type EventType string
// Group defines model for Group.
type Group struct {
// Id Group ID

View File

@@ -0,0 +1,55 @@
package http
import (
"fmt"
"github.com/netbirdio/netbird/management/server"
"github.com/netbirdio/netbird/management/server/event"
"github.com/netbirdio/netbird/management/server/http/api"
"github.com/netbirdio/netbird/management/server/http/util"
"github.com/netbirdio/netbird/management/server/jwtclaims"
log "github.com/sirupsen/logrus"
"net/http"
)
// Events HTTP handler
type Events struct {
accountManager server.AccountManager
authAudience string
jwtExtractor jwtclaims.ClaimsExtractor
}
// NewEvents creates a new Events HTTP handler
func NewEvents(accountManager server.AccountManager, authAudience string) *Events {
return &Events{
accountManager: accountManager,
authAudience: authAudience,
jwtExtractor: *jwtclaims.NewClaimsExtractor(nil),
}
}
// GetEvents list of the given account
func (h *Events) GetEvents(w http.ResponseWriter, r *http.Request) {
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
_, _, err := h.accountManager.GetAccountFromToken(claims)
if err != nil {
log.Error(err)
http.Redirect(w, r, "/", http.StatusInternalServerError)
return
}
var groups []*api.Event
util.WriteJSONObject(w, groups)
}
func toEventResponse(event *event.Event) *api.Event {
return &api.Event{
Id: fmt.Sprint(event.ID),
InitiatorId: event.ModifierID,
Operation: event.Operation,
OperationCode: int(event.OperationCode),
TargetId: event.TargetID,
Timestamp: event.Timestamp,
Type: event.Type,
}
}