diff --git a/cmd/climc/shell/compute/baremetalnetworks.go b/cmd/climc/shell/compute/baremetalnetworks.go index 841069c7a06..449a300935a 100644 --- a/cmd/climc/shell/compute/baremetalnetworks.go +++ b/cmd/climc/shell/compute/baremetalnetworks.go @@ -32,12 +32,11 @@ func init() { R(&HostNetworkListOptions{}, "host-network-list", "List baremetal network pairs", func(s *mcclient.ClientSession, args *HostNetworkListOptions) error { var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err - } + params = param.(*jsonutils.JSONDict) } if len(args.Network) > 0 { params.Add(jsonutils.NewString(args.Network), "network_id") diff --git a/cmd/climc/shell/compute/baremetalstorages.go b/cmd/climc/shell/compute/baremetalstorages.go index 748d960d14b..74902c210e3 100644 --- a/cmd/climc/shell/compute/baremetalstorages.go +++ b/cmd/climc/shell/compute/baremetalstorages.go @@ -31,12 +31,11 @@ func init() { R(&BaremetalStorageListOptions{}, "baremetal-storage-list", "List baremetal storage pairs", func(s *mcclient.ClientSession, args *BaremetalStorageListOptions) error { var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err - } + params = param.(*jsonutils.JSONDict) } var result *printutils.ListResult var err error diff --git a/cmd/climc/shell/compute/cloudprovider_quota.go b/cmd/climc/shell/compute/cloudprovider_quota.go index 2a16077c4fb..69ab710003a 100644 --- a/cmd/climc/shell/compute/cloudprovider_quota.go +++ b/cmd/climc/shell/compute/cloudprovider_quota.go @@ -31,10 +31,11 @@ func init() { } R(&CloudproviderQuotaListOptions{}, "cloud-provider-quota-list", "List cloudprovider quota", func(s *mcclient.ClientSession, args *CloudproviderQuotaListOptions) error { - params, err := args.Params() + param, err := args.Params() if err != nil { return err } + params := param.(*jsonutils.JSONDict) if len(args.QuotaType) > 0 { params.Add(jsonutils.NewString(args.QuotaType), "quota_type") diff --git a/cmd/climc/shell/compute/cloudproviderregions.go b/cmd/climc/shell/compute/cloudproviderregions.go index 53ad8304cc9..82c9fc041e0 100644 --- a/cmd/climc/shell/compute/cloudproviderregions.go +++ b/cmd/climc/shell/compute/cloudproviderregions.go @@ -33,12 +33,11 @@ func init() { R(&CloudproviderRegionListOptions{}, "cloud-provider-region-list", "List cloudprovider region synchronization status", func(s *mcclient.ClientSession, args *CloudproviderRegionListOptions) error { var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err - } + params = param.(*jsonutils.JSONDict) } var result *printutils.ListResult var err error diff --git a/cmd/climc/shell/compute/dynamicschedtags.go b/cmd/climc/shell/compute/dynamicschedtags.go index 132287d5278..b1a963fae37 100644 --- a/cmd/climc/shell/compute/dynamicschedtags.go +++ b/cmd/climc/shell/compute/dynamicschedtags.go @@ -29,11 +29,11 @@ func init() { R(&DynamicschedtagListOptions{}, "dynamic-schedtag-list", "List dynamic schedtag conditions", func(s *mcclient.ClientSession, args *DynamicschedtagListOptions) error { var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err } + params = param.(*jsonutils.JSONDict) } results, err := modules.Dynamicschedtags.List(s, params) if err != nil { diff --git a/cmd/climc/shell/compute/hostcachedimages.go b/cmd/climc/shell/compute/hostcachedimages.go index b1cf13a3929..f078e280164 100644 --- a/cmd/climc/shell/compute/hostcachedimages.go +++ b/cmd/climc/shell/compute/hostcachedimages.go @@ -32,12 +32,11 @@ func init() { R(&HostCachedImageListOptions{}, "host-cachedimage-list", "List host cached image pairs", func(s *mcclient.ClientSession, args *HostCachedImageListOptions) error { var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err - } + params = param.(*jsonutils.JSONDict) } var result *printutils.ListResult var err error diff --git a/cmd/climc/shell/compute/hoststorages.go b/cmd/climc/shell/compute/hoststorages.go index a9d52237859..05f658a4f3f 100644 --- a/cmd/climc/shell/compute/hoststorages.go +++ b/cmd/climc/shell/compute/hoststorages.go @@ -32,12 +32,11 @@ func init() { R(&HostStorageListOptions{}, "host-storage-list", "List host storage pairs", func(s *mcclient.ClientSession, args *HostStorageListOptions) error { var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err - } + params = param.(*jsonutils.JSONDict) } var result *printutils.ListResult var err error diff --git a/cmd/climc/shell/compute/isolated_device_models.go b/cmd/climc/shell/compute/isolated_device_models.go index 83f45f382b1..2f0e1f56878 100644 --- a/cmd/climc/shell/compute/isolated_device_models.go +++ b/cmd/climc/shell/compute/isolated_device_models.go @@ -43,12 +43,11 @@ func init() { "List host Isolated device model pairs", func(s *mcclient.ClientSession, args *HostIsolatedDeviceModelListOptions) error { var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err - } + params = param.(*jsonutils.JSONDict) } var result *printutils.ListResult var err error diff --git a/cmd/climc/shell/compute/isolatedevices.go b/cmd/climc/shell/compute/isolatedevices.go index 8071276eea4..cb443ef2eab 100644 --- a/cmd/climc/shell/compute/isolatedevices.go +++ b/cmd/climc/shell/compute/isolatedevices.go @@ -35,12 +35,11 @@ func init() { R(&DeviceListOptions{}, "isolated-device-list", "List isolated devices like GPU", func(s *mcclient.ClientSession, args *DeviceListOptions) error { var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err - } + params = param.(*jsonutils.JSONDict) } if len(args.Host) > 0 { params.Add(jsonutils.NewString(args.Host), "host") diff --git a/cmd/climc/shell/compute/loadbalancernetworks.go b/cmd/climc/shell/compute/loadbalancernetworks.go index fc023a2410a..80cab93cfb6 100644 --- a/cmd/climc/shell/compute/loadbalancernetworks.go +++ b/cmd/climc/shell/compute/loadbalancernetworks.go @@ -33,12 +33,11 @@ func init() { R(&LoadbalancerNetworkListOptions{}, "lbnetwork-list", "List loadbalancer network pairs", func(s *mcclient.ClientSession, args *LoadbalancerNetworkListOptions) error { var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err - } + params = param.(*jsonutils.JSONDict) } if len(args.Ip) > 0 { params.Add(jsonutils.NewString(args.Ip), "ip_addr") diff --git a/cmd/climc/shell/compute/metadata.go b/cmd/climc/shell/compute/metadata.go index 10217ff8d6d..15b5abf4612 100644 --- a/cmd/climc/shell/compute/metadata.go +++ b/cmd/climc/shell/compute/metadata.go @@ -24,20 +24,7 @@ import ( ) func init() { - R(&options.MetadataListOptions{}, "metadata-list", "List metadatas", func(s *mcclient.ClientSession, opts *options.MetadataListOptions) error { - params, err := options.ListStructToParams(opts) - if err != nil { - return err - } - result, err := modules.ComputeMetadatas.List(s, params) - if err != nil { - return err - } - printList(result, []string{}) - return nil - }) - - R(&options.TagListOptions{}, "tag-list", "List tags", func(s *mcclient.ClientSession, opts *options.TagListOptions) error { + R(&options.TagListOptions{}, "metadata-list", "List tags", func(s *mcclient.ClientSession, opts *options.TagListOptions) error { var mod modulebase.IResourceManager switch opts.Service { case "compute": diff --git a/cmd/climc/shell/compute/policy_assignment.go b/cmd/climc/shell/compute/policy_assignment.go index 3c9f0b2cf9b..286989e8aaf 100644 --- a/cmd/climc/shell/compute/policy_assignment.go +++ b/cmd/climc/shell/compute/policy_assignment.go @@ -28,10 +28,11 @@ func init() { Policydefinition string `help:"filter by policydefinition"` } R(&PolicyListOptions{}, "policy-assignment-list", "List policy assignments", func(s *mcclient.ClientSession, args *PolicyListOptions) error { - params, err := args.Params() + param, err := args.Params() if err != nil { return err } + params := param.(*jsonutils.JSONDict) if len(args.Policydefinition) > 0 { params.Add(jsonutils.NewString(args.Policydefinition), "policydefinition") } diff --git a/cmd/climc/shell/compute/reservedips.go b/cmd/climc/shell/compute/reservedips.go index 932b3ac4db5..901aa3d15f1 100644 --- a/cmd/climc/shell/compute/reservedips.go +++ b/cmd/climc/shell/compute/reservedips.go @@ -71,12 +71,11 @@ func init() { R(&ReservedIPListOptions{}, "reserved-ip-list", "Show all reserved IPs for any network", func(s *mcclient.ClientSession, args *ReservedIPListOptions) error { var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err - } + params = param.(*jsonutils.JSONDict) } if len(args.Network) > 0 { params.Add(jsonutils.NewString(args.Network), "network") diff --git a/cmd/climc/shell/compute/schedpolicies.go b/cmd/climc/shell/compute/schedpolicies.go index 365f7f2b717..2b7b568efed 100644 --- a/cmd/climc/shell/compute/schedpolicies.go +++ b/cmd/climc/shell/compute/schedpolicies.go @@ -29,11 +29,11 @@ func init() { R(&SchedpoliciesListOptions{}, "sched-policy-list", "List scheduler policies", func(s *mcclient.ClientSession, args *SchedpoliciesListOptions) error { var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err } + params = param.(*jsonutils.JSONDict) } results, err := modules.Schedpolicies.List(s, params) if err != nil { diff --git a/cmd/climc/shell/compute/serverdisks.go b/cmd/climc/shell/compute/serverdisks.go index d8bb8c488aa..c4d93cff72a 100644 --- a/cmd/climc/shell/compute/serverdisks.go +++ b/cmd/climc/shell/compute/serverdisks.go @@ -35,12 +35,11 @@ func init() { R(&ServerDiskListOptions{}, "server-disk-list", "List server disk pairs", func(s *mcclient.ClientSession, args *ServerDiskListOptions) error { var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err - } + params = param.(*jsonutils.JSONDict) } if args.Index >= 0 { params.Add(jsonutils.NewInt(args.Index), "index") diff --git a/cmd/climc/shell/compute/servernetworks.go b/cmd/climc/shell/compute/servernetworks.go index ef405161951..1e68963b674 100644 --- a/cmd/climc/shell/compute/servernetworks.go +++ b/cmd/climc/shell/compute/servernetworks.go @@ -40,12 +40,11 @@ func init() { R(&ServerNetworkListOptions{}, "server-network-list", "List server network pairs", func(s *mcclient.ClientSession, args *ServerNetworkListOptions) error { var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err - } + params = param.(*jsonutils.JSONDict) } if len(args.Mac) > 0 { params.Add(jsonutils.NewString(args.Mac), "mac_addr") diff --git a/cmd/climc/shell/compute/serversecgroups.go b/cmd/climc/shell/compute/serversecgroups.go index b82765f50a8..c1c49c8f5d2 100644 --- a/cmd/climc/shell/compute/serversecgroups.go +++ b/cmd/climc/shell/compute/serversecgroups.go @@ -32,12 +32,11 @@ func init() { R(&ServerSecgroupListOptions{}, "server-secgroup-list", "List server secgroup pairs", func(s *mcclient.ClientSession, args *ServerSecgroupListOptions) error { var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err - } + params = param.(*jsonutils.JSONDict) } var result *printutils.ListResult var err error diff --git a/cmd/climc/shell/compute/specs.go b/cmd/climc/shell/compute/specs.go index 21b911b51f7..8ff6de7116a 100644 --- a/cmd/climc/shell/compute/specs.go +++ b/cmd/climc/shell/compute/specs.go @@ -37,12 +37,11 @@ func init() { R(&ListOptions{}, "spec", "List all kinds of model specs", func(s *mcclient.ClientSession, args *ListOptions) error { var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err - } + params = param.(*jsonutils.JSONDict) } model := "" if len(args.Model) != 0 { @@ -108,12 +107,11 @@ func init() { } var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err - } + params = param.(*jsonutils.JSONDict) } if len(args.HostType) > 0 { params.Add(jsonutils.NewString(args.HostType), "host_type") @@ -153,12 +151,11 @@ func init() { } var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err - } + params = param.(*jsonutils.JSONDict) } resp, err := modules.Specs.SpecsQueryModelObjects(s, "isolated_devices", newSpecKeys(), params) if err != nil { diff --git a/cmd/climc/shell/compute/storagecachedimages.go b/cmd/climc/shell/compute/storagecachedimages.go index cff8a12cea3..41841cfbd89 100644 --- a/cmd/climc/shell/compute/storagecachedimages.go +++ b/cmd/climc/shell/compute/storagecachedimages.go @@ -32,12 +32,11 @@ func init() { R(&StorageCachedImageListOptions{}, "storage-cached-image-list", "List storage cached image pairs", func(s *mcclient.ClientSession, args *StorageCachedImageListOptions) error { var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err - } + params = param.(*jsonutils.JSONDict) } var result *printutils.ListResult var err error diff --git a/cmd/climc/shell/compute/vcenters.go b/cmd/climc/shell/compute/vcenters.go index f17ac8600a7..e7aa23ef3d9 100644 --- a/cmd/climc/shell/compute/vcenters.go +++ b/cmd/climc/shell/compute/vcenters.go @@ -29,12 +29,11 @@ func init() { R(&VCenterListOptions{}, "vcenter-list", "List VMWare vcenters", func(s *mcclient.ClientSession, args *VCenterListOptions) error { var params *jsonutils.JSONDict { - var err error - params, err = args.BaseListOptions.Params() + param, err := args.BaseListOptions.Params() if err != nil { return err - } + params = param.(*jsonutils.JSONDict) } result, err := modules.VCenters.List(s, params) if err != nil { diff --git a/cmd/climc/shell/image/imageguest.go b/cmd/climc/shell/image/imageguest.go index cc0f4c1621e..71cf97ecea5 100644 --- a/cmd/climc/shell/image/imageguest.go +++ b/cmd/climc/shell/image/imageguest.go @@ -71,10 +71,11 @@ func init() { R(&GuestImageListOptions{}, "guest-image-list", "List guest images", func(s *mcclient.ClientSession, args *GuestImageListOptions) error { - params, err := args.Params() + param, err := args.Params() if err != nil { return err } + params := param.(*jsonutils.JSONDict) if len(args.Name) > 0 { params.Add(jsonutils.NewString(args.Name), "name") diff --git a/cmd/climc/shell/yunionconf/tags.go b/cmd/climc/shell/yunionconf/tags.go new file mode 100644 index 00000000000..552556077fc --- /dev/null +++ b/cmd/climc/shell/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/cmd/climc/shell" + "yunion.io/x/onecloud/pkg/mcclient/modules/yunionconf" + "yunion.io/x/onecloud/pkg/mcclient/options" + opts "yunion.io/x/onecloud/pkg/mcclient/options/yunionconf" +) + +func init() { + cmd := shell.NewResourceCmd(&yunionconf.Tags) + cmd.List(&options.BaseListOptions{}) + cmd.Delete(&options.BaseIdOptions{}) + cmd.Show(&options.BaseIdOptions{}) + cmd.Create(&opts.TagCreateOptions{}) + cmd.Update(&opts.TagUpdateOptions{}) +} diff --git a/pkg/apihelper/db.go b/pkg/apihelper/db.go index d563c0f6deb..731d9f479d9 100644 --- a/pkg/apihelper/db.go +++ b/pkg/apihelper/db.go @@ -83,10 +83,11 @@ func GetDBModels(opts *GetDBModelsOptions) error { filter := inter.ModelFilter() listOptions.Filter = append(listOptions.Filter, filter...) } - params, err := listOptions.Params() + param, err := listOptions.Params() if err != nil { return fmt.Errorf("%s: making list params: %s", manKeyPlural, err) } + params := param.(*jsonutils.JSONDict) if inter, ok := opts.GetModelSet().(IModelListParam); ok { filter := inter.ModelParamFilter() params.Update(filter) diff --git a/pkg/apihelper/reflect.go b/pkg/apihelper/reflect.go index 8fb7c327601..27dde804185 100644 --- a/pkg/apihelper/reflect.go +++ b/pkg/apihelper/reflect.go @@ -86,10 +86,11 @@ func GetModels(opts *GetModelsOptions) error { listOptions.PendingDeleteAll = options.Bool(true) listOptions.DeleteAll = options.Bool(true) } - params, err := listOptions.Params() + param, err := listOptions.Params() if err != nil { return fmt.Errorf("%s: making list params: %s", manKeyPlural, err) } + params := param.(*jsonutils.JSONDict) if inter, ok := opts.ModelSet.(IModelListParam); ok { filter := inter.ModelParamFilter() params.Update(filter) diff --git a/pkg/apis/yunionconf/tags.go b/pkg/apis/yunionconf/tags.go new file mode 100644 index 00000000000..b0aed5fdd7a --- /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.StandaloneResourceListInput +} + +type TagDetails struct { + apis.StandaloneResourceDetails +} + +type TagCreateInput struct { + apis.StandaloneResourceCreateInput + + Values []string `json:"values"` +} diff --git a/pkg/mcclient/modules/yunionconf/mod_tags.go b/pkg/mcclient/modules/yunionconf/mod_tags.go new file mode 100644 index 00000000000..57c9dd39add --- /dev/null +++ b/pkg/mcclient/modules/yunionconf/mod_tags.go @@ -0,0 +1,37 @@ +// 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/mcclient/modulebase" + "yunion.io/x/onecloud/pkg/mcclient/modules" +) + +type TagManager struct { + modulebase.ResourceManager +} + +var ( + Tags TagManager +) + +func init() { + Tags = TagManager{modules.NewYunionConfManager( + "tag", "tags", + []string{}, + []string{}, + )} + modules.Register(&Tags) +} diff --git a/pkg/mcclient/options/base.go b/pkg/mcclient/options/base.go index 23461395408..f31d81397b4 100644 --- a/pkg/mcclient/options/base.go +++ b/pkg/mcclient/options/base.go @@ -301,7 +301,7 @@ func SplitTag(tag string) tagutils.TTagSet { return tags } -func (opts *BaseListOptions) Params() (*jsonutils.JSONDict, error) { +func (opts *BaseListOptions) Params() (jsonutils.JSONObject, error) { params, err := optionsStructToParams(opts) if err != nil { return nil, err diff --git a/pkg/mcclient/options/glance/image.go b/pkg/mcclient/options/glance/image.go index 4948ba14e1a..84ae9f05d98 100644 --- a/pkg/mcclient/options/glance/image.go +++ b/pkg/mcclient/options/glance/image.go @@ -39,10 +39,11 @@ type ImageListOptions struct { } func (o *ImageListOptions) Params() (jsonutils.JSONObject, error) { - params, err := o.BaseListOptions.Params() + param, err := o.BaseListOptions.Params() if err != nil { return nil, err } + params := param.(*jsonutils.JSONDict) if len(o.IsPublic) > 0 { params.Add(jsonutils.NewString(o.IsPublic), "is_public") } diff --git a/pkg/mcclient/options/k8s/base.go b/pkg/mcclient/options/k8s/base.go index 122a8ca4445..983653ca1cf 100644 --- a/pkg/mcclient/options/k8s/base.go +++ b/pkg/mcclient/options/k8s/base.go @@ -54,10 +54,11 @@ type BaseListOptions struct { } func (o BaseListOptions) Params() (*jsonutils.JSONDict, error) { - params, err := o.BaseListOptions.Params() + param, err := o.BaseListOptions.Params() if err != nil { return nil, err } + params := param.(*jsonutils.JSONDict) if o.Name != "" { params.Add(jsonutils.NewString(o.Name), "name") } diff --git a/pkg/mcclient/options/k8s/container_registry.go b/pkg/mcclient/options/k8s/container_registry.go index e44f0b981ea..c9a760a4513 100644 --- a/pkg/mcclient/options/k8s/container_registry.go +++ b/pkg/mcclient/options/k8s/container_registry.go @@ -26,10 +26,11 @@ type RegistryListOptions struct { } func (o *RegistryListOptions) Params() (jsonutils.JSONObject, error) { - params, err := o.BaseListOptions.Params() + param, err := o.BaseListOptions.Params() if err != nil { return nil, err } + params := param.(*jsonutils.JSONDict) if o.Type != "" { params.Add(jsonutils.NewString(o.Type), "type") } diff --git a/pkg/mcclient/options/k8s/repo.go b/pkg/mcclient/options/k8s/repo.go index 0bddeb02ddc..fe7feb1ae01 100644 --- a/pkg/mcclient/options/k8s/repo.go +++ b/pkg/mcclient/options/k8s/repo.go @@ -26,10 +26,11 @@ type RepoListOptions struct { } func (o *RepoListOptions) Params() (jsonutils.JSONObject, error) { - params, err := o.BaseListOptions.Params() + param, err := o.BaseListOptions.Params() if err != nil { return nil, err } + params := param.(*jsonutils.JSONDict) if o.Type != "" { params.Add(jsonutils.NewString(o.Type), "type") } diff --git a/pkg/mcclient/options/monitor/alert.go b/pkg/mcclient/options/monitor/alert.go index 5798f59d0ee..ab9f6003c75 100644 --- a/pkg/mcclient/options/monitor/alert.go +++ b/pkg/mcclient/options/monitor/alert.go @@ -210,7 +210,7 @@ type AlertNotificationListOptions struct { Notification string `help:"ID or name of notification" short-token:"n"` } -func (o AlertNotificationListOptions) Params() (*jsonutils.JSONDict, error) { +func (o AlertNotificationListOptions) Params() (jsonutils.JSONObject, error) { params, err := o.BaseListOptions.Params() if err != nil { return nil, err diff --git a/pkg/mcclient/options/monitor/alertresource.go b/pkg/mcclient/options/monitor/alertresource.go index 23bc32999dc..0942d883e58 100644 --- a/pkg/mcclient/options/monitor/alertresource.go +++ b/pkg/mcclient/options/monitor/alertresource.go @@ -26,10 +26,11 @@ type AlertResourceListOptions struct { } func (o AlertResourceListOptions) Params() (jsonutils.JSONObject, error) { - params, err := o.BaseListOptions.Params() + param, err := o.BaseListOptions.Params() if err != nil { return nil, err } + params := param.(*jsonutils.JSONDict) if o.Type != "" { params.Add(jsonutils.NewString(o.Type), "type") } diff --git a/pkg/mcclient/options/schedtags.go b/pkg/mcclient/options/schedtags.go index 657e839d775..df1ee1d566b 100644 --- a/pkg/mcclient/options/schedtags.go +++ b/pkg/mcclient/options/schedtags.go @@ -27,7 +27,7 @@ type SchedtagModelListOptions struct { Schedtag string `help:"ID or Name of schedtag"` } -func (o SchedtagModelListOptions) Params() (*jsonutils.JSONDict, error) { +func (o SchedtagModelListOptions) Params() (jsonutils.JSONObject, error) { params, err := o.BaseListOptions.Params() if err != nil { return nil, err @@ -60,11 +60,13 @@ type SchedtagListOptions struct { } func (o SchedtagListOptions) Params() (jsonutils.JSONObject, error) { - params, err := o.BaseListOptions.Params() + param, err := o.BaseListOptions.Params() if err != nil { return nil, err } + params := param.(*jsonutils.JSONDict) + if len(o.Type) > 0 { params.Add(jsonutils.NewString(o.Type), "resource_type") } diff --git a/pkg/mcclient/options/yunionconf/tags.go b/pkg/mcclient/options/yunionconf/tags.go new file mode 100644 index 00000000000..d31cb974794 --- /dev/null +++ b/pkg/mcclient/options/yunionconf/tags.go @@ -0,0 +1,47 @@ +// 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/jsonutils" + + "yunion.io/x/onecloud/pkg/mcclient/options" +) + +type TagCreateOptions struct { + options.BaseCreateOptions + Values []string +} + +func (o TagCreateOptions) Params() (jsonutils.JSONObject, error) { + return jsonutils.Marshal(o), nil +} + +type TagUpdateOptions struct { + options.BaseUpdateOptions + Values []string +} + +func (o TagUpdateOptions) Params() (jsonutils.JSONObject, error) { + param, err := o.BaseUpdateOptions.Params() + if err != nil { + return nil, err + } + params := param.(*jsonutils.JSONDict) + if len(o.Values) > 0 { + params.Set("values", jsonutils.Marshal(o.Values)) + } + return params, nil +} diff --git a/pkg/yunionconf/models/tags.go b/pkg/yunionconf/models/tags.go new file mode 100644 index 00000000000..a4548d7a639 --- /dev/null +++ b/pkg/yunionconf/models/tags.go @@ -0,0 +1,130 @@ +// 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" +) + +type STagManager struct { + db.SStandaloneResourceBaseManager +} + +var ( + TagManager *STagManager +) + +func init() { + TagManager = &STagManager{ + SStandaloneResourceBaseManager: db.NewStandaloneResourceBaseManager( + STag{}, + "tags_tbl", + "tag", + "tags", + ), + } + TagManager.SetVirtualObject(TagManager) +} + +type STag struct { + db.SStandaloneResourceBase + + 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.SStandaloneResourceBaseManager.ListItemFilter(ctx, q, userCred, query.StandaloneResourceListInput) + if err != nil { + return nil, errors.Wrap(err, "SStandaloneResourceBaseManager.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.SStandaloneResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.StandaloneResourceListInput) + if err != nil { + return nil, errors.Wrap(err, "SStandaloneResourceBaseManager.OrderByExtraFields") + } + return q, nil +} + +func (manager *STagManager) QueryDistinctExtraField(q *sqlchemy.SQuery, field string) (*sqlchemy.SQuery, error) { + var err error + q, err = manager.SStandaloneResourceBaseManager.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.SStandaloneResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList) + for i := range rows { + rows[i] = api.TagDetails{ + StandaloneResourceDetails: 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.StandaloneResourceCreateInput, err = manager.SStandaloneResourceBaseManager.ValidateCreateData(ctx, userCred, ownerId, query, input.StandaloneResourceCreateInput) + if err != nil { + return input, errors.Wrap(err, "SStandaloneResourceBaseManager.ValidateCreateData") + } + + return input, nil +} diff --git a/pkg/yunionconf/policy/defaults.go b/pkg/yunionconf/policy/defaults.go index fd0d2caeb6e..38392944e1d 100644 --- a/pkg/yunionconf/policy/defaults.go +++ b/pkg/yunionconf/policy/defaults.go @@ -92,6 +92,24 @@ var ( }, }, }, + { + Auth: true, + Scope: rbacscope.ScopeSystem, + Rules: []rbacutils.SRbacRule{ + { + Service: api.SERVICE_TYPE, + Resource: "tags", + Action: PolicyActionGet, + Result: rbacutils.Allow, + }, + { + Service: api.SERVICE_TYPE, + Resource: "tags", + Action: PolicyActionList, + Result: rbacutils.Allow, + }, + }, + }, } ) diff --git a/pkg/yunionconf/service/handlers.go b/pkg/yunionconf/service/handlers.go index dc11c62d729..790fd8e2aaf 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)