Skip to content

Commit

Permalink
fix: filter task by project (#20195)
Browse files Browse the repository at this point in the history
Co-authored-by: Qiu Jian <[email protected]>
  • Loading branch information
swordqiu and Qiu Jian authored May 4, 2024
1 parent 8b5afbf commit 5b80297
Show file tree
Hide file tree
Showing 18 changed files with 724 additions and 229 deletions.
87 changes: 63 additions & 24 deletions cmd/climc/shell/misc/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,80 @@
package misc

import (
"fmt"

"yunion.io/x/jsonutils"

"yunion.io/x/onecloud/pkg/apis"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/modulebase"
"yunion.io/x/onecloud/pkg/mcclient/modules/compute"
"yunion.io/x/onecloud/pkg/mcclient/modules/devtool"
"yunion.io/x/onecloud/pkg/mcclient/modules/identity"
"yunion.io/x/onecloud/pkg/mcclient/modules/image"
"yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
"yunion.io/x/onecloud/pkg/mcclient/modules/notify"
)

func init() {
type RegionTaskListOptions struct {
ObjName string `help:"object name"`
ObjId string `help:"object id"`
TaskName string `help:"task name"`
cmds := []struct {
service string
manager modulebase.Manager
}{
{
service: "region",
manager: &compute.ComputeTasks,
},
{
service: "devtool",
manager: &devtool.DevtoolTasks,
},
{
service: "image",
manager: &image.Tasks,
},
{
service: "identity",
manager: &identity.Tasks,
},
{
service: "k8s",
manager: k8s.KubeTasks,
},
{
service: "notify",
manager: &notify.Tasks,
},
}
R(&RegionTaskListOptions{}, "region-task-list", "List tasks on region server", func(s *mcclient.ClientSession, args *RegionTaskListOptions) error {
params := jsonutils.Marshal(args)
result, err := compute.ComputeTasks.List(s, params)
if err != nil {
return err
for i := range cmds {
c := cmds[i]
type TaskListOptions struct {
apis.TaskListInput
}
printList(result, compute.ComputeTasks.GetColumns(s))
return nil
})
R(&TaskListOptions{}, fmt.Sprintf("%s-task-list", c.service), "List tasks on region server", func(s *mcclient.ClientSession, args *TaskListOptions) error {
params := jsonutils.Marshal(args)
result, err := c.manager.List(s, params)
if err != nil {
return err
}
printList(result, c.manager.GetColumns(s))
return nil
})

type RegionTaskShowOptions struct {
ID string `help:"ID or name of the task"`
}
R(&RegionTaskShowOptions{}, "region-task-show", "Show details of a region task", func(s *mcclient.ClientSession, args *RegionTaskShowOptions) error {
result, err := compute.ComputeTasks.Get(s, args.ID, nil)
if err != nil {
return err
type TaskShowOptions struct {
ID string `help:"ID or name of the task"`
}
printObject(result)
return nil
})
R(&TaskShowOptions{}, "region-task-show", "Show details of a region task", func(s *mcclient.ClientSession, args *TaskShowOptions) error {
result, err := c.manager.GetById(s, args.ID, nil)
if err != nil {
return err
}
printObject(result)
return nil
})
}

type TaskListOptions struct {
/*type TaskListOptions struct {
ObjName string `help:"object name"`
ObjId string `help:"object id"`
TaskName string `help:"task name"`
Expand Down Expand Up @@ -80,6 +119,6 @@ func init() {
}
printObject(result)
return nil
})
})*/

}
6 changes: 3 additions & 3 deletions pkg/ansibleserver/models/ansibleplaybook_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
"yunion.io/x/onecloud/pkg/cloudcommon/workmanager"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/auth"
"yunion.io/x/onecloud/pkg/mcclient/modules"
"yunion.io/x/onecloud/pkg/mcclient/modules/compute"
"yunion.io/x/onecloud/pkg/mcclient/modules/devtool"
"yunion.io/x/onecloud/pkg/util/ansible"
"yunion.io/x/onecloud/pkg/util/ansiblev2"
)
Expand Down Expand Up @@ -216,7 +216,7 @@ var PlaybookWorker *workmanager.SWorkManager
func taskFailed(ctx context.Context, reason string) {
if taskId := ctx.Value(appctx.APP_CONTEXT_KEY_TASK_ID); taskId != nil {
session := auth.GetAdminSessionWithInternal(ctx, "")
modules.TaskFailed(&compute.DevtoolTasks, session, taskId.(string), reason)
devtool.DevtoolTasks.TaskFailed2(session, taskId.(string), reason)
} else {
log.Warningf("Reqeuest task failed missing task id, with reason: %s", reason)
}
Expand All @@ -225,7 +225,7 @@ func taskFailed(ctx context.Context, reason string) {
func taskCompleted(ctx context.Context, data jsonutils.JSONObject) {
if taskId := ctx.Value(appctx.APP_CONTEXT_KEY_TASK_ID); taskId != nil {
session := auth.GetAdminSessionWithInternal(ctx, "")
modules.TaskComplete(&compute.DevtoolTasks, session, taskId.(string), data)
devtool.DevtoolTasks.TaskComplete(session, taskId.(string), data)
} else {
log.Warningf("Reqeuest task failed missing task id, with data: %v", data)
}
Expand Down
64 changes: 64 additions & 0 deletions pkg/apis/tasks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package apis

import (
"time"

"yunion.io/x/jsonutils"
)

type TaskListInput struct {
ModelBaseListInput
ProjectizedResourceListInput

Id []string `json:"id" help:"id filter"`
ObjId []string `json:"obj_id" help:"object id filter"`
ObjName []string `json:"obj_name" help:"object type (in singular form) filter"`
TaskName []string `json:"task_name" help:"task name filter"`
IsMulti *bool `json:"is_multi" negative:"is_single" help:"is multi task"`
IsComplete *bool `json:"is_complete" negative:"not_complete" help:"is task completed, either fail or complete"`
IsInit *bool `json:"is_init" negative:"not_init" help:"is task started?"`
Stage []string `json:"stage" help:"task stage"`
ParentId []string `json:"parent_id" help:"filter tasks by parent_task_id"`
IsRoot *bool `json:"is_root" help:"filter root tasks"`
}

type TaskDetails struct {
ModelBaseDetails
ProjectizedResourceInfo

// 资源创建时间
CreatedAt time.Time `json:"created_at"`
// 资源更新时间
UpdatedAt time.Time `json:"updated_at"`
// 资源被更新次数
UpdateVersion int `json:"update_version"`
// 开始任务时间
StartAt time.Time `json:"start_at"`
// 完成任务时间
EndAt time.Time `json:"end_at"`

DomainId string `json:"domain_id"`
ProjectId string `json:"tenant_id"`

Id string
ObjName string
ObjId string
TaskName string
Params *jsonutils.JSONDict
Stage string
ParentTaskId string
}
3 changes: 2 additions & 1 deletion pkg/cloudcommon/db/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ func ValidateProjectizedResourceInput(ctx context.Context, input apis.Projectize
}

func (manager *SProjectizedResourceBaseManager) ListItemExportKeys(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, keys stringutils2.SSortedStrings) (*sqlchemy.SQuery, error) {
q, err := manager.SDomainizedResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
var err error
q, err = manager.SDomainizedResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
if err != nil {
return nil, errors.Wrap(err, "SDomainizedResourceBaseManager.ListItemExportKeys")
}
Expand Down
52 changes: 52 additions & 0 deletions pkg/cloudcommon/db/taskman/taskobjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,24 @@
package taskman

import (
"context"
"database/sql"

"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/rbacscope"
"yunion.io/x/sqlchemy"

"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/util/stringutils2"
)

type STaskObjectManager struct {
db.SModelBaseManager
db.SProjectizedResourceBaseManager
}

var TaskObjectManager *STaskObjectManager
Expand All @@ -34,6 +43,7 @@ func init() {

type STaskObject struct {
db.SModelBase
db.SProjectizedResourceBase

TaskId string `width:"36" charset:"ascii" nullable:"false" primary:"true" index:"true"` // Column(VARCHAR(36, charset='ascii'), nullable=False, primary_key=True, index=True)
ObjId string `width:"36" charset:"ascii" nullable:"false" primary:"true"` // Column(VARCHAR(36, charset='ascii'), nullable=False, primary_key=True)
Expand Down Expand Up @@ -62,3 +72,45 @@ func (manager *STaskObjectManager) GetObjectIds(task *STask) []string {
}
return ret
}

func (manager *STaskObjectManager) FetchOwnerId(ctx context.Context, data jsonutils.JSONObject) (mcclient.IIdentityProvider, error) {
return manager.SProjectizedResourceBaseManager.FetchOwnerId(ctx, data)
}

func (manager *STaskObjectManager) FilterByOwner(ctx context.Context, q *sqlchemy.SQuery, man db.FilterByOwnerProvider, userCred mcclient.TokenCredential, owner mcclient.IIdentityProvider, scope rbacscope.TRbacScope) *sqlchemy.SQuery {
return manager.SProjectizedResourceBaseManager.FilterByOwner(ctx, q, man, userCred, owner, scope)
}

func (manager *STaskObjectManager) ListItemExportKeys(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, keys stringutils2.SSortedStrings) (*sqlchemy.SQuery, error) {
var err error
q, err = manager.SModelBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
if err != nil {
return nil, errors.Wrap(err, "SModelBaseManager.ListItemExportKeys")
}
q, err = manager.SProjectizedResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
if err != nil {
return nil, errors.Wrap(err, "SProjectizedResourceBaseManager.ListItemExportKeys")
}
return q, nil
}

func (manager *STaskObjectManager) QueryDistinctExtraField(q *sqlchemy.SQuery, field string) (*sqlchemy.SQuery, error) {
var err error
q, err = manager.SModelBaseManager.QueryDistinctExtraField(q, field)
if err == nil {
return q, nil
}
q, err = manager.SProjectizedResourceBaseManager.QueryDistinctExtraField(q, field)
if err == nil {
return q, nil
}
return q, httperrors.ErrNotFound
}

func (manager *STaskObjectManager) ResourceScope() rbacscope.TRbacScope {
return manager.SProjectizedResourceBaseManager.ResourceScope()
}

func (taskObj *STaskObject) GetOwnerId() mcclient.IIdentityProvider {
return taskObj.SProjectizedResourceBase.GetOwnerId()
}
Loading

0 comments on commit 5b80297

Please sign in to comment.