Skip to content

Commit

Permalink
PET-198: add volume type parameter in list api
Browse files Browse the repository at this point in the history
Signed-off-by: bryan <bryan@raksmart.com>
  • Loading branch information
bryan-huangyan committed Dec 20, 2024
1 parent 0056cf8 commit 8e7cc2b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
7 changes: 6 additions & 1 deletion web/src/apis/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type VolumeResponse struct {
Status string `json:"status"`
Target string `json:"target"`
Href string `json:"href"`
Booting bool `json:"booting"`
Instance *BaseReference `json:"instance"`
IopsLimit int32 `json:"iops_limit"`
IopsBurst int32 `json:"iops_burst"`
Expand Down Expand Up @@ -203,6 +204,10 @@ func (v *VolumeAPI) List(c *gin.Context) {
ctx := c.Request.Context()
offsetStr := c.DefaultQuery("offset", "0")
limitStr := c.DefaultQuery("limit", "50")
nameStr := c.DefaultQuery("name", "")
// type: all, data, boot
// default data
typeStr := c.DefaultQuery("type", "data")
offset, err := strconv.Atoi(offsetStr)
if err != nil {
ErrorResponse(c, http.StatusBadRequest, "Invalid query offset: "+offsetStr, err)
Expand All @@ -217,7 +222,7 @@ func (v *VolumeAPI) List(c *gin.Context) {
ErrorResponse(c, http.StatusBadRequest, "Invalid query offset or limit", err)
return
}
total, volumes, err := volumeAdmin.List(ctx, int64(offset), int64(limit), "-created_at", "")
total, volumes, err := volumeAdmin.ListVolume(ctx, int64(offset), int64(limit), "-created_at", nameStr, typeStr)
if err != nil {
ErrorResponse(c, http.StatusBadRequest, "Failed to list volumes", err)
return
Expand Down
29 changes: 26 additions & 3 deletions web/src/routes/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,12 @@ func (a *VolumeAdmin) DeleteVolumeByUUID(ctx context.Context, uuID string) (err
return a.Delete(ctx, volume)
}

// list data volumes
func (a *VolumeAdmin) List(ctx context.Context, offset, limit int64, order, query string) (total int64, volumes []*model.Volume, err error) {
return a.ListVolume(ctx, offset, limit, order, query, "all")
}

func (a *VolumeAdmin) ListVolume(ctx context.Context, offset, limit int64, order, query string, volume_type string) (total int64, volumes []*model.Volume, err error) {
memberShip := GetMemberShip(ctx)
db := DB()
if limit == 0 {
Expand All @@ -292,10 +297,28 @@ func (a *VolumeAdmin) List(ctx context.Context, offset, limit int64, order, quer
query = fmt.Sprintf("name like '%%%s%%'", query)
}
where := memberShip.GetWhere()
volumes = []*model.Volume{}
if err = db.Model(&model.Volume{}).Where(where).Where(query).Count(&total).Error; err != nil {
booting_where := ""
if volume_type == "data" {
booting_where = fmt.Sprintf("booting=%t", false)
} else if volume_type == "boot" {
booting_where = fmt.Sprintf("booting=%t", true)
} else if volume_type == "all" {
booting_where = ""
} else {
err = fmt.Errorf("Invalid volume type %s", volume_type)
return
}

volumes = []*model.Volume{}
if booting_where != "" {
if err = db.Model(&model.Volume{}).Where(where).Where(query).Where(booting_where).Count(&total).Error; err != nil {
return
}
} else {
if err = db.Model(&model.Volume{}).Where(where).Where(query).Count(&total).Error; err != nil {
return
}
}
db = dbs.Sortby(db.Offset(offset).Limit(limit), order)
if err = db.Preload("Instance").Where(where).Where(query).Find(&volumes).Error; err != nil {
return
Expand Down Expand Up @@ -334,7 +357,7 @@ func (v *VolumeView) List(c *macaron.Context, store session.Store) {
order = "-created_at"
}
query := c.QueryTrim("q")
total, volumes, err := volumeAdmin.List(c.Req.Context(), offset, limit, order, query)
total, volumes, err := volumeAdmin.ListVolume(c.Req.Context(), offset, limit, order, query, "all")
if err != nil {
if c.Req.Header.Get("X-Json-Format") == "yes" {
c.JSON(500, map[string]interface{}{
Expand Down
2 changes: 2 additions & 0 deletions web/templates/volumes.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<th>{{.i18n.Tr "UUID"}}</th>
<th>{{.i18n.Tr "Size"}} (G)</th>
<th>{{.i18n.Tr "Status"}}</th>
<th>{{.i18n.Tr "Bootable"}}</th>
<th>{{.i18n.Tr "Attached_as"}}</th>
{{ if $.IsAdmin }}
<th>{{.i18n.Tr "Owner"}}</th>
Expand All @@ -47,6 +48,7 @@
<td><a href="{{$Link}}/{{.ID}}">{{.UUID}}</a></td>
<td>{{.Size}}</td>
<td>{{.Status}}</td>
<td>{{.Booting}}</td>
<td>{{ if .Instance }} {{.Instance.ID}}-{{.Instance.Hostname}}:{{.Target}} {{ end }}</td>
{{ if $.IsAdmin }}
<td>{{.OwnerInfo.Name}}</td>
Expand Down

0 comments on commit 8e7cc2b

Please sign in to comment.