yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ctyun/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/log" 23 "yunion.io/x/pkg/errors" 24 25 api "yunion.io/x/cloudmux/pkg/apis/compute" 26 "yunion.io/x/cloudmux/pkg/cloudprovider" 27 "yunion.io/x/onecloud/pkg/httperrors" 28 "yunion.io/x/onecloud/pkg/mcclient" 29 "yunion.io/x/cloudmux/pkg/multicloud/ctyun" 30 ) 31 32 type SCtyunProviderFactory struct { 33 cloudprovider.SPublicCloudBaseProviderFactory 34 } 35 36 func (self *SCtyunProviderFactory) GetId() string { 37 return ctyun.CLOUD_PROVIDER_CTYUN 38 } 39 40 func (self *SCtyunProviderFactory) GetName() string { 41 return ctyun.CLOUD_PROVIDER_CTYUN_CN 42 } 43 44 func (self *SCtyunProviderFactory) IsSupportPrepaidResources() bool { 45 return true 46 } 47 48 func (self *SCtyunProviderFactory) ValidateCreateCloudaccountData(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential) (cloudprovider.SCloudaccount, error) { 49 output := cloudprovider.SCloudaccount{} 50 if len(input.AccessKeyId) == 0 { 51 return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id") 52 } 53 if len(input.AccessKeySecret) == 0 { 54 return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret") 55 } 56 if len(input.Environment) == 0 { 57 return output, errors.Wrap(httperrors.ErrMissingParameter, "environment") 58 } 59 output.Account = input.AccessKeyId 60 output.Secret = input.AccessKeySecret 61 output.AccessUrl = input.Environment 62 return output, nil 63 } 64 65 func (self *SCtyunProviderFactory) ValidateUpdateCloudaccountCredential(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential, cloudaccount string) (cloudprovider.SCloudaccount, error) { 66 output := cloudprovider.SCloudaccount{} 67 if len(input.AccessKeyId) == 0 { 68 return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id") 69 } 70 if len(input.AccessKeySecret) == 0 { 71 return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret") 72 } 73 output = cloudprovider.SCloudaccount{ 74 Account: input.AccessKeyId, 75 Secret: input.AccessKeySecret, 76 } 77 return output, nil 78 } 79 80 func (self *SCtyunProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) { 81 segs := strings.Split(cfg.Account, "/") 82 projectId := "" 83 account := cfg.Account 84 if len(segs) == 2 { 85 projectId = segs[1] 86 account = segs[0] 87 } 88 89 options := cloudprovider.SCtyunExtraOptions{} 90 if cfg.Options != nil { 91 err := cfg.Options.Unmarshal(&options) 92 if err != nil { 93 log.Debugf("cfg.Options.Unmarshal %s", err) 94 } 95 } 96 97 client, err := ctyun.NewSCtyunClient( 98 ctyun.NewSCtyunClientConfig( 99 account, cfg.Secret, &options, 100 ).ProjectId(projectId).CloudproviderConfig(cfg), 101 ) 102 if err != nil { 103 return nil, err 104 } 105 return &SCtyunProvider{ 106 SBaseProvider: cloudprovider.NewBaseProvider(self), 107 client: client, 108 }, nil 109 } 110 111 func (self *SCtyunProviderFactory) GetClientRC(info cloudprovider.SProviderInfo) (map[string]string, error) { 112 ret := map[string]string{ 113 "CTYUN_ACCESS_URL": info.Url, 114 "CTYUN_ACCESS_KEY": info.Account, 115 "CTYUN_SECRET": info.Secret, 116 "CTYUN_REGION": ctyun.CTYUN_DEFAULT_REGION, 117 } 118 119 options := cloudprovider.SCtyunExtraOptions{} 120 if info.Options != nil { 121 err := info.Options.Unmarshal(&options) 122 if err != nil { 123 log.Debugf("info.Options.Unmarshal %s", err) 124 } 125 } 126 if len(options.CrmBizId) > 0 { 127 ret["CTYUN_CRM_BIZ_ID"] = options.CrmBizId 128 } 129 130 return ret, nil 131 } 132 133 func init() { 134 factory := SCtyunProviderFactory{} 135 cloudprovider.RegisterFactory(&factory) 136 } 137 138 type SCtyunProvider struct { 139 cloudprovider.SBaseProvider 140 client *ctyun.SCtyunClient 141 } 142 143 func (self *SCtyunProvider) GetSubAccounts() ([]cloudprovider.SSubAccount, error) { 144 return self.client.GetSubAccounts() 145 } 146 147 func (self *SCtyunProvider) GetAccountId() string { 148 return self.client.GetAccountId() 149 } 150 151 func (self *SCtyunProvider) GetIRegions() []cloudprovider.ICloudRegion { 152 return self.client.GetIRegions() 153 } 154 155 func (self *SCtyunProvider) GetSysInfo() (jsonutils.JSONObject, error) { 156 regions := self.client.GetIRegions() 157 info := jsonutils.NewDict() 158 info.Add(jsonutils.NewInt(int64(len(regions))), "region_count") 159 info.Add(jsonutils.NewString(ctyun.CTYUN_API_VERSION), "api_version") 160 return info, nil 161 } 162 163 func (self *SCtyunProvider) GetVersion() string { 164 return ctyun.CTYUN_API_VERSION 165 } 166 167 func (self *SCtyunProvider) GetIRegionById(id string) (cloudprovider.ICloudRegion, error) { 168 return self.client.GetIRegionById(id) 169 } 170 171 func (self *SCtyunProvider) GetBalance() (float64, string, error) { 172 return 0.0, api.CLOUD_PROVIDER_HEALTH_NORMAL, cloudprovider.ErrNotSupported 173 } 174 175 func (self *SCtyunProvider) GetIProjects() ([]cloudprovider.ICloudProject, error) { 176 return self.client.GetIProjects() 177 } 178 179 func (self *SCtyunProvider) GetStorageClasses(regionId string) []string { 180 return []string{ 181 "STANDARD", "WARM", "COLD", 182 } 183 } 184 185 func (self *SCtyunProvider) GetBucketCannedAcls(regionId string) []string { 186 return nil 187 } 188 189 func (self *SCtyunProvider) GetObjectCannedAcls(regionId string) []string { 190 return nil 191 } 192 193 func (self *SCtyunProvider) GetCloudRegionExternalIdPrefix() string { 194 return self.client.GetCloudRegionExternalIdPrefix() 195 } 196 197 func (self *SCtyunProvider) GetCapabilities() []string { 198 return self.client.GetCapabilities() 199 }