From 486b7b35e70c83cfced0ebb524eaa344f7c38b81 Mon Sep 17 00:00:00 2001 From: Qu Xuan Date: Fri, 6 Dec 2019 17:35:53 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20cloudevent=E8=BF=87=E6=BB=A4=E5=8F=8A?= =?UTF-8?q?=E8=AF=A6=E6=83=85=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/cloudevent/vars | 1 + cmd/climc/shell/cloudevents.go | 40 ++++++++++++++++++ pkg/apis/cloudevent/cloudevent.go | 29 +++++++++++++ pkg/cloudevent/models/cloudevents.go | 55 +++++++++++++++++++++++++ pkg/cloudevent/models/cloudproviders.go | 10 ++--- pkg/mcclient/modules/managers.go | 6 +++ pkg/mcclient/modules/mod_cloudevents.go | 30 ++++++++++++++ 7 files changed, 166 insertions(+), 5 deletions(-) create mode 100644 build/cloudevent/vars create mode 100644 cmd/climc/shell/cloudevents.go create mode 100644 pkg/apis/cloudevent/cloudevent.go create mode 100644 pkg/mcclient/modules/mod_cloudevents.go diff --git a/build/cloudevent/vars b/build/cloudevent/vars new file mode 100644 index 0000000000..60209b213e --- /dev/null +++ b/build/cloudevent/vars @@ -0,0 +1 @@ +DESCRIPTION="Yunion Cloudevent" diff --git a/cmd/climc/shell/cloudevents.go b/cmd/climc/shell/cloudevents.go new file mode 100644 index 0000000000..958bc7aa02 --- /dev/null +++ b/cmd/climc/shell/cloudevents.go @@ -0,0 +1,40 @@ +// 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 shell + +import ( + "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/mcclient/modules" + "yunion.io/x/onecloud/pkg/mcclient/options" +) + +func init() { + + type CloudeventListOptions struct { + options.BaseListOptions + } + R(&CloudeventListOptions{}, "cloud-event-list", "List cloud events", func(s *mcclient.ClientSession, opts *CloudeventListOptions) error { + params, err := options.ListStructToParams(opts) + if err != nil { + return err + } + result, err := modules.Cloudevents.List(s, params) + if err != nil { + return err + } + printList(result, modules.Cloudevents.GetColumns(s)) + return nil + }) +} diff --git a/pkg/apis/cloudevent/cloudevent.go b/pkg/apis/cloudevent/cloudevent.go new file mode 100644 index 0000000000..659340744d --- /dev/null +++ b/pkg/apis/cloudevent/cloudevent.go @@ -0,0 +1,29 @@ +// 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 cloudevent + +import ( + "yunion.io/x/onecloud/pkg/apis" +) + +type CloudeventListInput struct { + apis.BaseListInput + + apis.SharableVirtualResourceListInput + + Cloudprovider string `json:"cloudprovider"` + + Providers []string `json:"providers"` +} diff --git a/pkg/cloudevent/models/cloudevents.go b/pkg/cloudevent/models/cloudevents.go index 25fadde2dc..1b0f773974 100644 --- a/pkg/cloudevent/models/cloudevents.go +++ b/pkg/cloudevent/models/cloudevents.go @@ -16,15 +16,19 @@ package models import ( "context" + "database/sql" "fmt" "yunion.io/x/jsonutils" "yunion.io/x/log" "yunion.io/x/pkg/errors" + "yunion.io/x/sqlchemy" + api "yunion.io/x/onecloud/pkg/apis/cloudevent" "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/cloudevent/options" "yunion.io/x/onecloud/pkg/cloudprovider" + "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/mcclient/auth" "yunion.io/x/onecloud/pkg/mcclient/modulebase" @@ -75,6 +79,57 @@ func (self *SCloudevent) AllowUpdateItem(ctx context.Context, userCred mcclient. return false } +func (manager *SCloudeventManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, input *api.CloudeventListInput) (*sqlchemy.SQuery, error) { + q, err := manager.SVirtualResourceBaseManager.ListItemFilter(ctx, q, userCred, input.JSON(input)) + if err != nil { + return nil, err + } + log.Errorf("input: %s", jsonutils.Marshal(input).PrettyString()) + if len(input.Cloudprovider) > 0 { + providerObj, err := CloudproviderManager.FetchByIdOrName(userCred, input.Cloudprovider) + if err != nil { + if err == sql.ErrNoRows { + return nil, httperrors.NewResourceNotFoundError2(CloudproviderManager.Keyword(), input.Cloudprovider) + } else { + return nil, httperrors.NewGeneralError(err) + } + } + q = q.Equals("cloudprovider_id", providerObj.GetId()) + } + + if len(input.Providers) > 0 { + sq := CloudproviderManager.Query().SubQuery() + q = q.Join(sq, sqlchemy.Equals(q.Field("cloudprovider_id"), sq.Field("id"))). + Filter(sqlchemy.In(sq.Field("provider"), input.Providers)) + } + return q, nil +} + +func (self *SCloudevent) GetCloudprovider() (*SCloudprovider, error) { + cloudprovider, err := CloudproviderManager.FetchById(self.CloudproviderId) + if err != nil { + return nil, err + } + return cloudprovider.(*SCloudprovider), nil +} + +func (self *SCloudevent) GetExtraDetails(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (*jsonutils.JSONDict, error) { + extra, err := self.SVirtualResourceBase.GetExtraDetails(ctx, userCred, query) + if err != nil { + return nil, err + } + cloudprovider, err := self.GetCloudprovider() + if err != nil { + return nil, err + } + info := jsonutils.Marshal(map[string]string{ + "provider": cloudprovider.Provider, + "manager": cloudprovider.Name, + }) + extra.Update(info) + return extra, nil +} + func (self *SCloudeventManager) fetchMods(ctx context.Context, userCred mcclient.TokenCredential) { if len(mods) > 0 { return diff --git a/pkg/cloudevent/models/cloudproviders.go b/pkg/cloudevent/models/cloudproviders.go index 0219ee8098..d5e7f61a0d 100644 --- a/pkg/cloudevent/models/cloudproviders.go +++ b/pkg/cloudevent/models/cloudproviders.go @@ -56,16 +56,16 @@ func init() { type SCloudprovider struct { db.SEnabledStatusStandaloneResourceBase - HealthStatus string + HealthStatus string `width:"16" charset:"ascii" default:"normal" nullable:"false" list:"domain"` SyncStatus string LastSync time.Time LastSyncEndAt time.Time - AccessUrl string - Account string - Secret string + AccessUrl string `width:"64" charset:"ascii" nullable:"true" list:"domain" update:"domain"` + Account string `width:"128" charset:"ascii" nullable:"false" list:"domain"` + Secret string `length:"0" charset:"ascii" nullable:"false" list:"domain"` - Provider string + Provider string `width:"64" charset:"ascii" list:"domain"` } func (manager *SCloudproviderManager) GetRegionCloudproviders(ctx context.Context, userCred mcclient.TokenCredential) ([]SCloudprovider, error) { diff --git a/pkg/mcclient/modules/managers.go b/pkg/mcclient/modules/managers.go index 708da7dd9e..d62360ec9c 100644 --- a/pkg/mcclient/modules/managers.go +++ b/pkg/mcclient/modules/managers.go @@ -169,3 +169,9 @@ func NewDevtoolManager(keyword, keywordPlural string, columns, adminColumns []st BaseManager: *modulebase.NewBaseManager("devtool", "", "", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } + +func NewCloudeventManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { + return modulebase.ResourceManager{ + BaseManager: *modulebase.NewBaseManager("cloudevent", "", "", columns, adminColumns), + Keyword: keyword, KeywordPlural: keywordPlural} +} diff --git a/pkg/mcclient/modules/mod_cloudevents.go b/pkg/mcclient/modules/mod_cloudevents.go new file mode 100644 index 0000000000..4d03d8a6ff --- /dev/null +++ b/pkg/mcclient/modules/mod_cloudevents.go @@ -0,0 +1,30 @@ +// 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 modules + +import "yunion.io/x/onecloud/pkg/mcclient/modulebase" + +var ( + Cloudevents modulebase.ResourceManager +) + +func init() { + Cloudevents = NewCloudeventManager("cloudevent", "cloudevents", + []string{"ID", "Name", "Status", "Service", "Success", + "Resource_Type", "Action", "Cloudprovider_Id", "Cloudprovider"}, + []string{}) + + register(&Cloudevents) +}