yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/objectstore/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/objectstore" 28 ) 29 30 type SObjectStoreProviderFactory struct { 31 cloudprovider.SPremiseBaseProviderFactory 32 } 33 34 func (self *SObjectStoreProviderFactory) GetId() string { 35 return api.CLOUD_PROVIDER_GENERICS3 36 } 37 38 func (self *SObjectStoreProviderFactory) GetName() string { 39 return api.CLOUD_PROVIDER_GENERICS3 40 } 41 42 func (self *SObjectStoreProviderFactory) ValidateCreateCloudaccountData(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential) (cloudprovider.SCloudaccount, error) { 43 output := cloudprovider.SCloudaccount{} 44 if len(input.AccessKeyId) == 0 { 45 return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id") 46 } 47 if len(input.AccessKeySecret) == 0 { 48 return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret") 49 } 50 if len(input.Endpoint) == 0 { 51 return output, errors.Wrap(httperrors.ErrMissingParameter, "endpoint") 52 } 53 output.Account = input.AccessKeyId 54 output.Secret = input.AccessKeySecret 55 output.AccessUrl = input.Endpoint 56 return output, nil 57 } 58 59 func (self *SObjectStoreProviderFactory) ValidateUpdateCloudaccountCredential(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential, cloudaccount string) (cloudprovider.SCloudaccount, error) { 60 output := cloudprovider.SCloudaccount{} 61 if len(input.AccessKeyId) == 0 { 62 return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id") 63 } 64 if len(input.AccessKeySecret) == 0 { 65 return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret") 66 } 67 output = cloudprovider.SCloudaccount{ 68 Account: input.AccessKeyId, 69 Secret: input.AccessKeySecret, 70 } 71 return output, nil 72 } 73 74 func (self *SObjectStoreProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) { 75 client, err := objectstore.NewObjectStoreClient( 76 objectstore.NewObjectStoreClientConfig( 77 cfg.URL, cfg.Account, cfg.Secret, 78 ).CloudproviderConfig(cfg), 79 ) 80 if err != nil { 81 return nil, err 82 } 83 return NewObjectStoreProvider(self, client, []string{ 84 string(cloudprovider.ACLPrivate), 85 }), nil 86 } 87 88 func (self *SObjectStoreProviderFactory) GetClientRC(info cloudprovider.SProviderInfo) (map[string]string, error) { 89 return map[string]string{ 90 "S3_ACCESS_KEY": info.Account, 91 "S3_SECRET": info.Secret, 92 "S3_ACCESS_URL": info.Url, 93 "S3_BACKEND": api.CLOUD_PROVIDER_GENERICS3, 94 }, nil 95 } 96 97 func init() { 98 factory := SObjectStoreProviderFactory{} 99 cloudprovider.RegisterFactory(&factory) 100 } 101 102 type SObjectStoreProvider struct { 103 cloudprovider.SBaseProvider 104 client objectstore.IBucketProvider 105 supportedAcls []string 106 } 107 108 func NewObjectStoreProvider(factory cloudprovider.ICloudProviderFactory, client objectstore.IBucketProvider, acls []string) *SObjectStoreProvider { 109 return &SObjectStoreProvider{ 110 SBaseProvider: cloudprovider.NewBaseProvider(factory), 111 client: client, 112 supportedAcls: acls, 113 } 114 } 115 116 func (self *SObjectStoreProvider) GetIRegions() []cloudprovider.ICloudRegion { 117 return nil 118 } 119 120 func (self *SObjectStoreProvider) GetIRegionById(id string) (cloudprovider.ICloudRegion, error) { 121 return nil, cloudprovider.ErrNotSupported 122 } 123 124 func (self *SObjectStoreProvider) GetBalance() (float64, string, error) { 125 return 0.0, api.CLOUD_PROVIDER_HEALTH_NORMAL, cloudprovider.ErrNotSupported 126 } 127 128 func (self *SObjectStoreProvider) GetOnPremiseIRegion() (cloudprovider.ICloudRegion, error) { 129 return self.client, nil 130 } 131 132 func (self *SObjectStoreProvider) GetIProjects() ([]cloudprovider.ICloudProject, error) { 133 return nil, cloudprovider.ErrNotSupported 134 } 135 136 func (self *SObjectStoreProvider) GetSysInfo() (jsonutils.JSONObject, error) { 137 return self.client.About(), nil 138 } 139 140 func (self *SObjectStoreProvider) GetVersion() string { 141 return self.client.GetVersion() 142 } 143 144 func (self *SObjectStoreProvider) GetSubAccounts() ([]cloudprovider.SSubAccount, error) { 145 return self.client.GetSubAccounts() 146 } 147 148 func (self *SObjectStoreProvider) GetAccountId() string { 149 return self.client.GetAccountId() 150 } 151 152 func (self *SObjectStoreProvider) GetStorageClasses(regionId string) []string { 153 return []string{} 154 } 155 156 func (self *SObjectStoreProvider) GetBucketCannedAcls(regionId string) []string { 157 return self.supportedAcls 158 } 159 160 func (self *SObjectStoreProvider) GetObjectCannedAcls(regionId string) []string { 161 return self.supportedAcls 162 } 163 164 func (self *SObjectStoreProvider) GetCapabilities() []string { 165 return self.client.GetCapabilities() 166 }