Skip to content

Commit

Permalink
VMware resource pool同步
Browse files Browse the repository at this point in the history
  • Loading branch information
Qu Xuan committed Apr 21, 2020
1 parent 3a0b5c5 commit 1ef9a48
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/multicloud/aliyun/aliyun.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func (self *SAliyunClient) GetIProjects() ([]cloudprovider.ICloudProject, error)

func (region *SAliyunClient) GetCapabilities() []string {
caps := []string{
// cloudprovider.CLOUD_CAPABILITY_PROJECT,
cloudprovider.CLOUD_CAPABILITY_PROJECT,
cloudprovider.CLOUD_CAPABILITY_COMPUTE,
cloudprovider.CLOUD_CAPABILITY_NETWORK,
cloudprovider.CLOUD_CAPABILITY_LOADBALANCER,
Expand Down
35 changes: 32 additions & 3 deletions pkg/multicloud/esxi/datacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/vmware/govmomi/vim25/types"

"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/utils"

api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudprovider"
Expand All @@ -32,9 +33,10 @@ var DATACENTER_PROPS = []string{"name", "parent", "datastore", "network"}
type SDatacenter struct {
SManagedObject

ihosts []cloudprovider.ICloudHost
istorages []cloudprovider.ICloudStorage
inetworks []IVMNetwork
ihosts []cloudprovider.ICloudHost
istorages []cloudprovider.ICloudStorage
inetworks []IVMNetwork
iresoucePool []cloudprovider.ICloudProject

Name string
}
Expand Down Expand Up @@ -63,6 +65,25 @@ func (dc *SDatacenter) getObjectDatacenter() *object.Datacenter {
return object.NewDatacenter(dc.manager.client.Client, dc.object.Reference())
}

func (dc *SDatacenter) scanResourcePool() error {
if dc.iresoucePool == nil {
var pools []mo.ResourcePool
err := dc.manager.scanMObjects(dc.object.Entity().Self, RESOURCEPOOL_PROPS, &pools)
if err != nil {
return errors.Wrap(err, "scanMObjects")
}
dc.iresoucePool = []cloudprovider.ICloudProject{}
for i := 0; i < len(pools); i++ {
p := NewResourcePool(dc.manager, &pools[i], dc)
rpPath := p.GetPath()
if utils.IsInStringArray("Resources", rpPath) && rpPath[len(rpPath)-1] != "Resources" {
dc.iresoucePool = append(dc.iresoucePool, p)
}
}
}
return nil
}

func (dc *SDatacenter) scanHosts() error {
if dc.ihosts == nil {
var hosts []mo.HostSystem
Expand All @@ -88,6 +109,14 @@ func (dc *SDatacenter) scanHosts() error {
return nil
}

func (dc *SDatacenter) GetResourcePools() ([]cloudprovider.ICloudProject, error) {
err := dc.scanResourcePool()
if err != nil {
return nil, errors.Wrap(err, "dc.scanResourcePool")
}
return dc.iresoucePool, nil
}

func (dc *SDatacenter) GetIHosts() ([]cloudprovider.ICloudHost, error) {
err := dc.scanHosts()
if err != nil {
Expand Down
18 changes: 17 additions & 1 deletion pkg/multicloud/esxi/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,22 @@ func findDatacenterByMoId(dcs []*SDatacenter, dcId string) (*SDatacenter, error)
return nil, cloudprovider.ErrNotFound
}

func (cli *SESXiClient) GetIProjects() ([]cloudprovider.ICloudProject, error) {
dcs, err := cli.GetDatacenters()
if err != nil {
return nil, errors.Wrap(err, "GetDatacenters")
}
ret := []cloudprovider.ICloudProject{}
for i := 0; i < len(dcs); i++ {
iprojects, err := dcs[i].GetResourcePools()
if err != nil {
return nil, errors.Wrap(err, "GetResourcePools")
}
ret = append(ret, iprojects...)
}
return ret, nil
}

func (cli *SESXiClient) FindHostByMoId(moId string) (cloudprovider.ICloudHost, error) {
dcs, err := cli.GetDatacenters()
if err != nil {
Expand Down Expand Up @@ -492,7 +508,7 @@ func (cli *SESXiClient) IsValid() bool {

func (cli *SESXiClient) GetCapabilities() []string {
caps := []string{
// cloudprovider.CLOUD_CAPABILITY_PROJECT,
cloudprovider.CLOUD_CAPABILITY_PROJECT,
cloudprovider.CLOUD_CAPABILITY_COMPUTE,
// cloudprovider.CLOUD_CAPABILITY_NETWORK,
// cloudprovider.CLOUD_CAPABILITY_LOADBALANCER,
Expand Down
2 changes: 1 addition & 1 deletion pkg/multicloud/esxi/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (self *SESXiProvider) GetOnPremiseIRegion() (cloudprovider.ICloudRegion, er
}

func (self *SESXiProvider) GetIProjects() ([]cloudprovider.ICloudProject, error) {
return nil, cloudprovider.ErrNotSupported
return self.client.GetIProjects()
}

func (self *SESXiProvider) GetStorageClasses(regionId string) []string {
Expand Down
20 changes: 19 additions & 1 deletion pkg/multicloud/esxi/resourcepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,32 @@

package esxi

import "github.com/vmware/govmomi/vim25/mo"
import (
"github.com/vmware/govmomi/vim25/mo"

"yunion.io/x/onecloud/pkg/multicloud"
)

var RESOURCEPOOL_PROPS = []string{"name", "parent"}

type SResourcePool struct {
multicloud.SResourceBase
SManagedObject
}

func (pool *SResourcePool) GetGlobalId() string {
return pool.GetId()
}

func (pool *SResourcePool) GetStatus() string {
return ""
}

func (pool *SResourcePool) GetName() string {
path := pool.GetPath()
return path[len(path)-1]
}

func NewResourcePool(manager *SESXiClient, rp *mo.ResourcePool, dc *SDatacenter) *SResourcePool {
return &SResourcePool{SManagedObject: newManagedObject(manager, rp, dc)}
}
38 changes: 38 additions & 0 deletions pkg/multicloud/esxi/shell/resourcepool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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/multicloud/esxi"
"yunion.io/x/onecloud/pkg/util/shellutils"
)

func init() {
type ResourcePoolListOptions struct {
DATACENTER string `help:"List resource pool in datacenter"`
}
shellutils.R(&ResourcePoolListOptions{}, "resource-pool-list", "List all resource pools", func(cli *esxi.SESXiClient, args *ResourcePoolListOptions) error {
dc, err := cli.FindDatacenterByMoId(args.DATACENTER)
if err != nil {
return err
}
pools, err := dc.GetResourcePools()
if err != nil {
return err
}
printList(pools, nil)
return nil
})
}
7 changes: 7 additions & 0 deletions pkg/multicloud/esxi/virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,13 @@ func (self *SVirtualMachine) Renew(bc billing.SBillingCycle) error {
}

func (self *SVirtualMachine) GetProjectId() string {
pool, err := self.getResourcePool()
if err != nil {
return ""
}
if pool != nil {
return pool.GetId()
}
return ""
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/multicloud/huawei/huawei.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ func (self *SHuaweiClient) GetAccessEnv() string {

func (self *SHuaweiClient) GetCapabilities() []string {
caps := []string{
// cloudprovider.CLOUD_CAPABILITY_PROJECT,
cloudprovider.CLOUD_CAPABILITY_PROJECT,
cloudprovider.CLOUD_CAPABILITY_COMPUTE,
cloudprovider.CLOUD_CAPABILITY_NETWORK,
cloudprovider.CLOUD_CAPABILITY_LOADBALANCER,
Expand Down

0 comments on commit 1ef9a48

Please sign in to comment.