mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-07-01 07:14:30 +08:00
154 lines
4.0 KiB
Go
154 lines
4.0 KiB
Go
// 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 (
|
|
"context"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"golang.org/x/net/http/httpproxy"
|
|
|
|
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
|
"yunion.io/x/jsonutils"
|
|
"yunion.io/x/log"
|
|
"yunion.io/x/pkg/errors"
|
|
"yunion.io/x/pkg/gotypes"
|
|
"yunion.io/x/pkg/util/httputils"
|
|
"yunion.io/x/pkg/utils"
|
|
|
|
proxyapi "yunion.io/x/onecloud/pkg/apis/cloudcommon/proxy"
|
|
"yunion.io/x/onecloud/pkg/mcclient"
|
|
"yunion.io/x/onecloud/pkg/mcclient/modulebase"
|
|
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
|
)
|
|
|
|
type SCloudprovider struct {
|
|
modulebase.ResourceManager
|
|
}
|
|
|
|
var (
|
|
Cloudproviders SCloudprovider
|
|
)
|
|
|
|
func init() {
|
|
Cloudproviders = SCloudprovider{
|
|
modules.NewComputeManager("cloudprovider", "cloudproviders",
|
|
[]string{"ID", "Name", "Enabled", "Status", "Access_url", "Account",
|
|
"Sync_Status", "Last_sync", "Last_sync_end_at",
|
|
"health_status",
|
|
"Provider", "guest_count", "host_count", "vpc_count",
|
|
"storage_count", "storage_cache_count", "eip_count",
|
|
"tenant_id", "tenant"},
|
|
[]string{}),
|
|
}
|
|
|
|
modules.RegisterCompute(&Cloudproviders)
|
|
}
|
|
|
|
type SCloudDelegate struct {
|
|
Id string
|
|
Name string
|
|
Enabled bool
|
|
Status string
|
|
SyncStatus string
|
|
CloudaccountId string
|
|
|
|
AccessUrl string
|
|
Account string
|
|
Secret string
|
|
|
|
Provider string
|
|
Brand string
|
|
|
|
ReadOnly bool
|
|
|
|
Options struct {
|
|
cloudprovider.SHCSOEndpoints
|
|
Account string
|
|
Password string
|
|
}
|
|
ProxySetting proxyapi.SProxySetting
|
|
}
|
|
|
|
func (account *SCloudDelegate) getPassword() (string, error) {
|
|
return utils.DescryptAESBase64(account.Id, account.Secret)
|
|
}
|
|
|
|
func (account *SCloudDelegate) getAccessUrl() string {
|
|
return account.AccessUrl
|
|
}
|
|
|
|
func (account *SCloudDelegate) getOptions(ctx context.Context, s *mcclient.ClientSession) jsonutils.JSONObject {
|
|
ret := jsonutils.NewDict()
|
|
resp, _ := Cloudaccounts.GetById(s, account.CloudaccountId, jsonutils.Marshal(map[string]string{"scope": "system"}))
|
|
if !gotypes.IsNil(resp) {
|
|
options, _ := resp.Get("options")
|
|
ret.Update(options)
|
|
}
|
|
return ret
|
|
}
|
|
|
|
func (self *SCloudprovider) GetProvider(ctx context.Context, s *mcclient.ClientSession, id string) (cloudprovider.ICloudProvider, error) {
|
|
result, err := self.Get(s, id, jsonutils.Marshal(map[string]string{"scope": "system"}))
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "Cloudaccounts.Get")
|
|
}
|
|
account := &SCloudDelegate{}
|
|
err = result.Unmarshal(account)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "result.Unmarshal")
|
|
}
|
|
if !account.Enabled {
|
|
log.Warningf("Cloud account %s is disabled", account.Name)
|
|
}
|
|
|
|
accessUrl := account.getAccessUrl()
|
|
passwd, err := account.getPassword()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var proxyFunc httputils.TransportProxyFunc
|
|
{
|
|
cfg := &httpproxy.Config{
|
|
HTTPProxy: account.ProxySetting.HTTPProxy,
|
|
HTTPSProxy: account.ProxySetting.HTTPSProxy,
|
|
NoProxy: account.ProxySetting.NoProxy,
|
|
}
|
|
cfgProxyFunc := cfg.ProxyFunc()
|
|
proxyFunc = func(req *http.Request) (*url.URL, error) {
|
|
return cfgProxyFunc(req.URL)
|
|
}
|
|
}
|
|
options := account.getOptions(ctx, s)
|
|
defaultRegion, _ := options.GetString("default_region")
|
|
return cloudprovider.GetProvider(cloudprovider.ProviderConfig{
|
|
Id: account.Id,
|
|
Name: account.Name,
|
|
Vendor: account.Provider,
|
|
URL: accessUrl,
|
|
Account: account.Account,
|
|
Secret: passwd,
|
|
ProxyFunc: proxyFunc,
|
|
|
|
ReadOnly: account.ReadOnly,
|
|
|
|
DefaultRegion: defaultRegion,
|
|
Options: options.(*jsonutils.JSONDict),
|
|
|
|
AccountId: account.Id,
|
|
})
|
|
}
|