Skip to content

Commit

Permalink
size suggest implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
m1kepeter committed Sep 26, 2023
1 parent abaf74b commit 994030e
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 19 deletions.
47 changes: 43 additions & 4 deletions cmd/metal-api/internal/service/size-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ func (r *sizeResource) webService() *restful.WebService {
DefaultReturns("Error", httperrors.HTTPErrorResponse{}))

ws.Route(ws.POST("/suggest").
To(r.fromHardware).
To(r.suggestSize).
Operation("suggest").
Doc("Searches all sizes for one to match the given hardwarespecs. If nothing is found, a list of entries is returned which describe the constraint which did not match").
Doc("from a given machineID returns the appropiate size").
Metadata(restfulspec.KeyOpenAPITags, tags).
Metadata(auditing.Exclude, true).
Reads(v1.MachineHardware{}).
Returns(http.StatusOK, "OK", v1.SizeMatchingLog{}).
Reads(v1.SizeSuggestRequest{}).
Returns(http.StatusOK, "OK", []v1.SizeConstraint{}).
DefaultReturns("Error", httperrors.HTTPErrorResponse{}))

ws.Route(ws.DELETE("/{id}").
Expand Down Expand Up @@ -124,6 +124,45 @@ func (r *sizeResource) findSize(request *restful.Request, response *restful.Resp
r.send(request, response, http.StatusOK, v1.NewSizeResponse(s))
}

func (r *sizeResource) suggestSize(request *restful.Request, response *restful.Response) {
var requestPayload v1.SizeSuggestRequest
err := request.ReadEntity(&requestPayload)
if err != nil {
r.sendError(request, response, httperrors.BadRequest(err))
return
}

if requestPayload.MachineID == "" {
r.sendError(request, response, httperrors.BadRequest(fmt.Errorf("machineID must be given")))
return
}

m, err := r.ds.FindMachineByID(requestPayload.MachineID)
if err != nil {
r.sendError(request, response, defaultError(err))
return
}

r.send(request, response, http.StatusOK, []v1.SizeConstraint{
{
Type: metal.CoreConstraint,
Min: uint64(m.Hardware.CPUCores),
Max: uint64(m.Hardware.CPUCores),
},
{
Type: metal.MemoryConstraint,
Min: m.Hardware.Memory,
Max: m.Hardware.Memory,
},
{
Type: metal.StorageConstraint,
Min: m.Hardware.DiskCapacity(),
Max: m.Hardware.DiskCapacity(),
},
})

}

func (r *sizeResource) listSizes(request *restful.Request, response *restful.Response) {
ss, err := r.ds.ListSizes()
if err != nil {
Expand Down
38 changes: 26 additions & 12 deletions cmd/metal-api/internal/service/size-service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
v1 "github.com/metal-stack/metal-api/cmd/metal-api/internal/service/v1"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/testdata"
"github.com/metal-stack/metal-lib/httperrors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"

Expand Down Expand Up @@ -75,15 +76,6 @@ func TestSuggest(t *testing.T) {
testdata.InitMockDBData(mock)

createRequest := v1.SizeSuggestRequest{
Common: v1.Common{
Identifiable: v1.Identifiable{
ID: testdata.Sz1.ID,
},
Describable: v1.Describable{
Name: &testdata.Sz1.Name,
Description: &testdata.Sz1.Description,
},
},
MachineID: "1",
}
js, err := json.Marshal(createRequest)
Expand All @@ -92,16 +84,38 @@ func TestSuggest(t *testing.T) {

sizeservice := NewSize(zaptest.NewLogger(t).Sugar(), ds)
container := restful.NewContainer().Add(sizeservice)
req := httptest.NewRequest("POST", "/v1/size/suggest", nil)
req := httptest.NewRequest("POST", "/v1/size/suggest", body)
req.Header.Add("Content-Type", "application/json")
w := httptest.NewRecorder()
container.ServeHTTP(w, req)

resp := w.Result()
defer resp.Body.Close()
require.Equal(t, http.StatusOK, resp.StatusCode, w.Body.String())
var result []v1.SizeConstraint
err = json.NewDecoder(resp.Body).Decode(&result)
require.NoError(t, err)

require.Len(t, result, 3)

assert.Contains(t, result, v1.SizeConstraint{
Type: metal.MemoryConstraint,
Min: 1 << 30,
Max: 1 << 30,
})

assert.Contains(t, result, v1.SizeConstraint{
Type: metal.CoreConstraint,
Min: 8,
Max: 8,
})

assert.Contains(t, result, v1.SizeConstraint{
Type: metal.StorageConstraint,
Min: 3000,
Max: 3000,
})

var result httperrors.HTTPErrorResponse
err := json.NewDecoder(resp.Body).Decode(&result)
}

func TestGetSizeNotFound(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions cmd/metal-api/internal/service/v1/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ type SizeResponse struct {
}

type SizeSuggestRequest struct {
Common
MachineHardware []MachineHardwareBase `json:"type" hardware:"???" enum:"cores|memory|storage" description:"the type of the machine"`
SizeConstraints []SizeConstraint `json:"hardware" description:"a list of constraints that defines this size"`
MachineID string `json:"machineID" description:"machineID to retrieve size suggestion for"`
}

type SizeConstraintMatchingLog struct {
Expand Down
12 changes: 12 additions & 0 deletions cmd/metal-api/internal/testdata/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ var (
},
Hardware: metal.MachineHardware{
CPUCores: 8,
Memory: 1 << 30,
Disks: []metal.BlockDevice{
{
Size: 1000,
},
{
Size: 1000,
},
{
Size: 1000,
},
},
},

IPMI: IPMI1,
Expand Down
53 changes: 53 additions & 0 deletions spec/metal-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -4248,6 +4248,17 @@
"id"
]
},
"v1.SizeSuggestRequest": {
"properties": {
"machineID": {
"description": "machineID to retrieve size suggestion for",
"type": "string"
}
},
"required": [
"machineID"
]
},
"v1.SizeUpdateRequest": {
"properties": {
"constraints": {
Expand Down Expand Up @@ -8481,6 +8492,48 @@
]
}
},
"/v1/size/suggest": {
"post": {
"consumes": [
"application/json"
],
"operationId": "suggest",
"parameters": [
{
"in": "body",
"name": "body",
"required": true,
"schema": {
"$ref": "#/definitions/v1.SizeSuggestRequest"
}
}
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"items": {
"$ref": "#/definitions/v1.SizeConstraint"
},
"type": "array"
}
},
"default": {
"description": "Error",
"schema": {
"$ref": "#/definitions/httperrors.HTTPErrorResponse"
}
}
},
"summary": "from a given machineID returns the appropiate size",
"tags": [
"size"
]
}
},
"/v1/size/{id}": {
"delete": {
"consumes": [
Expand Down

0 comments on commit 994030e

Please sign in to comment.