-
Notifications
You must be signed in to change notification settings - Fork 82
API to remove device #1120
base: master
Are you sure you want to change the base?
API to remove device #1120
Changes from 3 commits
17496a8
fb3f69a
11efa13
c4d3db0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package e2e | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gluster/glusterd2/pkg/api" | ||
gutils "github.com/gluster/glusterd2/pkg/utils" | ||
deviceapi "github.com/gluster/glusterd2/plugins/device/api" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func editDevice(t *testing.T) { | ||
r := require.New(t) | ||
peerList, err := client.Peers() | ||
r.Nil(err) | ||
|
||
var deviceList []deviceapi.Info | ||
var peerID string | ||
for _, peer := range peerList { | ||
deviceList, err = client.DeviceList(peer.ID.String(), "") | ||
if len(deviceList) > 0 { | ||
peerID = peer.ID.String() | ||
break | ||
} | ||
} | ||
|
||
device := deviceList[0] | ||
if device.State == "enabled" { | ||
err = client.DeviceEdit(peerID, device.Device, "disabled") | ||
r.Nil(err) | ||
} else if device.State == "disabled" { | ||
err = client.DeviceEdit(peerID, device.Device, "enabled") | ||
r.Nil(err) | ||
} | ||
newDeviceList, err := client.DeviceList(peerID, "") | ||
r.Nil(err) | ||
for _, newDevice := range newDeviceList { | ||
if newDevice.Device == device.Device { | ||
r.NotEqual(newDevice.State, device.State) | ||
} | ||
} | ||
|
||
for _, peer := range peerList { | ||
deviceList, err := client.DeviceList(peer.ID.String(), "") | ||
r.Nil(err) | ||
for _, device := range deviceList { | ||
if device.State == "enabled" { | ||
err = client.DeviceEdit(peer.ID.String(), device.Device, "disabled") | ||
r.Nil(err) | ||
} | ||
} | ||
} | ||
smartvolname := formatVolName(t.Name()) | ||
|
||
// create Distribute Replicate(2x3) Volume | ||
createReq := api.VolCreateReq{ | ||
Name: smartvolname, | ||
Size: 40 * gutils.MiB, | ||
DistributeCount: 2, | ||
ReplicaCount: 3, | ||
SubvolZonesOverlap: true, | ||
} | ||
_, err = client.VolumeCreate(createReq) | ||
r.NotNil(err) | ||
|
||
for _, peer := range peerList { | ||
deviceList, err := client.DeviceList(peer.ID.String(), "") | ||
r.Nil(err) | ||
for _, device := range deviceList { | ||
if device.State == "disabled" { | ||
err = client.DeviceEdit(peer.ID.String(), device.Device, "enabled") | ||
r.Nil(err) | ||
} | ||
} | ||
} | ||
|
||
_, err = client.VolumeCreate(createReq) | ||
r.Nil(err) | ||
|
||
r.Nil(client.VolumeDelete(smartvolname)) | ||
} | ||
|
||
func testDeviceDelete(t *testing.T) { | ||
r := require.New(t) | ||
peerList, err := client.Peers() | ||
r.Nil(err) | ||
|
||
var deviceList []deviceapi.Info | ||
var peerID string | ||
for _, peer := range peerList { | ||
deviceList, err = client.DeviceList(peer.ID.String(), "") | ||
r.Nil(err) | ||
if len(deviceList) > 0 { | ||
peerID = peer.ID.String() | ||
break | ||
} | ||
} | ||
|
||
err = client.DeviceDelete(peerID, deviceList[0].Device) | ||
r.Nil(err) | ||
|
||
newDeviceList, err := client.DeviceList(peerID, "") | ||
r.Nil(err) | ||
|
||
r.Equal(len(deviceList)-1, len(newDeviceList)) | ||
} | ||
|
||
// TestDevice creates devices in the test environment | ||
// finally deletes the devices | ||
func TestDevice(t *testing.T) { | ||
var err error | ||
|
||
r := require.New(t) | ||
|
||
tc, err := setupCluster(t, "./config/1.toml", "./config/2.toml", "./config/3.toml") | ||
r.Nil(err) | ||
defer teardownCluster(tc) | ||
|
||
client, err = initRestclient(tc.gds[0]) | ||
r.Nil(err) | ||
r.NotNil(client) | ||
|
||
devicesDir := testTempDir(t, "devices") | ||
|
||
// Device Setup | ||
// Around 150MB will be reserved during pv/vg creation, create device with more size | ||
r.Nil(prepareLoopDevice(devicesDir+"/gluster_dev1.img", "1", "250M")) | ||
r.Nil(prepareLoopDevice(devicesDir+"/gluster_dev2.img", "2", "250M")) | ||
r.Nil(prepareLoopDevice(devicesDir+"/gluster_dev3.img", "3", "250M")) | ||
|
||
_, err = client.DeviceAdd(tc.gds[0].PeerID(), "/dev/gluster_loop1") | ||
r.Nil(err) | ||
dev, err := client.DeviceList(tc.gds[0].PeerID(), "/dev/gluster_loop1") | ||
r.Nil(err) | ||
r.Equal(dev[0].Device, "/dev/gluster_loop1") | ||
|
||
_, err = client.DeviceAdd(tc.gds[1].PeerID(), "/dev/gluster_loop2") | ||
r.Nil(err) | ||
dev, err = client.DeviceList(tc.gds[1].PeerID(), "/dev/gluster_loop2") | ||
r.Nil(err) | ||
r.Equal(dev[0].Device, "/dev/gluster_loop2") | ||
|
||
_, err = client.DeviceAdd(tc.gds[2].PeerID(), "/dev/gluster_loop3") | ||
r.Nil(err) | ||
dev, err = client.DeviceList(tc.gds[2].PeerID(), "/dev/gluster_loop3") | ||
r.Nil(err) | ||
r.Equal(dev[0].Device, "/dev/gluster_loop3") | ||
|
||
t.Run("Edit device", editDevice) | ||
t.Run("Delete device", testDeviceDelete) | ||
|
||
// // Device Cleanup | ||
r.Nil(loopDevicesCleanup(t)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,8 +89,9 @@ func deviceAddHandler(w http.ResponseWriter, r *http.Request) { | |
|
||
err = txn.Do() | ||
if err != nil { | ||
logger.WithError(err).Error("Transaction to prepare device failed") | ||
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "transaction to prepare device failed") | ||
logger.WithError(err).Error("failed to add device") | ||
status, err := restutils.ErrToStatusCode(err) | ||
restutils.SendHTTPError(ctx, w, status, err) | ||
return | ||
} | ||
deviceInfo, err := deviceutils.GetDevice(peerID, req.Device) | ||
|
@@ -194,3 +195,83 @@ func deviceEditHandler(w http.ResponseWriter, r *http.Request) { | |
|
||
restutils.SendHTTPResponse(ctx, w, http.StatusOK, nil) | ||
} | ||
|
||
func deviceDeleteHandler(w http.ResponseWriter, r *http.Request) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, I missed it earlier. We need to remove the device using |
||
ctx := r.Context() | ||
logger := gdctx.GetReqLogger(ctx) | ||
peerID := mux.Vars(r)["peerid"] | ||
if uuid.Parse(peerID) == nil { | ||
restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "invalid peer-id passed in url") | ||
return | ||
} | ||
deviceName := mux.Vars(r)["device"] | ||
if deviceName == "" { | ||
restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "device name not provided in URL") | ||
return | ||
} | ||
|
||
// Adding prefix(/) to device name | ||
deviceName = "/" + deviceName | ||
|
||
txn, err := transaction.NewTxnWithLocks(ctx, peerID+deviceName) | ||
if err != nil { | ||
status, err := restutils.ErrToStatusCode(err) | ||
restutils.SendHTTPError(ctx, w, status, err) | ||
return | ||
} | ||
defer txn.Done() | ||
|
||
peerInfo, err := peer.GetPeer(peerID) | ||
if err != nil { | ||
logger.WithError(err).WithField("peerid", peerID).Error("Peer ID not found in store") | ||
if err == errors.ErrPeerNotFound { | ||
restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrPeerNotFound) | ||
} else { | ||
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "failed to get peer details from store") | ||
} | ||
return | ||
} | ||
|
||
txn.Nodes = []uuid.UUID{peerInfo.ID} | ||
txn.Steps = []*transaction.Step{ | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aravindavk don't we need to check any volume present on the device before removing it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Vgremove will fail if the device is being used. We don't wipe the device There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no harm in checking any volume present on the device, so that user will get a meaning full message something like bricks present on the device, cannot delete the device, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
DoFunc: "delete-device", | ||
Nodes: txn.Nodes, | ||
}, | ||
} | ||
|
||
err = txn.Ctx.Set("peerid", &peerID) | ||
if err != nil { | ||
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) | ||
return | ||
} | ||
|
||
err = txn.Ctx.Set("device", &deviceName) | ||
if err != nil { | ||
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) | ||
return | ||
} | ||
|
||
err = txn.Do() | ||
if err != nil { | ||
logger.WithError(err).Error("failed to delete device") | ||
status, err := restutils.ErrToStatusCode(err) | ||
restutils.SendHTTPError(ctx, w, status, err) | ||
return | ||
} | ||
|
||
restutils.SendHTTPResponse(ctx, w, http.StatusNoContent, nil) | ||
} | ||
|
||
func listAllDevicesHandler(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
logger := gdctx.GetReqLogger(ctx) | ||
devices, err := deviceutils.GetDevices() | ||
if err != nil { | ||
logger.WithError(err).Error(err) | ||
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) | ||
return | ||
} | ||
|
||
restutils.SendHTTPResponse(ctx, w, http.StatusOK, devices) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we move this after device add? even if the device remove or device list fails, these block fo code won't get hit. it's good to do cleanup in
defer()
.@aravindavk let me know your thoughts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we move all the clean up task to defer? Like deleting and stopping volume etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is also called in main_test.go, so should be fine