| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- // 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
- DefaultRegion 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) (string, jsonutils.JSONObject) {
- regionId, 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)
- regionId, _ = resp.GetString("region_id")
- if len(regionId) == 0 {
- regionId, _ = ret.GetString("default_region")
- }
- }
- return regionId, 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, "Cloudprovider.Get")
- }
- account := &SCloudDelegate{}
- err = result.Unmarshal(account)
- if err != nil {
- return nil, errors.Wrap(err, "result.Unmarshal")
- }
- if !account.Enabled {
- log.Warningf("Cloud provider %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)
- }
- }
- regionId, options := account.getOptions(ctx, s)
- 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,
- RegionId: regionId,
- Options: options.(*jsonutils.JSONDict),
- AccountId: account.Id,
- })
- }
|