diff --git a/pkg/apis/compute/schedtag.go b/pkg/apis/compute/schedtag.go index 42d4ce88a0..d14e8b0c86 100644 --- a/pkg/apis/compute/schedtag.go +++ b/pkg/apis/compute/schedtag.go @@ -69,6 +69,9 @@ type SchedtagListInput struct { Type string `json:"type" yunion-deprecated-by:"resource_type"` DefaultStrategy []string `json:"default_strategy"` + + // filter by zone_id + ZoneId []string `json:"zone_id"` } type SchedtagDetails struct { diff --git a/pkg/compute/models/schedtags.go b/pkg/compute/models/schedtags.go index 2eb2fabc4d..370d32200a 100644 --- a/pkg/compute/models/schedtags.go +++ b/pkg/compute/models/schedtags.go @@ -220,6 +220,40 @@ func (manager *SSchedtagManager) ListItemFilter( q = q.Join(hostSchedtagSubq, sqlchemy.Equals(q.Field("id"), hostSchedtagSubq.Field("schedtag_id"))) } + if len(query.ZoneId) > 0 { + var err error + + storagesQ := StorageManager.Query("id") + storagesQ, err = StorageManager.SZoneResourceBaseManager.ListItemFilter(ctx, storagesQ, userCred, api.ZonalFilterListInput{ZonalFilterListBase: api.ZonalFilterListBase{ZoneIds: query.ZoneId}}) + if err != nil { + return nil, errors.Wrap(err, "StorageManager.SZoneResourceBaseManager.ListItemFilter") + } + storagesSubQ := storagesQ.SubQuery() + storageSchedtagQ := StorageschedtagManager.Query("schedtag_id") + storageSchedtagQ = storageSchedtagQ.Join(storagesSubQ, sqlchemy.Equals(storageSchedtagQ.Field("storage_id"), storagesSubQ.Field("id"))) + + networksQ := NetworkManager.Query("id") + networksQ, err = NetworkManager.SWireResourceBaseManager.ListItemFilter(ctx, networksQ, userCred, api.WireFilterListInput{ZonalFilterListBase: api.ZonalFilterListBase{ZoneIds: query.ZoneId}}) + if err != nil { + return nil, errors.Wrap(err, "NetworkManager.SWireResourceBaseManager.ListItemFilter") + } + networksSubQ := networksQ.SubQuery() + networkSchedtagQ := NetworkschedtagManager.Query("schedtag_id") + networkSchedtagQ = networkSchedtagQ.Join(networksSubQ, sqlchemy.Equals(networkSchedtagQ.Field("network_id"), networksSubQ.Field("id"))) + + hostQ := HostManager.Query("id") + hostQ, err = HostManager.SZoneResourceBaseManager.ListItemFilter(ctx, hostQ, userCred, api.ZonalFilterListInput{ZonalFilterListBase: api.ZonalFilterListBase{ZoneIds: query.ZoneId}}) + if err != nil { + return nil, errors.Wrap(err, "HostManager.SZoneResourceBaseManager.ListItemFilter") + } + hostSubQ := hostQ.SubQuery() + hostSchedtagQ := HostschedtagManager.Query("schedtag_id") + hostSchedtagQ = hostSchedtagQ.Join(hostSubQ, sqlchemy.Equals(hostSchedtagQ.Field("host_id"), hostSubQ.Field("id"))) + + unionSchedtagQ := sqlchemy.Union(storageSchedtagQ, networkSchedtagQ, hostSchedtagQ).Query().SubQuery() + q = q.In("id", unionSchedtagQ) + } + return q, nil } diff --git a/pkg/mcclient/options/compute/schedtags.go b/pkg/mcclient/options/compute/schedtags.go index 7bafbaa36b..7601e4624f 100644 --- a/pkg/mcclient/options/compute/schedtags.go +++ b/pkg/mcclient/options/compute/schedtags.go @@ -56,8 +56,9 @@ func (o SchedtagSetOptions) Params() (jsonutils.JSONObject, error) { type SchedtagListOptions struct { baseoptions.BaseListOptions - Type string `help:"Filter by resource type"` - CloudproviderId string `help:"Filter by cloudprovider id"` + Type string `help:"Filter by resource type"` + CloudproviderId string `help:"Filter by cloudprovider id"` + ZoneId []string `help:"Filter by zone id"` } func (o SchedtagListOptions) Params() (jsonutils.JSONObject, error) { @@ -72,7 +73,9 @@ func (o SchedtagListOptions) Params() (jsonutils.JSONObject, error) { if len(o.CloudproviderId) > 0 { params.Add(jsonutils.NewString(o.CloudproviderId), "cloudprovider_id") } - + if len(o.ZoneId) > 0 { + params.Add(jsonutils.NewStringArray(o.ZoneId), "zone_id") + } return params, nil }