Some cleanup and safety checks
* Add logging middleware * Remove some noise from logs * Add some safety checks when managing runners
This commit is contained in:
parent
5bc2bec991
commit
3a92a5be0e
15 changed files with 384 additions and 198 deletions
|
|
@ -141,14 +141,42 @@ func (s *sqlDatabase) DeleteInstance(ctx context.Context, poolID string, instanc
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *sqlDatabase) AddInstanceStatusMessage(ctx context.Context, instanceID string, statusMessage string) error {
|
||||
func (s *sqlDatabase) ListInstanceEvents(ctx context.Context, instanceID string, eventType params.EventType, eventLevel params.EventLevel) ([]params.StatusMessage, error) {
|
||||
var events []InstanceStatusUpdate
|
||||
query := s.conn.Model(&InstanceStatusUpdate{}).Where("instance_id = ?", instanceID)
|
||||
if eventLevel != "" {
|
||||
query = query.Where("event_level = ?", eventLevel)
|
||||
}
|
||||
|
||||
if eventType != "" {
|
||||
query = query.Where("event_type = ?", eventType)
|
||||
}
|
||||
|
||||
if result := query.Find(&events); result.Error != nil {
|
||||
return nil, errors.Wrap(result.Error, "fetching events")
|
||||
}
|
||||
|
||||
eventParams := make([]params.StatusMessage, len(events))
|
||||
for idx, val := range events {
|
||||
eventParams[idx] = params.StatusMessage{
|
||||
Message: val.Message,
|
||||
EventType: val.EventType,
|
||||
EventLevel: val.EventLevel,
|
||||
}
|
||||
}
|
||||
return eventParams, nil
|
||||
}
|
||||
|
||||
func (s *sqlDatabase) AddInstanceEvent(ctx context.Context, instanceID string, event params.EventType, eventLevel params.EventLevel, statusMessage string) error {
|
||||
instance, err := s.getInstanceByID(ctx, instanceID)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "updating instance")
|
||||
}
|
||||
|
||||
msg := InstanceStatusUpdate{
|
||||
Message: statusMessage,
|
||||
Message: statusMessage,
|
||||
EventType: event,
|
||||
EventLevel: eventLevel,
|
||||
}
|
||||
|
||||
if err := s.conn.Model(&instance).Association("StatusMessages").Append(&msg); err != nil {
|
||||
|
|
@ -214,13 +242,20 @@ func (s *sqlDatabase) UpdateInstance(ctx context.Context, instanceID string, par
|
|||
}
|
||||
|
||||
func (s *sqlDatabase) ListPoolInstances(ctx context.Context, poolID string) ([]params.Instance, error) {
|
||||
pool, err := s.getPoolByID(ctx, poolID, "Tags", "Instances")
|
||||
u, err := uuid.FromString(poolID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fetching pool")
|
||||
return nil, errors.Wrap(runnerErrors.ErrBadRequest, "parsing id")
|
||||
}
|
||||
|
||||
ret := make([]params.Instance, len(pool.Instances))
|
||||
for idx, inst := range pool.Instances {
|
||||
var instances []Instance
|
||||
query := s.conn.Model(&Instance{}).Where("pool_id = ?", u)
|
||||
|
||||
if err := query.Find(&instances); err.Error != nil {
|
||||
return nil, errors.Wrap(err.Error, "fetching instances")
|
||||
}
|
||||
|
||||
ret := make([]params.Instance, len(instances))
|
||||
for idx, inst := range instances {
|
||||
ret[idx] = s.sqlToParamsInstance(inst)
|
||||
}
|
||||
return ret, nil
|
||||
|
|
|
|||
|
|
@ -343,11 +343,11 @@ func (s *InstancesTestSuite) TestDeleteInstanceDBDeleteErr() {
|
|||
s.Require().Equal("deleting instance: mocked delete instance error", err.Error())
|
||||
}
|
||||
|
||||
func (s *InstancesTestSuite) TestAddInstanceStatusMessage() {
|
||||
func (s *InstancesTestSuite) TestAddInstanceEvent() {
|
||||
storeInstance := s.Fixtures.Instances[0]
|
||||
statusMsg := "test-status-message"
|
||||
|
||||
err := s.Store.AddInstanceStatusMessage(context.Background(), storeInstance.ID, statusMsg)
|
||||
err := s.Store.AddInstanceEvent(context.Background(), storeInstance.ID, params.StatusEvent, params.EventInfo, statusMsg)
|
||||
|
||||
s.Require().Nil(err)
|
||||
instance, err := s.Store.GetInstanceByName(context.Background(), storeInstance.Name)
|
||||
|
|
@ -358,13 +358,13 @@ func (s *InstancesTestSuite) TestAddInstanceStatusMessage() {
|
|||
s.Require().Equal(statusMsg, instance.StatusMessages[0].Message)
|
||||
}
|
||||
|
||||
func (s *InstancesTestSuite) TestAddInstanceStatusMessageInvalidPoolID() {
|
||||
err := s.Store.AddInstanceStatusMessage(context.Background(), "dummy-id", "dummy-message")
|
||||
func (s *InstancesTestSuite) TestAddInstanceEventInvalidPoolID() {
|
||||
err := s.Store.AddInstanceEvent(context.Background(), "dummy-id", params.StatusEvent, params.EventInfo, "dummy-message")
|
||||
|
||||
s.Require().Equal("updating instance: parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *InstancesTestSuite) TestAddInstanceStatusMessageDBUpdateErr() {
|
||||
func (s *InstancesTestSuite) TestAddInstanceEventDBUpdateErr() {
|
||||
instance := s.Fixtures.Instances[0]
|
||||
statusMsg := "test-status-message"
|
||||
|
||||
|
|
@ -390,7 +390,7 @@ func (s *InstancesTestSuite) TestAddInstanceStatusMessageDBUpdateErr() {
|
|||
WillReturnError(fmt.Errorf("mocked add status message error"))
|
||||
s.Fixtures.SQLMock.ExpectRollback()
|
||||
|
||||
err := s.StoreSQLMocked.AddInstanceStatusMessage(context.Background(), instance.ID, statusMsg)
|
||||
err := s.StoreSQLMocked.AddInstanceEvent(context.Background(), instance.ID, params.StatusEvent, params.EventInfo, statusMsg)
|
||||
|
||||
s.assertSQLMockExpectations()
|
||||
s.Require().NotNil(err)
|
||||
|
|
@ -496,7 +496,7 @@ func (s *InstancesTestSuite) TestListPoolInstances() {
|
|||
func (s *InstancesTestSuite) TestListPoolInstancesInvalidPoolID() {
|
||||
_, err := s.Store.ListPoolInstances(context.Background(), "dummy-pool-id")
|
||||
|
||||
s.Require().Equal("fetching pool: parsing id: invalid request", err.Error())
|
||||
s.Require().Equal("parsing id: invalid request", err.Error())
|
||||
}
|
||||
|
||||
func (s *InstancesTestSuite) TestListAllInstances() {
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ package sql
|
|||
|
||||
import (
|
||||
"garm/config"
|
||||
"garm/params"
|
||||
"garm/runner/providers/common"
|
||||
"time"
|
||||
|
||||
|
|
@ -118,7 +119,9 @@ type Address struct {
|
|||
type InstanceStatusUpdate struct {
|
||||
Base
|
||||
|
||||
Message string `gorm:"type:text"`
|
||||
EventType params.EventType `gorm:"index:eventType"`
|
||||
EventLevel params.EventLevel
|
||||
Message string `gorm:"type:text"`
|
||||
|
||||
InstanceID uuid.UUID
|
||||
Instance Instance `gorm:"foreignKey:InstanceID"`
|
||||
|
|
|
|||
|
|
@ -57,8 +57,10 @@ func (s *sqlDatabase) sqlToParamsInstance(instance Instance) params.Instance {
|
|||
|
||||
for _, msg := range instance.StatusMessages {
|
||||
ret.StatusMessages = append(ret.StatusMessages, params.StatusMessage{
|
||||
CreatedAt: msg.CreatedAt,
|
||||
Message: msg.Message,
|
||||
CreatedAt: msg.CreatedAt,
|
||||
Message: msg.Message,
|
||||
EventType: msg.EventType,
|
||||
EventLevel: msg.EventLevel,
|
||||
})
|
||||
}
|
||||
return ret
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue