mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
修正:policy默认采用json解码,只有climc才采用yaml
This commit is contained in:
@@ -20,9 +20,11 @@ func init() {
|
||||
Offset int64 `help:"Offset, default 0, i.e. no offset"`
|
||||
Search string `help:"Search by name"`
|
||||
Type string `help:"filter by type"`
|
||||
Format string `help:"policy format, default to yaml" default:"yaml" choices:"yaml|json"`
|
||||
}
|
||||
R(&PolicyListOptions{}, "policy-list", "List all policies", func(s *mcclient.ClientSession, args *PolicyListOptions) error {
|
||||
params := jsonutils.NewDict()
|
||||
params.Add(jsonutils.NewString(args.Format), "format")
|
||||
if len(args.Search) > 0 {
|
||||
params.Add(jsonutils.NewString(args.Search), "type__icontains")
|
||||
}
|
||||
@@ -115,10 +117,13 @@ func init() {
|
||||
})
|
||||
|
||||
type PolicyShowOptions struct {
|
||||
ID string `help:"ID of policy"`
|
||||
ID string `help:"ID of policy"`
|
||||
Format string `help:"policy format, default yaml" default:"yaml" choices:"yaml|json"`
|
||||
}
|
||||
R(&PolicyShowOptions{}, "policy-show", "Show policy", func(s *mcclient.ClientSession, args *PolicyShowOptions) error {
|
||||
result, err := modules.Policies.Get(s, args.ID, nil)
|
||||
query := jsonutils.NewDict()
|
||||
query.Add(jsonutils.NewString(args.Format), "format")
|
||||
result, err := modules.Policies.Get(s, args.ID, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -134,7 +139,9 @@ func init() {
|
||||
ID string `help:"ID of policy"`
|
||||
}
|
||||
R(&PolicyEditOptions{}, "policy-edit", "Edit and update policy", func(s *mcclient.ClientSession, args *PolicyEditOptions) error {
|
||||
result, err := modules.Policies.Get(s, args.ID, nil)
|
||||
query := jsonutils.NewDict()
|
||||
query.Add(jsonutils.NewString("yaml"), "format")
|
||||
result, err := modules.Policies.Get(s, args.ID, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -59,16 +59,11 @@ func parseJsonPolicy(obj jsonutils.JSONObject) (string, rbacutils.SRbacPolicy, e
|
||||
return "", policy, err
|
||||
}
|
||||
|
||||
blobStr, err := obj.GetString("policy")
|
||||
blob, err := obj.Get("policy")
|
||||
if err != nil {
|
||||
log.Errorf("get blob error %s", err)
|
||||
return "", policy, err
|
||||
}
|
||||
blob, err := jsonutils.ParseYAML(blobStr)
|
||||
if err != nil {
|
||||
log.Errorf("parse blob json error %s", err)
|
||||
return "", policy, err
|
||||
}
|
||||
err = policy.Decode(blob)
|
||||
if err != nil {
|
||||
log.Errorf("policy decode error %s", err)
|
||||
|
||||
@@ -39,10 +39,24 @@ var (
|
||||
"readmarks",
|
||||
"infos",
|
||||
}
|
||||
logAdminResources = []string {
|
||||
yunionconfAdminResources = []string{}
|
||||
logAdminResources = []string{}
|
||||
|
||||
adminResources = map[string][]string{
|
||||
"compute": computeAdminResources,
|
||||
"notify": notifyAdminResources,
|
||||
"meter": meterAdminResources,
|
||||
"k8s": k8sAdminResources,
|
||||
"yunionagent": yunionagentAdminResources,
|
||||
"yunionconf": yunionconfAdminResources,
|
||||
"log": logAdminResources,
|
||||
}
|
||||
)
|
||||
|
||||
func GetAdminResources() map[string][]string {
|
||||
return adminResources
|
||||
}
|
||||
|
||||
func isAdminResource(service string, resource string) bool {
|
||||
switch service {
|
||||
case "identity":
|
||||
@@ -79,4 +93,5 @@ func isAdminResource(service string, resource string) bool {
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -6,17 +6,17 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
)
|
||||
|
||||
func (this *ResourceManager) filterSingleResult(session *mcclient.ClientSession, result jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
func (this *ResourceManager) filterSingleResult(session *mcclient.ClientSession, result jsonutils.JSONObject, query jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
if this.enableFilter && this.readFilter != nil {
|
||||
return this.readFilter(session, result)
|
||||
return this.readFilter(session, result, query)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (this *ResourceManager) filterListResults(session *mcclient.ClientSession, results *ListResult) (*ListResult, error) {
|
||||
func (this *ResourceManager) filterListResults(session *mcclient.ClientSession, results *ListResult, query jsonutils.JSONObject) (*ListResult, error) {
|
||||
if this.enableFilter && this.readFilter != nil {
|
||||
for i := 0; i < len(results.Data); i += 1 {
|
||||
val, err := this.readFilter(session, results.Data[i])
|
||||
val, err := this.readFilter(session, results.Data[i], query)
|
||||
if err == nil {
|
||||
results.Data[i] = val
|
||||
} else {
|
||||
|
||||
@@ -34,7 +34,7 @@ func (this *JointResourceManager) Get(s *mcclient.ClientSession, mid, sid string
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.filterSingleResult(s, result)
|
||||
return this.filterSingleResult(s, result, params)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -62,7 +62,7 @@ func (this *JointResourceManager) ListDescendent(s *mcclient.ClientSession, mid
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.filterListResults(s, results)
|
||||
return this.filterListResults(s, results, params)
|
||||
}
|
||||
|
||||
func (this *JointResourceManager) ListDescendent2(s *mcclient.ClientSession, sid string, params jsonutils.JSONObject) (*ListResult, error) {
|
||||
@@ -81,7 +81,7 @@ func (this *JointResourceManager) ListAscendent(s *mcclient.ClientSession, mid s
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.filterListResults(s, results)
|
||||
return this.filterListResults(s, results, params)
|
||||
}
|
||||
|
||||
/* func (this *JointResourceManager) Exists(s *mcclient.ClientSession, mid, sid string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
@@ -101,7 +101,7 @@ func (this *JointResourceManager) Attach(s *mcclient.ClientSession, mid, sid str
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.filterSingleResult(s, result)
|
||||
return this.filterSingleResult(s, result, nil)
|
||||
}
|
||||
|
||||
func (this *JointResourceManager) BatchAttach(s *mcclient.ClientSession, mid string, sids []string, params jsonutils.JSONObject) []SubmitResult {
|
||||
@@ -122,7 +122,7 @@ func (this *JointResourceManager) Detach(s *mcclient.ClientSession, mid, sid str
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.filterSingleResult(s, result)
|
||||
return this.filterSingleResult(s, result, nil)
|
||||
}
|
||||
|
||||
func (this *JointResourceManager) BatchDetach(s *mcclient.ClientSession, mid string, sids []string) []SubmitResult {
|
||||
@@ -143,7 +143,7 @@ func (this *JointResourceManager) Update(s *mcclient.ClientSession, mid, sid str
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.filterSingleResult(s, result)
|
||||
return this.filterSingleResult(s, result, nil)
|
||||
}
|
||||
|
||||
func (this *JointResourceManager) Patch(s *mcclient.ClientSession, mid, sid string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
@@ -152,5 +152,5 @@ func (this *JointResourceManager) Patch(s *mcclient.ClientSession, mid, sid stri
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.filterSingleResult(s, result)
|
||||
return this.filterSingleResult(s, result, nil)
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ var (
|
||||
EndpointsV3 ResourceManager
|
||||
)
|
||||
|
||||
func endpointsV3ReadFilter(s *mcclient.ClientSession, result jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
func endpointsV3ReadFilter(s *mcclient.ClientSession, result jsonutils.JSONObject, query jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
resultDict := result.(*jsonutils.JSONDict)
|
||||
serviceId, _ := result.GetString("service_id")
|
||||
service, err := cachedResourceManager.getById(&ServicesV3, s, serviceId)
|
||||
|
||||
@@ -11,31 +11,43 @@ type SPolicyManager struct {
|
||||
|
||||
var Policies SPolicyManager
|
||||
|
||||
func policyReadFilter(session *mcclient.ClientSession, s jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
func policyReadFilter(session *mcclient.ClientSession, s jsonutils.JSONObject, query jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
ss := s.(*jsonutils.JSONDict)
|
||||
ret := ss.CopyIncludes("id", "type")
|
||||
blobStr, _ := ss.GetString("blob")
|
||||
if len(blobStr) > 0 {
|
||||
blobJson, _ := jsonutils.ParseString(blobStr)
|
||||
var policy string
|
||||
if blobJson != nil {
|
||||
policy = blobJson.YAMLString()
|
||||
var format string
|
||||
if query != nil {
|
||||
format, _ = query.GetString("format")
|
||||
}
|
||||
if format == "yaml" {
|
||||
var policy string
|
||||
if blobJson != nil {
|
||||
policy = blobJson.YAMLString()
|
||||
}
|
||||
ret.Add(jsonutils.NewString(policy), "policy")
|
||||
} else {
|
||||
ret.Add(blobJson, "policy")
|
||||
}
|
||||
ret.Add(jsonutils.NewString(policy), "policy")
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func policyWriteFilter(session *mcclient.ClientSession, s jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
func policyWriteFilter(session *mcclient.ClientSession, s jsonutils.JSONObject, query jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
ret := jsonutils.NewDict()
|
||||
if s.Contains("policy") {
|
||||
blobYaml, err := s.GetString("policy")
|
||||
blobJson, err := s.Get("policy")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
blobJson, err := jsonutils.ParseYAML(blobYaml)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
switch blob := blobJson.(type) {
|
||||
case *jsonutils.JSONString:
|
||||
blobStr, _ := blob.GetString()
|
||||
blobJson, err = jsonutils.ParseYAML(blobStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
ret.Add(jsonutils.NewString(blobJson.String()), "blob")
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
)
|
||||
|
||||
type TResourceFilter func(*mcclient.ClientSession, jsonutils.JSONObject) (jsonutils.JSONObject, error)
|
||||
type TResourceFilter func(*mcclient.ClientSession, jsonutils.JSONObject, jsonutils.JSONObject) (jsonutils.JSONObject, error)
|
||||
|
||||
const (
|
||||
DEFAULT_NAME_FIELD_NAME = "name"
|
||||
@@ -126,7 +126,7 @@ func (this *ResourceManager) GetByIdInContexts(session *mcclient.ClientSession,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.filterSingleResult(session, obj)
|
||||
return this.filterSingleResult(session, obj, params)
|
||||
}
|
||||
|
||||
func (this *ResourceManager) GetByName(session *mcclient.ClientSession, name string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
@@ -255,7 +255,7 @@ func (this *ResourceManager) ListInContexts(session *mcclient.ClientSession, par
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.filterListResults(session, results)
|
||||
return this.filterListResults(session, results, params)
|
||||
}
|
||||
|
||||
func (this *ResourceManager) Head(session *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
@@ -278,14 +278,14 @@ func (this *ResourceManager) HeadInContexts(session *mcclient.ClientSession, id
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.filterSingleResult(session, result)
|
||||
return this.filterSingleResult(session, result, params)
|
||||
}
|
||||
|
||||
func (this *ResourceManager) params2Body(s *mcclient.ClientSession, params jsonutils.JSONObject) *jsonutils.JSONDict {
|
||||
body := jsonutils.NewDict()
|
||||
if params != nil {
|
||||
if this.enableFilter && this.writeFilter != nil {
|
||||
val, err := this.writeFilter(s, params)
|
||||
val, err := this.writeFilter(s, params, nil)
|
||||
if err == nil {
|
||||
params = val
|
||||
} else {
|
||||
@@ -311,7 +311,7 @@ func (this *ResourceManager) CreateInContexts(session *mcclient.ClientSession, p
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.filterSingleResult(session, result)
|
||||
return this.filterSingleResult(session, result, nil)
|
||||
}
|
||||
|
||||
func (this *ResourceManager) BatchCreate(session *mcclient.ClientSession, params jsonutils.JSONObject, count int) []SubmitResult {
|
||||
@@ -349,7 +349,7 @@ func (this *ResourceManager) BatchCreateInContexts(session *mcclient.ClientSessi
|
||||
code, _ := json.Int("status")
|
||||
dat, _ := json.Get("body")
|
||||
if this.enableFilter && this.readFilter != nil {
|
||||
val, err := this.readFilter(session, dat)
|
||||
val, err := this.readFilter(session, dat, nil)
|
||||
if err != nil {
|
||||
log.Warningf("readFilter fail for %s: %s", dat, err)
|
||||
} else {
|
||||
@@ -381,7 +381,7 @@ func (this *ResourceManager) PutInContexts(session *mcclient.ClientSession, id s
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.filterSingleResult(session, result)
|
||||
return this.filterSingleResult(session, result, nil)
|
||||
}
|
||||
|
||||
func (this *ResourceManager) BatchUpdate(session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject) []SubmitResult {
|
||||
@@ -416,7 +416,7 @@ func (this *ResourceManager) PatchInContexts(session *mcclient.ClientSession, id
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.filterSingleResult(session, result)
|
||||
return this.filterSingleResult(session, result, nil)
|
||||
}
|
||||
|
||||
func (this *ResourceManager) BatchPatch(session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject) []SubmitResult {
|
||||
@@ -447,7 +447,7 @@ func (this *ResourceManager) PerformActionInContexts(session *mcclient.ClientSes
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.filterSingleResult(session, result)
|
||||
return this.filterSingleResult(session, result, nil)
|
||||
}
|
||||
|
||||
func (this *ResourceManager) PerformClassAction(session *mcclient.ClientSession, action string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
@@ -508,7 +508,7 @@ func (this *ResourceManager) deleteInContexts(session *mcclient.ClientSession, i
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.filterSingleResult(session, result)
|
||||
return this.filterSingleResult(session, result, nil)
|
||||
}
|
||||
|
||||
func (this *ResourceManager) BatchDelete(session *mcclient.ClientSession, idlist []string, body jsonutils.JSONObject) []SubmitResult {
|
||||
|
||||
Reference in New Issue
Block a user