Skip to content

Commit

Permalink
feat: add events API endpoint interaction (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
sachin120 authored Jan 24, 2024
1 parent 156185c commit adb547d
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 0 deletions.
8 changes: 8 additions & 0 deletions api/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package api

import "github.com/maas/gomaasclient/entity"

// Events is an interface for node events
type Events interface {
Get(params *entity.EventParams) (*entity.EventsResp, error)
}
2 changes: 2 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func constructClient(apiClient *APIClient) *Client {
DNSResources: &DNSResources{APIClient: *apiClient},
DNSResourceRecord: &DNSResourceRecord{APIClient: *apiClient},
DNSResourceRecords: &DNSResourceRecords{APIClient: *apiClient},
Events: &Events{APIClient: *apiClient},
Fabric: &Fabric{APIClient: *apiClient},
Fabrics: &Fabrics{APIClient: *apiClient},
VLAN: &VLAN{APIClient: *apiClient},
Expand Down Expand Up @@ -105,6 +106,7 @@ type Client struct {
DNSResourceRecords api.DNSResourceRecords
Domain api.Domain
Domains api.Domains
Events api.Events
Fabric api.Fabric
Fabrics api.Fabrics
VLAN api.VLAN
Expand Down
32 changes: 32 additions & 0 deletions client/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package client

import (
"encoding/json"

"github.com/google/go-querystring/query"
"github.com/maas/gomaasclient/entity"
)

// Events implements api.Events
type Events struct {
APIClient APIClient
}

func (e *Events) client() APIClient {
return e.APIClient.GetSubObject("events")
}

// Get events for nodes
func (e *Events) Get(params *entity.EventParams) (*entity.EventsResp, error) {
qsp, err := query.Values(params)
if err != nil {
return nil, err
}

events := &entity.EventsResp{}
err = e.client().Get("query", qsp, func(data []byte) error {
return json.Unmarshal(data, events)
})

return events, err
}
2 changes: 2 additions & 0 deletions entity/event/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package event contains entities related to events.
package event
14 changes: 14 additions & 0 deletions entity/event/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package event

// LogLevel correlates to a log level for MAAS events.
type LogLevel string

// Log levels referring from MAAS server https://github.com/maas/maas/blob/master/src/maasserver/models/eventtype.py#L25
const (
AUDIT LogLevel = "AUDIT"
CRITICAL LogLevel = "CRITICAL"
DEBUG LogLevel = "DEBUG"
ERROR LogLevel = "ERROR"
WARNING LogLevel = "WARNING"
INFO LogLevel = "INFO"
)
37 changes: 37 additions & 0 deletions entity/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package entity

import "github.com/maas/gomaasclient/entity/event"

// Event represents an event for MAAS node
type Event struct {
UserName string `json:"username,omitempty"`
Node string `json:"node,omitempty"`
Hostname string `json:"hostname,omitempty"`
Created string `json:"created,omitempty"`
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
Level event.LogLevel `json:"level,omitempty"`
ID int `json:"id,omitempty"`
}

// EventsResp represents the MAAS Events endpoint
type EventsResp struct {
NextURI string `json:"next_uri,omitempty"`
PrevURI string `json:"prev_uri,omitempty"`
Events []Event `json:"events,omitempty"`
Count int `json:"count,omitempty"`
}

// EventParams enumerates the parameters for the event get operation
type EventParams struct {
Hostname string `url:"hostname,omitempty"`
MACAddress string `url:"mac_address,omitempty"`
ID string `url:"id,omitempty"`
Zone string `url:"zone,omitempty"`
AgentName string `url:"agent_name,omitempty"`
Limit string `url:"limit,omitempty"`
Before string `url:"before,omitempty"`
After string `url:"after,omitempty"`
Owner string `url:"owner,omitempty"`
Level event.LogLevel `url:"level,omitempty"`
}

0 comments on commit adb547d

Please sign in to comment.