jdcloud.go 2.8 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 jdcloud
  15. import (
  16. "github.com/jdcloud-api/jdcloud-sdk-go/core"
  17. api "yunion.io/x/cloudmux/pkg/apis/compute"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. )
  20. type SJDCloudClient struct {
  21. *JDCloudClientConfig
  22. iregion []cloudprovider.ICloudRegion
  23. }
  24. type JDCloudClientConfig struct {
  25. cpcfg cloudprovider.ProviderConfig
  26. accessKey string
  27. accessSecret string
  28. debug bool
  29. }
  30. func NewJDCloudClientConfig(accessKey, accessSecret string) *JDCloudClientConfig {
  31. cfg := &JDCloudClientConfig{
  32. accessKey: accessKey,
  33. accessSecret: accessSecret,
  34. }
  35. return cfg
  36. }
  37. func (cfg *JDCloudClientConfig) CloudproviderConfig(cpcfg cloudprovider.ProviderConfig) *JDCloudClientConfig {
  38. cfg.cpcfg = cpcfg
  39. return cfg
  40. }
  41. func (cfg *JDCloudClientConfig) Debug(debug bool) *JDCloudClientConfig {
  42. cfg.debug = debug
  43. return cfg
  44. }
  45. func (self *SJDCloudClient) getCredential() *core.Credential {
  46. return core.NewCredentials(self.accessKey, self.accessSecret)
  47. }
  48. func NewJDCloudClient(cfg *JDCloudClientConfig) (*SJDCloudClient, error) {
  49. client := SJDCloudClient{
  50. JDCloudClientConfig: cfg,
  51. }
  52. _, err := client.DescribeAccountAmount()
  53. return &client, err
  54. }
  55. func (self *SJDCloudClient) GetIRegions() ([]cloudprovider.ICloudRegion, error) {
  56. if self.iregion != nil {
  57. return self.iregion, nil
  58. }
  59. self.iregion = []cloudprovider.ICloudRegion{}
  60. for id, name := range regionList {
  61. self.iregion = append(self.iregion, &SRegion{
  62. ID: id,
  63. Name: name,
  64. client: self,
  65. })
  66. }
  67. return self.iregion, nil
  68. }
  69. func (self *SJDCloudClient) GetSubAccounts() ([]cloudprovider.SSubAccount, error) {
  70. subAccount := cloudprovider.SSubAccount{}
  71. subAccount.Id = self.GetAccountId()
  72. subAccount.Name = self.cpcfg.Name
  73. subAccount.Account = self.accessKey
  74. subAccount.HealthStatus = api.CLOUD_PROVIDER_HEALTH_NORMAL
  75. return []cloudprovider.SSubAccount{subAccount}, nil
  76. }
  77. func (self *SJDCloudClient) GetAccountId() string {
  78. return self.accessKey
  79. }
  80. func (self *SJDCloudClient) GetRegion(regionId string) (*SRegion, error) {
  81. iregions, err := self.GetIRegions()
  82. if err != nil {
  83. return nil, err
  84. }
  85. for i := range iregions {
  86. if len(regionId) == 0 || iregions[i].GetId() == regionId {
  87. return iregions[i].(*SRegion), nil
  88. }
  89. }
  90. return nil, cloudprovider.ErrNotFound
  91. }