Skip to content

Commit

Permalink
Merge pull request #262 from opensds/development
Browse files Browse the repository at this point in the history
Merge development branch into master
  • Loading branch information
leonwanghui committed Feb 9, 2018
2 parents a150f85 + 7e58cf0 commit 8af3e76
Show file tree
Hide file tree
Showing 51 changed files with 1,348 additions and 550 deletions.
4 changes: 0 additions & 4 deletions .travis.yml
Expand Up @@ -2,9 +2,6 @@ dist: trusty
sudo: required
group: deprecated-2017Q4

services:
- docker

language: go
go_import_path: github.com/opensds/opensds

Expand Down Expand Up @@ -44,7 +41,6 @@ install:
- go get github.com/mattn/goveralls
- go get github.com/wadey/gocovmerge
- make
- sudo ./test/integration/prepare.sh

script:
- ./script/CI/coverage
Expand Down
19 changes: 19 additions & 0 deletions client/volume.go
Expand Up @@ -26,6 +26,11 @@ import (
// could be discussed if it's better to define an interface.
type VolumeBuilder *model.VolumeSpec

// ExtendVolumeBuilder contains request body of handling a extend volume request.
// Currently it's assigned as the pointer of ExtendVolumeSpec struct, but it
// could be discussed if it's better to define an interface.
type ExtendVolumeBuilder *model.ExtendVolumeSpec

// VolumeAttachmentBuilder contains request body of handling a volume request.
// Currently it's assigned as the pointer of VolumeSpec struct, but it
// could be discussed if it's better to define an interface.
Expand Down Expand Up @@ -116,6 +121,20 @@ func (v *VolumeMgr) UpdateVolume(volID string, body VolumeBuilder) (*model.Volum
return &res, nil
}

// ExtendVolume ...
func (v *VolumeMgr) ExtendVolume(volID string, body ExtendVolumeBuilder) (*model.VolumeSpec, error) {
var res model.VolumeSpec
url := strings.Join([]string{
v.Endpoint,
urls.GenerateNewVolumeURL(volID + "/action")}, "/")

if err := v.Recv(request, url, "POST", body, &res); err != nil {
return nil, err
}

return &res, nil
}

// CreateVolumeAttachment
func (v *VolumeMgr) CreateVolumeAttachment(body VolumeAttachmentBuilder) (*model.VolumeAttachmentSpec, error) {
var res model.VolumeAttachmentSpec
Expand Down
30 changes: 30 additions & 0 deletions client/volume_test.go
Expand Up @@ -227,6 +227,36 @@ func TestUpdateVolume(t *testing.T) {
}
}

func TestExtendVolume(t *testing.T) {
var volID = "bd5b12a8-a101-11e7-941e-d77981b584d8"
body := model.ExtendVolumeSpec{
Extend: model.ExtendSpec{NewSize: 1},
}

result, err := fv.ExtendVolume(volID, &body)
if err != nil {
t.Error(err)
return
}

expected := &model.VolumeSpec{
BaseModel: &model.BaseModel{
Id: "bd5b12a8-a101-11e7-941e-d77981b584d8",
},
Name: "sample-volume",
Description: "This is a sample volume for testing",
Size: int64(1),
Status: "available",
PoolId: "084bf71e-a102-11e7-88a8-e31fe6d52248",
ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c",
}

if !reflect.DeepEqual(result, expected) {
t.Errorf("Expected %v, got %v", expected, result)
return
}
}

func TestCreateVolumeAttachment(t *testing.T) {
var volID = "bd5b12a8-a101-11e7-941e-d77981b584d8"
expected := &model.VolumeAttachmentSpec{
Expand Down
38 changes: 38 additions & 0 deletions contrib/drivers/ceph/ceph.go
Expand Up @@ -192,6 +192,44 @@ func (d *Driver) CreateVolume(opt *pb.CreateVolumeOpts) (*model.VolumeSpec, erro
}, nil
}

// ExtendVolume ...
func (d *Driver) ExtendVolume(opt *pb.ExtendVolumeOpts) (*model.VolumeSpec, error) {
if err := d.initConn(); err != nil {
log.Error("Connect ceph failed.")
return nil, err
}
defer d.destroyConn()

img, _, err := d.getImage(opt.GetId())
if err != nil {
log.Error("When get image:", err)
return nil, err
}

if err = img.Open(); err != nil {
log.Error("When open image:", err)
return nil, err
}
defer img.Close()

size := opt.GetSize()
if err = img.Resize(uint64(size) << sizeShiftBit); err != nil {
log.Error("When resize image:", err)
return nil, err
}
log.Info("Resize image success, volume id =", opt.GetId())

return &model.VolumeSpec{
BaseModel: &model.BaseModel{
Id: opt.GetId(),
},
Name: opt.GetName(),
Size: size,
Description: opt.GetDescription(),
AvailabilityZone: opt.GetAvailabilityZone(),
}, nil
}

func (d *Driver) getImage(volID string) (*rbd.Image, *Name, error) {
imgNames, err := rbd.GetImageNames(d.ioctx)
if err != nil {
Expand Down
38 changes: 38 additions & 0 deletions contrib/drivers/ceph/ceph_test.go
Expand Up @@ -150,6 +150,44 @@ func TestDeleteVolme(t *testing.T) {
}
}

func TestExtendVolume(t *testing.T) {
defer monkey.UnpatchAll()
monkey.Patch((*Driver).initConn, func(d *Driver) error {
return nil
})
monkey.Patch(rbd.GetImageNames, func(ioctx *rados.IOContext) (names []string, err error) {
nameList := []string{opensdsPrefix + ":volume001:7ee11866-1f40-4f3c-b093-7a3684523a19"}
return nameList, nil
})

monkey.Patch((*rbd.Image).GetSize, func(r *rbd.Image) (size uint64, err error) {
return 1 << sizeShiftBit, nil
})
monkey.Patch((*rbd.Image).Remove, func(r *rbd.Image) error {
return nil
})
monkey.Patch((*rados.Conn).Shutdown, func(c *rados.Conn) {})
monkey.Patch((*rados.IOContext).Destroy, func(ioctx *rados.IOContext) {})
monkey.Patch((*rbd.Image).Resize, func(r *rbd.Image, size uint64) error {
return nil
})
monkey.Patch((*rbd.Image).Open, func(r *rbd.Image, args ...interface{}) error {
return nil
})

d := Driver{}
opt := &pb.ExtendVolumeOpts{Name: "volume001", Id: "7ee11866-1f40-4f3c-b093-7a3684523a19", Size: 123}
resp, err := d.ExtendVolume(opt)

if err != nil {
t.Errorf("Test Extend volume error")
}

if resp.Size != 123 {
t.Errorf("Test Extend volume size error")
}
}

func TestCreateSnapshot(t *testing.T) {
defer monkey.UnpatchAll()
monkey.Patch((*Driver).initConn, func(d *Driver) error {
Expand Down
2 changes: 2 additions & 0 deletions contrib/drivers/drivers.go
Expand Up @@ -45,6 +45,8 @@ type VolumeDriver interface {

DeleteVolume(opt *pb.DeleteVolumeOpts) error

ExtendVolume(opt *pb.ExtendVolumeOpts) (*model.VolumeSpec, error)

InitializeConnection(opt *pb.CreateAttachmentOpts) (*model.ConnectionInfo, error)

TerminateConnection(opt *pb.DeleteAttachmentOpts) error
Expand Down
11 changes: 11 additions & 0 deletions contrib/drivers/huawei/dorado/client.go
Expand Up @@ -232,6 +232,17 @@ func (c *DoradoClient) DeleteVolume(id string) error {
return err
}

// ExtendVolume ...
func (c *DoradoClient) ExtendVolume(capacity int64, id string) error {
data := map[string]interface{}{
"CAPACITY": capacity,
"ID": id,
}

err := c.request("PUT", "/lun/expand", data, nil)
return err
}

func (c *DoradoClient) CreateSnapshot(volId, name, desc string) (*Snapshot, error) {
data := map[string]interface{}{
"PARENTTYPE": 11,
Expand Down
21 changes: 21 additions & 0 deletions contrib/drivers/huawei/dorado/dorado.go
Expand Up @@ -135,6 +135,27 @@ func (d *Driver) DeleteVolume(opt *pb.DeleteVolumeOpts) error {
return nil
}

// ExtendVolume ...
func (d *Driver) ExtendVolume(opt *pb.ExtendVolumeOpts) (*model.VolumeSpec, error) {
//Convert the storage unit Giga to sector
err := d.client.ExtendVolume(d.gb2Sector(opt.GetSize()), opt.GetId())
if err != nil {
log.Error("Extend Volume Failed:", err)
return nil, err
}

log.Infof("Extend volume %s (%s) success.", opt.GetName(), opt.GetId())
return &model.VolumeSpec{
BaseModel: &model.BaseModel{
Id: opt.GetId(),
},
Name: opt.GetName(),
Size: opt.GetSize(),
Description: opt.GetDescription(),
AvailabilityZone: opt.GetAvailabilityZone(),
}, nil
}

func (d *Driver) getTargetInfo() (string, string, error) {
tgtIp := d.conf.TargetIp
resp, err := d.client.ListTgtPort()
Expand Down
33 changes: 32 additions & 1 deletion contrib/drivers/lvm/lvm.go
Expand Up @@ -145,6 +145,36 @@ func (d *Driver) DeleteVolume(opt *pb.DeleteVolumeOpts) error {
return nil
}

// ExtendVolume ...
func (d *Driver) ExtendVolume(opt *pb.ExtendVolumeOpts) (*model.VolumeSpec, error) {
lvPath, ok := opt.GetMetadata()["lvPath"]
if !ok {
err := errors.New("failed to find logic volume path in volume metadata")
log.Error(err)
return nil, err
}

var size = fmt.Sprint(opt.GetSize()) + "G"

if _, err := d.handler("lvresize", []string{
"-L", size,
lvPath,
}); err != nil {
log.Error("Failed to extend logic volume:", err)
return nil, err
}

return &model.VolumeSpec{
BaseModel: &model.BaseModel{
Id: opt.GetId(),
},
Name: opt.GetName(),
Size: opt.GetSize(),
Description: opt.GetDescription(),
Metadata: opt.GetMetadata(),
}, nil
}

func (d *Driver) InitializeConnection(opt *pb.CreateAttachmentOpts) (*model.ConnectionInfo, error) {
var initiator string
if initiator = opt.HostInfo.GetInitiator(); initiator == "" {
Expand Down Expand Up @@ -292,7 +322,6 @@ func (d *Driver) getVGList() (*[]VolumeGroup, error) {
return nil, err
}

log.Info("Got vgs info:", info)
lines := strings.Split(info, "\n")
vgs := make([]VolumeGroup, len(lines)/vgInfoLineCount)

Expand Down Expand Up @@ -358,10 +387,12 @@ func (*Driver) buildPoolParam(proper PoolProperties) *map[string]interface{} {
}

func execCmd(script string, cmd []string) (string, error) {
log.Infof("Command: %s %s", script, strings.Join(cmd, " "))
ret, err := exec.Command(script, cmd...).Output()
if err != nil {
log.Error(err.Error())
return "", err
}
log.Infof("Command Result:\n%s", string(ret))
return string(ret), nil
}
29 changes: 29 additions & 0 deletions contrib/drivers/lvm/lvm_test.go
Expand Up @@ -73,6 +73,8 @@ func fakeHandler(script string, cmd []string) (string, error) {
return string(sampleLV), nil
case "lvremove":
return "", nil
case "lvresize":
return "", nil
case "vgdisplay":
return string(sampleVG), nil
default:
Expand Down Expand Up @@ -134,6 +136,33 @@ func TestDeleteVolume(t *testing.T) {
}
}

func TestExtendVolume(t *testing.T) {
opt := &pb.ExtendVolumeOpts{
Metadata: map[string]string{
"lvPath": "/dev/vg001/test001",
},
Size: int64(1),
}

vol, err := fd.ExtendVolume(opt)
if err != nil {
t.Error("Failed to extend volume:", err)
}

if vol.Size != 1 {
t.Errorf("Expected %+v, got %+v\n", 1, vol.Size)
}

opt = &pb.ExtendVolumeOpts{
Size: int64(1),
}

vol, err = fd.ExtendVolume(opt)
if err.Error() != "failed to find logic volume path in volume metadata" {
t.Error("Error strings is not the same as expected:", err)
}
}

func TestCreateSnapshot(t *testing.T) {
opt := &pb.CreateVolumeSnapshotOpts{
Name: "snap001",
Expand Down
24 changes: 24 additions & 0 deletions contrib/drivers/openstack/cinder/cinder.go
Expand Up @@ -214,6 +214,30 @@ func (d *Driver) DeleteVolume(opt *pb.DeleteVolumeOpts) error {
return nil
}

// ExtendVolume ...
func (d *Driver) ExtendVolume(req *pb.ExtendVolumeOpts) (*model.VolumeSpec, error) {
//Configure create request body.
opts := &volumeactions.ExtendSizeOpts{
NewSize: int(req.GetSize()),
}

err := volumeactions.ExtendSize(d.blockStoragev2, req.GetId(), opts).ExtractErr()
if err != nil {
log.Error("Cannot extend volume:", err)
return nil, err
}

return &model.VolumeSpec{
BaseModel: &model.BaseModel{
Id: req.GetId(),
},
Name: req.GetName(),
Description: req.GetDescription(),
Size: int64(req.GetSize()),
AvailabilityZone: req.GetAvailabilityZone(),
}, nil
}

// InitializeConnection
func (d *Driver) InitializeConnection(req *pb.CreateAttachmentOpts) (*model.ConnectionInfo, error) {
opts := &volumeactions.InitializeConnectionOpts{
Expand Down

0 comments on commit 8af3e76

Please sign in to comment.