yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/jdcloud/provider/provider.go (about) 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 15 package provider 16 17 import ( 18 "context" 19 "strings" 20 21 "yunion.io/x/jsonutils" 22 "yunion.io/x/pkg/errors" 23 24 api "yunion.io/x/cloudmux/pkg/apis/compute" 25 "yunion.io/x/cloudmux/pkg/cloudprovider" 26 "yunion.io/x/onecloud/pkg/httperrors" 27 "yunion.io/x/onecloud/pkg/mcclient" 28 "yunion.io/x/cloudmux/pkg/multicloud/jdcloud" 29 ) 30 31 type SJdcloudProviderFactory struct { 32 cloudprovider.SPublicCloudBaseProviderFactory 33 } 34 35 func (f *SJdcloudProviderFactory) GetId() string { 36 return jdcloud.CLOUD_PROVIDER_JDCLOUD 37 } 38 39 func (f *SJdcloudProviderFactory) GetName() string { 40 return jdcloud.CLOUD_PROVIDER_JDCLOUD_CN 41 } 42 43 func (f *SJdcloudProviderFactory) IsSupportPrepaidResources() bool { 44 return true 45 } 46 47 func (f *SJdcloudProviderFactory) ValidateCreateCloudaccountData(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential) (cloudprovider.SCloudaccount, error) { 48 output := cloudprovider.SCloudaccount{} 49 if len(input.AccessKeyId) == 0 { 50 return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id") 51 } 52 if len(input.AccessKeySecret) == 0 { 53 return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret") 54 } 55 output.Account = input.AccessKeyId 56 output.Secret = input.AccessKeySecret 57 return output, nil 58 } 59 60 func (f *SJdcloudProviderFactory) ValidateUpdateCloudaccountCredential(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential, cloudaccount string) (cloudprovider.SCloudaccount, error) { 61 output := cloudprovider.SCloudaccount{} 62 if len(input.AccessKeyId) == 0 { 63 return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id") 64 } 65 if len(input.AccessKeySecret) == 0 { 66 return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret") 67 } 68 output = cloudprovider.SCloudaccount{ 69 Account: input.AccessKeyId, 70 Secret: input.AccessKeySecret, 71 } 72 return output, nil 73 } 74 75 func (f *SJdcloudProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) { 76 segs := strings.Split(cfg.Account, "/") 77 account := cfg.Account 78 if len(segs) == 2 { 79 account = segs[0] 80 } 81 client, err := jdcloud.NewJDCloudClient( 82 jdcloud.NewJDCloudClientConfig( 83 account, 84 cfg.Secret, 85 ).CloudproviderConfig(cfg), 86 ) 87 if err != nil { 88 return nil, err 89 } 90 91 return &SJdcloudProvider{ 92 SBaseProvider: cloudprovider.NewBaseProvider(f), 93 client: client, 94 }, nil 95 } 96 97 func (f *SJdcloudProviderFactory) GetClientRC(info cloudprovider.SProviderInfo) (map[string]string, error) { 98 return map[string]string{ 99 "JDCLOUD_ACCESS_KEY": info.Account, 100 "JDCLOUD_SECRET": info.Secret, 101 "JDCLOUD_REGION": jdcloud.JDCLOUD_DEFAULT_REGION, 102 }, nil 103 } 104 105 func init() { 106 factory := SJdcloudProviderFactory{} 107 cloudprovider.RegisterFactory(&factory) 108 } 109 110 type SJdcloudProvider struct { 111 cloudprovider.SBaseProvider 112 113 client *jdcloud.SJDCloudClient 114 } 115 116 func (p *SJdcloudProvider) GetSubAccounts() ([]cloudprovider.SSubAccount, error) { 117 return p.client.GetSubAccounts() 118 } 119 120 func (p *SJdcloudProvider) GetAccountId() string { 121 return p.client.GetAccountId() 122 } 123 124 func (p *SJdcloudProvider) GetIRegions() []cloudprovider.ICloudRegion { 125 return p.client.GetIRegions() 126 } 127 128 func (p *SJdcloudProvider) GetSysInfo() (jsonutils.JSONObject, error) { 129 iregions := p.GetIRegions() 130 info := jsonutils.NewDict() 131 info.Add(jsonutils.NewInt(int64(len(iregions))), "region_count") 132 return info, nil 133 } 134 135 func (p *SJdcloudProvider) GetVersion() string { 136 return "" 137 } 138 139 func (p *SJdcloudProvider) GetIRegionById(id string) (cloudprovider.ICloudRegion, error) { 140 iregions := p.GetIRegions() 141 for i := range iregions { 142 if iregions[i].GetGlobalId() == id { 143 return iregions[i], nil 144 } 145 } 146 return nil, cloudprovider.ErrNotFound 147 } 148 149 func (p *SJdcloudProvider) GetBalance() (float64, string, error) { 150 balance, err := p.client.DescribeAccountAmount() 151 if err != nil { 152 return 0.0, api.CLOUD_PROVIDER_HEALTH_NO_PERMISSION, errors.Wrap(err, "DescribeAccountAmount") 153 } 154 amount, _ := jsonutils.Marshal(balance).Float("totalAmount") 155 if amount < 0 { 156 return amount, api.CLOUD_PROVIDER_HEALTH_ARREARS, nil 157 } else if amount < 50 { 158 return amount, api.CLOUD_PROVIDER_HEALTH_INSUFFICIENT, nil 159 } 160 return amount, api.CLOUD_PROVIDER_HEALTH_NORMAL, nil 161 } 162 163 func (p *SJdcloudProvider) GetIProjects() ([]cloudprovider.ICloudProject, error) { 164 return nil, cloudprovider.ErrNotImplemented 165 } 166 167 func (p *SJdcloudProvider) GetStorageClasses(regionId string) []string { 168 // TODO 169 return nil 170 } 171 172 func (p *SJdcloudProvider) GetBucketCannedAcls(regionId string) []string { 173 return nil 174 } 175 176 func (p *SJdcloudProvider) GetObjectCannedAcls(regionId string) []string { 177 return nil 178 } 179 180 func (p *SJdcloudProvider) GetCloudRegionExternalIdPrefix() string { 181 return api.CLOUD_PROVIDER_JDCLOUD 182 } 183 184 func (p *SJdcloudProvider) GetCapabilities() []string { 185 iRegions := p.GetIRegions() 186 if len(iRegions) > 0 { 187 return iRegions[0].GetCapabilities() 188 } 189 caps := []string{ 190 cloudprovider.CLOUD_CAPABILITY_COMPUTE + cloudprovider.READ_ONLY_SUFFIX, 191 cloudprovider.CLOUD_CAPABILITY_NETWORK + cloudprovider.READ_ONLY_SUFFIX, 192 cloudprovider.CLOUD_CAPABILITY_EIP + cloudprovider.READ_ONLY_SUFFIX, 193 cloudprovider.CLOUD_CAPABILITY_RDS + cloudprovider.READ_ONLY_SUFFIX, 194 } 195 return caps 196 } 197 198 func (self *SJdcloudProvider) GetMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) { 199 return self.client.GetMetrics(opts) 200 }