diff --git a/cmd/climc/shell/compute/metadata.go b/cmd/climc/shell/compute/metadata.go index 10217ff8d6..8f29a6e7c6 100644 --- a/cmd/climc/shell/compute/metadata.go +++ b/cmd/climc/shell/compute/metadata.go @@ -37,7 +37,7 @@ func init() { return nil }) - R(&options.TagListOptions{}, "tag-list", "List tags", func(s *mcclient.ClientSession, opts *options.TagListOptions) error { + R(&options.TagListOptions{}, "service-metadata-list", "List service metadata", func(s *mcclient.ClientSession, opts *options.TagListOptions) error { var mod modulebase.IResourceManager switch opts.Service { case "compute": diff --git a/pkg/apis/yunionconf/consts.go b/pkg/apis/yunionconf/consts.go index bc0ee4a9ba..72e42ca493 100644 --- a/pkg/apis/yunionconf/consts.go +++ b/pkg/apis/yunionconf/consts.go @@ -17,7 +17,8 @@ package yunionconf import "yunion.io/x/onecloud/pkg/apis" const ( - SERVICE_TYPE = apis.SERVICE_TYPE_YUNIONCONF + SERVICE_TYPE = apis.SERVICE_TYPE_YUNIONCONF + SERVICE_VERSION = "" ) const ( diff --git a/pkg/apis/yunionconf/tags.go b/pkg/apis/yunionconf/tags.go new file mode 100644 index 0000000000..c54595c45b --- /dev/null +++ b/pkg/apis/yunionconf/tags.go @@ -0,0 +1,31 @@ +// 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 yunionconf + +import "yunion.io/x/onecloud/pkg/apis" + +type TagListInput struct { + apis.InfrasResourceBaseListInput +} + +type TagDetails struct { + apis.InfrasResourceBaseDetails +} + +type TagCreateInput struct { + apis.InfrasResourceBaseCreateInput + + Values []string `json:"values"` +} diff --git a/pkg/yunionconf/models/tags.go b/pkg/yunionconf/models/tags.go new file mode 100644 index 0000000000..fcd3333e86 --- /dev/null +++ b/pkg/yunionconf/models/tags.go @@ -0,0 +1,132 @@ +// 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 models + +import ( + "context" + + "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" + "yunion.io/x/sqlchemy" + + api "yunion.io/x/onecloud/pkg/apis/yunionconf" + "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" +) + +// +onecloud:swagger-gen-model-singular=tag +// +onecloud:swagger-gen-model-plural=tags +type STagManager struct { + db.SInfrasResourceBaseManager +} + +var ( + TagManager *STagManager +) + +func init() { + TagManager = &STagManager{ + SInfrasResourceBaseManager: db.NewInfrasResourceBaseManager( + STag{}, + "tags_tbl", + "tag", + "tags", + ), + } + TagManager.SetVirtualObject(TagManager) +} + +type STag struct { + db.SInfrasResourceBase + + Values []string `charset:"utf8" get:"user" list:"user" update:"admin" create:"required"` +} + +// 预置标签列表 +func (manager *STagManager) ListItemFilter( + ctx context.Context, + q *sqlchemy.SQuery, + userCred mcclient.TokenCredential, + query api.TagListInput, +) (*sqlchemy.SQuery, error) { + var err error + q, err = manager.SInfrasResourceBaseManager.ListItemFilter(ctx, q, userCred, query.InfrasResourceBaseListInput) + if err != nil { + return nil, errors.Wrap(err, "SInfrasResourceBaseManager.ListItemFilter") + } + return q, nil +} + +func (manager *STagManager) OrderByExtraFields( + ctx context.Context, + q *sqlchemy.SQuery, + userCred mcclient.TokenCredential, + query api.TagListInput, +) (*sqlchemy.SQuery, error) { + var err error + q, err = manager.SInfrasResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.InfrasResourceBaseListInput) + if err != nil { + return nil, errors.Wrap(err, "SInfrasResourceBaseManager.OrderByExtraFields") + } + return q, nil +} + +func (manager *STagManager) QueryDistinctExtraField(q *sqlchemy.SQuery, field string) (*sqlchemy.SQuery, error) { + var err error + q, err = manager.SInfrasResourceBaseManager.QueryDistinctExtraField(q, field) + if err == nil { + return q, nil + } + return q, httperrors.ErrNotFound +} + +func (manager *STagManager) FetchCustomizeColumns( + ctx context.Context, + userCred mcclient.TokenCredential, + query jsonutils.JSONObject, + objs []interface{}, + fields stringutils2.SSortedStrings, + isList bool, +) []api.TagDetails { + rows := make([]api.TagDetails, len(objs)) + + stdRows := manager.SInfrasResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList) + for i := range rows { + rows[i] = api.TagDetails{ + InfrasResourceBaseDetails: stdRows[i], + } + } + + return rows +} + +func (manager *STagManager) ValidateCreateData( + ctx context.Context, + userCred mcclient.TokenCredential, + ownerId mcclient.IIdentityProvider, + query jsonutils.JSONObject, + input *api.TagCreateInput, +) (*api.TagCreateInput, error) { + var err error + + input.InfrasResourceBaseCreateInput, err = manager.SInfrasResourceBaseManager.ValidateCreateData(ctx, userCred, ownerId, query, input.InfrasResourceBaseCreateInput) + if err != nil { + return input, errors.Wrap(err, "SInfrasResourceBaseManager.ValidateCreateData") + } + + return input, nil +} diff --git a/pkg/yunionconf/options/options.go b/pkg/yunionconf/options/options.go index c9782abbea..cac686f954 100644 --- a/pkg/yunionconf/options/options.go +++ b/pkg/yunionconf/options/options.go @@ -24,3 +24,14 @@ type YunionConfOptions struct { var ( Options YunionConfOptions ) + +func OnOptionsChange(oldO, newO interface{}) bool { + oldOpts := oldO.(*YunionConfOptions) + newOpts := newO.(*YunionConfOptions) + + changed := false + if common_options.OnCommonOptionsChange(&oldOpts.CommonOptions, &newOpts.CommonOptions) { + changed = true + } + return changed +} diff --git a/pkg/yunionconf/policy/defaults.go b/pkg/yunionconf/policy/defaults.go index 01d3018deb..fde4bc471b 100644 --- a/pkg/yunionconf/policy/defaults.go +++ b/pkg/yunionconf/policy/defaults.go @@ -93,6 +93,24 @@ var ( }, }, }, + { + Auth: true, + Scope: rbacscope.ScopeProject, + Rules: []rbacutils.SRbacRule{ + { + Service: api.SERVICE_TYPE, + Resource: "tags", + Action: PolicyActionList, + Result: rbacutils.Allow, + }, + { + Service: api.SERVICE_TYPE, + Resource: "tags", + Action: PolicyActionGet, + Result: rbacutils.Allow, + }, + }, + }, } ) diff --git a/pkg/yunionconf/service/handlers.go b/pkg/yunionconf/service/handlers.go index dc11c62d72..790fd8e2aa 100644 --- a/pkg/yunionconf/service/handlers.go +++ b/pkg/yunionconf/service/handlers.go @@ -52,6 +52,7 @@ func InitHandlers(app *appsrv.Application) { models.ParameterManager, models.ScopedPolicyBindingManager, models.ScopedPolicyManager, + models.TagManager, } { db.RegisterModelManager(manager) handler := db.NewModelHandler(manager) diff --git a/pkg/yunionconf/service/service.go b/pkg/yunionconf/service/service.go index 6f58b31af7..cfcd1380ea 100644 --- a/pkg/yunionconf/service/service.go +++ b/pkg/yunionconf/service/service.go @@ -46,6 +46,7 @@ func StartService() { app_common.InitAuth(commonOpts, func() { log.Infof("Auth complete!!") }) + common_options.StartOptionManager(opts, opts.ConfigSyncPeriodSeconds, api.SERVICE_TYPE, api.SERVICE_VERSION, options.OnOptionsChange) cloudcommon.InitDB(dbOpts)