mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-10-29 17:58:36 +08:00
Merge pull request #3423 from tb365/feature/tb-add-project-create-rpc
Feature/tb add project create rpc
This commit is contained in:
@@ -17,6 +17,8 @@ package modules
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
@@ -217,7 +219,7 @@ func (this *ProjectManagerV3) DoProjectBatchJoin(s *mcclient.ClientSession, para
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// Add Many user[uids] to project(pid) with role(rid)
|
||||
// Remove Many user[uids] to project(pid) with role(rid)
|
||||
func (this *ProjectManagerV3) DoProjectBatchDeleteUserGroup(s *mcclient.ClientSession, pid string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
// params format:
|
||||
// {
|
||||
@@ -279,6 +281,66 @@ func (this *ProjectManagerV3) FetchId(s *mcclient.ClientSession, project string,
|
||||
return this.GetId(s, project, query)
|
||||
}
|
||||
|
||||
func (this *ProjectManagerV3) JoinProject(s *mcclient.ClientSession, rid, pid, uid string) error {
|
||||
_, err := RolesV3.PutInContexts(s, rid, nil, []modulebase.ManagerContext{{&Projects, pid}, {&UsersV3, uid}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// create project and attach users & roles
|
||||
func (this *ProjectManagerV3) DoCreateProject(s *mcclient.ClientSession, p jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
/*
|
||||
params format:
|
||||
{
|
||||
user : ["TestA", "TestB"],
|
||||
role : ["RoleA", "RoleB"],
|
||||
}
|
||||
*/
|
||||
params := p.(*jsonutils.JSONDict)
|
||||
_user, _ := params.Get("user")
|
||||
_role, _ := params.Get("role")
|
||||
params.Remove("user")
|
||||
params.Remove("role")
|
||||
|
||||
// create project
|
||||
response, err := Projects.Create(s, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pid, err := response.GetString("id")
|
||||
if err != nil {
|
||||
return nil, httperrors.NewResourceNotFoundError("project is not found")
|
||||
}
|
||||
|
||||
// assgin users to project
|
||||
users := _user.(*jsonutils.JSONArray).GetStringArray()
|
||||
roles := _role.(*jsonutils.JSONArray).GetStringArray()
|
||||
if len(users) > 0 && len(roles) > 0 {
|
||||
var projectG errgroup.Group
|
||||
|
||||
for i := range users {
|
||||
uid := users[i]
|
||||
for j := range roles {
|
||||
rid := roles[j]
|
||||
|
||||
projectG.Go(func() error {
|
||||
return this.JoinProject(s, rid, pid, uid)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if err := projectG.Wait(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
Projects = ProjectManagerV3{NewIdentityV3Manager("project", "projects",
|
||||
[]string{},
|
||||
|
||||
@@ -17,6 +17,8 @@ package modules
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
@@ -160,6 +162,54 @@ func (this *UserManagerV3) DoJoinGroups(s *mcclient.ClientSession, uid string, p
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// create user && assgin user with project_domain、project、role
|
||||
func (this *UserManagerV3) DoCreateUser(s *mcclient.ClientSession, p jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
// params format:
|
||||
// {
|
||||
// "project": ["projectA", "projectB"],
|
||||
// "role": ["RoleA", "RoleB"],
|
||||
// }
|
||||
params := p.(*jsonutils.JSONDict)
|
||||
_project, _ := params.Get("project")
|
||||
_role, _ := params.Get("role")
|
||||
params.Remove("project")
|
||||
params.Remove("role")
|
||||
|
||||
response, err := UsersV3.Create(s, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
uid, err := response.GetString("id")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// assgin project & role
|
||||
projects := _project.(*jsonutils.JSONArray).GetStringArray()
|
||||
roles := _role.(*jsonutils.JSONArray).GetStringArray()
|
||||
if len(projects) > 0 && len(roles) > 0 {
|
||||
var projectG errgroup.Group
|
||||
|
||||
for i := range projects {
|
||||
pid := projects[i]
|
||||
for j := range roles {
|
||||
rid := roles[j]
|
||||
|
||||
projectG.Go(func() error {
|
||||
return Projects.JoinProject(s, rid, pid, uid)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if err := projectG.Wait(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (this *UserManagerV3) FetchId(s *mcclient.ClientSession, user string, domain string) (string, error) {
|
||||
userQuery := jsonutils.NewDict()
|
||||
if len(domain) > 0 {
|
||||
|
||||
Reference in New Issue
Block a user