mod_cloudproviders.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package compute
  15. import (
  16. "context"
  17. "net/http"
  18. "net/url"
  19. "golang.org/x/net/http/httpproxy"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/jsonutils"
  22. "yunion.io/x/log"
  23. "yunion.io/x/pkg/errors"
  24. "yunion.io/x/pkg/gotypes"
  25. "yunion.io/x/pkg/util/httputils"
  26. "yunion.io/x/pkg/utils"
  27. proxyapi "yunion.io/x/onecloud/pkg/apis/cloudcommon/proxy"
  28. "yunion.io/x/onecloud/pkg/mcclient"
  29. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  30. "yunion.io/x/onecloud/pkg/mcclient/modules"
  31. )
  32. type SCloudprovider struct {
  33. modulebase.ResourceManager
  34. }
  35. var (
  36. Cloudproviders SCloudprovider
  37. )
  38. func init() {
  39. Cloudproviders = SCloudprovider{
  40. modules.NewComputeManager("cloudprovider", "cloudproviders",
  41. []string{"ID", "Name", "Enabled", "Status", "Access_url", "Account",
  42. "Sync_Status", "Last_sync", "Last_sync_end_at",
  43. "health_status",
  44. "Provider", "guest_count", "host_count", "vpc_count",
  45. "storage_count", "storage_cache_count", "eip_count",
  46. "tenant_id", "tenant"},
  47. []string{}),
  48. }
  49. modules.RegisterCompute(&Cloudproviders)
  50. }
  51. type SCloudDelegate struct {
  52. Id string
  53. Name string
  54. Enabled bool
  55. Status string
  56. SyncStatus string
  57. CloudaccountId string
  58. AccessUrl string
  59. Account string
  60. Secret string
  61. Provider string
  62. Brand string
  63. ReadOnly bool
  64. Options struct {
  65. cloudprovider.SHCSOEndpoints
  66. Account string
  67. Password string
  68. DefaultRegion string
  69. }
  70. ProxySetting proxyapi.SProxySetting
  71. }
  72. func (account *SCloudDelegate) getPassword() (string, error) {
  73. return utils.DescryptAESBase64(account.Id, account.Secret)
  74. }
  75. func (account *SCloudDelegate) getAccessUrl() string {
  76. return account.AccessUrl
  77. }
  78. func (account *SCloudDelegate) getOptions(ctx context.Context, s *mcclient.ClientSession) (string, jsonutils.JSONObject) {
  79. regionId, ret := "", jsonutils.NewDict()
  80. resp, _ := Cloudaccounts.GetById(s, account.CloudaccountId, jsonutils.Marshal(map[string]string{"scope": "system"}))
  81. if !gotypes.IsNil(resp) {
  82. options, _ := resp.Get("options")
  83. ret.Update(options)
  84. regionId, _ = resp.GetString("region_id")
  85. if len(regionId) == 0 {
  86. regionId, _ = ret.GetString("default_region")
  87. }
  88. }
  89. return regionId, ret
  90. }
  91. func (self *SCloudprovider) GetProvider(ctx context.Context, s *mcclient.ClientSession, id string) (cloudprovider.ICloudProvider, error) {
  92. result, err := self.Get(s, id, jsonutils.Marshal(map[string]string{"scope": "system"}))
  93. if err != nil {
  94. return nil, errors.Wrap(err, "Cloudprovider.Get")
  95. }
  96. account := &SCloudDelegate{}
  97. err = result.Unmarshal(account)
  98. if err != nil {
  99. return nil, errors.Wrap(err, "result.Unmarshal")
  100. }
  101. if !account.Enabled {
  102. log.Warningf("Cloud provider %s is disabled", account.Name)
  103. }
  104. accessUrl := account.getAccessUrl()
  105. passwd, err := account.getPassword()
  106. if err != nil {
  107. return nil, err
  108. }
  109. var proxyFunc httputils.TransportProxyFunc
  110. {
  111. cfg := &httpproxy.Config{
  112. HTTPProxy: account.ProxySetting.HTTPProxy,
  113. HTTPSProxy: account.ProxySetting.HTTPSProxy,
  114. NoProxy: account.ProxySetting.NoProxy,
  115. }
  116. cfgProxyFunc := cfg.ProxyFunc()
  117. proxyFunc = func(req *http.Request) (*url.URL, error) {
  118. return cfgProxyFunc(req.URL)
  119. }
  120. }
  121. regionId, options := account.getOptions(ctx, s)
  122. return cloudprovider.GetProvider(cloudprovider.ProviderConfig{
  123. Id: account.Id,
  124. Name: account.Name,
  125. Vendor: account.Provider,
  126. URL: accessUrl,
  127. Account: account.Account,
  128. Secret: passwd,
  129. ProxyFunc: proxyFunc,
  130. ReadOnly: account.ReadOnly,
  131. RegionId: regionId,
  132. Options: options.(*jsonutils.JSONDict),
  133. AccountId: account.Id,
  134. })
  135. }