yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/zstack/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 20 "yunion.io/x/jsonutils" 21 "yunion.io/x/pkg/errors" 22 23 api "yunion.io/x/cloudmux/pkg/apis/compute" 24 "yunion.io/x/cloudmux/pkg/cloudprovider" 25 "yunion.io/x/onecloud/pkg/httperrors" 26 "yunion.io/x/onecloud/pkg/mcclient" 27 "yunion.io/x/cloudmux/pkg/multicloud/zstack" 28 ) 29 30 type SZStackProviderFactory struct { 31 cloudprovider.SPrivateCloudBaseProviderFactory 32 } 33 34 func (self *SZStackProviderFactory) GetId() string { 35 return zstack.CLOUD_PROVIDER_ZSTACK 36 } 37 38 func (self *SZStackProviderFactory) GetName() string { 39 return zstack.CLOUD_PROVIDER_ZSTACK 40 } 41 42 func (self *SZStackProviderFactory) GetSupportedBrands() []string { 43 return []string{api.ZSTACK_BRAND_DSTACK} 44 } 45 46 func (self *SZStackProviderFactory) ValidateCreateCloudaccountData(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential) (cloudprovider.SCloudaccount, error) { 47 output := cloudprovider.SCloudaccount{} 48 if len(input.AuthUrl) == 0 { 49 return output, errors.Wrap(httperrors.ErrMissingParameter, "auth_url") 50 } 51 output.AccessUrl = input.AuthUrl 52 //为了兼容以前用username的参数,2.12之后尽可能的使用access_key_id参数 53 if len(input.AccessKeyId) > 0 && len(input.AccessKeySecret) > 0 { 54 output.Account = input.AccessKeyId 55 output.Secret = input.AccessKeySecret 56 } else if len(input.Username) > 0 && len(input.Password) > 0 { 57 output.Account = input.Username 58 output.Secret = input.Password 59 } else { 60 return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id or access_key_secret") 61 } 62 return output, nil 63 } 64 65 func (self *SZStackProviderFactory) 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 && len(input.AccessKeySecret) > 0 { 68 output.Account = input.AccessKeyId 69 output.Secret = input.AccessKeySecret 70 } else if len(input.Username) > 0 && len(input.Password) > 0 { 71 output.Account = input.Username 72 output.Secret = input.Password 73 } else { 74 return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id or access_key_secret") 75 } 76 return output, nil 77 } 78 79 func (self *SZStackProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) { 80 client, err := zstack.NewZStackClient( 81 zstack.NewZstackClientConfig( 82 cfg.URL, cfg.Account, cfg.Secret, 83 ).CloudproviderConfig(cfg), 84 ) 85 if err != nil { 86 return nil, err 87 } 88 return &SZStackProvider{ 89 SBaseProvider: cloudprovider.NewBaseProvider(self), 90 client: client, 91 }, nil 92 } 93 94 func (self *SZStackProviderFactory) GetClientRC(info cloudprovider.SProviderInfo) (map[string]string, error) { 95 return map[string]string{ 96 "ZSTACK_AUTH_URL": info.Url, 97 "ZSTACK_USERNAME": info.Account, 98 "ZSTACK_PASSWORD": info.Secret, 99 "ZSTACK_REGION_ID": zstack.ZSTACK_DEFAULT_REGION, 100 }, nil 101 } 102 103 func init() { 104 factory := SZStackProviderFactory{} 105 cloudprovider.RegisterFactory(&factory) 106 } 107 108 type SZStackProvider struct { 109 cloudprovider.SBaseProvider 110 client *zstack.SZStackClient 111 } 112 113 func (self *SZStackProvider) GetVersion() string { 114 return "" 115 } 116 117 func (self *SZStackProvider) GetSysInfo() (jsonutils.JSONObject, error) { 118 return jsonutils.NewDict(), nil 119 } 120 121 func (self *SZStackProvider) GetSubAccounts() ([]cloudprovider.SSubAccount, error) { 122 return self.client.GetSubAccounts() 123 } 124 125 func (self *SZStackProvider) GetAccountId() string { 126 return "" 127 } 128 129 func (self *SZStackProvider) GetIRegions() []cloudprovider.ICloudRegion { 130 return self.client.GetIRegions() 131 } 132 133 func (self *SZStackProvider) GetIRegionById(extId string) (cloudprovider.ICloudRegion, error) { 134 return self.client.GetIRegionById(extId) 135 } 136 137 func (self *SZStackProvider) GetBalance() (float64, string, error) { 138 return 0.0, api.CLOUD_PROVIDER_HEALTH_UNKNOWN, cloudprovider.ErrNotSupported 139 } 140 141 func (self *SZStackProvider) GetCloudRegionExternalIdPrefix() string { 142 return self.client.GetCloudRegionExternalIdPrefix() 143 } 144 145 func (self *SZStackProvider) GetIProjects() ([]cloudprovider.ICloudProject, error) { 146 return self.client.GetIProjects() 147 } 148 149 func (self *SZStackProvider) GetStorageClasses(regionId string) []string { 150 return nil 151 } 152 153 func (self *SZStackProvider) GetBucketCannedAcls(regionId string) []string { 154 return nil 155 } 156 157 func (self *SZStackProvider) GetObjectCannedAcls(regionId string) []string { 158 return nil 159 } 160 161 func (self *SZStackProvider) GetCapabilities() []string { 162 return self.client.GetCapabilities() 163 } 164 165 func (self *SZStackProvider) GetMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) { 166 return self.client.GetEcsMetrics(opts) 167 }