mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-19 10:46:58 +08:00
Merge pull request #7031 from ioito/hotfix/qx-azure-resource-group-sync
fix: 避免azure同步资源组异常
This commit is contained in:
@@ -65,6 +65,8 @@ type SAzureClient struct {
|
||||
iregions []cloudprovider.ICloudRegion
|
||||
iBuckets []cloudprovider.ICloudBucket
|
||||
|
||||
subscriptions []SSubscription
|
||||
|
||||
debug bool
|
||||
}
|
||||
|
||||
@@ -415,6 +417,11 @@ func (self *SAzureClient) List(golbalResource string, retVal interface{}) error
|
||||
if len(self.subscriptionId) > 0 && len(golbalResource) > 0 {
|
||||
url += fmt.Sprintf("/%s", golbalResource)
|
||||
}
|
||||
if strings.ToLower(golbalResource) == "resourcegroups" && len(self.subscriptionId) == 0 { // 资源组必须要有订阅id
|
||||
if len(self.subscriptions) > 0 {
|
||||
url += fmt.Sprintf("/%s/%s", self.subscriptions[0].SubscriptionId, golbalResource)
|
||||
}
|
||||
}
|
||||
body, err := jsonRequest(cli, "GET", self.domain, url, self.subscriptionId, "", DefaultResource)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -931,8 +938,7 @@ func (self *SAzureClient) fetchRegions() error {
|
||||
self.iregions[i] = ®ions[i]
|
||||
}
|
||||
}
|
||||
_, err := self.ListSubscriptions()
|
||||
return err
|
||||
return self.fetchSubscriptions()
|
||||
}
|
||||
|
||||
func (self *SAzureClient) invalidateIBuckets() {
|
||||
@@ -979,31 +985,21 @@ func (self *SAzureClient) GetRegions() []SRegion {
|
||||
return regions
|
||||
}
|
||||
|
||||
func (self *SAzureClient) GetSubAccounts() (subAccounts []cloudprovider.SSubAccount, err error) {
|
||||
body, err := self.ListSubscriptions()
|
||||
func (self *SAzureClient) fetchSubscriptions() error {
|
||||
var err error
|
||||
self.subscriptions, err = self.GetSubscriptions()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return errors.Wrap(err, "GetSubscriptions")
|
||||
}
|
||||
subscriptions, err := body.GetArray("value")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
subAccounts = make([]cloudprovider.SSubAccount, len(subscriptions))
|
||||
for i, subscription := range subscriptions {
|
||||
subscriptionId, err := subscription.GetString("subscriptionId")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
subAccounts[i].Account = fmt.Sprintf("%s/%s", self.tenantId, subscriptionId)
|
||||
subAccounts[i].State, err = subscription.GetString("state")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
subAccounts[i].Name, err = subscription.GetString("displayName")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SAzureClient) GetSubAccounts() (subAccounts []cloudprovider.SSubAccount, err error) {
|
||||
subAccounts = make([]cloudprovider.SSubAccount, len(self.subscriptions))
|
||||
for i, subscription := range self.subscriptions {
|
||||
subAccounts[i].Account = fmt.Sprintf("%s/%s", self.tenantId, subscription.SubscriptionId)
|
||||
subAccounts[i].State = subscription.State
|
||||
subAccounts[i].Name = subscription.DisplayName
|
||||
subAccounts[i].HealthStatus = api.CLOUD_PROVIDER_HEALTH_NORMAL
|
||||
}
|
||||
return subAccounts, nil
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright 2019 Yunion
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package shell
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/multicloud/azure"
|
||||
"yunion.io/x/onecloud/pkg/util/shellutils"
|
||||
)
|
||||
|
||||
func init() {
|
||||
type SubscriptionListOptions struct {
|
||||
}
|
||||
shellutils.R(&SubscriptionListOptions{}, "subscription-list", "List subscriptions", func(cli *azure.SRegion, args *SubscriptionListOptions) error {
|
||||
subscriptions, err := cli.GetClient().GetSubscriptions()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printList(subscriptions, 0, 0, 0, nil)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package azure
|
||||
|
||||
import "yunion.io/x/pkg/errors"
|
||||
|
||||
type SSubscription struct {
|
||||
SubscriptionId string `json:"subscriptionId"`
|
||||
State string
|
||||
DisplayName string `json:"displayName"`
|
||||
}
|
||||
|
||||
func (self *SAzureClient) GetSubscriptions() ([]SSubscription, error) {
|
||||
resp, err := self.ListSubscriptions()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
subscriptions := []SSubscription{}
|
||||
err = resp.Unmarshal(&subscriptions, "value")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "resp.Unmarshal")
|
||||
}
|
||||
return subscriptions, nil
|
||||
}
|
||||
Reference in New Issue
Block a user