Skip to content

Commit

Permalink
fix: code autogen issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiu Jian committed Apr 24, 2020
1 parent fd82d1e commit 7ab3a06
Show file tree
Hide file tree
Showing 47 changed files with 271 additions and 128 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,11 @@ gen-swagger: gen-swagger-check
$(ROOT_DIR)/scripts/codegen.py swagger-code
$(ROOT_DIR)/scripts/codegen.py swagger-yaml

swagger-serve: gen-model-api gen-swagger
swagger-serve-only:
$(ROOT_DIR)/scripts/codegen.py swagger-serve

swagger-serve: gen-model-api gen-swagger swagger-serve-only

swagger-site: gen-model-api gen-swagger
$(ROOT_DIR)/scripts/codegen.py swagger-site

Expand Down
25 changes: 25 additions & 0 deletions cmd/climc/shell/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,29 @@ func init() {
printObject(sp)
return nil
})

type GroupAddUsersOptions struct {
GROUP string `help:"group Id or Name" json:"-"`
USER []string `help:"list of user id or name" json:"user"`
}
R(&GroupAddUsersOptions{}, "group-add-users", "Add users into group",
func(s *mcclient.ClientSession, args *GroupAddUsersOptions) error {
params := jsonutils.Marshal(args)
result, err := modules.Groups.PerformAction(s, args.GROUP, "add-users", params)
if err != nil {
return err
}
printObject(result)
return nil
})
R(&GroupAddUsersOptions{}, "group-remove-users", "Add users into group",
func(s *mcclient.ClientSession, args *GroupAddUsersOptions) error {
params := jsonutils.Marshal(args)
result, err := modules.Groups.PerformAction(s, args.GROUP, "remove-users", params)
if err != nil {
return err
}
printObject(result)
return nil
})
}
2 changes: 1 addition & 1 deletion pkg/apis/compute/cloudaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ type CloudaccountUpdateInput struct {
}

type CloudaccountPerformPublicInput struct {
apis.PerformPublicInput
apis.PerformPublicDomainInput

// 共享模式,可能值为provider_domain, system
// example: provider_domain
Expand Down
21 changes: 16 additions & 5 deletions pkg/apis/identity/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ type EndpointListInput struct {
}

type SJoinProjectsInput struct {
Projects []string
Roles []string
Projects []string `json:"projects"`
Roles []string `json:"roles"`
}

func (input SJoinProjectsInput) Validate() error {
Expand All @@ -209,11 +209,12 @@ func (input SJoinProjectsInput) Validate() error {
}

type SProjectRole struct {
Project string
Role string
Project string `json:"project"`
Role string `json:"role"`
}

type SLeaveProjectsInput struct {
ProjectRoles []SProjectRole
ProjectRoles []SProjectRole `json:"project_roles"`
}

func (input SLeaveProjectsInput) Validate() error {
Expand Down Expand Up @@ -451,3 +452,13 @@ type RoleCreateInput struct {

apis.SharableResourceBaseCreateInput
}

type PerformGroupAddUsersInput struct {
// 带添加用户列表(ID或名称)
User []string `json:"user"`
}

type PerformGroupRemoveUsersInput struct {
// 带删除用户列表(ID或名称)
User []string `json:"user"`
}
38 changes: 34 additions & 4 deletions pkg/apis/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,22 @@ type GetDetailsStatusOutput struct {
Status string `json:"status"`
}

type PerformPublicInput struct {
type PerformPublicDomainInput struct {
// 共享项目资源的共享范围,可能的值为:project, domain和system
// pattern: project|domain|system
Scope string `json:"scope"`

// 如果共享范围为项目,则在此列表中指定共享的目标项目
SharedProjects []string `json:"shared_projects"`

// 如果共享范围为域,则在此列表中指定共享的目标域
SharedDomains []string `json:"shared_domains"`
}

type PerformPublicProjectInput struct {
PerformPublicDomainInput

// 如果共享范围为项目,则在此列表中指定共享的目标项目
SharedProjects []string `json:"shared_projects"`
}

type PerformPrivateInput struct {
}

Expand Down Expand Up @@ -249,3 +253,29 @@ type OpsLogCreateInput struct {
OwnerDomainId string `json:"owner_domain_id"`
OwnerProjectId string `json:"owner_tenant_id"`
}

// 设置资源的标签(元数据)输入
type PerformMetadataInput map[string]string

// 设置资源的用户标签(元数据)输入
type PerformUserMetadataInput map[string]string

// 全量替换资源的用户标签(元数据)输入
type PerformSetUserMetadataInput map[string]string

// 获取资源的元数据输入
type GetMetadataInput struct {
// 指定需要获取的所有标签的KEY列表,如果列表为空,则获取全部标签
// 标签分为
//
// | 类型 | 说明 |
// |----------|---------------------------------------------|
// | 系统标签 | 平台定义的标签 |
// | 用户标签 | key以user:为前缀,用户自定义标签 |
// | 外部标签 | key以ext:为前缀,为从其他平台同步过来的标签 |
//
Field []string `json:"field"`
}

// 获取资源标签(元数据)输出
type GetMetadataOutput map[string]string
2 changes: 1 addition & 1 deletion pkg/apis/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type ModelBaseDetails struct {
// example: true
CanUpdate bool `json:"can_update"`

// 资源不能删除的原因
// 资源不能更新的原因
UpdateFailReason string `json:"update_fail_reason"`
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/cloudcommon/db/enabledstatusdomainresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (self *SEnabledStatusDomainLevelResourceBase) AllowPerformEnable(ctx contex
return IsDomainAllowPerform(userCred, self, "enable")
}

// 启用资源
func (self *SEnabledStatusDomainLevelResourceBase) PerformEnable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformEnableInput) (jsonutils.JSONObject, error) {
err := EnabledPerformEnable(self, ctx, userCred, true)
if err != nil {
Expand All @@ -59,6 +60,7 @@ func (self *SEnabledStatusDomainLevelResourceBase) AllowPerformDisable(ctx conte
return IsDomainAllowPerform(userCred, self, "disable")
}

// 禁用资源
func (self *SEnabledStatusDomainLevelResourceBase) PerformDisable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformDisableInput) (jsonutils.JSONObject, error) {
err := EnabledPerformEnable(self, ctx, userCred, false)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/cloudcommon/db/enabledstatusinfrasresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (self *SEnabledStatusInfrasResourceBase) AllowPerformEnable(ctx context.Con
return IsDomainAllowPerform(userCred, self, "enable")
}

// 启用资源
func (self *SEnabledStatusInfrasResourceBase) PerformEnable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformEnableInput) (jsonutils.JSONObject, error) {
err := EnabledPerformEnable(self, ctx, userCred, true)
if err != nil {
Expand All @@ -59,6 +60,7 @@ func (self *SEnabledStatusInfrasResourceBase) AllowPerformDisable(ctx context.Co
return IsDomainAllowPerform(userCred, self, "disable")
}

// 禁用资源
func (self *SEnabledStatusInfrasResourceBase) PerformDisable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformDisableInput) (jsonutils.JSONObject, error) {
err := EnabledPerformEnable(self, ctx, userCred, false)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/cloudcommon/db/enabledstatusstandalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (self *SEnabledStatusStandaloneResourceBase) AllowPerformEnable(ctx context
return IsAdminAllowPerform(userCred, self, "enable")
}

// 启用资源
func (self *SEnabledStatusStandaloneResourceBase) PerformEnable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformEnableInput) (jsonutils.JSONObject, error) {
err := EnabledPerformEnable(self, ctx, userCred, true)
if err != nil {
Expand All @@ -59,6 +60,7 @@ func (self *SEnabledStatusStandaloneResourceBase) AllowPerformDisable(ctx contex
return IsAdminAllowPerform(userCred, self, "disable")
}

// 禁用资源
func (self *SEnabledStatusStandaloneResourceBase) PerformDisable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformDisableInput) (jsonutils.JSONObject, error) {
err := EnabledPerformEnable(self, ctx, userCred, false)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/cloudcommon/db/infraresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ func (model *SInfrasResourceBase) IsShared() bool {
return SharableModelIsShared(model)
}

func (model *SInfrasResourceBase) AllowPerformPublic(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformPublicInput) bool {
func (model *SInfrasResourceBase) AllowPerformPublic(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformPublicDomainInput) bool {
return true
}

func (model *SInfrasResourceBase) PerformPublic(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformPublicInput) (jsonutils.JSONObject, error) {
err := SharablePerformPublic(model.GetIInfrasModel(), ctx, userCred, input)
func (model *SInfrasResourceBase) PerformPublic(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformPublicDomainInput) (jsonutils.JSONObject, error) {
err := SharablePerformPublic(model.GetIInfrasModel(), ctx, userCred, apis.PerformPublicProjectInput{PerformPublicDomainInput: input})
if err != nil {
return nil, errors.Wrap(err, "SharablePerformPublic")
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/cloudcommon/db/sharablebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,7 @@ func (m SSharableBaseResource) GetPublicScope() rbacutils.TRbacScope {
return rbacutils.String2Scope(m.PublicScope)
}

func SharablePerformPublic(model ISharableBaseModel, ctx context.Context, userCred mcclient.TokenCredential, input apis.PerformPublicInput) error {
log.Debugf("%s", jsonutils.Marshal(input))
func SharablePerformPublic(model ISharableBaseModel, ctx context.Context, userCred mcclient.TokenCredential, input apis.PerformPublicProjectInput) error {
var err error

resourceScope := model.GetModelManager().ResourceScope()
Expand All @@ -394,9 +393,8 @@ func SharablePerformPublic(model ISharableBaseModel, ctx context.Context, userCr
targetScope = rbacutils.ScopeDomain
}

shareResult := apis.PerformPublicInput{
Scope: string(targetScope),
}
shareResult := apis.PerformPublicProjectInput{}
shareResult.Scope = string(targetScope)

candidateIds := model.GetSharableTargetDomainIds()
requireIds := model.GetRequiredSharedDomainIds()
Expand Down
4 changes: 2 additions & 2 deletions pkg/cloudcommon/db/sharablevirtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ func (model *SSharableVirtualResourceBase) IsShared() bool {
return SharableModelIsShared(model)
}

func (model *SSharableVirtualResourceBase) AllowPerformPublic(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformPublicInput) bool {
func (model *SSharableVirtualResourceBase) AllowPerformPublic(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformPublicProjectInput) bool {
return true
}

func (model *SSharableVirtualResourceBase) PerformPublic(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformPublicInput) (jsonutils.JSONObject, error) {
func (model *SSharableVirtualResourceBase) PerformPublic(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformPublicProjectInput) (jsonutils.JSONObject, error) {
err := SharablePerformPublic(model.GetISharableVirtualModel(), ctx, userCred, input)
if err != nil {
return nil, errors.Wrap(err, "SharablePerformPublic")
Expand Down
87 changes: 39 additions & 48 deletions pkg/cloudcommon/db/standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,97 +361,88 @@ func (model *SStandaloneResourceBase) AllowGetDetailsMetadata(ctx context.Contex
return IsAllowGetSpec(rbacutils.ScopeSystem, userCred, model, "metadata")
}

func (model *SStandaloneResourceBase) GetDetailsMetadata(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (jsonutils.JSONObject, error) {
fields := jsonutils.GetQueryStringArray(query, "field")
val, err := Metadata.GetAll(model, fields, userCred)
// 获取资源标签(元数据)
func (model *SStandaloneResourceBase) GetDetailsMetadata(ctx context.Context, userCred mcclient.TokenCredential, input apis.GetMetadataInput) (apis.GetMetadataOutput, error) {
val, err := Metadata.GetAll(model, input.Field, userCred)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "Metadata.GetAll")
}
return jsonutils.Marshal(val), nil
return val, nil
}

func (model *SStandaloneResourceBase) AllowPerformMetadata(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
return IsAllowPerform(rbacutils.ScopeSystem, userCred, model, "metadata")
}

func (model *SStandaloneResourceBase) PerformMetadata(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
dict, ok := data.(*jsonutils.JSONDict)
if !ok {
return nil, httperrors.NewInputParameterError("input data not key value dict")
}
dictMap, err := dict.GetMap()
if err != nil {
return nil, err
}
// +onecloud:swagger-gen-ignore
func (model *SStandaloneResourceBase) PerformMetadata(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformMetadataInput) (jsonutils.JSONObject, error) {
dictStore := make(map[string]interface{})
for k, v := range dictMap {
for k, v := range input {
// 已双下滑线开头的metadata是系统内置,普通用户不可添加,只能查看
if strings.HasPrefix(k, SYS_TAG_PREFIX) && (userCred == nil || !IsAllowPerform(rbacutils.ScopeSystem, userCred, model, "metadata")) {
return nil, httperrors.NewForbiddenError("not allow to set system key, please remove the underscore at the beginning")
}
dictStore[k], _ = v.GetString()
dictStore[k] = v
}
err = model.SetAllMetadata(ctx, dictStore, userCred)
return nil, err
err := model.SetAllMetadata(ctx, dictStore, userCred)
if err != nil {
return nil, errors.Wrap(err, "SetAllMetadata")
}
return nil, nil
}

func (model *SStandaloneResourceBase) AllowPerformUserMetadata(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
return IsAllowPerform(rbacutils.ScopeSystem, userCred, model, "user-metadata")
}

func (model *SStandaloneResourceBase) PerformUserMetadata(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
dict, ok := data.(*jsonutils.JSONDict)
if !ok {
return nil, httperrors.NewInputParameterError("input data not key value dict")
// 更新资源的用户标签
// +onecloud:swagger-gen-ignore
func (model *SStandaloneResourceBase) PerformUserMetadata(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformUserMetadataInput) (jsonutils.JSONObject, error) {
dictStore := make(map[string]interface{})
for k, v := range input {
dictStore[USER_TAG_PREFIX+k] = v
}
dictMap, err := dict.GetMap()
err := model.SetUserMetadataValues(ctx, dictStore, userCred)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "SetUserMetadataValues")
}
dictStore := make(map[string]interface{})
for k, v := range dictMap {
dictStore[USER_TAG_PREFIX+k], _ = v.GetString()
}
err = model.SetUserMetadataValues(ctx, dictStore, userCred)
return nil, err
return nil, nil
}

func (model *SStandaloneResourceBase) AllowPerformSetUserMetadata(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
return IsAllowPerform(rbacutils.ScopeSystem, userCred, model, "set-user-metadata")
}

func (model *SStandaloneResourceBase) PerformSetUserMetadata(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
dict, ok := data.(*jsonutils.JSONDict)
if !ok {
return nil, httperrors.NewInputParameterError("input data not key value dict")
// 全量替换资源的所有用户标签
func (model *SStandaloneResourceBase) PerformSetUserMetadata(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformSetUserMetadataInput) (jsonutils.JSONObject, error) {
dictStore := make(map[string]interface{})
for k, v := range input {
dictStore[USER_TAG_PREFIX+k] = v
}
dictMap, err := dict.GetMap()
err := model.SetUserMetadataAll(ctx, dictStore, userCred)
if err != nil {
return nil, err
}
dictStore := make(map[string]interface{})
for k, v := range dictMap {
dictStore[USER_TAG_PREFIX+k], _ = v.GetString()
return nil, errors.Wrap(err, "SetUserMetadataAll")
}
err = model.SetUserMetadataAll(ctx, dictStore, userCred)
return nil, err
return nil, nil
}

func (model *SStandaloneResourceBase) PostUpdate(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) {
model.SResourceBase.PostUpdate(ctx, userCred, query, data)

jsonMeta, _ := data.Get("__meta__")
if jsonMeta != nil {
model.PerformMetadata(ctx, userCred, nil, jsonMeta)
meta := make(map[string]string)
err := data.Unmarshal(&meta, "__meta__")
if err == nil {
model.PerformMetadata(ctx, userCred, nil, meta)
}
}

func (model *SStandaloneResourceBase) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
model.SResourceBase.PostCreate(ctx, userCred, ownerId, query, data)

jsonMeta, _ := data.Get("__meta__")
if jsonMeta != nil {
model.PerformMetadata(ctx, userCred, nil, jsonMeta)
meta := make(map[string]string)
err := data.Unmarshal(&meta, "__meta__")
if err == nil {
model.PerformMetadata(ctx, userCred, nil, meta)
}
}

Expand Down

0 comments on commit 7ab3a06

Please sign in to comment.