Skip to content

Commit

Permalink
fix: clone dashboard parameters (#20587)
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 Jun 19, 2024
1 parent 799c113 commit a6ccf0a
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 18 deletions.
101 changes: 101 additions & 0 deletions cmd/climc/shell/yunionconf/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
package yunionconf

import (
"fmt"

"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/printutils"
"yunion.io/x/pkg/util/shellutils"

api "yunion.io/x/onecloud/pkg/apis/yunionconf"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/modules/identity"
"yunion.io/x/onecloud/pkg/mcclient/modules/yunionconf"
Expand Down Expand Up @@ -241,4 +246,100 @@ func init() {
printObject(parameter)
return nil
})

type ParameterCloneOptions struct {
User string `help:"Clone parameter of specificated user id"`
Service string `help:"Clone parameter of specificated service id"`
NAME string `help:"The name of parameter"`
DestUser string `help:"destination user id of clone action"`
DestService string `help:"destination service id of clone action"`
DestName string `help:"destination parameter name of clone action, may be empty"`
}
R(&ParameterCloneOptions{}, "parameter-clone", "clone parameter", func(s *mcclient.ClientSession, args *ParameterCloneOptions) error {
input := api.ParameterCloneInput{}

input.DestName = args.DestName

if len(args.DestUser) > 0 {
input.DestNs = api.NAMESPACE_USER
input.DestNsId = args.DestUser
} else if len(args.DestService) > 0 {
input.DestNs = api.NAMESPACE_SERVICE
input.DestNsId = args.DestService
} else {
return errors.Wrap(httperrors.ErrMissingParameter, "either dest_user or dest_service must be specified")
}
params := jsonutils.Marshal(input).(*jsonutils.JSONDict)

var parameter jsonutils.JSONObject
var err error
if len(args.User) > 0 {
parameter, err = yunionconf.Parameters.PerformActionInContext(s, args.NAME, "clone", params, &identity.UsersV3, args.User)
} else if len(args.Service) > 0 {
parameter, err = yunionconf.Parameters.PerformActionInContext(s, args.NAME, "clone", params, &identity.ServicesV3, args.Service)
} else {
parameter, err = yunionconf.Parameters.PerformAction(s, args.NAME, "clone", params)
}

if err != nil {
return err
}
printObject(parameter)
return nil
})

type ParameterCloneDashboardOptions struct {
SCOPE string `help:"dashboard scope" choices:"system|domain|project"`
SRC string `help:"source user id"`
DST string `help:"destination user id"`
}
R(&ParameterCloneDashboardOptions{}, "parameter-clone-dashboard", "clone dashboard parameter", func(s *mcclient.ClientSession, args *ParameterCloneDashboardOptions) error {
cloneUserParams := func(srcUid, destUid, name string) (jsonutils.JSONObject, error) {
paramId, err := yunionconf.Parameters.GetIdInContext(s, name, nil, &identity.UsersV3, srcUid)
if err != nil {
return nil, errors.Wrapf(err, "GetByName %s", name)
}
input := api.ParameterCloneInput{
DestNs: api.NAMESPACE_USER,
DestNsId: destUid,
}
params := jsonutils.Marshal(input).(*jsonutils.JSONDict)
return yunionconf.Parameters.PerformActionInContext(s, paramId, "clone", params, &identity.UsersV3, srcUid)
}

rootName := fmt.Sprintf("dashboard_%s", args.SCOPE)

confs := []struct {
Id string
Name string
}{}

paramObj, err := yunionconf.Parameters.GetInContext(s, rootName, nil, &identity.UsersV3, args.SRC)
if err != nil {
return errors.Wrap(err, "GetParameter")
}

err = paramObj.Unmarshal(&confs, "value")
if err != nil {
return errors.Wrap(err, "unmarshal value")
}

for _, conf := range confs {
_, err := cloneUserParams(args.SRC, args.DST, conf.Id)
if err != nil {
return errors.Wrapf(err, "clone %s", conf.Id)
}
fmt.Println("cloned", conf.Id)
}

// finally copy the root
_, err = cloneUserParams(args.SRC, args.DST, rootName)
if err != nil {
return errors.Wrapf(err, "clone %s", rootName)
}
fmt.Println("cloned", rootName)

return nil
})

}
6 changes: 6 additions & 0 deletions pkg/apis/yunionconf/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ const (
ANY_DOMAIN_ID = "[any_domain_id]"
ANY_PROJECT_ID = "[any_project_id]"
)

const (
NAMESPACE_USER = "user"
NAMESPACE_SERVICE = "service"
NAMESPACE_BUG_REPORT = "bug-report"
)
6 changes: 6 additions & 0 deletions pkg/apis/yunionconf/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ type ParameterListInput struct {
Name []string `json:"name"`
}

type ParameterCloneInput struct {
DestNs string `json:"dest_ns"`
DestNsId string `json:"dest_ns_id"`
DestName string `json:"dest_name"`
}

type ScopedPolicyCreateInput struct {
apis.InfrasResourceBaseCreateInput

Expand Down
Loading

0 comments on commit a6ccf0a

Please sign in to comment.