feat(glance): image support filter by os_type and distribution

This commit is contained in:
Zexi Li
2021-08-23 15:45:51 +08:00
parent 4f2aa13bbb
commit 7409e8cb19
3 changed files with 38 additions and 7 deletions
+15 -7
View File
@@ -142,13 +142,15 @@ func init() {
type ImageListOptions struct {
options.BaseListOptions
IsPublic string `help:"filter images public or not(True, False or None)" choices:"true|false"`
IsStandard string `help:"filter images standard or non-standard" choices:"true|false"`
Protected string `help:"filter images by protected" choices:"true|false"`
IsUefi bool `help:"list uefi image"`
Format []string `help:"Disk formats"`
SubFormats []string `help:"Sub formats"`
Name string `help:"Name filter"`
IsPublic string `help:"filter images public or not(True, False or None)" choices:"true|false"`
IsStandard string `help:"filter images standard or non-standard" choices:"true|false"`
Protected string `help:"filter images by protected" choices:"true|false"`
IsUefi bool `help:"list uefi image"`
Format []string `help:"Disk formats"`
SubFormats []string `help:"Sub formats"`
Name string `help:"Name filter"`
OsType []string `help:"Type of OS filter e.g. 'Windows, Linux, Freebsd, Android, macOS, VMWare'"`
Distribution []string `help:"Distribution filter, e.g. 'CentOS, Ubuntu, Debian, Windows'"`
}
R(&ImageListOptions{}, "image-list", "List images", func(s *mcclient.ClientSession, args *ImageListOptions) error {
params, err := args.Params()
@@ -187,6 +189,12 @@ func init() {
if len(args.SubFormats) > 0 {
params.Add(jsonutils.Marshal(args.SubFormats), "sub_formats")
}
if len(args.OsType) > 0 {
params.Add(jsonutils.NewStringArray(args.OsType), "os_types")
}
if len(args.Distribution) > 0 {
params.Add(jsonutils.NewStringArray(args.Distribution), "distributions")
}
result, err := modules.Images.List(s, params)
if err != nil {
return err
+6
View File
@@ -43,6 +43,12 @@ type ImageListInput struct {
// 是否为数据盘
IsData *bool `json:"is_data"`
// 操作系统类型,可能值为: Linux, Windows, FreeBSD 等
OsTypes []string `json:"os_types"`
// 发行版本,可能值为: CentOS, Ubuntu, Debian, ArchLinux, OpenEuler 等
Distributions []string `json:"distributions"`
}
type GuestImageListInput struct {
+17
View File
@@ -1272,6 +1272,23 @@ func (manager *SImageManager) ListItemFilter(
q = q.IsFalse("is_data")
}
}
propFilter := func(nameKeys []string, vals []string) {
if len(vals) == 0 {
return
}
propQ := ImagePropertyManager.Query().In("name", nameKeys)
conds := make([]sqlchemy.ICondition, 0)
for _, val := range vals {
conds = append(conds, sqlchemy.Like(propQ.Field("value"), val))
}
propQ.Filter(sqlchemy.OR(conds...))
propSq := propQ.SubQuery()
q = q.Join(propSq, sqlchemy.Equals(q.Field("id"), propSq.Field("image_id")))
}
propFilter([]string{api.IMAGE_OS_TYPE}, query.OsTypes)
propFilter([]string{api.IMAGE_OS_DISTRO, "distro"}, query.Distributions)
return q, nil
}