diff --git a/web/src/apis/volume.go b/web/src/apis/volume.go index 57e04706..36fe7cb1 100644 --- a/web/src/apis/volume.go +++ b/web/src/apis/volume.go @@ -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"` @@ -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) @@ -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 diff --git a/web/src/routes/volume.go b/web/src/routes/volume.go index b5b60a34..4bbaef16 100644 --- a/web/src/routes/volume.go +++ b/web/src/routes/volume.go @@ -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 { @@ -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 @@ -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{}{ diff --git a/web/templates/volumes.tmpl b/web/templates/volumes.tmpl index bc17891b..6583e8d8 100644 --- a/web/templates/volumes.tmpl +++ b/web/templates/volumes.tmpl @@ -29,6 +29,7 @@ {{.i18n.Tr "UUID"}} {{.i18n.Tr "Size"}} (G) {{.i18n.Tr "Status"}} + {{.i18n.Tr "Bootable"}} {{.i18n.Tr "Attached_as"}} {{ if $.IsAdmin }} {{.i18n.Tr "Owner"}} @@ -47,6 +48,7 @@ {{.UUID}} {{.Size}} {{.Status}} + {{.Booting}} {{ if .Instance }} {{.Instance.ID}}-{{.Instance.Hostname}}:{{.Target}} {{ end }} {{ if $.IsAdmin }} {{.OwnerInfo.Name}}