Skip to content

Commit

Permalink
implement machine deletion. #153 (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
muhittink authored Jan 13, 2021
1 parent a7e5468 commit 24fc80d
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
70 changes: 70 additions & 0 deletions cmd/metal-api/internal/service/machine-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ func (r machineResource) webService() *restful.WebService {
Returns(http.StatusOK, "OK", v1.MachineResponse{}).
DefaultReturns("Error", httperrors.HTTPErrorResponse{}))

ws.Route(ws.DELETE("/{id}").
To(admin(r.deleteMachine)).
Operation("deleteMachine").
Doc("deletes a machine from the database").
Param(ws.PathParameter("id", "identifier of the machine").DataType("string")).
Metadata(restfulspec.KeyOpenAPITags, tags).
Writes(v1.MachineResponse{}).
Returns(http.StatusOK, "OK", v1.MachineResponse{}).
DefaultReturns("Error", httperrors.HTTPErrorResponse{}))

ws.Route(ws.POST("/ipmi").
To(editor(r.ipmiReport)).
Operation("ipmiReport").
Expand Down Expand Up @@ -1486,6 +1496,66 @@ func (r machineResource) freeMachine(request *restful.Request, response *restful
}
}

func (r machineResource) deleteMachine(request *restful.Request, response *restful.Response) {
id := request.PathParameter("id")
m, err := r.ds.FindMachineByID(id)
if checkError(request, response, utils.CurrentFuncName(), err) {
return
}
logger := utils.Logger(request).Sugar()

if m.Allocation != nil {
checkError(request, response, utils.CurrentFuncName(), fmt.Errorf("cannot delete machine that is allocated"))
return
}

ec, err := r.ds.FindProvisioningEventContainer(id)

if checkError(request, response, utils.CurrentFuncName(), err) {
return
}

if !ec.Liveliness.Is(string(metal.MachineLivelinessDead)) {
checkError(request, response, utils.CurrentFuncName(), fmt.Errorf("can only delete dead machines"))
return
}

switches, err := r.ds.SearchSwitchesConnectedToMachine(m)
if checkError(request, response, utils.CurrentFuncName(), err) {
return
}

for _, old := range switches {
_, ok := old.MachineConnections[m.ID]
if !ok {
continue
}

new := old
new.MachineConnections = metal.ConnectionMap{}

for id, connection := range old.MachineConnections {
new.MachineConnections[id] = connection
}
delete(new.MachineConnections, m.ID)

err = r.ds.UpdateSwitch(&old, &new)
if checkError(request, response, utils.CurrentFuncName(), err) {
return
}
}

err = r.ds.DeleteMachine(m)
if checkError(request, response, utils.CurrentFuncName(), err) {
return
}

err = response.WriteHeaderAndEntity(http.StatusOK, makeMachineResponse(m, r.ds, logger))
if err != nil {
logger.Error("Failed to send response", zap.Error(err))
}
}

// reinstallMachine reinstalls the requested machine with given image by either allocating
// the machine if not yet allocated or not modifying any other allocation parameter than 'ImageID'
// and 'Reinstall' set to true.
Expand Down
36 changes: 36 additions & 0 deletions spec/metal-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -3962,6 +3962,42 @@
}
},
"/v1/machine/{id}": {
"delete": {
"consumes": [
"application/json"
],
"operationId": "deleteMachine",
"parameters": [
{
"description": "identifier of the machine",
"in": "path",
"name": "id",
"required": true,
"type": "string"
}
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/v1.MachineResponse"
}
},
"default": {
"description": "Error",
"schema": {
"$ref": "#/definitions/httperrors.HTTPErrorResponse"
}
}
},
"summary": "deletes a machine from the database",
"tags": [
"machine"
]
},
"get": {
"consumes": [
"application/json"
Expand Down

0 comments on commit 24fc80d

Please sign in to comment.