Skip to content

Commit

Permalink
api/v1: include acquire, release server conditions in composite
Browse files Browse the repository at this point in the history
update tests
  • Loading branch information
joelrebel committed Jul 3, 2024
1 parent 5b04cd8 commit c85f527
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 34 deletions.
22 changes: 20 additions & 2 deletions pkg/api/v1/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,16 @@ func TestFirmwareInstall(t *testing.T) {
AssetID: serverID,
},
mockStore: func(r *store.MockRepository) {
r.On("CreateMultiple", mock.Anything, serverID, mock.Anything, mock.Anything).
r.On(
"CreateMultiple",
mock.Anything,
serverID,
mock.Anything, // I haven't figured a way pass in a variable list of conditions to this r.On mock method
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
).
Return(nil).Once()
},
expectResponse: func() *v1types.ServerResponse {
Expand All @@ -343,7 +352,16 @@ func TestFirmwareInstall(t *testing.T) {
AssetID: serverID,
},
mockStore: func(r *store.MockRepository) {
r.On("CreateMultiple", mock.Anything, serverID, mock.Anything, mock.Anything).
r.On(
"CreateMultiple",
mock.Anything,
serverID,
mock.Anything, // I haven't figured a way pass in a variable list of conditions to this r.On mock method
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
).
Return(fmt.Errorf("%w:%s", store.ErrActiveCondition, "pound sand")).Once()
},
expectResponse: func() *v1types.ServerResponse {
Expand Down
97 changes: 65 additions & 32 deletions pkg/api/v1/routes/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,27 +313,8 @@ func (r *Routes) firmwareInstall(c *gin.Context) (int, *v1types.ServerResponse)
}
}

createTime := time.Now()

fwCondition := &rctypes.Condition{
Kind: rctypes.FirmwareInstall,
Version: rctypes.ConditionStructVersion,
Parameters: fw.MustJSON(),
State: rctypes.Pending,
FailOnCheckpointError: true,
CreatedAt: createTime,
}

invCondition := &rctypes.Condition{
Kind: rctypes.Inventory,
Version: rctypes.ConditionStructVersion,
Parameters: rctypes.MustDefaultInventoryJSON(serverID),
State: rctypes.Pending,
FailOnCheckpointError: true,
CreatedAt: createTime,
}

if err = r.repository.CreateMultiple(otelCtx, serverID, fwCondition, invCondition); err != nil {
serverConditions := r.firmwareInstallComposite(serverID, fw)
if err = r.repository.CreateMultiple(otelCtx, serverID, serverConditions.Conditions...); err != nil {
if errors.Is(err, store.ErrActiveCondition) {
return http.StatusConflict, &v1types.ServerResponse{
Message: err.Error(),
Expand All @@ -345,12 +326,13 @@ func (r *Routes) firmwareInstall(c *gin.Context) (int, *v1types.ServerResponse)
}
}

if err = r.publishCondition(otelCtx, serverID, facilityCode, fwCondition); err != nil {
r.logger.WithError(err).Warn("publishing firmware-install condition")
// mark firmwareInstall as failed
fwCondition.State = rctypes.Failed
fwCondition.Status = failedPublishStatus
if markErr := r.repository.Update(otelCtx, serverID, fwCondition); markErr != nil {
if err = r.publishCondition(otelCtx, serverID, facilityCode, serverConditions.Conditions[0], false); err != nil {
r.logger.WithField("kind", serverConditions.Conditions[0].Kind).WithError(err).Warn("error publishing condition")
// mark first condition as failed
serverConditions.Conditions[0].State = rctypes.Failed
serverConditions.Conditions[0].Status = failedPublishStatus

if markErr := r.repository.Update(otelCtx, serverID, serverConditions.Conditions[0]); markErr != nil {
// an operator is going to have to sort this out
r.logger.WithError(err).Warn("marking unpublished condition failed")
}
Expand All @@ -366,11 +348,62 @@ func (r *Routes) firmwareInstall(c *gin.Context) (int, *v1types.ServerResponse)
return http.StatusOK, &v1types.ServerResponse{
Message: "firmware install scheduled",
Records: &v1types.ConditionsResponse{
ServerID: serverID,
State: rctypes.Pending,
Conditions: []*rctypes.Condition{
fwCondition,
invCondition,
ServerID: serverID,
State: rctypes.Pending,
Conditions: serverConditions.Conditions,
},
}
}

func (r *Routes) firmwareInstallComposite(serverID uuid.UUID, fwtp rctypes.FirmwareInstallTaskParameters) *rctypes.ServerConditions {
createTime := time.Now()
return &rctypes.ServerConditions{
ServerID: serverID,
Conditions: []*rctypes.Condition{
{
Kind: rctypes.BrokerAcquireServer,
Version: rctypes.ConditionStructVersion,
Parameters: rctypes.NewBrokerTaskParameters(
serverID,
rctypes.AcquireServer,
rctypes.PurposeFirmwareInstall,
"Marked for firmware install",
).MustMarshal(),
State: rctypes.Pending,
CreatedAt: createTime,
},
{
Kind: rctypes.FirmwareInstallInband,
Version: rctypes.ConditionStructVersion,
Parameters: fwtp.MustJSON(),
State: rctypes.Pending,
CreatedAt: createTime,
},
{
Kind: rctypes.FirmwareInstall,
Version: rctypes.ConditionStructVersion,
Parameters: fwtp.MustJSON(),
State: rctypes.Pending,
CreatedAt: createTime,
},
{
Kind: rctypes.Inventory,
Version: rctypes.ConditionStructVersion,
Parameters: rctypes.MustDefaultInventoryJSON(serverID),
State: rctypes.Pending,
CreatedAt: createTime,
},
{
Kind: rctypes.BrokerReleaseServer,
Version: rctypes.ConditionStructVersion,
Parameters: rctypes.NewBrokerTaskParameters(
serverID,
rctypes.ReleaseServer,
"",
"Firmware install process completed",
).MustMarshal(),
State: rctypes.Pending,
CreatedAt: createTime,
},
},
}
Expand Down

0 comments on commit c85f527

Please sign in to comment.