Skip to content

Commit

Permalink
Fix switch state being overwritten by update. (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Aug 1, 2023
1 parent e124a3c commit 5c657fc
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 33 deletions.
26 changes: 15 additions & 11 deletions cmd/metal-api/internal/service/switch-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,33 +211,37 @@ func (r *switchResource) notifySwitch(request *restful.Request, response *restfu

id := request.PathParameter("id")

ss, err := r.ds.GetSwitchStatus(id)
if err != nil {
if !metal.IsNotFound(err) {
r.sendError(request, response, defaultError(err))
return
}

ss = &metal.SwitchStatus{
Base: metal.Base{ID: id},
}
}

sync := &metal.SwitchSync{
Time: time.Now(),
Duration: requestPayload.Duration,
}
ss := &metal.SwitchStatus{
Base: metal.Base{ID: id},
}

resp := &v1.SwitchNotifyResponse{
Common: v1.Common{Identifiable: v1.Identifiable{ID: id}},
}

if requestPayload.Error == nil {
resp.LastSync = &v1.SwitchSync{Time: sync.Time, Duration: sync.Duration}
ss.LastSync = sync
} else {
resp.LastSyncError = &v1.SwitchSync{Time: sync.Time, Duration: sync.Duration, Error: requestPayload.Error}
sync.Error = requestPayload.Error
ss.LastSyncError = sync
ss.LastSyncError.Error = requestPayload.Error
}

err = r.ds.SetSwitchStatus(ss)
if err != nil {
r.sendError(request, response, defaultError(err))
return
}
r.send(request, response, http.StatusOK, resp)

r.send(request, response, http.StatusOK, v1.NewSwitchNotifyResponse(ss))
}

func (r *switchResource) updateSwitch(request *restful.Request, response *restful.Response) {
Expand Down
63 changes: 41 additions & 22 deletions cmd/metal-api/internal/service/v1/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,8 @@ func NewSwitchResponse(s *metal.Switch, ss *metal.SwitchStatus, p *metal.Partiti
return nil
}

var lastSync *SwitchSync
if ss != nil && ss.LastSync != nil {
lastSync = &SwitchSync{
Time: ss.LastSync.Time,
Duration: ss.LastSync.Duration,
Error: ss.LastSync.Error,
}
} else {
lastSync = &SwitchSync{}
}
var lastSyncError *SwitchSync
if ss != nil && ss.LastSyncError != nil {
lastSyncError = &SwitchSync{
Time: ss.LastSyncError.Time,
Duration: ss.LastSyncError.Duration,
Error: ss.LastSyncError.Error,
}
} else {
lastSyncError = &SwitchSync{}
}
snr := NewSwitchNotifyResponse(ss)

var os *SwitchOS
if s.OS != nil {
os = &SwitchOS{
Expand Down Expand Up @@ -153,15 +135,52 @@ func NewSwitchResponse(s *metal.Switch, ss *metal.SwitchStatus, p *metal.Partiti
Nics: nics,
Partition: *NewPartitionResponse(p),
Connections: cons,
LastSync: lastSync,
LastSyncError: lastSyncError,
LastSync: snr.LastSync,
LastSyncError: snr.LastSyncError,
Timestamps: Timestamps{
Created: s.Created,
Changed: s.Changed,
},
}
}

func NewSwitchNotifyResponse(s *metal.SwitchStatus) *SwitchNotifyResponse {
if s == nil {
return &SwitchNotifyResponse{}
}

var lastSync *SwitchSync
if s.LastSync != nil {
lastSync = &SwitchSync{
Time: s.LastSync.Time,
Duration: s.LastSync.Duration,
}
}

var lastSyncError *SwitchSync
if s.LastSyncError != nil {
lastSyncError = &SwitchSync{
Time: s.LastSyncError.Time,
Duration: s.LastSyncError.Duration,
Error: s.LastSyncError.Error,
}
}

return &SwitchNotifyResponse{
Common: Common{
Identifiable: Identifiable{
ID: s.ID,
},
Describable: Describable{
Name: &s.Name,
Description: &s.Description,
},
},
LastSync: lastSync,
LastSyncError: lastSyncError,
}
}

func NewSwitch(r SwitchRegisterRequest) *metal.Switch {
nics := metal.Nics{}
for i := range r.Nics {
Expand Down
5 changes: 5 additions & 0 deletions cmd/metal-api/internal/testdata/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,11 @@ func InitMockDBData(mock *r.Mock) {
mock.On(r.DB("mockdb").Table("switch").Get("switch404")).Return(nil, errors.New("Test Error"))
mock.On(r.DB("mockdb").Table("switch").Get("switch999")).Return(nil, nil)
mock.On(r.DB("mockdb").Table("switchstatus").Get("switch999")).Return(nil, nil)
mock.On(r.DB("mockdb").Table("switchstatus").Get(Switch1.ID)).Return(metal.SwitchStatus{
Base: metal.Base{
ID: Switch1.ID,
},
}, nil)
mock.On(r.DB("mockdb").Table("wait").Get("3").Changes()).Return([]interface{}{
map[string]interface{}{"new_val": M3},
}, nil)
Expand Down

0 comments on commit 5c657fc

Please sign in to comment.