Skip to content

Commit

Permalink
fix: allwo filter resource by with-meta and with-user-meta filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiu Jian committed Oct 15, 2020
1 parent f5a45fc commit ff2b7eb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
12 changes: 10 additions & 2 deletions pkg/apis/list.go
Expand Up @@ -195,8 +195,16 @@ type MetadataResourceListInput struct {
// 通过标签过滤
OrderByTag string `json:"order_by_tag"`

// 返回资源的标签不包含特定的用户标签
WithoutUserMeta bool `json:"without_user_meta"`
// deprecated
// 返回资源的标签不包含用户标签
WithoutUserMeta *bool `json:"without_user_meta"`

// 返回资源的标签包含用户标签
WithUserMeta *bool `json:"with_user_meta"`

// 返回资源的标签包含外部标签
WithCloudMeta *bool `json:"with_cloud_meta"`

// 返回列表数据中包含资源的标签数据(Metadata)
WithMeta *bool `json:"with_meta"`
}
Expand Down
28 changes: 26 additions & 2 deletions pkg/cloudcommon/db/metadataresource.go
Expand Up @@ -70,10 +70,34 @@ func (meta *SMetadataResourceBaseModelManager) ListItemFilter(
q = q.Filter(sqlchemy.In(q.Field("id"), sq))
}

if input.WithoutUserMeta {
if input.WithoutUserMeta != nil || input.WithUserMeta != nil {
metadatas := Metadata.Query().Equals("obj_type", manager.Keyword()).SubQuery()
sq := metadatas.Query(metadatas.Field("obj_id")).Startswith("key", USER_TAG_PREFIX).Distinct().SubQuery()
q.Filter(sqlchemy.NotIn(q.Field("id"), sq))
if (input.WithoutUserMeta != nil && *input.WithoutUserMeta) || (input.WithUserMeta != nil && !*input.WithUserMeta) {
q = q.Filter(sqlchemy.NotIn(q.Field("id"), sq))
} else {
q = q.Filter(sqlchemy.In(q.Field("id"), sq))
}
}

if input.WithCloudMeta != nil {
metadatas := Metadata.Query().Equals("obj_type", manager.Keyword()).SubQuery()
sq := metadatas.Query(metadatas.Field("obj_id")).Startswith("key", CLOUD_TAG_PREFIX).Distinct().SubQuery()
if *input.WithCloudMeta {
q = q.Filter(sqlchemy.In(q.Field("id"), sq))
} else {
q = q.Filter(sqlchemy.NotIn(q.Field("id"), sq))
}
}

if input.WithMeta != nil {
metadatas := Metadata.Query().Equals("obj_type", manager.Keyword()).SubQuery()
sq := metadatas.Query(metadatas.Field("obj_id")).Distinct().SubQuery()
if *input.WithMeta {
q = q.Filter(sqlchemy.In(q.Field("id"), sq))
} else {
q = q.Filter(sqlchemy.NotIn(q.Field("id"), sq))
}
}

return q
Expand Down
4 changes: 4 additions & 0 deletions pkg/mcclient/options/servers.go
Expand Up @@ -67,6 +67,10 @@ type ServerListOptions struct {
BaseListOptions

VpcProvider string `help:"filter by vpc's provider" json:"vpc_provider"`

WithMeta *bool `help:"filter by metadata" negative:"without_meta"`

WithUserMeta *bool `help:"filter by user metadata" negative:"without_user_meta"`
}

func (o *ServerListOptions) Params() (jsonutils.JSONObject, error) {
Expand Down

0 comments on commit ff2b7eb

Please sign in to comment.