Skip to content

Commit

Permalink
Adding profile to CreateFileShareAcl and snapshot issue (#794)
Browse files Browse the repository at this point in the history
* Adding profile to CreateFileShareAcl and snapshot issue

* Dock and controller code for deleteaccess api
  • Loading branch information
PravinRanjan10 authored and leonwanghui committed May 30, 2019
1 parent 81e6694 commit 2513f02
Show file tree
Hide file tree
Showing 16 changed files with 491 additions and 20 deletions.
2 changes: 2 additions & 0 deletions contrib/drivers/filesharedrivers/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type FileShareDriver interface {

CreateFileShareAcl(opt *pb.CreateFileShareAclOpts) (*model.FileShareAclSpec, error)

DeleteFileShareAcl(opt *pb.DeleteFileShareAclOpts) (*model.FileShareAclSpec, error)

ListPools() ([]*model.StoragePoolSpec, error)

DeleteFileShare(opts *pb.DeleteFileShareOpts) (*model.FileShareSpec, error)
Expand Down
25 changes: 25 additions & 0 deletions contrib/drivers/filesharedrivers/nfs/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ func (c *Cli) CreateAccess(accessto, accesscapability, fname string) error {
return err
}

func (c *Cli) DeleteAccess(accessto, fname string) error {
var accesstoAndMount string
sharePath := path.Join("var/", fname)
accesstoAndMount = fmt.Sprintf("%s:/%s", accessto, strings.Replace(sharePath, "-", "_", -1))
cmd := []string{
"env", "LC_ALL=C",
"exportfs",
"-u",
accesstoAndMount,
}
_, err := c.execute(cmd...)

return err
}

func (c *Cli) UnMount(dirName string) error {
cmd := []string{
"env", "LC_ALL=C",
Expand Down Expand Up @@ -106,6 +121,16 @@ func (c *Cli) CreateDirectory(dirName string) error {
return err
}

func (c *Cli) DeleteDirectory(dirName string) error {
cmd := []string{
"env", "LC_ALL=C",
"rm", "-rf",
dirName,
}
_, err := c.execute(cmd...)
return err
}

func (c *Cli) SetPermission(dirName string) error {
cmd := []string{
"env", "LC_ALL=C",
Expand Down
45 changes: 32 additions & 13 deletions contrib/drivers/filesharedrivers/nfs/nfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ const (
)

const (
KLvPath = "lvPath"
KLvsPath = "lvsPath"
KFileshareName = "nfsFileshareName"
KFileshareID = "nfsFileshareID"
AccessLevelRo = "ro"
AccessLevelRw = "rw"
KLvPath = "lvPath"
KLvsPath = "lvsPath"
KFileshareName = "nfsFileshareName"
KFileshareID = "nfsFileshareID"
KFileshareSnapName = "snapshotName"
KFileshareSnapID = "snapshotID"
AccessLevelRo = "ro"
AccessLevelRw = "rw"
)

type NFSConfig struct {
Expand Down Expand Up @@ -91,7 +93,7 @@ func (d *Driver) CreateFileShareAcl(opt *pb.CreateFileShareAclOpts) (fshare *mod
// get accessCapability list
accessCapability = opt.GetAccessCapability()
// get fileshare name
fname := opt.GetName()
fname := opt.GetMetadata()[KFileshareName]

for _, value := range accessCapability {
if value == "w" {
Expand All @@ -108,6 +110,19 @@ func (d *Driver) CreateFileShareAcl(opt *pb.CreateFileShareAclOpts) (fshare *mod
return fshare, nil
}

func (d *Driver) DeleteFileShareAcl(opt *pb.DeleteFileShareAclOpts) (fshare *model.FileShareAclSpec, err error) {
var accessTo []string
// Get accessto list
accessTo = opt.GetAccessTo()
// get fileshare name
fname := opt.GetMetadata()[KFileshareName]

for _, server := range accessTo {
err = d.cli.DeleteAccess(server, fname)
}
return fshare, nil
}

func (d *Driver) CreateFileShare(opt *pb.CreateFileShareOpts) (fshare *model.FileShareSpec, err error) {
//get the server ip for configuration
var server = d.conf.TgtBindIp
Expand Down Expand Up @@ -233,7 +248,11 @@ func (d *Driver) DeleteFileShare(opt *pb.DeleteFileShareOpts) (fshare *model.Fil
log.Error("failed to remove logic volume:", err)
return fshare, err
}

// Delete the directory
if err := d.cli.DeleteDirectory(dirName); err != nil {
log.Error("failed to delete the directory:", err)
return nil, err
}
return fshare, nil
}

Expand All @@ -246,7 +265,6 @@ func (d *Driver) CreateFileShareSnapshot(opt *pb.CreateFileShareSnapshotOpts) (*
return nil, err
}
snapName := opt.GetName()

fields := strings.Split(lvPath, "/")

vg, sourceLvName := fields[2], fields[3]
Expand All @@ -263,23 +281,24 @@ func (d *Driver) CreateFileShareSnapshot(opt *pb.CreateFileShareSnapshotOpts) (*
SnapshotSize: opt.GetSize(),
Description: opt.GetDescription(),
Metadata: map[string]string{
KFileshareName: snapName,
KFileshareID: opt.GetId(),
KLvPath: lvPath,
KFileshareSnapName: snapName,
KFileshareSnapID: opt.GetId(),
KLvPath: lvPath,
},
}, nil
}

// DeleteFileShareSnapshot
func (d *Driver) DeleteFileShareSnapshot(opt *pb.DeleteFileShareSnapshotOpts) (*model.FileShareSnapshotSpec, error) {
lvsPath, ok := opt.GetMetadata()[KLvPath]
snapName := opt.GetMetadata()[KFileshareSnapName]
if !ok {
err := errors.New("can't find 'lvsPath' in snapshot metadata, ingnore it!")
log.Error(err)
return nil, nil
}
fields := strings.Split(lvsPath, "/")
vg, snapName := fields[2], fields[3]
vg := fields[2]
if !d.cli.Exists(snapName) {
log.Warningf("Snapshot(%s) does not exist, nothing to remove", snapName)
return nil, nil
Expand Down
61 changes: 58 additions & 3 deletions pkg/api/controllers/fileshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type FileSharePortal struct {
// Function to store Acl's related entry into databse
func (f *FileSharePortal) CreateFileShareAcl() {
ctx := c.GetContext(f.Ctx)
// Get profile
var prf *model.ProfileSpec
var fileshareacl = model.FileShareAclSpec{
BaseModel: &model.BaseModel{},
}
Expand All @@ -55,13 +57,25 @@ func (f *FileSharePortal) CreateFileShareAcl() {
return
}
result, err := util.CreateFileShareAclDBEntry(c.GetContext(f.Ctx), &fileshareacl)

if err != nil {
reason := fmt.Sprintf("create access rules for fileshare failed: %s", err.Error())
reason := fmt.Sprintf("createFileshareAcldbentry failed: %s", err.Error())
f.ErrorHandle(model.ErrorBadRequest, reason)
log.Error(reason)
return
}
fileshare, err := db.C.GetFileShare(ctx, result.FileShareId)
if err != nil {
reason := fmt.Sprintf("getFileshare failed in createfileshare acl: %s", err.Error())
f.ErrorHandle(model.ErrorBadRequest, reason)
log.Error(reason)
return
}
prf, err = db.C.GetProfile(ctx, fileshare.ProfileId)
if err != nil {
errMsg := fmt.Sprintf("get profile failed: %s", err.Error())
f.ErrorHandle(model.ErrorBadRequest, errMsg)
return
}
// Marshal the result.
body, err := json.Marshal(result)
if err != nil {
Expand All @@ -70,7 +84,6 @@ func (f *FileSharePortal) CreateFileShareAcl() {
log.Error(reason)
return
}

f.SuccessHandle(StatusAccepted, body)

// FileShare acl access creation request is sent to dock and drivers
Expand All @@ -87,7 +100,9 @@ func (f *FileSharePortal) CreateFileShareAcl() {
Type: result.Type,
AccessCapability: result.AccessCapability,
AccessTo: result.AccessTo,
Metadata: fileshare.Metadata,
Context: ctx.ToJson(),
Profile: prf.ToJson(),
}

if _, err = f.CtrClient.CreateFileShareAcl(context.Background(), opt); err != nil {
Expand Down Expand Up @@ -300,6 +315,9 @@ func (f *FileSharePortal) UpdateFileShare() {
}

func (f *FileSharePortal) DeleteFileShareAcl() {
// Get profile
var prf *model.ProfileSpec

ctx := c.GetContext(f.Ctx)

var err error
Expand All @@ -311,12 +329,48 @@ func (f *FileSharePortal) DeleteFileShareAcl() {
return
}

fileshare, err := db.C.GetFileShare(ctx, acl.FileShareId)
if err != nil {
errMsg := fmt.Sprintf("fileshare for the acl %s not found: %s", id, err.Error())
f.ErrorHandle(model.ErrorNotFound, errMsg)
return
}

prf, err = db.C.GetProfile(ctx, fileshare.ProfileId)
if err != nil {
errMsg := fmt.Sprintf("get profile failed: %s", err.Error())
f.ErrorHandle(model.ErrorBadRequest, errMsg)
return
}

if err := db.C.DeleteFileShareAcl(ctx, acl.Id); err != nil {
errMsg := fmt.Sprintf("delete fileshare acl failed: %v", err.Error())
f.ErrorHandle(model.ErrorInternalServer, errMsg)
return
}
f.SuccessHandle(StatusAccepted, nil)
// NOTE: The real file share deletion process.
// File Share deletion request is sent to the Dock. Dock will delete file share from driver
// and database or update file share status to "errorDeleting" if deletion from driver failed.
if err := f.CtrClient.Connect(CONF.OsdsLet.ApiEndpoint); err != nil {
log.Error("when connecting controller client:", err)
return
}
defer f.CtrClient.Close()
opt := &pb.DeleteFileShareAclOpts{
FileShareId: acl.FileShareId,
Description: acl.Description,
Type: acl.Type,
AccessCapability: acl.AccessCapability,
AccessTo: acl.AccessTo,
Metadata: fileshare.Metadata,
Context: ctx.ToJson(),
Profile: prf.ToJson(),
}
if _, err = f.CtrClient.DeleteFileShareAcl(context.Background(), opt); err != nil {
log.Error("delete fileshare failed in controller service:", err)
return
}
return
}

Expand Down Expand Up @@ -581,6 +635,7 @@ func (f *FileShareSnapshotPortal) DeleteFileShareSnapshot() {
FileshareId: snapshot.FileShareId,
Context: ctx.ToJson(),
Profile: prf.ToJson(),
Metadata: snapshot.Metadata,
}
if _, err = f.CtrClient.DeleteFileShareSnapshot(context.Background(), opt); err != nil {
log.Error("delete file share snapshot failed in controller service:", err)
Expand Down
1 change: 1 addition & 0 deletions pkg/api/util/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func CreateFileShareSnapshotDBEntry(ctx *c.Context, in *model.FileShareSnapshotS
}

in.Status = model.FileShareSnapCreating
in.Metadata = fshare.Metadata
return db.C.CreateFileShareSnapshot(ctx, in)
}

Expand Down
40 changes: 38 additions & 2 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,44 @@ func (c *Controller) CreateFileShareAcl(contx context.Context, opt *pb.CreateFil
return pb.GenericResponseResult(result), nil
}

// DeleteFileShareAcl implements pb.ControllerServer.DeleteFileShare
func (c *Controller) DeleteFileShareAcl(contx context.Context, opt *pb.DeleteFileShareAclOpts) (*pb.GenericResponse, error) {
var err error
log.Info("controller server receive create file share acl request, vr =", opt)
ctx := osdsCtx.NewContextFromJson(opt.GetContext())

fileshare, err := db.C.GetFileShare(ctx, opt.FileShareId)
if err != nil {
db.UpdateFileShareStatus(ctx, db.C, opt.Id, model.FileShareError)
return pb.GenericResponseError(err), err
}
polInfo, err := c.selector.SelectSupportedPoolForFileShare(fileshare)
if err != nil {
db.UpdateFileShareStatus(ctx, db.C, opt.Id, model.FileShareError)
return pb.GenericResponseError(err), err
}

dockInfo, err := db.C.GetDock(ctx, polInfo.DockId)
if err != nil {
db.UpdateFileShareStatus(ctx, db.C, opt.Id, model.FileShareError)
log.Error("when search supported dock resource:", err.Error())
return pb.GenericResponseError(err), err
}
c.fileshareController.SetDock(dockInfo)
opt.DriverName = dockInfo.DriverName
opt.Name = fileshare.Name

result, err := c.fileshareController.DeleteFileShareAcl((*pb.DeleteFileShareAclOpts)(opt))
if err != nil {
// Change the status of the file share acl to error when the creation faild
defer db.UpdateFileShareStatus(ctx, db.C, opt.Id, model.FileShareError)
log.Error("when create file share:", err.Error())
return pb.GenericResponseError(err), err
}

return pb.GenericResponseResult(result), nil
}

// CreateFileShareSnapshot implements pb.ControllerServer.CreateFileShareSnapshot
func (c *Controller) CreateFileShareSnapshot(contx context.Context, opt *pb.CreateFileShareSnapshotOpts) (*pb.GenericResponse, error) {

Expand All @@ -1019,8 +1057,6 @@ func (c *Controller) CreateFileShareSnapshot(contx context.Context, opt *pb.Crea
return pb.GenericResponseError(err), err
}
opt.Size = fileshare.Size
opt.Metadata = utils.MergeStringMaps(opt.Metadata, fileshare.Metadata)

dockInfo, err := db.C.GetDockByPoolId(ctx, fileshare.PoolId)
if err != nil {
log.Error("when search supported dock resource: ", err)
Expand Down
30 changes: 30 additions & 0 deletions pkg/controller/fileshare/filesharecontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type Controller interface {

CreateFileShareAcl(opt *pb.CreateFileShareAclOpts) (*model.FileShareAclSpec, error)

DeleteFileShareAcl(opt *pb.DeleteFileShareAclOpts) (*model.FileShareAclSpec, error)

DeleteFileShare(opt *pb.DeleteFileShareOpts) error

CreateFileShareSnapshot(opt *pb.CreateFileShareSnapshotOpts) (*model.FileShareSnapshotSpec, error)
Expand Down Expand Up @@ -87,6 +89,34 @@ func (c *controller) CreateFileShareAcl(opt *pb.CreateFileShareAclOpts) (*model.

}

func (c *controller) DeleteFileShareAcl(opt *pb.DeleteFileShareAclOpts) (*model.FileShareAclSpec, error) {
if err := c.Client.Connect(c.DockInfo.Endpoint); err != nil {
log.Error("when connecting dock client:", err)
return nil, err
}

response, err := c.Client.DeleteFileShareAcl(context.Background(), opt)
if err != nil {
log.Error("delete file share acl failed in file share controller:", err)
return nil, err
}
defer c.Client.Close()

if errorMsg := response.GetError(); errorMsg != nil {
return nil,
fmt.Errorf("failed to delete file share acl in file share controller, code: %v, message: %v",
errorMsg.GetCode(), errorMsg.GetDescription())
}

var fileshare = &model.FileShareAclSpec{}
if err = json.Unmarshal([]byte(response.GetResult().GetMessage()), fileshare); err != nil {
log.Error("delete file share acl failed in file share controller:", err)
return nil, err
}

return fileshare, nil
}

func (c *controller) CreateFileShare(opt *pb.CreateFileShareOpts) (*model.FileShareSpec, error) {
if err := c.Client.Connect(c.DockInfo.Endpoint); err != nil {
log.Error("when connecting dock client:", err)
Expand Down
11 changes: 11 additions & 0 deletions pkg/controller/fileshare/filesharecontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ func (fc *fakefileshareClient) DeleteFileShare(ctx context.Context, in *pb.Delet
}, nil
}

// DeleteFileShareAcl provides a mock function with given fields: ctx, in, opts
func (fc *fakefileshareClient) DeleteFileShareAcl(ctx context.Context, in *pb.DeleteFileShareAclOpts, opts ...grpc.CallOption) (*pb.GenericResponse, error) {
return &pb.GenericResponse{
Reply: &pb.GenericResponse_Result_{
Result: &pb.GenericResponse_Result{
Message: ByteFileShareAcl,
},
},
}, nil
}

func (fc *fakefileshareClient) CreateFileShareSnapshot(ctx context.Context, in *pb.CreateFileShareSnapshotOpts, opts ...grpc.CallOption) (*pb.GenericResponse, error) {
return &pb.GenericResponse{
Reply: &pb.GenericResponse_Result_{
Expand Down

0 comments on commit 2513f02

Please sign in to comment.