diff --git a/cmd/climc/shell/serversecgroups.go b/cmd/climc/shell/serversecgroups.go new file mode 100644 index 0000000000..0725d2fa25 --- /dev/null +++ b/cmd/climc/shell/serversecgroups.go @@ -0,0 +1,72 @@ +// 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/jsonutils" + + "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/mcclient/modulebase" + "yunion.io/x/onecloud/pkg/mcclient/modules" + "yunion.io/x/onecloud/pkg/mcclient/options" +) + +func init() { + type ServerSecgroupListOptions struct { + options.BaseListOptions + Server string `help:"ID or Name of Server"` + Secgroup string `help:"Secgroup ID or name"` + } + 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() + if err != nil { + return err + + } + } + var result *modulebase.ListResult + var err error + if len(args.Server) > 0 { + result, err = modules.Serversecgroups.ListDescendent(s, args.Server, params) + } else if len(args.Secgroup) > 0 { + result, err = modules.Serversecgroups.ListDescendent2(s, args.Secgroup, params) + } else { + result, err = modules.Serversecgroups.List(s, params) + } + if err != nil { + return err + } + printList(result, modules.Serversecgroups.GetColumns(s)) + return nil + }) + + type ServerSecgroupDetailOptions struct { + SERVER string `help:"ID or Name of Server"` + SECGROUP string `help:"ID or Name of Security Group"` + } + R(&ServerSecgroupDetailOptions{}, "server-secgroup-show", "Show server security group details", func(s *mcclient.ClientSession, args *ServerSecgroupDetailOptions) error { + query := jsonutils.NewDict() + result, err := modules.Serversecgroups.Get(s, args.SERVER, args.SECGROUP, query) + if err != nil { + return err + } + printObject(result) + return nil + }) + +} diff --git a/pkg/apis/compute/guestnetwork.go b/pkg/apis/compute/guestnetwork.go index fe37c0f25f..fd16fd839c 100644 --- a/pkg/apis/compute/guestnetwork.go +++ b/pkg/apis/compute/guestnetwork.go @@ -20,7 +20,7 @@ type GuestnetworkDetails struct { SGuestnetwork // IP子网名称 - Network string + Network string `json:"network"` } type GuestnetworkShortDesc struct { diff --git a/pkg/apis/compute/secgroup.go b/pkg/apis/compute/secgroup.go index 1a5d521015..06000bc41f 100644 --- a/pkg/apis/compute/secgroup.go +++ b/pkg/apis/compute/secgroup.go @@ -213,3 +213,12 @@ type GuestsecgroupListInput struct { GuestJointsListInput SecgroupFilterListInput } + +type GuestsecgroupDetails struct { + GuestJointResourceDetails + + SGuestsecgroup + + // 安全组名称 + Secgroup string `json:"secgroup"` +} diff --git a/pkg/cloudcommon/db/project.go b/pkg/cloudcommon/db/project.go index 7af55d2ce7..bc6cc55cb5 100644 --- a/pkg/cloudcommon/db/project.go +++ b/pkg/cloudcommon/db/project.go @@ -183,6 +183,9 @@ func fetchProjects(ctx context.Context, projectIds []string, isDomain bool) map[ ret[projects[i].Id] = projects[i] } for _, pid := range projectIds { + if len(pid) == 0 { + continue + } if _, ok := ret[pid]; !ok { // not found var t *STenant diff --git a/pkg/compute/models/guestnetworks.go b/pkg/compute/models/guestnetworks.go index ae2b9e1ece..b37f1589e1 100644 --- a/pkg/compute/models/guestnetworks.go +++ b/pkg/compute/models/guestnetworks.go @@ -123,7 +123,7 @@ func (self *SGuestnetwork) GetExtraDetails( return api.GuestnetworkDetails{}, nil } -func (manager SGuestnetworkManager) FetchCustomizeColumns( +func (manager *SGuestnetworkManager) FetchCustomizeColumns( ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, diff --git a/pkg/compute/models/guestsecgroups.go b/pkg/compute/models/guestsecgroups.go index 1a40ee1878..d942374de0 100644 --- a/pkg/compute/models/guestsecgroups.go +++ b/pkg/compute/models/guestsecgroups.go @@ -18,6 +18,7 @@ import ( "context" "fmt" + "yunion.io/x/jsonutils" "yunion.io/x/log" "yunion.io/x/pkg/errors" "yunion.io/x/sqlchemy" @@ -194,3 +195,44 @@ func (manager *SGuestsecgroupManager) ListItemExportKeys(ctx context.Context, return q, nil } + +func (self *SGuestsecgroup) GetExtraDetails( + ctx context.Context, + userCred mcclient.TokenCredential, + query jsonutils.JSONObject, + isList bool, +) (api.GuestsecgroupDetails, error) { + return api.GuestsecgroupDetails{}, nil +} + +func (manager *SGuestsecgroupManager) FetchCustomizeColumns( + ctx context.Context, + userCred mcclient.TokenCredential, + query jsonutils.JSONObject, + objs []interface{}, + fields stringutils2.SSortedStrings, + isList bool, +) []api.GuestsecgroupDetails { + rows := make([]api.GuestsecgroupDetails, len(objs)) + + guestRows := manager.SGuestJointsManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList) + secgroupIds := make([]string, len(rows)) + for i := range rows { + rows[i].GuestJointResourceDetails = guestRows[i] + secgroupIds[i] = objs[i].(*SGuestsecgroup).SecgroupId + } + + secgroupIdMaps, err := db.FetchIdNameMap2(SecurityGroupManager, secgroupIds) + if err != nil { + log.Errorf("FetchIdNameMap2 fail %s", err) + return rows + } + + for i := range rows { + if name, ok := secgroupIdMaps[secgroupIds[i]]; ok { + rows[i].Secgroup = name + } + } + + return rows +} diff --git a/pkg/mcclient/modules/mod_serversecgroups.go b/pkg/mcclient/modules/mod_serversecgroups.go new file mode 100644 index 0000000000..04b330c77d --- /dev/null +++ b/pkg/mcclient/modules/mod_serversecgroups.go @@ -0,0 +1,32 @@ +// 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 ( + Serversecgroups modulebase.JointResourceManager +) + +func init() { + Serversecgroups = NewJointComputeManager( + "guestsecgroup", + "guestsecgroups", + []string{}, + []string{}, + &Servers, + &SecGroups) + registerCompute(&Serversecgroups) +}