fix(region): google replace globalid

This commit is contained in:
Qu Xuan
2021-11-24 09:56:38 +08:00
committed by ioito
parent eddc7861e2
commit d87901b259
83 changed files with 1281 additions and 682 deletions
+9 -88
View File
@@ -15,95 +15,16 @@
package compute
import (
"fmt"
"yunion.io/x/jsonutils"
"yunion.io/x/onecloud/pkg/mcclient"
modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
"yunion.io/x/onecloud/pkg/mcclient/options"
"yunion.io/x/onecloud/cmd/climc/shell"
"yunion.io/x/onecloud/pkg/mcclient/modules/compute"
options "yunion.io/x/onecloud/pkg/mcclient/options/compute"
)
func init() {
type GlobalVpcListOptions struct {
options.BaseListOptions
}
R(&GlobalVpcListOptions{}, "global-vpc-list", "List global vpcs", func(s *mcclient.ClientSession, args *GlobalVpcListOptions) error {
params, err := options.ListStructToParams(args)
if err != nil {
return err
}
result, err := modules.GlobalVpcs.List(s, params)
if err != nil {
return err
}
printList(result, modules.GlobalVpcs.GetColumns(s))
return nil
})
type GlobalVpcShowOptions struct {
ID string `help:"ID or Name of globalvpc"`
}
R(&GlobalVpcShowOptions{}, "global-vpc-show", "Show details of a global vpc", func(s *mcclient.ClientSession, args *GlobalVpcShowOptions) error {
result, err := modules.GlobalVpcs.GetById(s, args.ID, nil)
if err != nil {
return err
}
printObject(result)
return nil
})
type GlobalVpcPublicOptions struct {
ID string `help:"ID or name of global vpc" json:"-"`
Scope string `help:"sharing scope" choices:"system|domain"`
SharedDomains []string `help:"share to domains"`
}
R(&GlobalVpcPublicOptions{}, "global-vpc-public", "Make global vpc public", func(s *mcclient.ClientSession, args *GlobalVpcPublicOptions) error {
params := jsonutils.Marshal(args)
result, err := modules.GlobalVpcs.PerformAction(s, args.ID, "public", params)
if err != nil {
return err
}
printObject(result)
return nil
})
type GlobalVpcPrivateOptions struct {
ID string `help:"ID or name of global vpc" json:"-"`
}
R(&GlobalVpcPrivateOptions{}, "global-vpc-private", "Make global vpc private", func(s *mcclient.ClientSession, args *GlobalVpcPrivateOptions) error {
params := jsonutils.Marshal(args)
result, err := modules.GlobalVpcs.PerformAction(s, args.ID, "private", params)
if err != nil {
return err
}
printObject(result)
return nil
})
R(&GlobalVpcShowOptions{}, "global-vpc-change-owner-candidate-domains", "Show candiate domains of a global vpc for changing owner", func(s *mcclient.ClientSession, args *GlobalVpcShowOptions) error {
result, err := modules.GlobalVpcs.GetSpecific(s, args.ID, "change-owner-candidate-domains", nil)
if err != nil {
return err
}
printObject(result)
return nil
})
type GlobalVpcChangeOwnerOptions struct {
ID string `help:"ID or name of vpc" json:"-"`
ProjectDomain string `json:"project_domain" help:"target domain"`
}
R(&GlobalVpcChangeOwnerOptions{}, "global-vpc-change-owner", "Change owner domain of a global vpc", func(s *mcclient.ClientSession, args *GlobalVpcChangeOwnerOptions) error {
if len(args.ProjectDomain) == 0 {
return fmt.Errorf("empty project_domain")
}
params := jsonutils.Marshal(args)
ret, err := modules.GlobalVpcs.PerformAction(s, args.ID, "change-owner", params)
if err != nil {
return err
}
printObject(ret)
return nil
})
cmd := shell.NewResourceCmd(&compute.GlobalVpcs).WithKeyword("global-vpc")
cmd.List(&options.GlobalVpcListOptions{})
cmd.Show(&options.GlobalVpcIdOption{})
cmd.Create(&options.GlobalVpcCreateOptions{})
cmd.Perform("syncstatus", &options.GlobalVpcIdOption{})
cmd.Get("change-owner-candidate-domains", &options.GlobalVpcIdOption{})
}
+1 -1
View File
@@ -500,6 +500,6 @@ type SyncRangeInput struct {
Host []string `json:"host"`
// 按资源类型同步,可输入多个
// enmu: compute, loadbalancer, objectstore, rds, cache, nat, nas, waf, mongodb, es, kafka, app, container
// enmu: compute, loadbalancer, objectstore, rds, cache, nat, nas, waf, mongodb, es, kafka, app, container, quota, intervpcnetwork, cdn, dnszone
Resources []string `json:"resources" choices:"compute|loadbalancer|objectstore|rds|cache|nat|nas|waf|mongodb|es|kafka|app|container"`
}
+3
View File
@@ -20,6 +20,9 @@ import (
type GlobalVpcCreateInput struct {
apis.EnabledStatusInfrasResourceBaseCreateInput
// 目前仅支持谷歌云创建
CloudproviderResourceInput
}
type GlobalVpcDetails struct {
+4
View File
@@ -77,6 +77,10 @@ type VpcCreateInput struct {
// CIDR_BLOCK
CidrBlock string `json:"cidr_block"`
// 仅对谷歌云有用,若谷歌云订阅只有一个全局VPC,此参数可不传
// 若有多个全局VPC,谷歌云需要指定其中一个全局VPC
GlobalvpcId string `json:"globalvpc_id"`
// Vpc外网访问模式
ExternalAccessMode string `json:"external_access_mode"`
}
+20 -5
View File
@@ -271,7 +271,6 @@ type ICloudProvider interface {
GetObjectCannedAcls(regionId string) []string
GetCapabilities() []string
GetICloudQuotas() ([]ICloudQuota, error)
IsClouduserSupportPassword() bool
GetICloudusers() ([]IClouduser, error)
@@ -300,6 +299,10 @@ type ICloudProvider interface {
GetICloudDnsZoneById(id string) (ICloudDnsZone, error)
CreateICloudDnsZone(opts *SDnsZoneCreateOptions) (ICloudDnsZone, error)
GetICloudGlobalVpcs() ([]ICloudGlobalVpc, error)
CreateICloudGlobalVpc(opts *GlobalVpcCreateOptions) (ICloudGlobalVpc, error)
GetICloudGlobalVpcById(id string) (ICloudGlobalVpc, error)
GetICloudInterVpcNetworks() ([]ICloudInterVpcNetwork, error)
GetICloudInterVpcNetworkById(id string) (ICloudInterVpcNetwork, error)
CreateICloudInterVpcNetwork(opts *SInterVpcNetworkCreateOptions) (ICloudInterVpcNetwork, error)
@@ -321,6 +324,10 @@ func IsSupportProject(prod ICloudProvider) bool {
return IsSupportCapability(prod, CLOUD_CAPABILITY_PROJECT)
}
func IsSupportQuota(prod ICloudProvider) bool {
return IsSupportCapability(prod, CLOUD_CAPABILITY_QUOTA)
}
func IsSupportDnsZone(prod ICloudProvider) bool {
return IsSupportCapability(prod, CLOUD_CAPABILITY_DNSZONE)
}
@@ -459,10 +466,6 @@ func (self *SBaseProvider) GetOnPremiseIRegion() (ICloudRegion, error) {
return nil, ErrNotImplemented
}
func (self *SBaseProvider) GetICloudQuotas() ([]ICloudQuota, error) {
return nil, ErrNotImplemented
}
func (self *SBaseProvider) GetIamLoginUrl() string {
return ""
}
@@ -579,6 +582,18 @@ func (self *SBaseProvider) CreateICloudInterVpcNetwork(opts *SInterVpcNetworkCre
return nil, ErrNotImplemented
}
func (self *SBaseProvider) GetICloudGlobalVpcs() ([]ICloudGlobalVpc, error) {
return nil, errors.Wrapf(ErrNotImplemented, "GetICloudGlobalVpcs")
}
func (self *SBaseProvider) GetICloudGlobalVpcById(id string) (ICloudGlobalVpc, error) {
return nil, errors.Wrapf(ErrNotImplemented, "GetICloudGlobalVpcById")
}
func (self *SBaseProvider) CreateICloudGlobalVpc(opts *GlobalVpcCreateOptions) (ICloudGlobalVpc, error) {
return nil, errors.Wrapf(ErrNotImplemented, "CreateICloudGlobalVpc")
}
func (self *SBaseProvider) GetICloudCDNDomains() ([]ICloudCDNDomain, error) {
return nil, errors.Wrapf(ErrNotImplemented, "GetICloudCDNDomains")
}
+1
View File
@@ -56,6 +56,7 @@ const (
CLOUD_CAPABILITY_PUBLIC_IP = "public_ip"
CLOUD_CAPABILITY_INTERVPCNETWORK = "intervpcnetwork"
CLOUD_CAPABILITY_SAML_AUTH = "saml_auth" // 是否支持SAML 2.0
CLOUD_CAPABILITY_QUOTA = "quota" // 配额
CLOUD_CAPABILITY_NAT = "nat" // NAT网关
CLOUD_CAPABILITY_NAS = "nas" // NAS
CLOUD_CAPABILITY_WAF = "waf" // WAF
+1 -1
View File
@@ -91,7 +91,7 @@ func (region *SFakeOnPremiseRegion) GetIEipById(id string) (ICloudEIP, error) {
return nil, ErrNotSupported
}
func (region *SFakeOnPremiseRegion) CreateIVpc(name string, desc string, cidr string) (ICloudVpc, error) {
func (region *SFakeOnPremiseRegion) CreateIVpc(opts *VpcCreateOptions) (ICloudVpc, error) {
return nil, ErrNotSupported
}
+21
View File
@@ -0,0 +1,21 @@
// 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 cloudprovider
type GlobalVpcCreateOptions struct {
NAME string
Desc string
}
+9 -3
View File
@@ -90,7 +90,7 @@ type ICloudRegion interface {
GetISecurityGroupByName(opts *SecurityGroupFilterOptions) (ICloudSecurityGroup, error)
CreateISecurityGroup(conf *SecurityGroupCreateInput) (ICloudSecurityGroup, error)
CreateIVpc(name string, desc string, cidr string) (ICloudVpc, error)
CreateIVpc(opts *VpcCreateOptions) (ICloudVpc, error)
CreateInternetGateway() (ICloudInternetGateway, error)
CreateEIP(eip *SEip) (ICloudEIP, error)
@@ -547,10 +547,16 @@ type ICloudSnapshotPolicy interface {
GetTimePoints() ([]int, error)
}
type ICloudVpc interface {
// GetGlobalId() // 若vpc属于globalvpc,此函数返回格式必须是 'region.GetGlobalId()/vpc.GetGlobalId()'
type ICloudGlobalVpc interface {
ICloudResource
Delete() error
}
type ICloudVpc interface {
ICloudResource
GetGlobalVpcId() string
IsSupportSetExternalAccess() bool // 是否支持Attach互联网网关.
GetExternalAccessMode() string
AttachInternetGateway(igwId string) error
+22
View File
@@ -0,0 +1,22 @@
// 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 cloudprovider
type VpcCreateOptions struct {
NAME string
CIDR string
GlobalVpcExternalId string
Desc string
}
+1 -59
View File
@@ -2203,8 +2203,7 @@ func (account *SCloudaccount) SubmitSyncAccountTask(ctx context.Context, userCre
defer cloudaccountPendingSyncsMutex.Unlock()
delete(cloudaccountPendingSyncs, account.Id)
}()
account.SyncAccountResources(ctx, userCred)
SyncCloudaccountResources(ctx, userCred, account, &SSyncRange{})
log.Debugf("syncAccountStatus %s %s", account.Id, account.Name)
err := account.syncAccountStatus(ctx, userCred)
if waitChan != nil {
@@ -2927,63 +2926,6 @@ func (self *SCloudaccount) SyncDnsZones(ctx context.Context, userCred mcclient.T
return localZones, remoteZones, result
}
func (self *SCloudaccount) SyncAccountResources(ctx context.Context, userCred mcclient.TokenCredential) error {
provider, err := self.GetProvider()
if err != nil {
return errors.Wrapf(err, "GetProvider")
}
if cloudprovider.IsSupportProject(provider) {
err = func() error {
lockman.LockRawObject(ctx, "projects", self.Id)
defer lockman.ReleaseRawObject(ctx, "projects", self.Id)
projects, err := provider.GetIProjects()
if err != nil {
return errors.Wrapf(err, "provider.GetIProjects")
}
result := ExternalProjectManager.SyncProjects(ctx, userCred, self, projects)
log.Infof("Sync project for cloudaccount %s result: %s", self.Name, result.Result())
return nil
}()
if err != nil {
log.Errorf("sync project for account %s error: %v", self.Name, err)
}
}
if cloudprovider.IsSupportDnsZone(provider) {
err = func() error {
lockman.LockRawObject(ctx, "dns_zones", self.Id)
defer lockman.ReleaseRawObject(ctx, "dns_zones", self.Id)
dnsZones, err := provider.GetICloudDnsZones()
if err != nil {
return errors.Wrapf(err, "GetICloudDnsZones")
}
localZones, remoteZones, result := self.SyncDnsZones(ctx, userCred, dnsZones)
log.Infof("Sync dns zones for cloudaccount %s result: %s", self.Name, result.Result())
for i := 0; i < len(localZones); i++ {
func() {
lockman.LockObject(ctx, &localZones[i])
defer lockman.ReleaseObject(ctx, &localZones[i])
if localZones[i].Deleted {
return
}
result := localZones[i].SyncDnsRecordSets(ctx, userCred, self.Provider, remoteZones[i])
log.Infof("Sync dns records for dns zone %s result: %s", localZones[i].GetName(), result.Result())
}()
}
return nil
}()
if err != nil {
log.Errorf("sync dns zone for account %s error: %v", self.Name, err)
}
}
return nil
}
type SVs2Wire struct {
WireId string
VsId string
+6 -2
View File
@@ -197,8 +197,12 @@ func (manager *SCloudproviderQuotaManager) GetQuotas(provider *SCloudprovider, r
}
func (manager *SCloudproviderQuotaManager) SyncQuotas(ctx context.Context, userCred mcclient.TokenCredential, syncOwnerId mcclient.IIdentityProvider, provider *SCloudprovider, region *SCloudregion, quotaRange string, iQuotas []cloudprovider.ICloudQuota) compare.SyncResult {
lockman.LockRawObject(ctx, "quotas", fmt.Sprintf("%s-%s", provider.Id, region.Id))
defer lockman.ReleaseRawObject(ctx, "quotas", fmt.Sprintf("%s-%s", provider.Id, region.Id))
key := provider.Id
if region != nil {
key = fmt.Sprintf("%s-%s", key, region.Id)
}
lockman.LockRawObject(ctx, manager.Keyword(), key)
defer lockman.ReleaseRawObject(ctx, manager.Keyword(), key)
result := compare.SyncResult{}
dbQuotas, err := manager.GetQuotas(provider, region, quotaRange)
+1 -31
View File
@@ -1411,6 +1411,7 @@ func (self *SCloudprovider) RealDelete(ctx context.Context, userCred mcclient.To
WafInstanceManager,
AppManager,
VpcManager,
GlobalVpcManager,
ElasticipManager,
MongoDBManager,
ElasticSearchManager,
@@ -1807,37 +1808,6 @@ func (self *SCloudprovider) SyncInterVpcNetwork(ctx context.Context, userCred mc
return localNetworks, remoteNetworks, result
}
func (self *SCloudprovider) SyncCallSyncCloudproviderInterVpcNetwork(ctx context.Context, userCred mcclient.TokenCredential) {
driver, err := self.GetProvider()
if err != nil {
log.Errorf("failed to get ICloudProvider from SCloudprovider:%s %s", self.GetName(), self.Id)
return
}
if cloudprovider.IsSupportInterVpcNetwork(driver) {
networks, err := driver.GetICloudInterVpcNetworks()
if err != nil {
log.Errorf("failed to get inter vpc network for Manager %s error: %v", self.Id, err)
return
} else {
localNetwork, remoteNetwork, result := self.SyncInterVpcNetwork(ctx, userCred, networks)
if result.IsError() {
return
}
for i := range localNetwork {
lockman.LockObject(ctx, &localNetwork[i])
defer lockman.ReleaseObject(ctx, &localNetwork[i])
if localNetwork[i].Deleted {
return
}
localNetwork[i].SyncInterVpcNetworkRouteSets(ctx, userCred, remoteNetwork[i])
}
log.Infof("Sync inter vpc network for cloudaccount %s result: %s", self.GetName(), result.Result())
return
}
}
}
func (manager *SCloudproviderManager) ListItemExportKeys(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, keys stringutils2.SSortedStrings) (*sqlchemy.SQuery, error) {
q, err := manager.SEnabledStatusStandaloneResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
if err != nil {
+144 -1
View File
@@ -1724,7 +1724,9 @@ func syncPublicCloudProviderInfo(
storageCachePairs := make([]sStoragecacheSyncPair, 0)
syncRegionQuotas(ctx, userCred, syncResults, driver, provider, localRegion, remoteRegion)
if cloudprovider.IsSupportQuota(driver) && syncRange.NeedSyncResource(cloudprovider.CLOUD_CAPABILITY_QUOTA) {
syncRegionQuotas(ctx, userCred, syncResults, driver, provider, localRegion, remoteRegion)
}
localZones, remoteZones, _ := syncRegionZones(ctx, userCred, syncResults, provider, localRegion, remoteRegion)
@@ -2132,3 +2134,144 @@ func SyncCloudDomain(userCred mcclient.TokenCredential, model db.IDomainLevelMod
}
model.SyncCloudDomainId(userCred, newOwnerId)
}
func SyncCloudaccountResources(ctx context.Context, userCred mcclient.TokenCredential, account *SCloudaccount, syncRange *SSyncRange) error {
provider, err := account.GetProvider()
if err != nil {
return errors.Wrapf(err, "GetProvider")
}
if cloudprovider.IsSupportProject(provider) && syncRange.NeedSyncResource(cloudprovider.CLOUD_CAPABILITY_PROJECT) {
syncProjects(ctx, userCred, SSyncResultSet{}, account, provider)
}
if cloudprovider.IsSupportDnsZone(provider) && syncRange.NeedSyncResource(cloudprovider.CLOUD_CAPABILITY_DNSZONE) {
syncDns(ctx, userCred, SSyncResultSet{}, account, provider)
}
return nil
}
func syncProjects(ctx context.Context, userCred mcclient.TokenCredential, syncResults SSyncResultSet, account *SCloudaccount, provider cloudprovider.ICloudProvider) error {
lockman.LockRawObject(ctx, ExternalProjectManager.Keyword(), account.Id)
defer lockman.ReleaseRawObject(ctx, ExternalProjectManager.Keyword(), account.Id)
projects, err := func() ([]cloudprovider.ICloudProject, error) {
defer syncResults.AddRequestCost(ExternalProjectManager)()
return provider.GetIProjects()
}()
if err != nil {
return errors.Wrapf(err, "GetIProjects")
}
result := func() compare.SyncResult {
defer syncResults.AddSqlCost(ExternalProjectManager)()
return ExternalProjectManager.SyncProjects(ctx, userCred, account, projects)
}()
syncResults.Add(ExternalProjectManager, result)
msg := result.Result()
log.Infof("SyncProjects for account %s result: %s", account.Name, msg)
if result.IsError() {
return err
}
return nil
}
func syncDns(ctx context.Context, userCred mcclient.TokenCredential, syncResults SSyncResultSet, account *SCloudaccount, provider cloudprovider.ICloudProvider) error {
lockman.LockRawObject(ctx, DnsZoneCacheManager.Keyword(), account.Id)
defer lockman.ReleaseRawObject(ctx, DnsZoneCacheManager.Keyword(), account.Id)
dnsZones, err := provider.GetICloudDnsZones()
if err != nil {
return errors.Wrapf(err, "GetICloudDnsZones")
}
localZones, remoteZones, result := account.SyncDnsZones(ctx, userCred, dnsZones)
log.Infof("Sync dns zones for cloudaccount %s result: %s", account.Name, result.Result())
for i := 0; i < len(localZones); i++ {
func() {
lockman.LockObject(ctx, &localZones[i])
defer lockman.ReleaseObject(ctx, &localZones[i])
if localZones[i].Deleted {
return
}
result := localZones[i].SyncDnsRecordSets(ctx, userCred, account.Provider, remoteZones[i])
log.Infof("Sync dns records for dns zone %s result: %s", localZones[i].GetName(), result.Result())
}()
}
return nil
}
func SyncCloudproviderResources(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, syncRange *SSyncRange) error {
driver, err := provider.GetProvider()
if err != nil {
return errors.Wrapf(err, "GetProvider")
}
if cloudprovider.IsSupportCDN(driver) && syncRange.NeedSyncResource(cloudprovider.CLOUD_CAPABILITY_CDN) {
err = syncCdnDomains(ctx, userCred, SSyncResultSet{}, provider, driver)
if err != nil {
log.Errorf("syncCdnDomains error: %v", err)
}
}
if cloudprovider.IsSupportInterVpcNetwork(driver) && syncRange.NeedSyncResource(cloudprovider.CLOUD_CAPABILITY_INTERVPCNETWORK) {
err = syncInterVpcNetworks(ctx, userCred, SSyncResultSet{}, provider, driver)
if err != nil {
log.Errorf("syncInterVpcNetworks error: %v", err)
}
}
if syncRange.NeedSyncResource(cloudprovider.CLOUD_CAPABILITY_NETWORK) {
err = syncGlobalVpcs(ctx, userCred, SSyncResultSet{}, provider, driver)
if err != nil {
log.Errorf("syncGlobalVpcs error: %v", err)
}
}
return nil
}
func syncCdnDomains(ctx context.Context, userCred mcclient.TokenCredential, syncResults SSyncResultSet, provider *SCloudprovider, driver cloudprovider.ICloudProvider) error {
domains, err := driver.GetICloudCDNDomains()
if err != nil {
return err
}
result := provider.SyncCDNDomains(ctx, userCred, domains)
log.Infof("Sync CDN for provider %s result: %s", provider.Name, result.Result())
return nil
}
func syncInterVpcNetworks(ctx context.Context, userCred mcclient.TokenCredential, syncResults SSyncResultSet, provider *SCloudprovider, driver cloudprovider.ICloudProvider) error {
networks, err := driver.GetICloudInterVpcNetworks()
if err != nil {
return errors.Wrapf(err, "GetICloudInterVpcNetworks")
}
localNetwork, remoteNetwork, result := provider.SyncInterVpcNetwork(ctx, userCred, networks)
log.Infof("Sync inter vpc network for cloudprovider %s result: %s", provider.GetName(), result.Result())
for i := range localNetwork {
lockman.LockObject(ctx, &localNetwork[i])
defer lockman.ReleaseObject(ctx, &localNetwork[i])
if localNetwork[i].Deleted {
continue
}
localNetwork[i].SyncInterVpcNetworkRouteSets(ctx, userCred, remoteNetwork[i])
}
return nil
}
func syncGlobalVpcs(ctx context.Context, userCred mcclient.TokenCredential, syncResults SSyncResultSet, provider *SCloudprovider, driver cloudprovider.ICloudProvider) error {
gvpcs, err := driver.GetICloudGlobalVpcs()
if err != nil {
return err
}
result := provider.SyncGlobalVpcs(ctx, userCred, gvpcs)
log.Infof("Sync global vpcs for cloudprovider %s result: %s", provider.GetName(), result.Result())
return nil
}
+175 -4
View File
@@ -16,16 +16,23 @@ package models
import (
"context"
"fmt"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/tristate"
"yunion.io/x/pkg/util/compare"
"yunion.io/x/sqlchemy"
"yunion.io/x/onecloud/pkg/apis"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
"yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
"yunion.io/x/onecloud/pkg/cloudcommon/validators"
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/util/rbacutils"
@@ -34,6 +41,8 @@ import (
type SGlobalVpcManager struct {
db.SEnabledStatusInfrasResourceBaseManager
db.SExternalizedResourceBaseManager
SManagedResourceBaseManager
}
var GlobalVpcManager *SGlobalVpcManager
@@ -52,15 +61,18 @@ func init() {
type SGlobalVpc struct {
db.SEnabledStatusInfrasResourceBase
db.SExternalizedResourceBase
SManagedResourceBase
}
func (self *SGlobalVpc) ValidateDeleteCondition(ctx context.Context, info jsonutils.JSONObject) error {
vpcs, err := self.GetVpcs()
if err != nil {
return errors.Wrap(err, "self.GetVpcs")
return httperrors.NewInternalServerError("GetVpcs fail %s", err)
}
if len(vpcs) > 0 {
return fmt.Errorf("not an empty globalvpc")
return httperrors.NewNotEmptyError("global vpc has associate %d vpcs", len(vpcs))
}
return self.SEnabledStatusInfrasResourceBase.ValidateDeleteCondition(ctx, nil)
}
@@ -110,12 +122,20 @@ func (manager *SGlobalVpcManager) ValidateCreateData(
query jsonutils.JSONObject,
input api.GlobalVpcCreateInput,
) (api.GlobalVpcCreateInput, error) {
input.Status = api.GLOBAL_VPC_STATUS_AVAILABLE
input.Status = apis.STATUS_CREATING
var err error
input.EnabledStatusInfrasResourceBaseCreateInput, err = manager.SEnabledStatusInfrasResourceBaseManager.ValidateCreateData(ctx, userCred, ownerId, query, input.EnabledStatusInfrasResourceBaseCreateInput)
if err != nil {
return input, errors.Wrap(err, "manager.SEnabledStatusInfrasResourceBaseManager.ValidateCreateData")
}
if len(input.CloudproviderId) == 0 {
return input, httperrors.NewMissingParameterError("cloudprovider_id")
}
_, err = validators.ValidateModel(userCred, CloudproviderManager, &input.CloudproviderId)
if err != nil {
return input, err
}
input.ManagerId = input.CloudproviderId
quota := &SDomainQuota{
SBaseDomainQuotaKeys: quotas.SBaseDomainQuotaKeys{
DomainId: ownerId.GetProjectDomainId(),
@@ -142,6 +162,15 @@ func (self *SGlobalVpc) PostCreate(ctx context.Context, userCred mcclient.TokenC
if err != nil {
log.Errorf("CancelPendingUsage %s", err)
}
self.StartCreateTask(ctx, userCred, "")
}
func (self *SGlobalVpc) StartCreateTask(ctx context.Context, userCred mcclient.TokenCredential, parentTaskId string) error {
task, err := taskman.TaskManager.NewTask(ctx, "GlobalVpcCreateTask", self, userCred, nil, parentTaskId, "", nil)
if err != nil {
return errors.Wrapf(err, "NewTask")
}
return task.ScheduleRun(nil)
}
func (self *SGlobalVpc) ValidateUpdateData(
@@ -241,3 +270,145 @@ func (globalVpc *SGlobalVpc) GetChangeOwnerRequiredDomainIds() []string {
}
return requires
}
func (self *SCloudprovider) GetGlobalVpcs() ([]SGlobalVpc, error) {
q := GlobalVpcManager.Query().Equals("manager_id", self.Id)
vpcs := []SGlobalVpc{}
err := db.FetchModelObjects(GlobalVpcManager, q, &vpcs)
if err != nil {
return nil, errors.Wrapf(err, "db.FetchModelObjects")
}
return vpcs, nil
}
func (self *SCloudprovider) SyncGlobalVpcs(ctx context.Context, userCred mcclient.TokenCredential, exts []cloudprovider.ICloudGlobalVpc) compare.SyncResult {
lockman.LockRawObject(ctx, GlobalVpcManager.Keyword(), self.Id)
defer lockman.ReleaseRawObject(ctx, GlobalVpcManager.Keyword(), self.Id)
result := compare.SyncResult{}
dbVpcs, err := self.GetGlobalVpcs()
if err != nil {
result.Error(err)
return result
}
removed := make([]SGlobalVpc, 0)
commondb := make([]SGlobalVpc, 0)
commonext := make([]cloudprovider.ICloudGlobalVpc, 0)
added := make([]cloudprovider.ICloudGlobalVpc, 0)
err = compare.CompareSets(dbVpcs, exts, &removed, &commondb, &commonext, &added)
if err != nil {
result.Error(err)
return result
}
for i := 0; i < len(removed); i += 1 {
err = removed[i].syncRemoveGlobalVpc(ctx, userCred)
if err != nil {
result.DeleteError(err)
continue
}
result.Delete()
}
for i := 0; i < len(commondb); i += 1 {
err = commondb[i].SyncWithCloudGlobalVpc(ctx, userCred, commonext[i])
if err != nil {
result.UpdateError(err)
continue
}
result.Update()
}
for i := 0; i < len(added); i += 1 {
_, err := self.newFromCloudGlobalVpc(ctx, userCred, added[i])
if err != nil {
result.AddError(err)
continue
}
result.Add()
}
return result
}
func (self *SGlobalVpc) Delete(ctx context.Context, userCred mcclient.TokenCredential) error {
return nil
}
func (self *SGlobalVpc) RealDelete(ctx context.Context, userCred mcclient.TokenCredential) error {
return self.SEnabledStatusInfrasResourceBase.Delete(ctx, userCred)
}
func (self *SGlobalVpc) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error {
return self.StartDeleteTask(ctx, userCred, "")
}
func (self *SGlobalVpc) StartDeleteTask(ctx context.Context, userCred mcclient.TokenCredential, parentTaskId string) error {
task, err := taskman.TaskManager.NewTask(ctx, "GlobalVpcDeleteTask", self, userCred, nil, parentTaskId, "", nil)
if err != nil {
return errors.Wrapf(err, "NewTask")
}
self.SetStatus(userCred, apis.STATUS_DELETING, "")
return task.ScheduleRun(nil)
}
func (self *SGlobalVpc) GetICloudGlobalVpc() (cloudprovider.ICloudGlobalVpc, error) {
if len(self.ExternalId) == 0 {
return nil, errors.Wrapf(cloudprovider.ErrNotFound, "empty external id")
}
provider, err := self.GetDriver()
if err != nil {
return nil, errors.Wrapf(err, "GetDriver")
}
return provider.GetICloudGlobalVpcById(self.ExternalId)
}
func (self *SGlobalVpc) syncRemoveGlobalVpc(ctx context.Context, userCred mcclient.TokenCredential) error {
err := self.ValidateDeleteCondition(ctx, nil)
if err != nil {
self.SetStatus(userCred, apis.STATUS_UNKNOWN, "sync remove")
return err
}
return self.RealDelete(ctx, userCred)
}
func (self *SGlobalVpc) SyncWithCloudGlobalVpc(ctx context.Context, userCred mcclient.TokenCredential, ext cloudprovider.ICloudGlobalVpc) error {
_, err := db.Update(self, func() error {
self.Status = ext.GetStatus()
return nil
})
return err
}
func (self *SCloudprovider) newFromCloudGlobalVpc(ctx context.Context, userCred mcclient.TokenCredential, ext cloudprovider.ICloudGlobalVpc) (*SGlobalVpc, error) {
gvpc := &SGlobalVpc{}
gvpc.SetModelManager(GlobalVpcManager, gvpc)
gvpc.Name = ext.GetName()
gvpc.Status = ext.GetStatus()
gvpc.ExternalId = ext.GetGlobalId()
gvpc.ManagerId = self.Id
gvpc.DomainId = self.DomainId
gvpc.Enabled = tristate.True
return gvpc, GlobalVpcManager.TableSpec().Insert(ctx, gvpc)
}
// 同步全局VPC状态
func (self *SGlobalVpc) PerformSyncstatus(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.SyncstatusInput) (jsonutils.JSONObject, error) {
var openTask = true
count, err := taskman.TaskManager.QueryTasksOfObject(self, time.Now().Add(-3*time.Minute), &openTask).CountWithError()
if err != nil {
return nil, err
}
if count > 0 {
return nil, httperrors.NewBadRequestError("Globalvpc has %d task active, can't sync status", count)
}
return nil, self.StartSyncstatusTask(ctx, userCred, "")
}
func (self *SGlobalVpc) StartSyncstatusTask(ctx context.Context, userCred mcclient.TokenCredential, parentTaskId string) error {
return StartResourceSyncStatusTask(ctx, userCred, self, "GlobalVpcSyncstatusTask", parentTaskId)
}
+15
View File
@@ -848,6 +848,21 @@ func (manager *SVpcManager) purgeAll(ctx context.Context, userCred mcclient.Toke
return nil
}
func (manager *SGlobalVpcManager) purgeAll(ctx context.Context, userCred mcclient.TokenCredential, providerId string) error {
gvpcs := make([]SGlobalVpc, 0)
err := fetchByManagerId(manager, providerId, &gvpcs)
if err != nil {
return err
}
for i := range gvpcs {
err := gvpcs[i].RealDelete(ctx, userCred)
if err != nil {
return err
}
}
return nil
}
func (net *SNetwork) purgeGuestnetworks(ctx context.Context, userCred mcclient.TokenCredential) error {
q := GuestnetworkManager.Query().Equals("network_id", net.Id)
gns := make([]SGuestnetwork, 0)
+2
View File
@@ -47,6 +47,7 @@ type SSecurityGroupCacheManager struct {
SCloudregionResourceBaseManager
SVpcResourceBaseManager
SSecurityGroupResourceBaseManager
SGlobalVpcResourceBaseManager
}
type SSecurityGroupCache struct {
@@ -55,6 +56,7 @@ type SSecurityGroupCache struct {
SCloudregionResourceBase
SManagedResourceBase
SSecurityGroupResourceBase
SGlobalVpcResourceBase `width:"36" charset:"ascii" list:"user" json:"globalvpc_id"`
// 被其他安全组引用的次数
ReferenceCount int `nullable:"false" list:"user" json:"reference_count"`
+23 -79
View File
@@ -480,10 +480,6 @@ func (manager *SVpcManager) SyncVPCs(ctx context.Context, userCred mcclient.Toke
localVPCs = append(localVPCs, commondb[i])
remoteVPCs = append(remoteVPCs, commonext[i])
syncResult.Update()
err = commondb[i].SyncGlobalVpc(ctx, userCred, provider.GetOwnerId(), provider)
if err != nil {
log.Errorf("%s(%s) sync global vpc error: %v", commondb[i].Name, commondb[i].Id, err)
}
}
for i := 0; i < len(added); i += 1 {
newVpc, err := manager.newFromCloudVpc(ctx, userCred, added[i], provider, region)
@@ -495,10 +491,6 @@ func (manager *SVpcManager) SyncVPCs(ctx context.Context, userCred mcclient.Toke
localVPCs = append(localVPCs, *newVpc)
remoteVPCs = append(remoteVPCs, added[i])
syncResult.Add()
err = newVpc.SyncGlobalVpc(ctx, userCred, provider.GetOwnerId(), provider)
if err != nil {
log.Errorf("%s(%s) sync global vpc error: %v", newVpc.Name, newVpc.Id, err)
}
}
return localVPCs, remoteVPCs, syncResult
@@ -531,72 +523,6 @@ func (self *SVpc) syncRemoveCloudVpc(ctx context.Context, userCred mcclient.Toke
return err
}
func (self *SVpc) SyncGlobalVpc(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, provider *SCloudprovider) error {
if len(self.GlobalvpcId) > 0 {
gv, _ := self.GetGlobalVpc()
SyncCloudDomain(userCred, gv, ownerId)
gv.SyncShareState(ctx, userCred, provider.getAccountShareInfo())
return nil
}
region, err := self.GetRegion()
if err != nil {
return errors.Wrap(err, "GetRegion")
}
if region.GetDriver().IsVpcBelongGlobalVpc() {
externalId := strings.Replace(self.ExternalId, region.ExternalId+"/", "", -1)
vpcs := []SVpc{}
sq := VpcManager.Query().SubQuery()
q := sq.Query().Filter(
sqlchemy.AND(
sqlchemy.Equals(sq.Field("manager_id"), self.ManagerId),
sqlchemy.NOT(sqlchemy.IsNullOrEmpty(sq.Field("globalvpc_id"))),
sqlchemy.Endswith(sq.Field("external_id"), externalId),
),
)
err := db.FetchModelObjects(VpcManager, q, &vpcs)
if err != nil {
return errors.Wrap(err, "db.FetchModelObjects")
}
globalvpcId := ""
if len(vpcs) > 0 {
globalvpcId = vpcs[0].GlobalvpcId
} else {
gv := &SGlobalVpc{}
gv.Name = self.Name
idx := strings.Index(gv.Name, "(")
if idx > 0 {
gv.Name = gv.Name[:idx]
}
gv.SetEnabled(true)
gv.Status = api.GLOBAL_VPC_STATUS_AVAILABLE
gv.SetModelManager(GlobalVpcManager, gv)
err = func() error {
lockman.LockRawObject(ctx, GlobalVpcManager.Keyword(), "name")
defer lockman.ReleaseRawObject(ctx, GlobalVpcManager.Keyword(), "name")
gv.Name, err = db.GenerateName(ctx, GlobalVpcManager, userCred, gv.Name)
if err != nil {
return errors.Wrap(err, "db.GenerateName")
}
return GlobalVpcManager.TableSpec().Insert(ctx, gv)
}()
if err != nil {
return errors.Wrap(err, "GlobalVpcManager.Insert")
}
SyncCloudDomain(userCred, gv, ownerId)
gv.SyncShareState(ctx, userCred, provider.getAccountShareInfo())
globalvpcId = gv.Id
}
_, err = db.Update(self, func() error {
self.GlobalvpcId = globalvpcId
return nil
})
return err
}
return nil
}
func (self *SVpc) SyncWithCloudVpc(ctx context.Context, userCred mcclient.TokenCredential, extVPC cloudprovider.ICloudVpc, provider *SCloudprovider) error {
diff, err := db.UpdateWithLock(ctx, self, func() error {
extVPC.Refresh()
@@ -609,6 +535,17 @@ func (self *SVpc) SyncWithCloudVpc(ctx context.Context, userCred mcclient.TokenC
self.IsEmulated = extVPC.IsEmulated()
self.ExternalAccessMode = extVPC.GetExternalAccessMode()
if gId := extVPC.GetGlobalVpcId(); len(gId) > 0 {
gVpc, err := db.FetchByExternalIdAndManagerId(GlobalVpcManager, gId, func(q *sqlchemy.SQuery) *sqlchemy.SQuery {
return q.Equals("manager_id", self.ManagerId)
})
if err != nil {
log.Errorf("FetchGlobalVpc %s error: %v", gId, err)
} else {
self.GlobalvpcId = gVpc.GetId()
}
}
return nil
})
if err != nil {
@@ -636,8 +573,17 @@ func (manager *SVpcManager) newFromCloudVpc(ctx context.Context, userCred mcclie
vpc.CidrBlock = extVPC.GetCidrBlock()
vpc.ExternalAccessMode = extVPC.GetExternalAccessMode()
vpc.CloudregionId = region.Id
vpc.ManagerId = provider.Id
if gId := extVPC.GetGlobalVpcId(); len(gId) > 0 {
gVpc, err := db.FetchByExternalIdAndManagerId(GlobalVpcManager, gId, func(q *sqlchemy.SQuery) *sqlchemy.SQuery {
return q.Equals("manager_id", provider.Id)
})
if err != nil {
log.Errorf("FetchGlobalVpc %s error: %v", gId, err)
} else {
vpc.GlobalvpcId = gVpc.GetId()
}
}
vpc.IsEmulated = extVPC.IsEmulated()
@@ -923,13 +869,11 @@ func (self *SVpc) GetIVpc() (cloudprovider.ICloudVpc, error) {
iregion, err = provider.GetIRegionById(region.ExternalId)
}
if err != nil {
log.Errorf("fail to find iregion: %s", err)
return nil, err
return nil, errors.Wrapf(err, "find iregion")
}
ivpc, err := iregion.GetIVpcById(self.ExternalId)
if err != nil {
log.Errorf("fail to find ivpc by id %s %s", self.ExternalId, err)
return nil, err
return nil, errors.Wrapf(err, "GetIVpcById")
}
return ivpc, nil
}
+30 -38
View File
@@ -71,10 +71,6 @@ func (self *SGoogleRegionDriver) IsVpcBelongGlobalVpc() bool {
return true
}
func (self *SGoogleRegionDriver) IsVpcCreateNeedInputCidr() bool {
return false
}
func (self *SGoogleRegionDriver) RequestCreateVpc(ctx context.Context, userCred mcclient.TokenCredential, region *models.SCloudregion, vpc *models.SVpc, task taskman.ITask) error {
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
provider := vpc.GetCloudprovider()
@@ -89,7 +85,17 @@ func (self *SGoogleRegionDriver) RequestCreateVpc(ctx context.Context, userCred
if err != nil {
return nil, errors.Wrap(err, "vpc.GetIRegion")
}
ivpc, err := iregion.CreateIVpc(vpc.Name, vpc.Description, vpc.CidrBlock)
gvpc, err := vpc.GetGlobalVpc()
if err != nil {
return nil, errors.Wrapf(err, "GetGlobalVpc")
}
opts := &cloudprovider.VpcCreateOptions{
NAME: vpc.Name,
CIDR: vpc.CidrBlock,
GlobalVpcExternalId: gvpc.ExternalId,
Desc: vpc.Description,
}
ivpc, err := iregion.CreateIVpc(opts)
if err != nil {
return nil, errors.Wrap(err, "iregion.CreateIVpc")
}
@@ -130,48 +136,15 @@ func (self *SGoogleRegionDriver) RequestDeleteVpc(ctx context.Context, userCred
ivpc, err := region.GetIVpcById(vpc.GetExternalId())
if err != nil {
if errors.Cause(err) == cloudprovider.ErrNotFound {
err = vpc.Purge(ctx, userCred)
if err != nil {
return nil, errors.Wrap(err, "vpc.Purge")
}
return nil, nil
}
return nil, errors.Wrap(err, "region.GetIVpcById")
}
globalVpc, err := vpc.GetGlobalVpc()
if err != nil {
return nil, errors.Wrap(err, "vpc.GetGlobalVpc")
}
vpcs, err := globalVpc.GetVpcs()
if err != nil {
return nil, errors.Wrap(err, "globalVpc.GetVpcs")
}
for i := range vpcs {
if vpcs[i].Status == api.VPC_STATUS_AVAILABLE && vpcs[i].ManagerId == vpc.ManagerId {
err = vpc.ValidateDeleteCondition(ctx, nil)
if err != nil {
return nil, errors.Wrapf(err, "vpc %s(%s) not empty", vpc.Name, vpc.Id)
}
}
}
err = ivpc.Delete()
if err != nil {
return nil, errors.Wrap(err, "ivpc.Delete")
}
for i := range vpcs {
if vpcs[i].ManagerId == vpc.ManagerId && vpcs[i].Id != vpc.Id {
err = vpcs[i].Purge(ctx, userCred)
if err != nil {
return nil, errors.Wrapf(err, "vpc.Purge %s(%s)", vpc.Name, vpc.Id)
}
}
}
return nil, nil
})
return nil
@@ -277,5 +250,24 @@ func (self *SGoogleRegionDriver) ValidateCreateVpcData(ctx context.Context, user
if cidrV.Value.MaskLen < 8 || cidrV.Value.MaskLen > 29 {
return input, httperrors.NewInputParameterError("%s request the mask range should be between 8 and 29", self.GetProvider())
}
if len(input.GlobalvpcId) == 0 {
_manager, err := validators.ValidateModel(userCred, models.CloudproviderManager, &input.CloudproviderId)
if err != nil {
return input, err
}
manager := _manager.(*models.SCloudprovider)
globalVpcs, err := manager.GetGlobalVpcs()
if err != nil {
return input, errors.Wrapf(err, "GetGlobalVpcs")
}
if len(globalVpcs) != 1 {
return input, httperrors.NewMissingParameterError("globalvpc_id")
}
input.GlobalvpcId = globalVpcs[0].Id
}
_, err := validators.ValidateModel(userCred, models.GlobalVpcManager, &input.GlobalvpcId)
if err != nil {
return input, err
}
return input, nil
}
+11 -2
View File
@@ -1206,7 +1206,12 @@ func (self *SManagedVirtualizationRegionDriver) RequestCreateVpc(ctx context.Con
if err != nil {
return nil, errors.Wrap(err, "vpc.GetIRegion")
}
ivpc, err := iregion.CreateIVpc(vpc.Name, vpc.Description, vpc.CidrBlock)
opts := &cloudprovider.VpcCreateOptions{
NAME: vpc.Name,
CIDR: vpc.CidrBlock,
Desc: vpc.Description,
}
ivpc, err := iregion.CreateIVpc(opts)
if err != nil {
return nil, errors.Wrap(err, "iregion.CreateIVpc")
}
@@ -1512,7 +1517,11 @@ func (self *SManagedVirtualizationRegionDriver) RequestPreSnapshotPolicyApply(ct
func (self *SManagedVirtualizationRegionDriver) GetSecurityGroupVpcId(ctx context.Context, userCred mcclient.TokenCredential, region *models.SCloudregion, host *models.SHost, vpc *models.SVpc, classic bool) (string, error) {
if region.GetDriver().IsSecurityGroupBelongGlobalVpc() {
return strings.TrimPrefix(vpc.ExternalId, region.ExternalId+"/"), nil
gvpc, err := vpc.GetGlobalVpc()
if err != nil {
return "", err
}
return gvpc.ExternalId, nil
} else if region.GetDriver().IsSupportClassicSecurityGroup() && (classic || (host != nil && strings.HasSuffix(host.Name, "-classic"))) {
return "classic", nil
} else if region.GetDriver().IsSecurityGroupBelongVpc() {
+1 -1
View File
@@ -84,7 +84,7 @@ func (self *CloudAccountSyncInfoTask) OnCloudaccountSyncReady(ctx context.Contex
}
if syncRange.FullSync {
err := cloudaccount.SyncAccountResources(ctx, self.GetUserCred())
err := models.SyncCloudaccountResources(ctx, self.GetUserCred(), cloudaccount, &syncRange)
if err != nil {
log.Errorf("SyncAccountResources error: %v", err)
}
@@ -16,13 +16,10 @@ package tasks
import (
"context"
"fmt"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/appsrv"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
@@ -76,24 +73,10 @@ func (self *CloudProviderSyncInfoTask) OnInit(ctx context.Context, obj db.IStand
self.SetStage("OnSyncCloudProviderPreInfoComplete", nil)
syncRange := self.GetSyncRange()
taskman.LocalTaskRun(self, func() (jsonutils.JSONObject, error) {
p, err := provider.GetProvider()
if err != nil {
return nil, errors.Wrap(err, "GetProvider")
}
quotas, err := p.GetICloudQuotas()
if err == nil {
result := models.CloudproviderQuotaManager.SyncQuotas(ctx, self.GetUserCred(), provider.GetOwnerId(), provider, nil, api.CLOUD_PROVIDER_QUOTA_RANGE_CLOUDPROVIDER, quotas)
msg := result.Result()
notes := fmt.Sprintf("SyncQuotas for provider %s result: %s", provider.Name, msg)
log.Infof(notes)
}
domains, err := p.GetICloudCDNDomains()
if err == nil {
result := provider.SyncCDNDomains(ctx, self.GetUserCred(), domains)
log.Infof("Sync CDN for provider %s result: %s", provider.Name, result.Result())
}
return nil, nil
return nil, models.SyncCloudproviderResources(ctx, self.GetUserCred(), provider, &syncRange)
})
}
@@ -106,7 +89,6 @@ func (self *CloudProviderSyncInfoTask) OnSyncCloudProviderPreInfoComplete(ctx co
taskman.LocalTaskRunWithWorkers(self, func() (jsonutils.JSONObject, error) {
provider.SyncCallSyncCloudproviderRegions(ctx, self.UserCred, syncRange)
provider.SyncCallSyncCloudproviderInterVpcNetwork(ctx, self.UserCred)
return nil, nil
}, syncLocalTaskWorkerMan)
}
+108
View File
@@ -0,0 +1,108 @@
// 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.
// 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 gvpcreed 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 langugvpce governing permissions and
// limitations under the License.
package tasks
import (
"context"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/onecloud/pkg/apis"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
"yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/util/logclient"
)
type GlobalVpcCreateTask struct {
taskman.STask
}
func init() {
taskman.RegisterTask(GlobalVpcCreateTask{})
}
func (self *GlobalVpcCreateTask) taskFailed(ctx context.Context, gvpc *models.SGlobalVpc, err error) {
gvpc.SetStatus(self.UserCred, apis.STATUS_CREATE_FAILED, err.Error())
logclient.AddActionLogWithStartable(self, gvpc, logclient.ACT_ALLOCATE, err, self.UserCred, false)
self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
}
func (self *GlobalVpcCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
gvpc := obj.(*models.SGlobalVpc)
opts := &cloudprovider.GlobalVpcCreateOptions{
NAME: gvpc.Name,
Desc: gvpc.Description,
}
log.Infof("global vpc create params: %s", jsonutils.Marshal(opts).String())
provider, err := gvpc.GetDriver()
if err != nil {
self.taskFailed(ctx, gvpc, errors.Wrapf(err, "GetDriver"))
return
}
iVpc, err := provider.CreateICloudGlobalVpc(opts)
if err != nil {
self.taskFailed(ctx, gvpc, errors.Wrapf(err, "CreateICloudGlobalVpc"))
return
}
db.SetExternalId(gvpc, self.GetUserCred(), iVpc.GetGlobalId())
cloudprovider.WaitMultiStatus(iVpc, []string{
api.GLOBAL_VPC_STATUS_AVAILABLE,
apis.STATUS_CREATE_FAILED,
apis.STATUS_UNKNOWN,
}, time.Second*5, time.Minute*10)
notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
Obj: self,
Action: notifyclient.ActionCreate,
})
self.SetStage("OnSyncstatusComplete", nil)
gvpc.StartSyncstatusTask(ctx, self.GetUserCred(), self.GetTaskId())
}
func (self *GlobalVpcCreateTask) OnSyncstatusComplete(ctx context.Context, gvpc *models.SGlobalVpc, data jsonutils.JSONObject) {
self.SetStageComplete(ctx, nil)
}
func (self *GlobalVpcCreateTask) OnSyncstatusCompleteFailed(ctx context.Context, gvpc *models.SGlobalVpc, data jsonutils.JSONObject) {
self.SetStageFailed(ctx, data)
}
@@ -0,0 +1,88 @@
// 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.
// 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 gvpcreed 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 langugvpce governing permissions and
// limitations under the License.
package tasks
import (
"context"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/onecloud/pkg/apis"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
"yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/util/logclient"
)
type GlobalVpcDeleteTask struct {
taskman.STask
}
func init() {
taskman.RegisterTask(GlobalVpcDeleteTask{})
}
func (self *GlobalVpcDeleteTask) taskFailed(ctx context.Context, gvpc *models.SGlobalVpc, err error) {
gvpc.SetStatus(self.UserCred, apis.STATUS_DELETE_FAILED, err.Error())
logclient.AddActionLogWithStartable(self, gvpc, logclient.ACT_DELOCATE, err, self.UserCred, false)
self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
}
func (self *GlobalVpcDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
gvpc := obj.(*models.SGlobalVpc)
iVpc, err := gvpc.GetICloudGlobalVpc()
if err != nil {
if errors.Cause(err) == cloudprovider.ErrNotFound {
self.taskComplete(ctx, gvpc)
return
}
self.taskFailed(ctx, gvpc, errors.Wrapf(err, "gvpc.GetICloudGlobalVpc"))
return
}
err = iVpc.Delete()
if err != nil {
self.taskFailed(ctx, gvpc, errors.Wrapf(err, "iVpc.Delete"))
return
}
cloudprovider.WaitDeleted(iVpc, time.Second*10, time.Minute*5)
self.taskComplete(ctx, gvpc)
}
func (self *GlobalVpcDeleteTask) taskComplete(ctx context.Context, gvpc *models.SGlobalVpc) {
gvpc.RealDelete(ctx, self.GetUserCred())
notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
Obj: self,
Action: notifyclient.ActionDelete,
})
self.SetStageComplete(ctx, nil)
}
@@ -0,0 +1,61 @@
// 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 tasks
import (
"context"
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/onecloud/pkg/apis"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
"yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/util/logclient"
)
type GlobalVpcSyncstatusTask struct {
taskman.STask
}
func init() {
taskman.RegisterTask(GlobalVpcSyncstatusTask{})
}
func (self *GlobalVpcSyncstatusTask) taskFail(ctx context.Context, gvpc *models.SGlobalVpc, err error) {
gvpc.SetStatus(self.GetUserCred(), apis.STATUS_UNKNOWN, err.Error())
db.OpsLog.LogEvent(gvpc, db.ACT_SYNC_STATUS, err, self.UserCred)
logclient.AddActionLogWithStartable(self, gvpc, logclient.ACT_SYNC_STATUS, err, self.UserCred, false)
self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
}
func (self *GlobalVpcSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
gvpc := obj.(*models.SGlobalVpc)
iVpc, err := gvpc.GetICloudGlobalVpc()
if err != nil {
self.taskFail(ctx, gvpc, errors.Wrapf(err, "gvpc.GetICloudGlobalVpc"))
return
}
err = gvpc.SyncWithCloudGlobalVpc(ctx, self.GetUserCred(), iVpc)
if err != nil {
self.taskFail(ctx, gvpc, errors.Wrapf(err, "gvpc.SyncWithCloudGlobalVpc"))
return
}
self.SetStageComplete(ctx, nil)
}
+50
View File
@@ -0,0 +1,50 @@
// 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 compute
import (
"yunion.io/x/jsonutils"
"yunion.io/x/onecloud/pkg/mcclient/options"
)
type GlobalVpcListOptions struct {
options.BaseListOptions
}
func (opts *GlobalVpcListOptions) Params() (jsonutils.JSONObject, error) {
return options.ListStructToParams(opts)
}
type GlobalVpcIdOption struct {
ID string `help:"Global vpc Id"`
}
func (opts *GlobalVpcIdOption) GetId() string {
return opts.ID
}
func (opts *GlobalVpcIdOption) Params() (jsonutils.JSONObject, error) {
return nil, nil
}
type GlobalVpcCreateOptions struct {
NAME string `help:"Global vpc name"`
MANAGER string `help:"Cloudprovider Id"`
}
func (opts *GlobalVpcCreateOptions) Params() (jsonutils.JSONObject, error) {
return options.StructToParams(opts)
}
+4
View File
@@ -50,6 +50,7 @@ type VpcCreateOptions struct {
Desc string `help:"Description of the VPC"`
Manager string `help:"ID or Name of Cloud provider" json:"manager_id"`
ExternalAccessMode string `help:"Filter by external access mode" choices:"distgw|eip|eip-distgw" default:""`
GlobalvpcId string `help:"Global vpc id, Only for Google Cloud"`
}
func (opts *VpcCreateOptions) Params() (jsonutils.JSONObject, error) {
@@ -72,6 +73,9 @@ func (opts *VpcCreateOptions) Params() (jsonutils.JSONObject, error) {
if len(opts.Manager) > 0 {
params.Add(jsonutils.NewString(opts.Manager), "manager_id")
}
if len(opts.GlobalvpcId) > 0 {
params.Add(jsonutils.NewString(opts.GlobalvpcId), "globalvpc_id")
}
return params, nil
}
+1
View File
@@ -708,6 +708,7 @@ func (region *SAliyunClient) GetCapabilities() []string {
cloudprovider.CLOUD_CAPABILITY_NAT,
cloudprovider.CLOUD_CAPABILITY_NAS,
cloudprovider.CLOUD_CAPABILITY_WAF,
cloudprovider.CLOUD_CAPABILITY_QUOTA + cloudprovider.READ_ONLY_SUFFIX,
cloudprovider.CLOUD_CAPABILITY_MONGO_DB + cloudprovider.READ_ONLY_SUFFIX,
cloudprovider.CLOUD_CAPABILITY_ES + cloudprovider.READ_ONLY_SUFFIX,
cloudprovider.CLOUD_CAPABILITY_KAFKA + cloudprovider.READ_ONLY_SUFFIX,
+16 -8
View File
@@ -685,16 +685,16 @@ func (self *SRegion) ModifyInstanceVNCUrlPassword(instanceId string, passwd stri
return err
}
func (self *SRegion) CreateIVpc(name string, desc string, cidr string) (cloudprovider.ICloudVpc, error) {
func (self *SRegion) CreateVpc(opts *cloudprovider.VpcCreateOptions) (*SVpc, error) {
params := make(map[string]string)
if len(cidr) > 0 {
params["CidrBlock"] = cidr
if len(opts.CIDR) > 0 {
params["CidrBlock"] = opts.CIDR
}
if len(name) > 0 {
params["VpcName"] = name
if len(opts.NAME) > 0 {
params["VpcName"] = opts.NAME
}
if len(desc) > 0 {
params["Description"] = desc
if len(opts.Desc) > 0 {
params["Description"] = opts.Desc
}
params["ClientToken"] = utils.GenRequestId(20)
body, err := self.ecsRequest("CreateVpc", params)
@@ -709,7 +709,15 @@ func (self *SRegion) CreateIVpc(name string, desc string, cidr string) (cloudpro
if err != nil {
return nil, err
}
return self.GetIVpcById(vpcId)
return self.getVpc(vpcId)
}
func (self *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
vpc, err := self.CreateVpc(opts)
if err != nil {
return nil, err
}
return vpc, nil
}
func (self *SRegion) DeleteVpc(vpcId string) error {
+7 -1
View File
@@ -15,6 +15,7 @@
package shell
import (
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/onecloud/pkg/multicloud/aliyun"
"yunion.io/x/onecloud/pkg/util/shellutils"
)
@@ -40,7 +41,12 @@ func init() {
}
shellutils.R(&VpcCreateOptions{}, "vpc-create", "Create vpc", func(cli *aliyun.SRegion, args *VpcCreateOptions) error {
vpc, err := cli.CreateIVpc(args.Name, args.Desc, args.CIDR)
opts := cloudprovider.VpcCreateOptions{
NAME: args.Name,
CIDR: args.CIDR,
Desc: args.Desc,
}
vpc, err := cli.CreateIVpc(&opts)
if err != nil {
return err
}
+1
View File
@@ -516,6 +516,7 @@ func (region *SApsaraClient) GetCapabilities() []string {
cloudprovider.CLOUD_CAPABILITY_OBJECTSTORE,
cloudprovider.CLOUD_CAPABILITY_RDS,
cloudprovider.CLOUD_CAPABILITY_CACHE,
cloudprovider.CLOUD_CAPABILITY_QUOTA + cloudprovider.READ_ONLY_SUFFIX,
}
return caps
}
+7 -7
View File
@@ -553,16 +553,16 @@ func (self *SRegion) ModifyInstanceVNCUrlPassword(instanceId string, passwd stri
return err
}
func (self *SRegion) CreateIVpc(name string, desc string, cidr string) (cloudprovider.ICloudVpc, error) {
func (self *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
params := make(map[string]string)
if len(cidr) > 0 {
params["CidrBlock"] = cidr
if len(opts.CIDR) > 0 {
params["CidrBlock"] = opts.CIDR
}
if len(name) > 0 {
params["VpcName"] = name
if len(opts.NAME) > 0 {
params["VpcName"] = opts.NAME
}
if len(desc) > 0 {
params["Description"] = desc
if len(opts.Desc) > 0 {
params["Description"] = opts.Desc
}
params["ClientToken"] = utils.GenRequestId(20)
body, err := self.ecsRequest("CreateVpc", params)
+6 -6
View File
@@ -693,14 +693,14 @@ func (self *SRegion) GetIStoragecacheById(id string) (cloudprovider.ICloudStorag
}
func (self *SRegion) CreateIVpc(name string, desc string, cidr string) (cloudprovider.ICloudVpc, error) {
func (self *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
tagspec := TagSpec{ResourceType: "vpc"}
if len(name) > 0 {
tagspec.SetNameTag(name)
if len(opts.NAME) > 0 {
tagspec.SetNameTag(opts.NAME)
}
if len(desc) > 0 {
tagspec.SetDescTag(desc)
if len(opts.Desc) > 0 {
tagspec.SetDescTag(opts.Desc)
}
spec, err := tagspec.GetTagSpecifications()
@@ -714,7 +714,7 @@ func (self *SRegion) CreateIVpc(name string, desc string, cidr string) (cloudpro
}
// start create vpc
vpc, err := ec2Client.CreateVpc(&ec2.CreateVpcInput{CidrBlock: &cidr})
vpc, err := ec2Client.CreateVpc(&ec2.CreateVpcInput{CidrBlock: &opts.CIDR})
if err != nil {
return nil, errors.Wrap(err, "CreateVpc")
}
+1
View File
@@ -995,6 +995,7 @@ func (self *SAzureClient) GetCapabilities() []string {
cloudprovider.CLOUD_CAPABILITY_CLOUDID,
cloudprovider.CLOUD_CAPABILITY_SAML_AUTH,
cloudprovider.CLOUD_CAPABILITY_WAF,
cloudprovider.CLOUD_CAPABILITY_QUOTA + cloudprovider.READ_ONLY_SUFFIX,
cloudprovider.CLOUD_CAPABILITY_CACHE + cloudprovider.READ_ONLY_SUFFIX,
cloudprovider.CLOUD_CAPABILITY_APP + cloudprovider.READ_ONLY_SUFFIX,
cloudprovider.CLOUD_CAPABILITY_CONTAINER + cloudprovider.READ_ONLY_SUFFIX,
+3 -3
View File
@@ -165,14 +165,14 @@ func (self *SRegion) GetStatus() string {
return api.CLOUD_REGION_STATUS_INSERVER
}
func (self *SRegion) CreateIVpc(name string, desc string, cidr string) (cloudprovider.ICloudVpc, error) {
func (self *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
vpc := SVpc{
region: self,
Name: name,
Name: opts.NAME,
Location: self.Name,
Properties: VirtualNetworkPropertiesFormat{
AddressSpace: AddressSpace{
AddressPrefixes: []string{cidr},
AddressPrefixes: []string{opts.CIDR},
},
},
Type: "Microsoft.Network/virtualNetworks",
+7 -1
View File
@@ -15,6 +15,7 @@
package shell
import (
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/onecloud/pkg/multicloud/azure"
"yunion.io/x/onecloud/pkg/util/shellutils"
)
@@ -57,7 +58,12 @@ func init() {
}
shellutils.R(&VpcCreateOptions{}, "vpc-create", "Create vpc", func(cli *azure.SRegion, args *VpcCreateOptions) error {
vpc, err := cli.CreateIVpc(args.NAME, args.Desc, args.CIDR)
opts := &cloudprovider.VpcCreateOptions{
NAME: args.NAME,
CIDR: args.CIDR,
Desc: args.Desc,
}
vpc, err := cli.CreateIVpc(opts)
if err != nil {
return err
}
+4 -4
View File
@@ -122,11 +122,11 @@ func (self *SRegion) CreateWire(opts *cloudprovider.SWireCreateOptions, vpcId, d
return wire, self.create(&modules.Wires, input, wire)
}
func (self *SRegion) CreateIVpc(name string, desc string, cidr string) (cloudprovider.ICloudVpc, error) {
func (self *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
input := api.VpcCreateInput{}
input.Name = name
input.Description = desc
input.CidrBlock = cidr
input.Name = opts.NAME
input.Description = opts.Desc
input.CidrBlock = opts.CIDR
input.CloudregionId = self.Id
vpc := &SVpc{region: self}
return vpc, self.create(&modules.Vpcs, input, vpc)
+2 -2
View File
@@ -295,8 +295,8 @@ func (self *SRegion) DeleteSecurityGroup(securityGroupId string) error {
return nil
}
func (self *SRegion) CreateIVpc(name string, desc string, cidr string) (cloudprovider.ICloudVpc, error) {
return self.CreateVpc(name, cidr)
func (self *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
return self.CreateVpc(opts.NAME, opts.CIDR)
}
func (self *SRegion) CreateEIP(eip *cloudprovider.SEip) (cloudprovider.ICloudEIP, error) {
-1
View File
@@ -73,7 +73,6 @@ type SBucket struct {
Kind string
SelfLink string
Id string
Name string
ProjectNumber string
Metageneration string
-12
View File
@@ -378,18 +378,6 @@ func (rds *SDBInstance) GetZone3Id() string {
}
func (rds *SDBInstance) GetIVpcId() string {
if len(rds.Settings.IpConfiguration.PrivateNetwork) > 0 {
globalnetwork, err := rds.region.client.GetGlobalNetwork(rds.Settings.IpConfiguration.PrivateNetwork)
if err != nil {
log.Errorf("failed to get global network %s error: %v", rds.Settings.IpConfiguration.PrivateNetwork, err)
return ""
}
vpc := &SVpc{
region: rds.region,
globalnetwork: globalnetwork,
}
return vpc.GetGlobalId()
}
return ""
}
+2 -3
View File
@@ -34,7 +34,6 @@ type SDisk struct {
multicloud.SDisk
multicloud.GoogleTags
Id string
CreationTimestamp time.Time
SizeGB int
Zone string
@@ -67,7 +66,7 @@ func (region *SRegion) GetDisks(zone string, storageType string, maxResults int,
func (region *SRegion) GetDisk(id string) (*SDisk, error) {
disk := &SDisk{}
return disk, region.Get(id, disk)
return disk, region.Get("disks", id, disk)
}
func (disk *SDisk) GetStatus() string {
@@ -92,7 +91,7 @@ func (disk *SDisk) IsEmulated() bool {
}
func (disk *SDisk) Refresh() error {
_disk, err := disk.storage.zone.region.GetDisk(disk.SelfLink)
_disk, err := disk.storage.zone.region.GetDisk(disk.Id)
if err != nil {
return err
}
+5 -6
View File
@@ -35,7 +35,6 @@ type SAddress struct {
multicloud.SEipBase
multicloud.GoogleTags
Id string
CreationTimestamp time.Time
Description string
Address string
@@ -59,7 +58,7 @@ func (region *SRegion) GetEips(address string, maxResults int, pageToken string)
func (region *SRegion) GetEip(id string) (*SAddress, error) {
eip := &SAddress{region: region}
return eip, region.Get(id, eip)
return eip, region.Get("addresses", id, eip)
}
func (addr *SAddress) GetStatus() string {
@@ -99,15 +98,15 @@ func (addr *SAddress) GetBillingType() string {
return billing.BILLING_TYPE_POSTPAID
}
func (addr *SAddress) Refresh() error {
if addr.IsEmulated() {
func (self *SAddress) Refresh() error {
if self.IsEmulated() {
return nil
}
_addr, err := addr.region.GetEip(addr.SelfLink)
addr, err := self.region.GetEip(self.Id)
if err != nil {
return err
}
return jsonutils.Update(addr, _addr)
return jsonutils.Update(self, addr)
}
func (addr *SAddress) GetIpAddr() string {
+66 -10
View File
@@ -19,12 +19,18 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/onecloud/pkg/multicloud"
)
type SGlobalNetwork struct {
multicloud.GoogleTags
SResourceBase
Id string
client *SGoogleClient
CreationTimestamp time.Time
Description string
AutoCreateSubnetworks bool
@@ -33,9 +39,33 @@ type SGlobalNetwork struct {
Kind string
}
func (self *SGlobalNetwork) GetStatus() string {
return api.GLOBAL_VPC_STATUS_AVAILABLE
}
func (self *SGlobalNetwork) IsEmulated() bool {
return false
}
func (self *SGlobalNetwork) Delete() error {
return self.client.ecsDelete(self.SelfLink, nil)
}
func (self *SGlobalNetwork) Refresh() error {
gvpc, err := self.client.GetGlobalNetwork(self.Id)
if err != nil {
return err
}
return jsonutils.Update(self, gvpc)
}
func (cli *SGoogleClient) GetGlobalNetwork(id string) (*SGlobalNetwork, error) {
net := &SGlobalNetwork{}
return net, cli.ecsGet(id, net)
net := &SGlobalNetwork{client: cli}
return net, cli.ecsGet("global/networks", id, net)
}
func (self *SGoogleClient) GetICloudGlobalVpcById(id string) (cloudprovider.ICloudGlobalVpc, error) {
return self.GetGlobalNetwork(id)
}
func (cli *SGoogleClient) GetGlobalNetworks(maxResults int, pageToken string) ([]SGlobalNetwork, error) {
@@ -62,15 +92,41 @@ func (cli *SGoogleClient) GetGlobalNetworks(maxResults int, pageToken string) ([
return networks, nil
}
func (region *SRegion) CreateGlobalNetwork(name string, desc string) (*SGlobalNetwork, error) {
body := map[string]string{
"name": name,
"description": desc,
func (self *SGoogleClient) CreateGlobalNetwork(name string, desc string) (*SGlobalNetwork, error) {
body := map[string]interface{}{
"name": name,
"description": desc,
"autoCreateSubnetworks": false,
"mtu": 1460,
"routingConfig": map[string]string{
"routingMode": "REGIONAL",
},
}
globalnetwork := &SGlobalNetwork{}
err := region.Insert("global/networks", jsonutils.Marshal(body), globalnetwork)
globalnetwork := &SGlobalNetwork{client: self}
err := self.Insert("global/networks", jsonutils.Marshal(body), globalnetwork)
if err != nil {
return nil, errors.Wrap(err, "region.Insert")
return nil, errors.Wrap(err, "self.Insert")
}
return globalnetwork, nil
}
func (self *SGoogleClient) CreateICloudGlobalVpc(opts *cloudprovider.GlobalVpcCreateOptions) (cloudprovider.ICloudGlobalVpc, error) {
gvpc, err := self.CreateGlobalNetwork(opts.NAME, opts.Desc)
if err != nil {
return nil, errors.Wrapf(err, "CreateICloudGlobalVpc")
}
return gvpc, nil
}
func (self *SGoogleClient) GetICloudGlobalVpcs() ([]cloudprovider.ICloudGlobalVpc, error) {
gvpcs, err := self.GetGlobalNetworks(0, "")
if err != nil {
return nil, errors.Wrapf(err, "GetGlobalNetworks")
}
ret := []cloudprovider.ICloudGlobalVpc{}
for i := range gvpcs {
gvpcs[i].client = self
ret = append(ret, &gvpcs[i])
}
return ret, nil
}
+43 -9
View File
@@ -233,18 +233,51 @@ func jsonRequest(client *http.Client, method httputils.THttpMethod, domain, apiV
return _jsonRequest(client, method, _url, body, debug)
}
func (self *SGoogleClient) ecsGet(resource string, retval interface{}) error {
resp, err := jsonRequest(self.client, "GET", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, resource, nil, nil, self.debug)
if err != nil {
return err
func (self *SGoogleClient) ecsGet(resourceType, id string, retval interface{}) error {
params := map[string]string{
"filter": fmt.Sprintf(`id="%s"`, id),
}
if retval != nil {
err = resp.Unmarshal(retval)
if err != nil {
return errors.Wrap(err, "resp.Unmarshal")
resource := fmt.Sprintf("aggregated/%s", resourceType)
if strings.Contains(resourceType, "/") {
resource = resourceType
}
resp, err := self.ecsList(resource, params)
if err != nil {
return errors.Wrapf(err, "ecsList")
}
if resp.Contains("items") {
if strings.HasPrefix(resource, "aggregated/") {
items, err := resp.GetMap("items")
if err != nil {
return errors.Wrapf(err, "resp.GetMap(items)")
}
for _, values := range items {
if values.Contains(resourceType) {
arr, err := values.GetArray(resourceType)
if err != nil {
return errors.Wrapf(err, "v.GetArray(%s)", resourceType)
}
for i := range arr {
if _id, _ := arr[i].GetString("id"); _id == id {
return arr[i].Unmarshal(retval)
}
}
}
}
} else if strings.HasPrefix(resource, "global/") {
items, err := resp.GetArray("items")
if err != nil {
return errors.Wrapf(err, "resp.GetMap(items)")
}
for i := range items {
if _id, _ := items[i].GetString("id"); _id == id {
return items[i].Unmarshal(retval)
}
}
}
}
return nil
return errors.Wrapf(cloudprovider.ErrNotFound, id)
}
func (self *SGoogleClient) ecsList(resource string, params map[string]string) (jsonutils.JSONObject, error) {
@@ -940,6 +973,7 @@ func (self *SGoogleClient) GetCapabilities() []string {
// cloudprovider.CLOUD_CAPABILITY_CACHE,
// cloudprovider.CLOUD_CAPABILITY_EVENT,
cloudprovider.CLOUD_CAPABILITY_CLOUDID,
cloudprovider.CLOUD_CAPABILITY_QUOTA + cloudprovider.READ_ONLY_SUFFIX,
}
return caps
}
+16 -2
View File
@@ -44,11 +44,13 @@ type SImage struct {
multicloud.GoogleTags
storagecache *SStoragecache
SResourceBase
Name string
SelfLink string
Id string
// normalized image info
imgInfo *imagetools.ImageInfo
Id string
CreationTimestamp time.Time
Description string
SourceType string
@@ -66,6 +68,18 @@ type SImage struct {
Kind string
}
func (self *SImage) GetId() string {
return self.SelfLink
}
func (self *SImage) GetGlobalId() string {
return strings.TrimPrefix(self.SelfLink, fmt.Sprintf("%s/%s/", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION))
}
func (self *SImage) GetName() string {
return self.Name
}
func (region *SRegion) SetProjectId(id string) {
region.client.projectId = id
}
@@ -112,7 +126,7 @@ func (region *SRegion) GetImages(project string, maxResults int, pageToken strin
func (region *SRegion) GetImage(id string) (*SImage, error) {
image := &SImage{}
return image, region.Get(id, image)
return image, region.GetBySelfId(id, image)
}
func (image *SImage) GetMinRamSizeMb() int {
+19 -17
View File
@@ -90,7 +90,6 @@ type SInstance struct {
host *SHost
SResourceBase
Id string
CreationTimestamp time.Time
Description string
Tags SInstanceTag
@@ -126,7 +125,7 @@ func (region *SRegion) GetInstances(zone string, maxResults int, pageToken strin
func (region *SRegion) GetInstance(id string) (*SInstance, error) {
instance := &SInstance{}
return instance, region.Get(id, instance)
return instance, region.Get("instances", id, instance)
}
func (instance *SInstance) GetHostname() string {
@@ -147,16 +146,16 @@ func (instance *SInstance) fetchMachineType() error {
return nil
}
func (instance *SInstance) Refresh() error {
_instance, err := instance.host.zone.region.GetInstance(instance.SelfLink)
func (self *SInstance) Refresh() error {
instance, err := self.host.zone.region.GetInstance(self.Id)
if err != nil {
return err
}
err = jsonutils.Update(instance, _instance)
err = jsonutils.Update(self, instance)
if err != nil {
return err
}
instance.Labels = _instance.Labels
instance.Labels = self.Labels
return nil
}
@@ -211,7 +210,8 @@ func (instance *SInstance) GetIHostId() string {
func (instance *SInstance) GetIDisks() ([]cloudprovider.ICloudDisk, error) {
idisks := []cloudprovider.ICloudDisk{}
for _, disk := range instance.Disks {
_disk, err := instance.host.zone.region.GetDisk(disk.Source)
_disk := &SDisk{}
err := instance.host.zone.region.GetBySelfId(disk.Source, _disk)
if err != nil {
return nil, errors.Wrap(err, "GetDisk")
}
@@ -252,10 +252,10 @@ func (instance *SInstance) GetIEIP() (cloudprovider.ICloudEIP, error) {
}
eip := &SAddress{
region: instance.host.zone.region,
Id: instance.SelfLink,
Status: "IN_USE",
Address: conf.NatIP,
}
eip.Id = instance.Id
eip.SelfLink = instance.SelfLink
return eip, nil
}
@@ -353,11 +353,10 @@ func (instance *SInstance) GetSecurityGroupIds() ([]string, error) {
secgroupIds := []string{}
isecgroups := []cloudprovider.ICloudSecurityGroup{}
for _, networkinterface := range instance.NetworkInterfaces {
globalnetwork, err := instance.host.zone.region.client.GetGlobalNetwork(networkinterface.Network)
vpc, err := instance.host.zone.region.GetVpc(networkinterface.Subnetwork)
if err != nil {
return nil, errors.Wrap(err, "GetGlobalNetwork")
}
vpc := &SVpc{globalnetwork: globalnetwork, region: instance.host.zone.region}
_isecgroups, err := vpc.GetISecurityGroups()
if err != nil {
return nil, errors.Wrap(err, "vpc.GetISecurityGroups")
@@ -367,7 +366,9 @@ func (instance *SInstance) GetSecurityGroupIds() ([]string, error) {
if len(instance.ServiceAccounts) > 0 && isecgroup.GetName() == instance.ServiceAccounts[0].Email {
secgroupIds = append(secgroupIds, isecgroup.GetGlobalId())
}
if isecgroup.GetName() == globalnetwork.Name && !strings.Contains(isecgroup.GetGlobalId(), fmt.Sprintf("/%s/", SECGROUP_TYPE_TAG)) {
gvpcInfo := strings.Split(vpc.Network, "/")
gvpcName := gvpcInfo[len(gvpcInfo)-1]
if isecgroup.GetName() == gvpcName && !strings.Contains(isecgroup.GetGlobalId(), fmt.Sprintf("/%s/", SECGROUP_TYPE_TAG)) {
secgroupIds = append(secgroupIds, isecgroup.GetGlobalId())
}
}
@@ -462,7 +463,7 @@ func (instance *SInstance) UpdateUserData(userData string) error {
}
func (instance *SInstance) RebuildRoot(ctx context.Context, desc *cloudprovider.SManagedVMRebuildRootConfig) (string, error) {
diskId, err := instance.host.zone.region.RebuildRoot(instance.SelfLink, desc.ImageId, desc.SysSizeGB)
diskId, err := instance.host.zone.region.RebuildRoot(instance.Id, desc.ImageId, desc.SysSizeGB)
if err != nil {
return "", errors.Wrap(err, "region.RebuildRoot")
}
@@ -609,7 +610,7 @@ func (region *SRegion) getSecgroupByIds(ids []string) (map[string][]string, erro
}
func (region *SRegion) _createVM(zone string, desc *cloudprovider.SManagedVMCreateConfig) (*SInstance, error) {
network, err := region.GetNetwork(desc.ExternalNetworkId)
vpc, err := region.GetVpc(desc.ExternalNetworkId)
if err != nil {
return nil, errors.Wrap(err, "region.GetNetwork")
}
@@ -653,8 +654,8 @@ func (region *SRegion) _createVM(zone string, desc *cloudprovider.SManagedVMCrea
})
}
networkInterface := map[string]string{
"network": network.Network,
"subnetwork": network.SelfLink,
"network": vpc.Network,
"subnetwork": vpc.SelfLink,
}
if len(desc.IpAddr) > 0 {
networkInterface["networkIp"] = desc.IpAddr
@@ -774,7 +775,7 @@ func (region *SRegion) getSerialPortOutput(id string, port int, start int) (stri
Start int
Next int
}{}
err := region.Get(resource, &result)
err := region.GetBySelfId(resource, &result)
if err != nil {
return "", result.Next, errors.Wrap(err, "")
}
@@ -843,7 +844,8 @@ func (region *SRegion) RebuildRoot(instanceId string, imageId string, sysDiskSiz
}
if len(oldDisk) > 0 {
disk, err := region.GetDisk(oldDisk)
disk := &SDisk{}
err := region.GetBySelfId(oldDisk, disk)
if err != nil {
return "", errors.Wrap(err, "region.GetDisk")
}
+7 -6
View File
@@ -15,7 +15,6 @@
package google
import (
"yunion.io/x/log"
"yunion.io/x/pkg/util/netutils"
"yunion.io/x/onecloud/pkg/cloudprovider"
@@ -57,12 +56,14 @@ func (nic *SNetworkInterface) InClassicNetwork() bool {
}
func (nic *SNetworkInterface) GetINetwork() cloudprovider.ICloudNetwork {
network, err := nic.instance.host.zone.region.GetNetwork(nic.Subnetwork)
vpc := &SVpc{region: nic.instance.host.zone.region}
err := nic.instance.host.zone.region.GetBySelfId(nic.Subnetwork, vpc)
if err != nil {
log.Errorf("failed to found network(%s) for nic error: %v", nic.Subnetwork, err)
return nil
}
wire := nic.instance.host.GetWire()
network.wire = wire
return network
networks, _ := vpc.getWire().GetINetworks()
for i := range networks {
return networks[i]
}
return nil
}
+6 -19
View File
@@ -154,8 +154,8 @@ func (self *SLoadbalancer) GetNetworkIds() []string {
selfLinks := make([]string, 0)
networkIds := make([]string, 0)
for i := range igs {
if utils.IsInStringArray(igs[i].Network, selfLinks) {
selfLinks = append(selfLinks, igs[i].Network)
if utils.IsInStringArray(igs[i].Subnetwork, selfLinks) {
selfLinks = append(selfLinks, igs[i].Subnetwork)
network := SResourceBase{
Name: "",
SelfLink: igs[i].Network,
@@ -173,24 +173,11 @@ func (self *SLoadbalancer) GetVpcId() string {
return ""
}
if len(networkIds) >= 1 {
network, err := self.region.GetNetwork(networkIds[0])
if err == nil && network != nil {
wire := network.GetIWire()
if wire == nil {
return ""
}
vpc := wire.GetIVpc()
if vpc == nil {
return ""
}
vpc, err := self.region.GetVpc(networkIds[0])
if err == nil && vpc != nil {
return vpc.GetGlobalId()
}
log.Debugf("GetVpcId %s", err)
}
return ""
}
@@ -428,7 +415,7 @@ func (self *SRegion) GetLoadbalancer(resourceId string) (SLoadbalancer, error) {
var err error
if strings.Contains(resourceId, "/urlMaps/") {
ret := SUrlMap{}
err = self.Get(resourceId, &ret)
err = self.GetBySelfId(resourceId, &ret)
lb.isHttpLb = true
lb.urlMap = &ret
lb.SResourceBase = SResourceBase{
@@ -437,7 +424,7 @@ func (self *SRegion) GetLoadbalancer(resourceId string) (SLoadbalancer, error) {
}
} else {
ret := SBackendServices{}
err = self.Get(resourceId, &ret)
err = self.GetBySelfId(resourceId, &ret)
lb.backendServices = []SBackendServices{ret}
lb.SResourceBase = SResourceBase{
Name: ret.Name,
+1 -1
View File
@@ -145,7 +145,7 @@ func (self *SRegion) GetILoadBalancerCertificates() ([]cloudprovider.ICloudLoadb
func (self *SRegion) GetILoadBalancerCertificateById(certId string) (cloudprovider.ICloudLoadbalancerCertificate, error) {
ret := SLoadbalancerCertificate{}
err := self.Get(certId, &ret)
err := self.GetBySelfId(certId, &ret)
if err != nil {
return nil, errors.Wrap(err, "Get")
}
@@ -13,7 +13,6 @@ import (
type SUrlMap struct {
SResourceBase
ID string `json:"id"`
CreationTimestamp string `json:"creationTimestamp"`
HostRules []HostRule `json:"hostRules"`
PathMatchers []PathMatcher `json:"pathMatchers"`
@@ -26,7 +25,6 @@ type SUrlMap struct {
type SForwardingRule struct {
SResourceBase
ID string `json:"id"`
CreationTimestamp string `json:"creationTimestamp"`
Description string `json:"description"`
Region string `json:"region"`
@@ -52,7 +50,6 @@ type SForwardingRule struct {
type SBackendServices struct {
SResourceBase
ID string `json:"id"`
CreationTimestamp string `json:"creationTimestamp"`
Description string `json:"description"`
Backends []Backend `json:"backends"`
@@ -81,7 +78,6 @@ type SBackendServices struct {
type STargetHttpProxy struct {
SResourceBase
ID string `json:"id"`
CreationTimestamp string `json:"creationTimestamp"`
Description string `json:"description"`
URLMap string `json:"urlMap"`
@@ -94,7 +90,6 @@ type STargetHttpProxy struct {
type STargetHttpsProxy struct {
SResourceBase
ID string `json:"id"`
CreationTimestamp string `json:"creationTimestamp"`
Description string `json:"description"`
URLMap string `json:"urlMap"`
@@ -114,7 +109,6 @@ type SInstanceGroup struct {
region *SRegion
instances []SInstanceGroupInstance
ID string `json:"id"`
CreationTimestamp string `json:"creationTimestamp"`
Description string `json:"description"`
NamedPorts []NamedPort `json:"namedPorts"`
@@ -340,7 +334,6 @@ type ConnectionDraining struct {
type HealthChecks struct {
SResourceBase
ID string `json:"id"`
CreationTimestamp string `json:"creationTimestamp"`
Description string `json:"description"`
CheckIntervalSEC int64 `json:"checkIntervalSec"`
+1 -1
View File
@@ -49,7 +49,7 @@ func (region *SRegion) GetMachineTypes(zone string, maxResults int, pageToken st
func (region *SRegion) GetMachineType(id string) (*SMachineType, error) {
machine := &SMachineType{}
err := region.client.ecsGet(id, machine)
err := region.client.ecsGet("machineTypes", id, machine)
if err != nil {
return nil, err
}
+24 -65
View File
@@ -15,10 +15,6 @@
package google
import (
"fmt"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/util/netutils"
api "yunion.io/x/onecloud/pkg/apis/compute"
@@ -28,49 +24,28 @@ import (
)
type SNetwork struct {
wire *SWire
SResourceBase
multicloud.GoogleTags
Id string
CreationTimestamp time.Time
Network string
IpCidrRange string
Region string
GatewayAddress string
Status string
AvailableCpuPlatforms []string
PrivateIpGoogleAccess bool
Fingerprint string
Purpose string
Kind string
}
func (region *SRegion) GetNetworks(network string, maxResults int, pageToken string) ([]SNetwork, error) {
networks := []SNetwork{}
params := map[string]string{}
if len(network) > 0 {
params["filter"] = fmt.Sprintf(`network="%s"`, network)
}
resource := fmt.Sprintf("regions/%s/subnetworks", region.Name)
return networks, region.List(resource, params, maxResults, pageToken, &networks)
}
func (region *SRegion) GetNetwork(id string) (*SNetwork, error) {
network := &SNetwork{}
return network, region.Get(id, network)
wire *SWire
}
func (network *SNetwork) GetProjectId() string {
return network.wire.vpc.region.GetProjectId()
}
func (network *SNetwork) Refresh() error {
_network, err := network.wire.vpc.region.GetNetwork(network.SelfLink)
if err != nil {
return err
}
return jsonutils.Update(network, _network)
func (self *SNetwork) GetName() string {
return self.wire.vpc.GetName()
}
func (self *SNetwork) GetId() string {
return self.wire.vpc.GetId()
}
func (self *SNetwork) GetGlobalId() string {
return self.wire.vpc.GetGlobalId()
}
func (self *SNetwork) Refresh() error {
return self.wire.vpc.Refresh()
}
func (network *SNetwork) IsEmulated() bool {
@@ -82,7 +57,7 @@ func (network *SNetwork) GetStatus() string {
}
func (network *SNetwork) Delete() error {
return network.wire.vpc.region.Delete(network.SelfLink)
return network.wire.vpc.Delete()
}
func (network *SNetwork) GetAllocTimeoutSeconds() int {
@@ -93,27 +68,27 @@ func (network *SNetwork) GetIWire() cloudprovider.ICloudWire {
return network.wire
}
func (network *SNetwork) GetIpStart() string {
pref, _ := netutils.NewIPV4Prefix(network.IpCidrRange)
func (self *SNetwork) GetIpStart() string {
pref, _ := netutils.NewIPV4Prefix(self.wire.vpc.IpCidrRange)
startIp := pref.Address.NetAddr(pref.MaskLen) // 0
startIp = startIp.StepUp() // 1
return startIp.String()
}
func (network *SNetwork) GetIpEnd() string {
pref, _ := netutils.NewIPV4Prefix(network.IpCidrRange)
func (self *SNetwork) GetIpEnd() string {
pref, _ := netutils.NewIPV4Prefix(self.wire.vpc.IpCidrRange)
endIp := pref.Address.BroadcastAddr(pref.MaskLen) // 255
endIp = endIp.StepDown() // 254
return endIp.String()
}
func (network *SNetwork) GetIpMask() int8 {
pref, _ := netutils.NewIPV4Prefix(network.IpCidrRange)
func (self *SNetwork) GetIpMask() int8 {
pref, _ := netutils.NewIPV4Prefix(self.wire.vpc.IpCidrRange)
return pref.MaskLen
}
func (network *SNetwork) GetGateway() string {
return network.GatewayAddress
func (self *SNetwork) GetGateway() string {
return self.wire.vpc.GatewayAddress
}
func (network *SNetwork) GetServerType() string {
@@ -127,19 +102,3 @@ func (network *SNetwork) GetIsPublic() bool {
func (network *SNetwork) GetPublicScope() rbacutils.TRbacScope {
return rbacutils.ScopeDomain
}
func (region *SRegion) CreateNetwork(name string, vpc string, cidr string, desc string) (*SNetwork, error) {
body := map[string]interface{}{
"name": name,
"description": desc,
"network": vpc,
"ipCidrRange": cidr,
}
resource := fmt.Sprintf("regions/%s/subnetworks", region.Name)
network := &SNetwork{}
err := region.Insert(resource, jsonutils.Marshal(body), network)
if err != nil {
return nil, err
}
return network, nil
}
+4 -4
View File
@@ -44,19 +44,19 @@ type SOperation struct {
Kind string
}
func (region *SRegion) GetOperation(id string) (*SOperation, error) {
func (self *SGoogleClient) GetOperation(id string) (*SOperation, error) {
operation := &SOperation{}
err := region.Get(id, &operation)
err := self.GetBySelfId(id, &operation)
if err != nil {
return nil, err
}
return operation, nil
}
func (region *SRegion) WaitOperation(id string, resource, action string) (string, error) {
func (self *SGoogleClient) WaitOperation(id string, resource, action string) (string, error) {
targetLink := ""
err := cloudprovider.Wait(time.Second*5, time.Minute*5, func() (bool, error) {
operation, err := region.GetOperation(id)
operation, err := self.GetOperation(id)
if err != nil {
return false, err
}
@@ -297,3 +297,15 @@ func (self *SGoogleProvider) CreateICloudpolicy(opts *cloudprovider.SCloudpolicy
func (self *SGoogleProvider) GetSamlEntityId() string {
return cloudprovider.SAML_ENTITY_ID_GOOGLE
}
func (self *SGoogleProvider) GetICloudGlobalVpcs() ([]cloudprovider.ICloudGlobalVpc, error) {
return self.client.GetICloudGlobalVpcs()
}
func (self *SGoogleProvider) GetICloudGlobalVpcById(id string) (cloudprovider.ICloudGlobalVpc, error) {
return self.client.GetICloudGlobalVpcById(id)
}
func (self *SGoogleProvider) CreateICloudGlobalVpc(opts *cloudprovider.GlobalVpcCreateOptions) (cloudprovider.ICloudGlobalVpc, error) {
return self.client.CreateICloudGlobalVpc(opts)
}
+48 -41
View File
@@ -172,51 +172,39 @@ func (region *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error)
return nil, cloudprovider.ErrNotFound
}
func (region *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) {
if utils.IsInStringArray(region.Name, MultiRegions) || utils.IsInStringArray(region.Name, DualRegions) {
func (self *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) {
if utils.IsInStringArray(self.Name, MultiRegions) || utils.IsInStringArray(self.Name, DualRegions) {
return []cloudprovider.ICloudVpc{}, nil
}
globalnetworks, err := region.client.fetchGlobalNetwork()
vpcs, err := self.GetVpcs()
if err != nil {
return nil, errors.Wrap(err, "fetchGlobalNetwork")
return nil, errors.Wrapf(err, "GetVpcs")
}
ivpcs := []cloudprovider.ICloudVpc{}
for i := range globalnetworks {
vpc := SVpc{region: region, globalnetwork: &globalnetworks[i]}
ivpcs = append(ivpcs, &vpc)
ret := []cloudprovider.ICloudVpc{}
for i := range vpcs {
vpcs[i].region = self
ret = append(ret, &vpcs[i])
}
return ivpcs, nil
return ret, nil
}
func (region *SRegion) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) {
if utils.IsInStringArray(region.Name, MultiRegions) || utils.IsInStringArray(region.Name, DualRegions) {
return nil, cloudprovider.ErrNotFound
}
ivpcs, err := region.GetIVpcs()
func (self *SRegion) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) {
vpc, err := self.GetVpc(id)
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "GetVpc")
}
for i := range ivpcs {
if ivpcs[i].GetGlobalId() == id {
return ivpcs[i], nil
}
}
return nil, cloudprovider.ErrNotFound
return vpc, nil
}
func (region *SRegion) CreateIVpc(name string, desc string, cidr string) (cloudprovider.ICloudVpc, error) {
if utils.IsInStringArray(region.Name, MultiRegions) || utils.IsInStringArray(region.Name, DualRegions) {
func (self *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
if utils.IsInStringArray(self.Name, MultiRegions) || utils.IsInStringArray(self.Name, DualRegions) {
return nil, cloudprovider.ErrNotSupported
}
globalnetwork, err := region.CreateGlobalNetwork(name, desc)
gvpc, err := self.client.GetGlobalNetwork(opts.GlobalVpcExternalId)
if err != nil {
return nil, errors.Wrap(err, "region.CreateGlobalNetwork")
return nil, errors.Wrapf(err, "GetGlobalNetwork")
}
vpc := &SVpc{region: region, globalnetwork: globalnetwork}
return vpc, nil
return self.CreateVpc(opts.NAME, gvpc.SelfLink, opts.CIDR, opts.Desc)
}
func (region *SRegion) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
@@ -529,8 +517,23 @@ func (region *SRegion) List(resource string, params map[string]string, maxResult
return nil
}
func (region *SRegion) Get(id string, retval interface{}) error {
return region.client.ecsGet(id, retval)
func (region *SRegion) Get(resourceType, id string, retval interface{}) error {
return region.client.ecsGet(resourceType, id, retval)
}
func (self *SGoogleClient) GetBySelfId(id string, retval interface{}) error {
resp, err := jsonRequest(self.client, "GET", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, id, nil, nil, self.debug)
if err != nil {
return err
}
if retval != nil {
return resp.Unmarshal(retval)
}
return nil
}
func (self *SRegion) GetBySelfId(id string, retval interface{}) error {
return self.client.GetBySelfId(id, retval)
}
func (region *SRegion) StorageListAll(resource string, params map[string]string, retval interface{}) error {
@@ -573,7 +576,7 @@ func (region *SRegion) StorageDo(id string, action string, params map[string]str
return err
}
if strings.Index(opId, "/operations/") > 0 {
_, err = region.WaitOperation(opId, id, action)
_, err = region.client.WaitOperation(opId, id, action)
return err
}
return nil
@@ -585,7 +588,7 @@ func (region *SRegion) Do(id string, action string, params map[string]string, bo
return err
}
if strings.Index(opId, "/operations/") > 0 {
_, err = region.WaitOperation(opId, id, action)
_, err = region.client.WaitOperation(opId, id, action)
return err
}
return nil
@@ -597,7 +600,7 @@ func (region *SRegion) Patch(id string, action string, params map[string]string,
return err
}
if strings.Index(opId, "/operations/") > 0 {
_, err = region.WaitOperation(opId, id, action)
_, err = region.client.WaitOperation(opId, id, action)
return err
}
return nil
@@ -613,7 +616,7 @@ func (region *SRegion) Delete(id string) error {
if err != nil {
return errors.Wrap(err, "client.ecsDelete")
}
_, err = region.WaitOperation(operation.SelfLink, id, "delete")
_, err = region.client.WaitOperation(operation.SelfLink, id, "delete")
if err != nil {
return errors.Wrapf(err, "region.WaitOperation(%s)", operation.SelfLink)
}
@@ -658,17 +661,21 @@ func (region *SRegion) cloudbuildGet(id string, retval interface{}) error {
return region.client.cloudbuildGet(id, retval)
}
func (region *SRegion) Insert(resource string, body jsonutils.JSONObject, retval interface{}) error {
func (self *SGoogleClient) Insert(resource string, body jsonutils.JSONObject, retval interface{}) error {
operation := &SOperation{}
err := region.client.ecsInsert(resource, body, operation)
err := self.ecsInsert(resource, body, operation)
if err != nil {
return err
}
resourceId, err := region.WaitOperation(operation.SelfLink, resource, "insert")
resourceId, err := self.WaitOperation(operation.SelfLink, resource, "insert")
if err != nil {
return errors.Wrapf(err, "region.WaitOperation(%s)", operation.SelfLink)
}
return region.Get(resourceId, retval)
return self.GetBySelfId(resourceId, retval)
}
func (self *SRegion) Insert(resource string, body jsonutils.JSONObject, retval interface{}) error {
return self.client.Insert(resource, body, retval)
}
func (region *SRegion) fetchResourcePolicies() ([]SResourcePolicy, error) {
@@ -695,7 +702,7 @@ func (region *SRegion) GetISnapshotPolicies() ([]cloudprovider.ICloudSnapshotPol
ipolicies := []cloudprovider.ICloudSnapshotPolicy{}
for i := range policies {
policies[i].region = region
if strings.Contains(region.Name, policies[i].SnapshotSchedulePolicy.SnapshotProperties.StorageLocations[0]) {
if utils.IsInStringArray(region.Name, policies[i].SnapshotSchedulePolicy.SnapshotProperties.StorageLocations) {
ipolicies = append(ipolicies, &policies[i])
}
}
+1 -1
View File
@@ -49,5 +49,5 @@ func (region *SRegion) GetRegionDisks(storageType string, maxResults int, pageTo
func (region *SRegion) GetRegionDisk(id string) (*SRegionDisk, error) {
disk := &SRegionDisk{}
return disk, region.Get(id, disk)
return disk, region.GetBySelfId(id, disk)
}
+1 -1
View File
@@ -41,5 +41,5 @@ func (region *SRegion) GetRegionStorages(maxResults int, pageToken string) ([]SR
func (region *SRegion) GetRegionStorage(id string) (*SRegionStorage, error) {
storage := &SRegionStorage{region: region}
return storage, region.Get(id, storage)
return storage, region.GetBySelfId(id, storage)
}
+7
View File
@@ -22,13 +22,20 @@ import (
type SResourceBase struct {
Name string
SelfLink string
Id string
}
func (r *SResourceBase) GetId() string {
if len(r.Id) > 0 {
return r.Id
}
return r.SelfLink
}
func (r *SResourceBase) GetGlobalId() string {
if len(r.Id) > 0 {
return r.Id
}
return strings.TrimPrefix(r.SelfLink, fmt.Sprintf("%s/%s/", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION))
}
+2 -4
View File
@@ -77,8 +77,6 @@ type SResourcePolicy struct {
SResourceBase
multicloud.GoogleTags
Id string
CreationTimestamp time.Time
Region string
Status string
@@ -95,7 +93,7 @@ func (region *SRegion) GetResourcePolicies(maxResults int, pageToken string) ([]
func (region *SRegion) GetResourcePolicy(id string) (*SResourcePolicy, error) {
policy := &SResourcePolicy{region: region}
return policy, region.Get(id, policy)
return policy, region.Get("resourcePolicies", id, policy)
}
func (policy *SResourcePolicy) GetStatus() string {
@@ -109,7 +107,7 @@ func (policy *SResourcePolicy) GetStatus() string {
}
func (policy *SResourcePolicy) Refresh() error {
_policy, err := policy.region.GetResourcePolicy(policy.SelfLink)
_policy, err := policy.region.GetResourcePolicy(policy.Id)
if err != nil {
return err
}
+22 -25
View File
@@ -61,25 +61,25 @@ type SFirewall struct {
type SSecurityGroup struct {
multicloud.SSecurityGroup
multicloud.GoogleTags
vpc *SVpc
gvpc *SGlobalNetwork
ServiceAccount string
Tag string
}
func (region *SRegion) GetFirewalls(network string, maxResults int, pageToken string) ([]SFirewall, error) {
func (self *SGoogleClient) GetFirewalls(network string, maxResults int, pageToken string) ([]SFirewall, error) {
firewalls := []SFirewall{}
params := map[string]string{"filter": "disabled = false"}
resource := "global/firewalls"
if len(network) > 0 {
params["filter"] = fmt.Sprintf(`(disabled = false) AND (network="%s")`, network)
}
return firewalls, region.List(resource, params, maxResults, pageToken, &firewalls)
return firewalls, self._ecsListAll("GET", resource, params, &firewalls)
}
func (region *SRegion) GetFirewall(id string) (*SFirewall, error) {
func (self *SGoogleClient) GetFirewall(id string) (*SFirewall, error) {
firewall := &SFirewall{}
return firewall, region.Get(id, firewall)
return firewall, self.ecsGet("global/firewalls", id, firewall)
}
func (firewall *SFirewall) _toRules(action secrules.TSecurityRuleAction) ([]cloudprovider.SecurityRule, error) {
@@ -164,7 +164,7 @@ func (firewall *SFirewall) toRules() ([]cloudprovider.SecurityRule, error) {
}
func (secgroup *SSecurityGroup) GetId() string {
return secgroup.vpc.globalnetwork.GetGlobalId()
return secgroup.gvpc.GetGlobalId()
}
func (secgroup *SSecurityGroup) GetGlobalId() string {
@@ -188,7 +188,7 @@ func (secgroup *SSecurityGroup) GetName() string {
if len(secgroup.ServiceAccount) > 0 {
return secgroup.ServiceAccount
}
return secgroup.vpc.globalnetwork.Name
return secgroup.gvpc.Name
}
func (secgroup *SSecurityGroup) GetStatus() string {
@@ -209,7 +209,7 @@ func (secgroup *SSecurityGroup) Delete() error {
return errors.Wrap(err, "GetRules")
}
for _, rule := range rules {
err = secgroup.vpc.region.DeleteSecgroupRule(rule)
err = secgroup.gvpc.client.DeleteSecgroupRule(rule)
if err != nil {
return errors.Wrapf(err, "DeleteSecgroupRule(%s)", rule.Description)
}
@@ -222,11 +222,11 @@ func (secgroup *SSecurityGroup) GetProjectId() string {
}
func (secgroup *SSecurityGroup) GetVpcId() string {
return secgroup.vpc.GetGlobalId()
return secgroup.gvpc.GetGlobalId()
}
func (self *SSecurityGroup) GetRules() ([]cloudprovider.SecurityRule, error) {
_firewalls, err := self.vpc.region.GetFirewalls(self.vpc.globalnetwork.SelfLink, 0, "")
_firewalls, err := self.gvpc.client.GetFirewalls(self.gvpc.SelfLink, 0, "")
if err != nil {
return nil, err
}
@@ -253,8 +253,8 @@ func (self *SSecurityGroup) GetRules() ([]cloudprovider.SecurityRule, error) {
return rules, nil
}
func (region *SRegion) DeleteSecgroupRule(rule cloudprovider.SecurityRule) error {
firwall, err := region.GetFirewall(rule.ExternalId)
func (self *SGoogleClient) DeleteSecgroupRule(rule cloudprovider.SecurityRule) error {
firwall, err := self.GetFirewall(rule.ExternalId)
if err != nil {
return errors.Wrap(err, "region.GetFirewall")
}
@@ -266,19 +266,19 @@ func (region *SRegion) DeleteSecgroupRule(rule cloudprovider.SecurityRule) error
for _, _rule := range currentRule {
if _rule.String() != rule.String() {
for _, tag := range firwall.TargetTags {
err = region.CreateSecurityGroupRule(_rule, firwall.Network, tag, "")
err = self.CreateSecurityGroupRule(_rule, firwall.Network, tag, "")
if err != nil {
return errors.Wrap(err, "region.CreateSecurityGroupRule")
}
}
for _, serviceAccount := range firwall.TargetServiceAccounts {
err = region.CreateSecurityGroupRule(_rule, firwall.Network, "", serviceAccount)
err = self.CreateSecurityGroupRule(_rule, firwall.Network, "", serviceAccount)
if err != nil {
return errors.Wrap(err, "region.CreateSecurityGroupRule")
}
}
if len(firwall.TargetTags)+len(firwall.TargetServiceAccounts) == 0 {
err = region.CreateSecurityGroupRule(_rule, firwall.Network, "", "")
err = self.CreateSecurityGroupRule(_rule, firwall.Network, "", "")
if err != nil {
return errors.Wrap(err, "region.CreateSecurityGroupRule")
}
@@ -286,18 +286,18 @@ func (region *SRegion) DeleteSecgroupRule(rule cloudprovider.SecurityRule) error
}
}
}
return region.Delete(firwall.SelfLink)
return self.ecsDelete(firwall.SelfLink, nil)
}
func (secgroup *SSecurityGroup) SyncRules(common, inAdds, outAdds, inDels, outDels []cloudprovider.SecurityRule) error {
for _, r := range append(inDels, outDels...) {
err := secgroup.vpc.region.DeleteSecgroupRule(r)
err := secgroup.gvpc.client.DeleteSecgroupRule(r)
if err != nil {
return errors.Wrap(err, "DeleteSecgroupRule")
}
}
for _, r := range append(inAdds, outAdds...) {
err := secgroup.vpc.region.CreateSecurityGroupRule(r, secgroup.vpc.globalnetwork.SelfLink, secgroup.Tag, secgroup.ServiceAccount)
err := secgroup.gvpc.client.CreateSecurityGroupRule(r, secgroup.gvpc.SelfLink, secgroup.Tag, secgroup.ServiceAccount)
if err != nil {
return errors.Wrapf(err, "CreateSecurityGroupRule(%s)", r.String())
}
@@ -343,7 +343,7 @@ func (region *SRegion) GetISecurityGroupByName(opts *cloudprovider.SecurityGroup
return nil, cloudprovider.ErrNotFound
}
func (region *SRegion) CreateSecurityGroupRule(rule cloudprovider.SecurityRule, vpcId string, tag string, serviceAccount string) error {
func (self *SGoogleClient) CreateSecurityGroupRule(rule cloudprovider.SecurityRule, vpcId string, tag string, serviceAccount string) error {
name := fmt.Sprintf("%s-%d", rule.String(), rule.Priority)
if len(tag) > 0 {
name = fmt.Sprintf("for-tag-%s-%s", tag, name)
@@ -405,7 +405,7 @@ func (region *SRegion) CreateSecurityGroupRule(rule cloudprovider.SecurityRule,
}
firwall := &SFirewall{}
err := region.Insert("global/firewalls", jsonutils.Marshal(body), firwall)
err := self.Insert("global/firewalls", jsonutils.Marshal(body), firwall)
if err != nil {
if strings.Index(err.Error(), "already exists") >= 0 {
return nil
@@ -416,14 +416,11 @@ func (region *SRegion) CreateSecurityGroupRule(rule cloudprovider.SecurityRule,
}
func (region *SRegion) CreateISecurityGroup(conf *cloudprovider.SecurityGroupCreateInput) (cloudprovider.ICloudSecurityGroup, error) {
conf.VpcId = fmt.Sprintf("%s/%s", region.GetGlobalId(), conf.VpcId)
ivpc, err := region.GetIVpcById(conf.VpcId)
gvpc, err := region.client.GetGlobalNetwork(conf.VpcId)
if err != nil {
return nil, errors.Wrapf(err, "region.GetIVpcById(%s)", conf.VpcId)
}
vpc := ivpc.(*SVpc)
secgroup := &SSecurityGroup{vpc: vpc, Tag: strings.ToLower(conf.Name)}
secgroup := &SSecurityGroup{gvpc: gvpc, Tag: strings.ToLower(conf.Name)}
return secgroup, nil
}
+2 -2
View File
@@ -26,7 +26,7 @@ func init() {
PageToken string
}
shellutils.R(&FirewallListOptions{}, "firewall-list", "List firewalls", func(cli *google.SRegion, args *FirewallListOptions) error {
firewalls, err := cli.GetFirewalls(args.Network, args.MaxResults, args.PageToken)
firewalls, err := cli.GetClient().GetFirewalls(args.Network, args.MaxResults, args.PageToken)
if err != nil {
return err
}
@@ -38,7 +38,7 @@ func init() {
ID string
}
shellutils.R(&FirewallShowOptions{}, "firewall-show", "Show firewall", func(cli *google.SRegion, args *FirewallShowOptions) error {
firewall, err := cli.GetFirewall(args.ID)
firewall, err := cli.GetClient().GetFirewall(args.ID)
if err != nil {
return err
}
+1 -1
View File
@@ -51,7 +51,7 @@ func init() {
}
shellutils.R(&GlobalNetworkCreateOptions{}, "global-network-create", "Create globalnetwork", func(cli *google.SRegion, args *GlobalNetworkCreateOptions) error {
globalnetwork, err := cli.CreateGlobalNetwork(args.NAME, args.Desc)
globalnetwork, err := cli.GetClient().CreateGlobalNetwork(args.NAME, args.Desc)
if err != nil {
return err
}
+8 -11
View File
@@ -21,12 +21,9 @@ import (
func init() {
type NetworkListOptions struct {
Network string
MaxResults int
PageToken string
}
shellutils.R(&NetworkListOptions{}, "network-list", "List networks", func(cli *google.SRegion, args *NetworkListOptions) error {
networks, err := cli.GetNetworks(args.Network, args.MaxResults, args.PageToken)
shellutils.R(&NetworkListOptions{}, "vpc-list", "List networks", func(cli *google.SRegion, args *NetworkListOptions) error {
networks, err := cli.GetVpcs()
if err != nil {
return err
}
@@ -38,16 +35,16 @@ func init() {
ID string
}
shellutils.R(&NetworkIdOptions{}, "network-show", "Show network", func(cli *google.SRegion, args *NetworkIdOptions) error {
network, err := cli.GetNetwork(args.ID)
shellutils.R(&NetworkIdOptions{}, "vpc-show", "Show network", func(cli *google.SRegion, args *NetworkIdOptions) error {
vpc, err := cli.GetVpc(args.ID)
if err != nil {
return err
}
printObject(network)
printObject(vpc)
return nil
})
shellutils.R(&NetworkIdOptions{}, "network-delete", "Delete network", func(cli *google.SRegion, args *NetworkIdOptions) error {
shellutils.R(&NetworkIdOptions{}, "vpc-delete", "Delete network", func(cli *google.SRegion, args *NetworkIdOptions) error {
return cli.Delete(args.ID)
})
@@ -58,8 +55,8 @@ func init() {
Desc string
}
shellutils.R(&NetworkCreateOptions{}, "network-create", "Create network", func(cli *google.SRegion, args *NetworkCreateOptions) error {
network, err := cli.CreateNetwork(args.NAME, args.VPC, args.CIDR, args.Desc)
shellutils.R(&NetworkCreateOptions{}, "vpc-create", "Create network", func(cli *google.SRegion, args *NetworkCreateOptions) error {
network, err := cli.CreateVpc(args.NAME, args.VPC, args.CIDR, args.Desc)
if err != nil {
return err
}
+2 -3
View File
@@ -29,7 +29,6 @@ type SSnapshot struct {
SResourceBase
multicloud.GoogleTags
Id string
CreationTimestamp time.Time
Status string
SourceDisk string
@@ -56,7 +55,7 @@ func (region *SRegion) GetSnapshots(disk string, maxResults int, pageToken strin
func (region *SRegion) GetSnapshot(id string) (*SSnapshot, error) {
snapshot := &SSnapshot{region: region}
return snapshot, region.Get(id, snapshot)
return snapshot, region.Get("global/snapshots", id, snapshot)
}
//CREATING, DELETING, FAILED, READY, or UPLOADING
@@ -80,7 +79,7 @@ func (snapshot *SSnapshot) IsEmulated() bool {
}
func (snapshot *SSnapshot) Refresh() error {
_snapshot, err := snapshot.region.GetSnapshot(snapshot.SelfLink)
_snapshot, err := snapshot.region.GetSnapshot(snapshot.Id)
if err != nil {
return err
}
+1 -1
View File
@@ -50,7 +50,7 @@ func (region *SRegion) GetStorages(zone string, maxResults int, pageToken string
func (region *SRegion) GetStorage(id string) (*SStorage, error) {
storage := &SStorage{}
return storage, region.Get(id, storage)
return storage, region.GetBySelfId(id, storage)
}
func (storage *SStorage) GetName() string {
+64 -17
View File
@@ -16,7 +16,9 @@ package google
import (
"fmt"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/utils"
@@ -28,25 +30,43 @@ import (
type SVpc struct {
multicloud.SVpc
multicloud.GoogleTags
globalnetwork *SGlobalNetwork
SResourceBase
region *SRegion
CreationTimestamp time.Time
Network string
IpCidrRange string
Region string
GatewayAddress string
Status string
AvailableCpuPlatforms []string
PrivateIpGoogleAccess bool
Fingerprint string
Purpose string
Kind string
}
func (vpc *SVpc) GetName() string {
return fmt.Sprintf("%s(%s)", vpc.globalnetwork.Name, vpc.region.Name)
func (self *SVpc) GetGlobalVpcId() string {
gvpc := &SGlobalNetwork{}
err := self.region.GetBySelfId(self.Network, gvpc)
if err != nil {
return ""
}
return gvpc.Id
}
func (vpc *SVpc) GetId() string {
return vpc.globalnetwork.GetGlobalId()
func (self *SVpc) Refresh() error {
vpc, err := self.region.GetVpc(self.Id)
if err != nil {
return errors.Wrapf(err, "GetVpc")
}
return jsonutils.Update(self, vpc)
}
func (vpc *SVpc) GetGlobalId() string {
return fmt.Sprintf("%s/%s", vpc.region.GetGlobalId(), vpc.GetId())
}
func (vpc *SVpc) Refresh() error {
return nil
func (self *SRegion) GetVpc(id string) (*SVpc, error) {
vpc := &SVpc{region: self}
return vpc, self.Get("subnetworks", id, vpc)
}
func (vpc *SVpc) GetStatus() string {
@@ -54,11 +74,11 @@ func (vpc *SVpc) GetStatus() string {
}
func (vpc *SVpc) Delete() error {
return vpc.region.Delete(vpc.globalnetwork.SelfLink)
return vpc.region.Delete(vpc.SelfLink)
}
func (vpc *SVpc) GetCidrBlock() string {
return ""
return vpc.IpCidrRange
}
func (vpc *SVpc) IsEmulated() bool {
@@ -82,23 +102,28 @@ func (self *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudR
}
func (vpc *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
firewalls, err := vpc.region.GetFirewalls(vpc.globalnetwork.SelfLink, 0, "")
firewalls, err := vpc.region.client.GetFirewalls(vpc.Network, 0, "")
if err != nil {
return nil, errors.Wrap(err, "GetFirewalls")
}
gvpc := &SGlobalNetwork{client: vpc.region.client}
err = vpc.region.GetBySelfId(vpc.Network, gvpc)
if err != nil {
return nil, errors.Wrapf(err, "GetGlobalNetwork")
}
isecgroups := []cloudprovider.ICloudSecurityGroup{}
allInstance := false
tags := []string{}
for _, firewall := range firewalls {
if len(firewall.TargetServiceAccounts) > 0 {
secgroup := &SSecurityGroup{vpc: vpc, ServiceAccount: firewall.TargetServiceAccounts[0]}
secgroup := &SSecurityGroup{gvpc: gvpc, ServiceAccount: firewall.TargetServiceAccounts[0]}
isecgroups = append(isecgroups, secgroup)
} else if len(firewall.TargetTags) > 0 && !utils.IsInStringArray(firewall.TargetTags[0], tags) {
secgroup := &SSecurityGroup{vpc: vpc, Tag: firewall.TargetTags[0]}
secgroup := &SSecurityGroup{gvpc: gvpc, Tag: firewall.TargetTags[0]}
tags = append(tags, firewall.TargetTags[0])
isecgroups = append(isecgroups, secgroup)
} else if !allInstance {
secgroup := &SSecurityGroup{vpc: vpc}
secgroup := &SSecurityGroup{gvpc: gvpc}
isecgroups = append(isecgroups, secgroup)
allInstance = true
}
@@ -121,3 +146,25 @@ func (vpc *SVpc) GetIWireById(id string) (cloudprovider.ICloudWire, error) {
}
return &SWire{vpc: vpc}, nil
}
func (self *SRegion) CreateVpc(name string, gvpcId string, cidr string, desc string) (*SVpc, error) {
body := map[string]interface{}{
"name": name,
"description": desc,
"network": gvpcId,
"ipCidrRange": cidr,
}
resource := fmt.Sprintf("regions/%s/subnetworks", self.Name)
vpc := &SVpc{region: self}
err := self.Insert(resource, jsonutils.Marshal(body), vpc)
if err != nil {
return nil, err
}
return vpc, nil
}
func (self *SRegion) GetVpcs() ([]SVpc, error) {
vpcs := []SVpc{}
resource := fmt.Sprintf("regions/%s/subnetworks", self.Name)
return vpcs, self.List(resource, nil, 0, "", &vpcs)
}
+14 -24
View File
@@ -17,6 +17,8 @@ package google
import (
"fmt"
"yunion.io/x/pkg/errors"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/onecloud/pkg/multicloud"
@@ -41,12 +43,7 @@ func (wire *SWire) GetName() string {
}
func (wire *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
network, err := wire.vpc.region.CreateNetwork(opts.Name, wire.vpc.globalnetwork.SelfLink, opts.Cidr, opts.Desc)
if err != nil {
return nil, err
}
network.wire = wire
return network, nil
return nil, cloudprovider.ErrNotSupported
}
func (wire *SWire) GetIVpc() cloudprovider.ICloudVpc {
@@ -57,29 +54,22 @@ func (wire *SWire) GetIZone() cloudprovider.ICloudZone {
return nil
}
func (wire *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
networks, err := wire.vpc.region.GetNetworks(wire.vpc.globalnetwork.SelfLink, 0, "")
if err != nil {
return nil, err
}
inetworks := []cloudprovider.ICloudNetwork{}
for i := range networks {
networks[i].wire = wire
inetworks = append(inetworks, &networks[i])
}
return inetworks, nil
func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
network := SNetwork{wire: self}
return []cloudprovider.ICloudNetwork{&network}, nil
}
func (wire *SWire) GetINetworkById(id string) (cloudprovider.ICloudNetwork, error) {
network, err := wire.vpc.region.GetNetwork(id)
func (self *SWire) GetINetworkById(id string) (cloudprovider.ICloudNetwork, error) {
networks, err := self.GetINetworks()
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "GetINetwork")
}
if network.Network != wire.vpc.globalnetwork.SelfLink {
return nil, cloudprovider.ErrNotFound
for i := range networks {
if networks[i].GetGlobalId() == id {
return networks[i], nil
}
}
network.wire = wire
return network, nil
return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
}
func (wire *SWire) GetBandwidth() int {
+1 -1
View File
@@ -39,7 +39,7 @@ type SZone struct {
func (region *SRegion) GetZone(id string) (*SZone, error) {
zone := &SZone{}
return zone, region.Get(id, zone)
return zone, region.GetBySelfId(id, zone)
}
func (region *SRegion) GetZones(regionId string, maxResults int, pageToken string) ([]SZone, error) {
+1
View File
@@ -448,6 +448,7 @@ func (self *SHuaweiClient) GetCapabilities() []string {
cloudprovider.CLOUD_CAPABILITY_NETWORK,
cloudprovider.CLOUD_CAPABILITY_LOADBALANCER,
// cloudprovider.CLOUD_CAPABILITY_OBJECTSTORE,
cloudprovider.CLOUD_CAPABILITY_QUOTA + cloudprovider.READ_ONLY_SUFFIX,
cloudprovider.CLOUD_CAPABILITY_RDS,
cloudprovider.CLOUD_CAPABILITY_CACHE,
cloudprovider.CLOUD_CAPABILITY_EVENT,
+2 -2
View File
@@ -552,8 +552,8 @@ func (self *SRegion) CreateISecurityGroup(conf *cloudprovider.SecurityGroupCreat
}
// https://support.huaweicloud.com/api-vpc/zh-cn_topic_0020090608.html
func (self *SRegion) CreateIVpc(name string, desc string, cidr string) (cloudprovider.ICloudVpc, error) {
return self.CreateVpc(name, cidr, desc)
func (self *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
return self.CreateVpc(opts.NAME, opts.CIDR, opts.Desc)
}
func (self *SRegion) CreateVpc(name, cidr, desc string) (*SVpc, error) {
+1
View File
@@ -510,6 +510,7 @@ func (self *SHuaweiClient) GetCapabilities() []string {
cloudprovider.CLOUD_CAPABILITY_SAML_AUTH,
cloudprovider.CLOUD_CAPABILITY_NAT,
cloudprovider.CLOUD_CAPABILITY_NAS,
cloudprovider.CLOUD_CAPABILITY_QUOTA + cloudprovider.READ_ONLY_SUFFIX,
}
// huawei objectstore is shared across projects(subscriptions)
// to avoid multiple project access the same bucket
+2 -2
View File
@@ -537,8 +537,8 @@ func (self *SRegion) CreateISecurityGroup(conf *cloudprovider.SecurityGroupCreat
}
// https://support.huaweicloud.com/api-vpc/zh-cn_topic_0020090608.html
func (self *SRegion) CreateIVpc(name string, desc string, cidr string) (cloudprovider.ICloudVpc, error) {
return self.CreateVpc(name, cidr, desc)
func (self *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
return self.CreateVpc(opts.NAME, opts.CIDR, opts.Desc)
}
func (self *SRegion) CreateVpc(name, cidr, desc string) (*SVpc, error) {
+1 -1
View File
@@ -254,7 +254,7 @@ func (cli *SObjectStoreClient) SyncSecurityGroup(secgroupId string, vpcId string
return "", cloudprovider.ErrNotSupported
}
func (cli *SObjectStoreClient) CreateIVpc(name string, desc string, cidr string) (cloudprovider.ICloudVpc, error) {
func (cli *SObjectStoreClient) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
return nil, cloudprovider.ErrNotSupported
}
+1
View File
@@ -549,6 +549,7 @@ func (self *SOpenStackClient) GetCapabilities() []string {
cloudprovider.CLOUD_CAPABILITY_COMPUTE,
cloudprovider.CLOUD_CAPABILITY_NETWORK,
cloudprovider.CLOUD_CAPABILITY_LOADBALANCER,
cloudprovider.CLOUD_CAPABILITY_QUOTA + cloudprovider.READ_ONLY_SUFFIX,
// cloudprovider.CLOUD_CAPABILITY_OBJECTSTORE,
// cloudprovider.CLOUD_CAPABILITY_RDS,
// cloudprovider.CLOUD_CAPABILITY_CACHE,
+2 -2
View File
@@ -105,8 +105,8 @@ func (region *SRegion) GetMaxVersion(service string) (string, error) {
return region.client.GetMaxVersion(region.Name, service)
}
func (region *SRegion) CreateIVpc(name string, desc string, cidr string) (cloudprovider.ICloudVpc, error) {
vpc, err := region.CreateVpc(name, desc)
func (region *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
vpc, err := region.CreateVpc(opts.NAME, opts.Desc)
if err != nil {
return nil, errors.Wrap(err, "CreateVp")
}
+1
View File
@@ -1111,6 +1111,7 @@ func (self *SQcloudClient) GetCapabilities() []string {
cloudprovider.CLOUD_CAPABILITY_PUBLIC_IP,
cloudprovider.CLOUD_CAPABILITY_INTERVPCNETWORK,
cloudprovider.CLOUD_CAPABILITY_SAML_AUTH,
cloudprovider.CLOUD_CAPABILITY_QUOTA + cloudprovider.READ_ONLY_SUFFIX,
cloudprovider.CLOUD_CAPABILITY_MONGO_DB + cloudprovider.READ_ONLY_SUFFIX,
cloudprovider.CLOUD_CAPABILITY_ES + cloudprovider.READ_ONLY_SUFFIX,
cloudprovider.CLOUD_CAPABILITY_KAFKA + cloudprovider.READ_ONLY_SUFFIX,
+5 -5
View File
@@ -246,13 +246,13 @@ func (self *SRegion) GetCloudEnv() string {
return ""
}
func (self *SRegion) CreateIVpc(name string, desc string, cidr string) (cloudprovider.ICloudVpc, error) {
func (self *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
params := make(map[string]string)
if len(cidr) > 0 {
params["CidrBlock"] = cidr
if len(opts.CIDR) > 0 {
params["CidrBlock"] = opts.CIDR
}
if len(name) > 0 {
params["VpcName"] = name
if len(opts.NAME) > 0 {
params["VpcName"] = opts.NAME
}
body, err := self.vpcRequest("CreateVpc", params)
if err != nil {
+5 -1
View File
@@ -39,7 +39,11 @@ func init() {
CIDR string `help:"Cidr for vpc" choices:"10.0.0.0/16|172.16.0.0/12|192.168.0.0/16"`
}
shellutils.R(&VpcCreateOptions{}, "vpc-create", "Create vpc", func(cli *qcloud.SRegion, args *VpcCreateOptions) error {
vpc, err := cli.CreateIVpc(args.NAME, "", args.CIDR)
opts := &cloudprovider.VpcCreateOptions{
NAME: args.NAME,
CIDR: args.CIDR,
}
vpc, err := cli.CreateIVpc(opts)
if err != nil {
return err
}
+1 -1
View File
@@ -261,7 +261,7 @@ func (self *SRegionZoneBase) GetIZoneById(id string) (cloudprovider.ICloudZone,
type SRegionVpcBase struct {
}
func (self *SRegionVpcBase) CreateIVpc(name string, desc string, cidr string) (cloudprovider.ICloudVpc, error) {
func (self *SRegionVpcBase) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
return nil, errors.Wrapf(cloudprovider.ErrNotImplemented, "CreateIVpc")
}
+4 -4
View File
@@ -234,11 +234,11 @@ func (self *SRegion) CreateISecurityGroup(conf *cloudprovider.SecurityGroupCreat
// 绑定防火墙组的资源类型,默认为全部资源类型。枚举值为:"unatgw"NAT网关; "uhost",云主机; "upm",物理云主机; "hadoophost"hadoop节点; "fortresshost",堡垒机; "udhost",私有专区主机;"udockhost",容器;"dbaudit",数据库审计.
// todo: 是否需要过滤出仅绑定云主机的安全组?
func (self *SRegion) CreateIVpc(name string, desc string, cidr string) (cloudprovider.ICloudVpc, error) {
func (self *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
params := NewUcloudParams()
params.Set("Name", name)
params.Set("Remark", desc)
for i, cidr := range strings.Split(cidr, ",") {
params.Set("Name", opts.NAME)
params.Set("Remark", opts.Desc)
for i, cidr := range strings.Split(opts.CIDR, ",") {
params.Set(fmt.Sprintf("Network.%d", i), cidr)
}
+4
View File
@@ -84,3 +84,7 @@ func (self *SVpc) CreateINatGateway(opts *cloudprovider.NatGatewayCreateOptions)
func (self *SVpc) CreateIWire(opts *cloudprovider.SWireCreateOptions) (cloudprovider.ICloudWire, error) {
return nil, errors.Wrapf(cloudprovider.ErrNotImplemented, "CreateIWire")
}
func (self *SVpc) GetGlobalVpcId() string {
return ""
}
+1 -1
View File
@@ -240,7 +240,7 @@ func (region *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) {
return region.ivpcs, nil
}
func (region *SRegion) CreateIVpc(name string, desc string, cidr string) (cloudprovider.ICloudVpc, error) {
func (region *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
return nil, cloudprovider.ErrNotSupported
}
+1
View File
@@ -507,6 +507,7 @@ func (self *SZStackClient) GetCapabilities() []string {
// cloudprovider.CLOUD_CAPABILITY_PROJECT,
cloudprovider.CLOUD_CAPABILITY_COMPUTE,
cloudprovider.CLOUD_CAPABILITY_NETWORK,
cloudprovider.CLOUD_CAPABILITY_QUOTA + cloudprovider.READ_ONLY_SUFFIX,
// cloudprovider.CLOUD_CAPABILITY_LOADBALANCER,
// cloudprovider.CLOUD_CAPABILITY_OBJECTSTORE,
// cloudprovider.CLOUD_CAPABILITY_RDS,