Skip to content

Commit

Permalink
disallow switch migrate to a switch that already has machine connections
Browse files Browse the repository at this point in the history
  • Loading branch information
iljarotar committed Sep 30, 2024
1 parent bbb1e32 commit f1bae9e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/metal-api/internal/service/switch-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,10 @@ func (r *switchResource) migrate(request *restful.Request, response *restful.Res
return
}

if len(new.MachineConnections) > 0 {
r.sendError(request, response, httperrors.BadRequest(fmt.Errorf("target switch already has machine connections")))
}

s, err := adoptConfiguration(old, new)
if err != nil {
r.sendError(request, response, defaultError(err))
Expand Down
31 changes: 31 additions & 0 deletions cmd/metal-api/internal/service/switch-service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,37 @@ func TestReplaceSwitch(t *testing.T) {
require.Empty(t, result.Connections)
}

func TestSwitchMigrateConnectionsExistError(t *testing.T) {
ds, mock := datastore.InitMockDB(t)
testdata.InitMockDBData(mock)
log := slog.Default()

switchservice := NewSwitch(log, ds)
container := restful.NewContainer().Add(switchservice)

migrateRequest := v1.SwitchMigrateRequest{
OldSwitchID: testdata.Switch2.ID,
NewSwitchID: testdata.Switch1.ID,
}
js, err := json.Marshal(migrateRequest)
require.NoError(t, err)
body := bytes.NewBuffer(js)
req := httptest.NewRequest("POST", "/v1/switch/migrate", body)
req.Header.Add("Content-Type", "application/json")
container = injectEditor(log, container, req)
w := httptest.NewRecorder()
container.ServeHTTP(w, req)

resp := w.Result()
defer resp.Body.Close()
require.Equal(t, http.StatusBadRequest, resp.StatusCode, w.Body.String())
var errorResponse httperrors.HTTPErrorResponse
err = json.NewDecoder(resp.Body).Decode(&errorResponse)
require.NoError(t, err)
require.Equal(t, "target switch already has machine connections", errorResponse.Message)
require.Equal(t, http.StatusBadRequest, errorResponse.StatusCode)
}

func TestConnectMachineWithSwitches(t *testing.T) {
partitionID := "1"
s1swp1 := metal.Nic{
Expand Down

0 comments on commit f1bae9e

Please sign in to comment.