VMware resource pool同步

This commit is contained in:
Qu Xuan
2020-04-21 21:17:14 +08:00
parent 3a0b5c5e21
commit 1ef9a481c3
8 changed files with 116 additions and 8 deletions
+1 -1
View File
@@ -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,
+32 -3
View File
@@ -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"
@@ -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
}
@@ -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
@@ -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 {
+17 -1
View File
@@ -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 {
@@ -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,
+1 -1
View File
@@ -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 {
+19 -1
View File
@@ -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
View File
@@ -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
View File
@@ -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 ""
}
+1 -1
View File
@@ -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,