yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/incloudsphere/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 "fmt" 20 "strings" 21 22 "yunion.io/x/jsonutils" 23 "yunion.io/x/pkg/errors" 24 "yunion.io/x/pkg/util/regutils" 25 26 api "yunion.io/x/cloudmux/pkg/apis/compute" 27 "yunion.io/x/cloudmux/pkg/cloudprovider" 28 "yunion.io/x/onecloud/pkg/httperrors" 29 "yunion.io/x/onecloud/pkg/mcclient" 30 "yunion.io/x/cloudmux/pkg/multicloud/incloudsphere" 31 ) 32 33 type SInCloudSphereProviderFactory struct { 34 cloudprovider.SPrivateCloudBaseProviderFactory 35 } 36 37 func (self *SInCloudSphereProviderFactory) GetId() string { 38 return incloudsphere.CLOUD_PROVIDER_INCLOUD_SPHERE 39 } 40 41 func (self *SInCloudSphereProviderFactory) GetName() string { 42 return incloudsphere.CLOUD_PROVIDER_INCLOUD_SPHERE 43 } 44 45 func (self *SInCloudSphereProviderFactory) ValidateChangeBandwidth(instanceId string, bandwidth int64) error { 46 return fmt.Errorf("Changing %s bandwidth is not supported", incloudsphere.CLOUD_PROVIDER_INCLOUD_SPHERE) 47 } 48 49 func (self *SInCloudSphereProviderFactory) ValidateCreateCloudaccountData(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential) (cloudprovider.SCloudaccount, error) { 50 output := cloudprovider.SCloudaccount{} 51 if len(input.AccessKeyId) == 0 { 52 return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id") 53 } 54 if len(input.AccessKeySecret) == 0 { 55 return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret") 56 } 57 if len(input.Host) == 0 { 58 return output, errors.Wrap(httperrors.ErrMissingParameter, "host") 59 } 60 input.Host = strings.TrimPrefix(input.Host, "https://") 61 input.Host = strings.TrimPrefix(input.Host, "http://") 62 if !regutils.MatchIPAddr(input.Host) && !regutils.MatchDomainName(input.Host) { 63 return output, errors.Wrap(httperrors.ErrInputParameter, "host should be ip or domain name") 64 } 65 output.AccessUrl = input.Host 66 output.Account = input.AccessKeyId 67 output.Secret = input.AccessKeySecret 68 return output, nil 69 } 70 71 func (self *SInCloudSphereProviderFactory) ValidateUpdateCloudaccountCredential(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential, cloudaccount string) (cloudprovider.SCloudaccount, error) { 72 output := cloudprovider.SCloudaccount{} 73 if len(input.AccessKeyId) == 0 { 74 return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id") 75 } 76 if len(input.AccessKeySecret) == 0 { 77 return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret") 78 } 79 output = cloudprovider.SCloudaccount{ 80 Account: input.AccessKeyId, 81 Secret: input.AccessKeySecret, 82 } 83 return output, nil 84 } 85 86 func (self *SInCloudSphereProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) { 87 client, err := incloudsphere.NewSphereClient( 88 incloudsphere.NewSphereClientConfig( 89 cfg.URL, cfg.Account, cfg.Secret, 90 ).CloudproviderConfig(cfg), 91 ) 92 if err != nil { 93 return nil, err 94 } 95 return &SInCloudSphereProvider{ 96 SBaseProvider: cloudprovider.NewBaseProvider(self), 97 client: client, 98 }, nil 99 } 100 101 func (self *SInCloudSphereProviderFactory) GetClientRC(info cloudprovider.SProviderInfo) (map[string]string, error) { 102 return map[string]string{ 103 "INCLOUD_SPHERE_HOST": info.Url, 104 "INCLOUD_SPHERE_ACCESS_KEY_ID": info.Account, 105 "INCLOUD_SPHERE_ACCESS_KEY_SECRET": info.Secret, 106 }, nil 107 } 108 109 func init() { 110 factory := SInCloudSphereProviderFactory{} 111 cloudprovider.RegisterFactory(&factory) 112 } 113 114 type SInCloudSphereProvider struct { 115 cloudprovider.SBaseProvider 116 client *incloudsphere.SphereClient 117 } 118 119 func (self *SInCloudSphereProvider) GetSysInfo() (jsonutils.JSONObject, error) { 120 return jsonutils.NewDict(), nil 121 } 122 123 func (self *SInCloudSphereProvider) GetVersion() string { 124 return "5.8" 125 } 126 127 func (self *SInCloudSphereProvider) GetSubAccounts() ([]cloudprovider.SSubAccount, error) { 128 return self.client.GetSubAccounts() 129 } 130 131 func (self *SInCloudSphereProvider) GetAccountId() string { 132 return self.client.GetAccountId() 133 } 134 135 func (self *SInCloudSphereProvider) GetIRegions() []cloudprovider.ICloudRegion { 136 return self.client.GetIRegions() 137 } 138 139 func (self *SInCloudSphereProvider) GetIRegionById(id string) (cloudprovider.ICloudRegion, error) { 140 regions := self.GetIRegions() 141 for i := range regions { 142 if regions[i].GetGlobalId() == id { 143 return regions[i], nil 144 } 145 } 146 return nil, cloudprovider.ErrNotFound 147 } 148 149 func (self *SInCloudSphereProvider) GetBalance() (float64, string, error) { 150 return 0.0, api.CLOUD_PROVIDER_HEALTH_NORMAL, cloudprovider.ErrNotSupported 151 } 152 153 func (self *SInCloudSphereProvider) GetIProjects() ([]cloudprovider.ICloudProject, error) { 154 return []cloudprovider.ICloudProject{}, nil 155 } 156 157 func (self *SInCloudSphereProvider) GetStorageClasses(regionId string) []string { 158 return nil 159 } 160 161 func (self *SInCloudSphereProvider) GetBucketCannedAcls(regionId string) []string { 162 return nil 163 } 164 165 func (self *SInCloudSphereProvider) GetObjectCannedAcls(regionId string) []string { 166 return nil 167 } 168 169 func (self *SInCloudSphereProvider) GetCapabilities() []string { 170 return self.client.GetCapabilities() 171 }