Skip to content

Commit d5091b2

Browse files
Shruthi-1MNShruthi-1MN
authored andcommitted
File share name fixes
1 parent f8581c3 commit d5091b2

File tree

5 files changed

+66
-125
lines changed

5 files changed

+66
-125
lines changed

openapi-spec/swagger.yaml

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,12 +2294,7 @@ components:
22942294
content:
22952295
application/json:
22962296
schema:
2297-
type: object
2298-
properties:
2299-
name:
2300-
type: string
2301-
description:
2302-
type: string
2297+
$ref: '#/components/schemas/FileShareUpdateSpec'
23032298
FileShareSnapshotCreateSpec:
23042299
content:
23052300
application/json:
@@ -2676,8 +2671,8 @@ components:
26762671
name:
26772672
type: string
26782673
minLength: 1
2679-
maxLength: 255
2680-
pattern: '[A-Za-z0-9_-]'
2674+
maxLength: 103
2675+
pattern: '^[\w\- ]+$'
26812676
description:
26822677
type: string
26832678
size:
@@ -2689,7 +2684,26 @@ components:
26892684
default: default
26902685
profileId:
26912686
type: string
2692-
2687+
FileShareUpdateSpec:
2688+
type: object
2689+
properties:
2690+
name:
2691+
type: string
2692+
minLength: 1
2693+
maxLength: 103
2694+
pattern: '^[\w\- ]+$'
2695+
description:
2696+
type: string
2697+
pattern: '^[\w\- ]+$'
2698+
size:
2699+
type: integer
2700+
format: int64
2701+
example: 2
2702+
availabilityZone:
2703+
type: string
2704+
default: default
2705+
profileId:
2706+
type: string
26932707
FileShareRespSpec:
26942708
description: >-
26952709
FileShare is a file system created by nfs southbound driver, it can be

pkg/api/controllers/fileshare.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,18 @@ func (f *FileSharePortal) UpdateFileShare() {
370370
f.ErrorHandle(model.ErrorBadRequest, errMsg)
371371
return
372372
}
373+
374+
fshare1, err := db.C.GetFileShare(c.GetContext(f.Ctx), id)
375+
if err != nil {
376+
errMsg := fmt.Sprintf("fileshare %s not found: %s", id, err.Error())
377+
f.ErrorHandle(model.ErrorNotFound, errMsg)
378+
return
379+
}
380+
if fshare1.Status == "error" {
381+
errMsg := fmt.Sprintf("can't update file share %s because it's status is error", fshare1.Id)
382+
f.ErrorHandle(model.ErrorBadRequest, errMsg)
383+
return
384+
}
373385

374386
fshare.Id = id
375387
result, err := db.C.UpdateFileShare(c.GetContext(f.Ctx), &fshare)

pkg/api/controllers/fileshare_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func TestUpdateFileShare(t *testing.T) {
192192
fileshare := model.FileShareSpec{BaseModel: &model.BaseModel{}}
193193
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&fileshare)
194194
mockClient := new(dbtest.Client)
195+
mockClient.On("GetFileShare", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(&expected, nil)
195196
mockClient.On("UpdateFileShare", c.NewAdminContext(), &fileshare).Return(&expected, nil)
196197
db.C = mockClient
197198

@@ -212,6 +213,7 @@ func TestUpdateFileShare(t *testing.T) {
212213
fileshare := model.FileShareSpec{BaseModel: &model.BaseModel{}}
213214
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&fileshare)
214215
mockClient := new(dbtest.Client)
216+
mockClient.On("GetFileShare", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(&expected, nil)
215217
mockClient.On("UpdateFileShare", c.NewAdminContext(), &fileshare).Return(nil, errors.New("db error"))
216218
db.C = mockClient
217219

pkg/api/util/db.go

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"errors"
2323
"fmt"
2424
"net"
25-
"regexp"
2625
"strings"
2726
"time"
2827

@@ -170,28 +169,23 @@ func CreateFileShareDBEntry(ctx *c.Context, in *model.FileShareSpec) (*model.Fil
170169
if in.UpdatedAt == "" {
171170
in.UpdatedAt = time.Now().Format(constants.TimeFormat)
172171
}
173-
//validate the name
174-
if in.Name == "" {
175-
errMsg := fmt.Sprintf("empty fileshare name is not allowed. Please give valid name.")
176-
log.Error(errMsg)
177-
return nil, errors.New(errMsg)
178-
}
179-
if len(in.Name) > 255 {
180-
errMsg := fmt.Sprintf("fileshare name length should not be more than 255 characters. input name length is : %d", len(in.Name))
181-
log.Error(errMsg)
182-
return nil, errors.New(errMsg)
183-
}
184172

185-
reg, err := regexp.Compile("^[a-zA-Z0-9_-]+$")
173+
shares, err := db.C.ListFileShares(ctx)
186174
if err != nil {
187-
errMsg := fmt.Sprintf("regex compilation for file name validation failed")
188-
log.Error(errMsg)
189-
return nil, errors.New(errMsg)
190-
}
191-
if reg.MatchString(in.Name) == false {
192-
errMsg := fmt.Sprintf("invalid fileshare name it only contain english char and number : %v", in.Name)
193-
log.Error(errMsg)
194-
return nil, errors.New(errMsg)
175+
return nil, err
176+
} else {
177+
for _, fshare := range shares {
178+
if fshare.Name == in.Name {
179+
errMsg := fmt.Sprintf("file share name already exists")
180+
log.Error(errMsg)
181+
return nil, errors.New(errMsg)
182+
}
183+
if fshare.Id == in.Id {
184+
errMsg := fmt.Sprintf("file share id already exists")
185+
log.Error(errMsg)
186+
return nil, errors.New(errMsg)
187+
}
188+
}
195189
}
196190

197191
in.UserId = ctx.UserId

pkg/api/util/db_test.go

Lines changed: 14 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ package util
1717
import (
1818
"fmt"
1919
"reflect"
20-
"strconv"
2120
"testing"
2221

23-
"github.com/opensds/opensds/pkg/utils"
24-
2522
"github.com/opensds/opensds/pkg/context"
2623
"github.com/opensds/opensds/pkg/db"
2724
"github.com/opensds/opensds/pkg/model"
@@ -390,6 +387,7 @@ func TestCreateFileShareDBEntry(t *testing.T) {
390387
t.Run("Everything should work well", func(t *testing.T) {
391388
mockClient := new(dbtest.Client)
392389
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
390+
mockClient.On("ListFileShares", context.NewAdminContext()).Return(nil, nil)
393391
db.C = mockClient
394392

395393
var expected = &SampleFileShares[0]
@@ -404,6 +402,7 @@ func TestCreateFileShareDBEntry(t *testing.T) {
404402
in.Size = int64(-2)
405403
mockClient := new(dbtest.Client)
406404
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
405+
mockClient.On("ListFileShares", context.NewAdminContext()).Return(nil, nil)
407406
db.C = mockClient
408407

409408
_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
@@ -415,117 +414,37 @@ func TestCreateFileShareDBEntry(t *testing.T) {
415414
in.ProfileId = ""
416415
mockClient := new(dbtest.Client)
417416
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
417+
mockClient.On("ListFileShares", context.NewAdminContext()).Return(nil, nil)
418418
db.C = mockClient
419419

420420
_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
421421
expectedError := "profile id can not be empty when creating fileshare in db!"
422422
assertTestResult(t, err.Error(), expectedError)
423423
})
424424

425-
t.Run("Empty file share name is allowed", func(t *testing.T) {
426-
in.Size, in.Name, in.ProfileId = int64(1), "", "b3585ebe-c42c-120g-b28e-f373746a71ca"
427-
mockClient := new(dbtest.Client)
428-
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
429-
db.C = mockClient
430-
431-
_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
432-
expectedError := "empty fileshare name is not allowed. Please give valid name."
433-
assertTestResult(t, err.Error(), expectedError)
434-
})
435-
436-
t.Run("File share name length equal to 0 character are not allowed", func(t *testing.T) {
437-
in.Name = utils.RandSeqWithAlnum(0)
438-
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
439-
mockClient := new(dbtest.Client)
440-
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
441-
db.C = mockClient
442-
443-
_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
444-
expectedError := "empty fileshare name is not allowed. Please give valid name."
445-
assertTestResult(t, err.Error(), expectedError)
446-
})
447-
448-
t.Run("File share name length equal to 1 character are allowed", func(t *testing.T) {
449-
in.Name = utils.RandSeqWithAlnum(1)
450-
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
451-
mockClient := new(dbtest.Client)
452-
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
453-
db.C = mockClient
454-
455-
var expected = &SampleFileShares[0]
456-
result, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
457-
if err != nil {
458-
t.Errorf("failed to create fileshare err is %v\n", err)
459-
}
460-
assertTestResult(t, result, expected)
461-
})
462-
463-
t.Run("File share name length equal to 10 characters are allowed", func(t *testing.T) {
464-
in.Name = utils.RandSeqWithAlnum(10)
465-
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
466-
mockClient := new(dbtest.Client)
467-
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
468-
db.C = mockClient
469-
470-
var expected = &SampleFileShares[0]
471-
result, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
472-
if err != nil {
473-
t.Errorf("failed to create fileshare err is %v\n", err)
474-
}
475-
assertTestResult(t, result, expected)
476-
})
477-
478-
t.Run("File share name length equal to 254 characters are allowed", func(t *testing.T) {
479-
in.Name = utils.RandSeqWithAlnum(254)
480-
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
481-
mockClient := new(dbtest.Client)
482-
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
483-
db.C = mockClient
484-
485-
var expected = &SampleFileShares[0]
486-
result, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
487-
if err != nil {
488-
t.Errorf("failed to create fileshare err is %v\n", err)
489-
}
490-
assertTestResult(t, result, expected)
491-
})
492-
493-
t.Run("File share name length equal to 255 characters are allowed", func(t *testing.T) {
494-
in.Name = utils.RandSeqWithAlnum(255)
495-
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
496-
mockClient := new(dbtest.Client)
497-
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
498-
db.C = mockClient
499-
500-
var expected = &SampleFileShares[0]
501-
result, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
502-
if err != nil {
503-
t.Errorf("failed to create fileshare err is %v\n", err)
504-
}
505-
assertTestResult(t, result, expected)
506-
})
507-
508-
t.Run("File share name length more than 255 characters are not allowed", func(t *testing.T) {
509-
in.Name = utils.RandSeqWithAlnum(256)
510-
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
425+
t.Run("Duplicate file share name are not allowed", func(t *testing.T) {
426+
var sampleshares = []*model.FileShareSpec{&SampleFileShares[0]}
427+
in.Size, in.Name, in.ProfileId, in.Description = int64(1), "sample-fileshare-01", "b3585ebe-c42c-120g-b28e-f373746a71ca", "File share test"
511428
mockClient := new(dbtest.Client)
512429
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
430+
mockClient.On("ListFileShares", context.NewAdminContext()).Return(sampleshares, nil)
513431
db.C = mockClient
514432

515433
_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
516-
expectedError := "fileshare name length should not be more than 255 characters. input name length is : " + strconv.Itoa(len(in.Name))
434+
expectedError := "file share name already exists"
517435
assertTestResult(t, err.Error(), expectedError)
518436
})
519437

520-
t.Run("File share name length more than 255 characters are not allowed", func(t *testing.T) {
521-
in.Name = utils.RandSeqWithAlnum(257)
522-
in.Size, in.ProfileId = int64(1), "b3585ebe-c42c-120g-b28e-f373746a71ca"
438+
t.Run("Overwritting existing file share resource are not allowed", func(t *testing.T) {
439+
var sampleshares = []*model.FileShareSpec{&SampleFileShares[0]}
440+
in.Size, in.Name, in.ProfileId, in.Description, in.Id = int64(1), "sample-fileshare-05", "b3585ebe-c42c-120g-b28e-f373746a71ca", "File share test", "d2975ebe-d82c-430f-b28e-f373746a71ca"
523441
mockClient := new(dbtest.Client)
524442
mockClient.On("CreateFileShare", context.NewAdminContext(), in).Return(&SampleFileShares[0], nil)
443+
mockClient.On("ListFileShares", context.NewAdminContext()).Return(sampleshares, nil)
525444
db.C = mockClient
526445

527446
_, err := CreateFileShareDBEntry(context.NewAdminContext(), in)
528-
expectedError := "fileshare name length should not be more than 255 characters. input name length is : " + strconv.Itoa(len(in.Name))
447+
expectedError := "file share id already exists"
529448
assertTestResult(t, err.Error(), expectedError)
530449
})
531450
}
@@ -811,4 +730,4 @@ func TestDeleteFileShareSnapshotDBEntry(t *testing.T) {
811730
expectedError := fmt.Sprintf("only the fileshare snapshot with the status available, error, error_deleting can be deleted, the fileshare status is %s", in.Status)
812731
assertTestResult(t, err.Error(), expectedError)
813732
})
814-
}
733+
}

0 commit comments

Comments
 (0)