Skip to content

Commit

Permalink
snapshot fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Shruthi-1MN committed Dec 6, 2019
1 parent 2e72330 commit ee0db6b
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 84 deletions.
136 changes: 126 additions & 10 deletions openapi-spec/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,16 @@ paths:
tags:
- File Share Snapshots
description: Lists information for all file share spanshots.
parameters:
- $ref: '#/components/parameters/limit'
- $ref: '#/components/parameters/offset'
- $ref: '#/components/parameters/sortDir'
- name: name
in: query
schema:
minLength: 1
maxLength: 255
type: string
responses:
'200':
description: OK
Expand All @@ -1480,11 +1490,33 @@ paths:
schema:
type: array
items:
$ref: '#/components/schemas/FileShareSnapshotSpec'
$ref: '#/components/schemas/FileShareSnapshotRespSpec'
examples:
response:
value:
- id: 405ded84-b6ff-4ac6-8a3c-cb0e82dcf372
createdAt: 2019-12-04T13:23:13
updatedAt: 2019-12-04T13:31:03
tenantId: e93b4c0934da416eb9c8d120c5d04d96
userId:
fileshareId: eedfbfcf-de50-4476-a2b6-058e6bfa3558
name: snaptest
description: Testing file share snapshot
shareSize: 1
snapshotSize: 1
status: available
poolId: 52599613-6f64-5aae-9819-7d2a2c790629
protocols: [nfs]
profileId: ed273ef0-4602-4790-ae68-09bdbb22d133
metadata: {"lvPath": "/dev/opensds-files-default/test","snapshotID": "405ded84-b6ff-4ac6-8a3c-cb0e82dcf372","snapshotName": "snaptest"}
'400':
$ref: '#/components/responses/HTTPStatus400'
'401':
$ref: '#/components/responses/HTTPStatus401'
'403':
$ref: '#/components/responses/HTTPStatus403'
'404':
$ref: '#/components/responses/HTTPStatus404'
'500':
$ref: '#/components/responses/HTTPStatus500'
post:
Expand All @@ -1493,11 +1525,26 @@ paths:
description: Creates a file share snapshot.
responses:
'202':
description: Accepted
description: Creating
content:
application/json:
schema:
$ref: '#/components/schemas/FileShareSnapshotSpec'
$ref: '#/components/schemas/FileShareSnapshotRespSpec'
examples:
response:
value:
- id: 405ded84-b6ff-4ac6-8a3c-cb0e82dcf372
createdAt: 2019-12-04T13:23:13
tenantId: e93b4c0934da416eb9c8d120c5d04d96
userId:
fileshareId: eedfbfcf-de50-4476-a2b6-058e6bfa3558
name: snaptest
description: Testing file share snapshot
shareSize: 1
snapshotSize: 1
status: creating
profileId: ed273ef0-4602-4790-ae68-09bdbb22d133
metadata: {"lvPath": "/dev/opensds-files-default/test","nfsFileshareID": "eedfbfcf-de50-4476-a2b6-058e6bfa3558","nfsFileshareName": "test","snapshotName": ""}
'400':
$ref: '#/components/responses/HTTPStatus400'
'401':
Expand All @@ -1510,7 +1557,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/FileShareSnapshotSpec'
$ref: '#/components/schemas/FileShareSnapshotCreateSpec'
'/v1beta/{tenantId}/file/snapshots/{shareSnapshotId}':
parameters:
- $ref: '#/components/parameters/tenantId'
Expand All @@ -1525,7 +1572,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/FileShareSnapshotSpec'
$ref: '#/components/schemas/FileShareSnapshotRespSpec'
'401':
$ref: '#/components/responses/HTTPStatus401'
'403':
Expand All @@ -1544,7 +1591,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/FileShareSnapshotSpec'
$ref: '#/components/schemas/FileShareSnapshotRespSpec'
'401':
$ref: '#/components/responses/HTTPStatus401'
'403':
Expand Down Expand Up @@ -2029,6 +2076,21 @@ components:
application/json:
schema:
$ref: '#/components/schemas/HostUpdateSpec'
FileShareSnapshotCreateSpec:
content:
application/json:
schema:
$ref: '#/components/schemas/FileShareSnapshotCreateSpec'
FileShareSnapshotUpdateSpec:
content:
application/json:
schema:
$ref: '#/components/schemas/FileShareSnapshotUpdateSpec'
FileShareSnapshotRespSpec:
content:
application/json:
schema:
$ref: '#/components/schemas/FileShareSnapshotRespSpec'
securitySchemes:
basicAuth:
type: http
Expand Down Expand Up @@ -2424,16 +2486,13 @@ components:
lvPath: string
nfsFileshareID: string
nfsFileshareName: string
FileShareSnapshotSpec:
FileShareSnapshotCreateSpec:
description: Snapshot is a description of fileshare snapshot resource.
allOf:
- $ref: '#/components/schemas/BaseModel'
- type: object
required:
- tenantId
- name
- status
- snapshotId
- fileshareId
properties:
fileshareId:
Expand All @@ -2445,6 +2504,8 @@ components:
type: string
readOnly: true
name:
minLength: 1
maxLength: 255
type: string
description:
type: string
Expand All @@ -2462,6 +2523,61 @@ components:
snapshotId: string
snapshotName: string
lvPath: string
FileShareSnapshotUpdateSpec:
description: Snapshot is a description of fileshare snapshot resource.
allOf:
- $ref: '#/components/schemas/BaseModel'
- type: object
properties:
fileshareId:
type: string
tenantId:
type: string
readOnly: true
userId:
type: string
readOnly: true
name:
minLength: 1
maxLength: 255
type: string
description:
type: string
FileShareSnapshotRespSpec:
description: Snapshot is a description of fileshare snapshot resource.
allOf:
- $ref: '#/components/schemas/BaseModel'
- type: object
required:
- name
- fileshareId
properties:
fileshareId:
type: string
tenantId:
type: string
readOnly: true
userId:
type: string
readOnly: true
name:
type: string
description:
type: string
status:
type: string
readOnly: true
profileId:
type: string
metadata:
type: object
readOnly: true
additionalProperties:
type: string
example:
snapshotId: string
snapshotName: string
lvPath: string
FileShareAclSpec:
description: File Share Acl API describes the acl of file share resource
allOf:
Expand Down
18 changes: 18 additions & 0 deletions pkg/api/controllers/fileshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,24 @@ func (f *FileShareSnapshotPortal) UpdateFileShareSnapshot() {
}
snapshot.Id = id

reg, err := utils.Special_Character_Regex_Match_Pattern()
if err != nil {
log.Error(err)
return
}
if reg.MatchString(snapshot.Name) {
errMsg := fmt.Sprintf("invalid snapshot name because it has some special characters")
log.Error(errMsg)
f.ErrorHandle(model.ErrorBadRequest, errMsg)
return
}
if reg.MatchString(snapshot.Description) {
errMsg := fmt.Sprintf("invalid snapshot description and it has some special characters")
log.Error(errMsg)
f.ErrorHandle(model.ErrorBadRequest, errMsg)
return
}

result, err := db.C.UpdateFileShareSnapshot(c.GetContext(f.Ctx), id, &snapshot)
if err != nil {
errMsg := fmt.Sprintf("update fileshare snapshot failed: %s", err.Error())
Expand Down
46 changes: 46 additions & 0 deletions pkg/api/controllers/fileshare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,52 @@ func TestUpdateFileShareSnapshot(t *testing.T) {
assertTestResult(t, &output, &expected)
})

t.Run("Should return 400 invalid snapshot name because it has some special characters", func(t *testing.T) {
var jsonStr = []byte(`{
"name": "#Snap !$!test",
"description":"fake snapshot"
}`)
snapshot := model.FileShareSnapshotSpec{BaseModel: &model.BaseModel{}}
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&snapshot)

mockClient := new(dbtest.Client)
mockClient.On("UpdateFileShareSnapshot", c.NewAdminContext(), snapshot.Id, &snapshot).
Return(&expected, nil)
db.C = mockClient

r, _ := http.NewRequest("PUT", "/v1beta/file/snapshots/3769855c-a102-11e7-b772-17b880d2f537", bytes.NewBuffer(jsonStr))
w := httptest.NewRecorder()
r.Header.Set("Content-Type", "application/JSON")
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
httpCtx.Input.SetData("context", c.NewAdminContext())
})
beego.BeeApp.Handlers.ServeHTTP(w, r)
assertTestResult(t, w.Code, 400)
})

t.Run("Should return 400 invalid snapshot description and it has some special characters", func(t *testing.T) {
var jsonStr = []byte(`{
"name":"fake snapshot",
"description": "#Share !$!test"
}`)
snapshot := model.FileShareSnapshotSpec{BaseModel: &model.BaseModel{}}
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&snapshot)

mockClient := new(dbtest.Client)
mockClient.On("UpdateFileShareSnapshot", c.NewAdminContext(), snapshot.Id, &snapshot).
Return(&expected, nil)
db.C = mockClient

r, _ := http.NewRequest("PUT", "/v1beta/file/snapshots/3769855c-a102-11e7-b772-17b880d2f537", bytes.NewBuffer(jsonStr))
w := httptest.NewRecorder()
r.Header.Set("Content-Type", "application/JSON")
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
httpCtx.Input.SetData("context", c.NewAdminContext())
})
beego.BeeApp.Handlers.ServeHTTP(w, r)
assertTestResult(t, w.Code, 400)
})

t.Run("Should return 500 if update fileshare snapshot with bad request", func(t *testing.T) {
snapshot := model.FileShareSnapshotSpec{BaseModel: &model.BaseModel{}}
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&snapshot)
Expand Down
32 changes: 21 additions & 11 deletions pkg/api/util/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,6 @@ func CreateFileShareDBEntry(ctx *c.Context, in *model.FileShareSpec) (*model.Fil
if in.UpdatedAt == "" {
in.UpdatedAt = time.Now().Format(constants.TimeFormat)
}
//validate the name
if in.Name == "" {
errMsg := fmt.Sprintf("empty fileshare name is not allowed. Please give valid name.")
log.Error(errMsg)
return nil, errors.New(errMsg)
}
if len(in.Name) > 255 {
errMsg := fmt.Sprintf("fileshare name length should not be more than 255 characters. input name length is : %d", len(in.Name))
log.Error(errMsg)
return nil, errors.New(errMsg)
}

// validate the description
reg, err := regexp.Compile("[^a-zA-Z0-9 ]+")
Expand Down Expand Up @@ -288,6 +277,27 @@ func CreateFileShareSnapshotDBEntry(ctx *c.Context, in *model.FileShareSnapshotS
log.Error(errMsg)
return nil, errors.New(errMsg)
}
if len(in.Name) > 255 {
errMsg := fmt.Sprintf("snapshot name length should not be more than 255 characters. input name length is : %d", len(in.Name))
log.Error(errMsg)
return nil, errors.New(errMsg)
}
reg, err := utils.Special_Character_Regex_Match_Pattern()
if err != nil {
errMsg := fmt.Sprintf("regex compilation for file share description validation failed")
log.Error(errMsg)
return nil, errors.New(errMsg)
}
if reg.MatchString(in.Name) {
errMsg := fmt.Sprintf("invalid fileshare snapshot name because it has some special characters")
log.Error(errMsg)
return nil, errors.New(errMsg)
}
if reg.MatchString(in.Description) {
errMsg := fmt.Sprintf("invalid fileshare snapshot description because it has some special characters")
log.Error(errMsg)
return nil, errors.New(errMsg)
}
if strings.HasPrefix(in.Name, "snapshot") {
errMsg := fmt.Sprintf("names starting 'snapshot' are reserved. Please choose a different snapshot name.")
log.Error(errMsg)
Expand Down
Loading

0 comments on commit ee0db6b

Please sign in to comment.