cloudproviders.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 models
  15. import (
  16. "context"
  17. "net/http"
  18. "net/url"
  19. "time"
  20. "golang.org/x/net/http/httpproxy"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/pkg/errors"
  23. "yunion.io/x/pkg/gotypes"
  24. "yunion.io/x/pkg/util/httputils"
  25. "yunion.io/x/pkg/utils"
  26. proxyapi "yunion.io/x/onecloud/pkg/apis/cloudcommon/proxy"
  27. "yunion.io/x/onecloud/pkg/mcclient"
  28. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  29. "yunion.io/x/onecloud/pkg/s3gateway/session"
  30. "yunion.io/x/onecloud/pkg/util/hashcache"
  31. )
  32. type SCloudproviderManagerDelegate struct {
  33. providers *hashcache.Cache
  34. }
  35. var CloudproviderManager *SCloudproviderManagerDelegate
  36. func init() {
  37. CloudproviderManager = &SCloudproviderManagerDelegate{
  38. providers: hashcache.NewCache(2048, time.Minute*15),
  39. }
  40. }
  41. /*
  42. {
  43. "account":"oH7Qrw75AqrI4BXn",
  44. "can_delete":false,
  45. "can_update":true,
  46. "cloudaccount":"testaliyun",
  47. "cloudaccount_id":"f1a927b4-a433-486e-86ae-e9aa36e447b1",
  48. "created_at":"2019-04-16T13:55:11.000000Z",
  49. "domain":"Default",
  50. "domain_id":"default",
  51. "eip_count":9,
  52. "enabled":true,
  53. "guest_count":1,
  54. "health_status":"normal",
  55. "host_count":61,
  56. "id":"57c84d93-8f06-4a85-8963-4ce42eabb339",
  57. "is_emulated":false,
  58. "last_sync":"2019-07-23T15:29:34.000000Z",
  59. "last_sync_end_at":"2019-07-23T15:33:34.000000Z",
  60. "loadbalancer_count":13,
  61. "name":"testaliyun",
  62. "project_count":0,
  63. "provider":"Aliyun",
  64. "secret":"Y5YFmuwVI4frJ8kVgWL0z5Kan/sJ3JMyjyFRxAXwXvsUKd8aNohPp2T/Kr1BqA==",
  65. "snapshot_count":6,
  66. "status":"connected",
  67. "storage_cache_count":20,
  68. "storage_count":141,
  69. "sync_region_count":20,
  70. "sync_status":"idle",
  71. "sync_status2":"idle",
  72. "tenant":"system",
  73. "tenant_id":"5d65667d112e47249ae66dbd7bc07030",
  74. "update_version":5003,
  75. "updated_at":"2019-08-18T14:47:42.000000Z",
  76. "vpc_count":13,
  77. }
  78. */
  79. type SCloudproviderDelegate struct {
  80. SBaseModelDelegate
  81. Enabled bool
  82. Status string
  83. SyncStatus string
  84. AccessUrl string
  85. Account string
  86. Secret string
  87. Provider string
  88. Brand string
  89. ProxySetting proxyapi.SProxySetting
  90. }
  91. func (manager *SCloudproviderManagerDelegate) GetById(ctx context.Context, userCred mcclient.TokenCredential, id string) (*SCloudproviderDelegate, error) {
  92. val := manager.providers.AtomicGet(id)
  93. if !gotypes.IsNil(val) {
  94. return val.(*SCloudproviderDelegate), nil
  95. }
  96. s := session.GetSession(ctx, userCred)
  97. result, err := modules.Cloudproviders.Get(s, id, nil)
  98. if err != nil {
  99. return nil, errors.Wrap(err, "modules.Cloudproviders.Get")
  100. }
  101. provider := &SCloudproviderDelegate{}
  102. err = result.Unmarshal(provider)
  103. if err != nil {
  104. return nil, errors.Wrap(err, "result.Unmarshal")
  105. }
  106. manager.providers.AtomicSet(provider.Id, provider)
  107. return provider, nil
  108. }
  109. func (provider *SCloudproviderDelegate) getPassword() (string, error) {
  110. return utils.DescryptAESBase64(provider.Id, provider.Secret)
  111. }
  112. func (provider *SCloudproviderDelegate) getAccessUrl() string {
  113. return provider.AccessUrl
  114. }
  115. func (provider *SCloudproviderDelegate) GetProviderFactory() (cloudprovider.ICloudProviderFactory, error) {
  116. return cloudprovider.GetProviderFactory(provider.Provider)
  117. }
  118. func (provider *SCloudproviderDelegate) GetProvider() (cloudprovider.ICloudProvider, error) {
  119. if !provider.Enabled {
  120. return nil, errors.Error("Cloud provider is not enabled")
  121. }
  122. accessUrl := provider.getAccessUrl()
  123. passwd, err := provider.getPassword()
  124. if err != nil {
  125. return nil, err
  126. }
  127. var proxyFunc httputils.TransportProxyFunc
  128. {
  129. cfg := &httpproxy.Config{
  130. HTTPProxy: provider.ProxySetting.HTTPProxy,
  131. HTTPSProxy: provider.ProxySetting.HTTPSProxy,
  132. NoProxy: provider.ProxySetting.NoProxy,
  133. }
  134. cfgProxyFunc := cfg.ProxyFunc()
  135. proxyFunc = func(req *http.Request) (*url.URL, error) {
  136. return cfgProxyFunc(req.URL)
  137. }
  138. }
  139. return cloudprovider.GetProvider(cloudprovider.ProviderConfig{
  140. Id: provider.Id,
  141. Name: provider.Name,
  142. Vendor: provider.Provider,
  143. URL: accessUrl,
  144. Account: provider.Account,
  145. Secret: passwd,
  146. ProxyFunc: proxyFunc,
  147. })
  148. }