fix: cloudevent过滤及详情信息

This commit is contained in:
Qu Xuan
2019-12-06 17:35:53 +08:00
parent 9b260935f3
commit 486b7b35e7
7 changed files with 166 additions and 5 deletions

1
build/cloudevent/vars Normal file
View File

@@ -0,0 +1 @@
DESCRIPTION="Yunion Cloudevent"

View File

@@ -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
})
}

View File

@@ -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"`
}

View File

@@ -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

View File

@@ -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) {

View File

@@ -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}
}

View File

@@ -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)
}