mod_cloudaccounts.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/util/httputils"
  25. "yunion.io/x/onecloud/pkg/mcclient"
  26. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  27. "yunion.io/x/onecloud/pkg/mcclient/modules"
  28. )
  29. type SCloudaccount struct {
  30. modulebase.ResourceManager
  31. }
  32. var (
  33. Cloudaccounts SCloudaccount
  34. )
  35. func init() {
  36. Cloudaccounts = SCloudaccount{
  37. modules.NewComputeManager("cloudaccount", "cloudaccounts",
  38. []string{"ID", "Name", "Enabled", "Status", "Access_url",
  39. "balance", "currency", "error_count", "health_status",
  40. "Sync_Status", "Last_sync",
  41. "guest_count", "project_domain", "domain_id",
  42. "Provider", "Brand",
  43. "Enable_Auto_Sync", "Sync_Interval_Seconds",
  44. "Share_Mode", "is_public", "public_scope",
  45. "auto_create_project",
  46. },
  47. []string{}),
  48. }
  49. modules.RegisterCompute(&Cloudaccounts)
  50. }
  51. func (self *SCloudaccount) GetProvider(ctx context.Context, s *mcclient.ClientSession, id string) (cloudprovider.ICloudProvider, error) {
  52. result, err := self.Get(s, id, jsonutils.Marshal(map[string]string{"scope": "system"}))
  53. if err != nil {
  54. return nil, errors.Wrap(err, "Cloudaccounts.Get")
  55. }
  56. account := &SCloudDelegate{}
  57. err = result.Unmarshal(account)
  58. if err != nil {
  59. return nil, errors.Wrap(err, "result.Unmarshal")
  60. }
  61. if !account.Enabled {
  62. log.Warningf("Cloud account %s is disabled", account.Name)
  63. }
  64. accessUrl := account.getAccessUrl()
  65. passwd, err := account.getPassword()
  66. if err != nil {
  67. return nil, err
  68. }
  69. var proxyFunc httputils.TransportProxyFunc
  70. {
  71. cfg := &httpproxy.Config{
  72. HTTPProxy: account.ProxySetting.HTTPProxy,
  73. HTTPSProxy: account.ProxySetting.HTTPSProxy,
  74. NoProxy: account.ProxySetting.NoProxy,
  75. }
  76. cfgProxyFunc := cfg.ProxyFunc()
  77. proxyFunc = func(req *http.Request) (*url.URL, error) {
  78. return cfgProxyFunc(req.URL)
  79. }
  80. }
  81. regionId, options := account.getOptions(ctx, s)
  82. return cloudprovider.GetProvider(cloudprovider.ProviderConfig{
  83. Id: account.Id,
  84. Name: account.Name,
  85. Vendor: account.Provider,
  86. URL: accessUrl,
  87. Account: account.Account,
  88. Secret: passwd,
  89. ProxyFunc: proxyFunc,
  90. ReadOnly: account.ReadOnly,
  91. RegionId: regionId,
  92. Options: options.(*jsonutils.JSONDict),
  93. })
  94. }