mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-08-29 03:51:54 +08:00
objectstore: use ObjectStoreClientConfig
This commit is contained in:
+15
-3
@@ -94,11 +94,23 @@ func newClient(options *BaseOptions) (cloudprovider.ICloudRegion, error) {
|
||||
}
|
||||
|
||||
if options.Backend == api.CLOUD_PROVIDER_CEPH {
|
||||
return ceph.NewCephRados("", "", options.AccessUrl, options.AccessKey, options.Secret, options.Debug)
|
||||
return ceph.NewCephRados(
|
||||
objectstore.NewObjectStoreClientConfig(
|
||||
options.AccessUrl, options.AccessKey, options.Secret,
|
||||
).Debug(options.Debug),
|
||||
)
|
||||
} else if options.Backend == api.CLOUD_PROVIDER_XSKY {
|
||||
return xsky.NewXskyClient("", "", options.AccessUrl, options.AccessKey, options.Secret, options.Debug)
|
||||
return xsky.NewXskyClient(
|
||||
objectstore.NewObjectStoreClientConfig(
|
||||
options.AccessUrl, options.AccessKey, options.Secret,
|
||||
).Debug(options.Debug),
|
||||
)
|
||||
}
|
||||
return objectstore.NewObjectStoreClient("", "", options.AccessUrl, options.AccessKey, options.Secret, options.Debug)
|
||||
return objectstore.NewObjectStoreClient(
|
||||
objectstore.NewObjectStoreClientConfig(
|
||||
options.AccessUrl, options.AccessKey, options.Secret,
|
||||
).Debug(options.Debug),
|
||||
)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -38,12 +38,18 @@ type SCephRadosClient struct {
|
||||
userInfo *SUserInfo
|
||||
}
|
||||
|
||||
func NewCephRados(providerId string, providerName string, endpoint string, accessKey string, secret string, isDebug bool) (*SCephRadosClient, error) {
|
||||
s3store, err := objectstore.NewObjectStoreClientAndFetch(providerId, providerName, endpoint, accessKey, secret, isDebug, false)
|
||||
func NewCephRados(cfg *objectstore.ObjectStoreClientConfig) (*SCephRadosClient, error) {
|
||||
s3store, err := objectstore.NewObjectStoreClientAndFetch(cfg, false)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "NewObjectStoreClient")
|
||||
}
|
||||
adminApi := newCephAdminApi(accessKey, secret, endpoint, isDebug, "")
|
||||
adminApi := newCephAdminApi(
|
||||
cfg.GetAccessKey(),
|
||||
cfg.GetAccessSecret(),
|
||||
cfg.GetEndpoint(),
|
||||
cfg.GetDebug(),
|
||||
"",
|
||||
)
|
||||
|
||||
client := SCephRadosClient{
|
||||
SObjectStoreClient: s3store,
|
||||
@@ -75,7 +81,7 @@ func NewCephRados(providerId string, providerName string, endpoint string, acces
|
||||
log.Errorf("adminApi.GetUserInfo fail: %s", err)
|
||||
}
|
||||
}
|
||||
if isDebug {
|
||||
if cfg.GetDebug() {
|
||||
log.Debugf("%#v %#v %#v", userQuota, bucketQuota, userInfo)
|
||||
}
|
||||
client.userQuota = userQuota
|
||||
|
||||
@@ -17,6 +17,7 @@ package provider
|
||||
import (
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
"yunion.io/x/onecloud/pkg/multicloud/objectstore"
|
||||
"yunion.io/x/onecloud/pkg/multicloud/objectstore/ceph"
|
||||
s3provider "yunion.io/x/onecloud/pkg/multicloud/objectstore/provider"
|
||||
)
|
||||
@@ -34,7 +35,11 @@ func (self *SCephRadosProviderFactory) GetName() string {
|
||||
}
|
||||
|
||||
func (self *SCephRadosProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
|
||||
client, err := ceph.NewCephRados(cfg.Id, cfg.Name, cfg.URL, cfg.Account, cfg.Secret, false)
|
||||
client, err := ceph.NewCephRados(
|
||||
objectstore.NewObjectStoreClientConfig(
|
||||
cfg.URL, cfg.Account, cfg.Secret,
|
||||
).CloudproviderConfig(cfg),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -31,42 +31,80 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/util/httputils"
|
||||
)
|
||||
|
||||
type ObjectStoreClientConfig struct {
|
||||
cpcfg cloudprovider.ProviderConfig
|
||||
|
||||
endpoint string
|
||||
accessKey string
|
||||
accessSecret string
|
||||
|
||||
debug bool
|
||||
}
|
||||
|
||||
func NewObjectStoreClientConfig(endpoint, accessKey, accessSecret string) *ObjectStoreClientConfig {
|
||||
cfg := &ObjectStoreClientConfig{
|
||||
endpoint: endpoint,
|
||||
accessKey: accessKey,
|
||||
accessSecret: accessSecret,
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
func (cfg *ObjectStoreClientConfig) CloudproviderConfig(cpcfg cloudprovider.ProviderConfig) *ObjectStoreClientConfig {
|
||||
cfg.cpcfg = cpcfg
|
||||
return cfg
|
||||
}
|
||||
|
||||
func (cfg *ObjectStoreClientConfig) Debug(debug bool) *ObjectStoreClientConfig {
|
||||
cfg.debug = debug
|
||||
return cfg
|
||||
}
|
||||
|
||||
func (cfg *ObjectStoreClientConfig) GetCloudproviderConfig() cloudprovider.ProviderConfig {
|
||||
return cfg.cpcfg
|
||||
}
|
||||
|
||||
func (cfg *ObjectStoreClientConfig) GetEndpoint() string {
|
||||
return cfg.endpoint
|
||||
}
|
||||
|
||||
func (cfg *ObjectStoreClientConfig) GetAccessKey() string {
|
||||
return cfg.accessKey
|
||||
}
|
||||
|
||||
func (cfg *ObjectStoreClientConfig) GetAccessSecret() string {
|
||||
return cfg.accessSecret
|
||||
}
|
||||
|
||||
func (cfg *ObjectStoreClientConfig) GetDebug() bool {
|
||||
return cfg.debug
|
||||
}
|
||||
|
||||
type SObjectStoreClient struct {
|
||||
object.SObject
|
||||
|
||||
*ObjectStoreClientConfig
|
||||
|
||||
cloudprovider.SFakeOnPremiseRegion
|
||||
multicloud.SRegion
|
||||
|
||||
providerId string
|
||||
providerName string
|
||||
endpoint string
|
||||
accessKey string
|
||||
secret string
|
||||
|
||||
ownerId string
|
||||
ownerName string
|
||||
|
||||
iBuckets []cloudprovider.ICloudBucket
|
||||
|
||||
client *s3cli.Client
|
||||
|
||||
Debug bool
|
||||
}
|
||||
|
||||
func NewObjectStoreClient(providerId string, providerName string, endpoint string, accessKey string, secret string, isDebug bool) (*SObjectStoreClient, error) {
|
||||
return NewObjectStoreClientAndFetch(providerId, providerName, endpoint, accessKey, secret, isDebug, true)
|
||||
func NewObjectStoreClient(cfg *ObjectStoreClientConfig) (*SObjectStoreClient, error) {
|
||||
return NewObjectStoreClientAndFetch(cfg, true)
|
||||
}
|
||||
|
||||
func NewObjectStoreClientAndFetch(providerId string, providerName string, endpoint string, accessKey string, secret string, isDebug bool, doFetch bool) (*SObjectStoreClient, error) {
|
||||
func NewObjectStoreClientAndFetch(cfg *ObjectStoreClientConfig, doFetch bool) (*SObjectStoreClient, error) {
|
||||
client := SObjectStoreClient{
|
||||
providerId: providerId,
|
||||
providerName: providerName,
|
||||
endpoint: endpoint,
|
||||
accessKey: accessKey,
|
||||
secret: secret,
|
||||
Debug: isDebug,
|
||||
ObjectStoreClientConfig: cfg,
|
||||
}
|
||||
parts, err := url.Parse(endpoint)
|
||||
parts, err := url.Parse(cfg.endpoint)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "url.Parse endpoint")
|
||||
}
|
||||
@@ -74,7 +112,13 @@ func NewObjectStoreClientAndFetch(providerId string, providerName string, endpoi
|
||||
if parts.Scheme == "https" {
|
||||
useSsl = true
|
||||
}
|
||||
cli, err := s3cli.New(parts.Host, accessKey, secret, useSsl, client.Debug)
|
||||
cli, err := s3cli.New(
|
||||
parts.Host,
|
||||
client.accessKey,
|
||||
client.accessSecret,
|
||||
useSsl,
|
||||
client.debug,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "minio.New")
|
||||
}
|
||||
@@ -85,7 +129,7 @@ func NewObjectStoreClientAndFetch(providerId string, providerName string, endpoi
|
||||
client.client = cli
|
||||
client.SetVirtualObject(&client)
|
||||
|
||||
if isDebug {
|
||||
if client.debug {
|
||||
cli.TraceOn(os.Stderr)
|
||||
}
|
||||
|
||||
@@ -102,7 +146,7 @@ func NewObjectStoreClientAndFetch(providerId string, providerName string, endpoi
|
||||
func (cli *SObjectStoreClient) GetSubAccounts() ([]cloudprovider.SSubAccount, error) {
|
||||
subAccount := cloudprovider.SSubAccount{
|
||||
Account: cli.accessKey,
|
||||
Name: cli.providerName,
|
||||
Name: cli.cpcfg.Name,
|
||||
HealthStatus: api.CLOUD_PROVIDER_HEALTH_NORMAL,
|
||||
}
|
||||
return []cloudprovider.SSubAccount{subAccount}, nil
|
||||
@@ -158,7 +202,7 @@ func (cli *SObjectStoreClient) S3Client() *s3cli.Client {
|
||||
func (cli *SObjectStoreClient) GetClientRC() map[string]string {
|
||||
return map[string]string{
|
||||
"S3_ACCESS_KEY": cli.accessKey,
|
||||
"S3_SECRET": cli.secret,
|
||||
"S3_SECRET": cli.accessSecret,
|
||||
"S3_ACCESS_URL": cli.endpoint,
|
||||
"S3_BACKEND": api.CLOUD_PROVIDER_GENERICS3,
|
||||
}
|
||||
|
||||
@@ -72,9 +72,10 @@ func (self *SObjectStoreProviderFactory) ValidateUpdateCloudaccountCredential(ct
|
||||
}
|
||||
|
||||
func (self *SObjectStoreProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
|
||||
dbg := false
|
||||
client, err := objectstore.NewObjectStoreClient(
|
||||
cfg.Id, cfg.Name, cfg.URL, cfg.Account, cfg.Secret, dbg,
|
||||
objectstore.NewObjectStoreClientConfig(
|
||||
cfg.URL, cfg.Account, cfg.Secret,
|
||||
).CloudproviderConfig(cfg),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -17,6 +17,7 @@ package provider
|
||||
import (
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
"yunion.io/x/onecloud/pkg/multicloud/objectstore"
|
||||
s3provider "yunion.io/x/onecloud/pkg/multicloud/objectstore/provider"
|
||||
"yunion.io/x/onecloud/pkg/multicloud/objectstore/xsky"
|
||||
)
|
||||
@@ -34,7 +35,11 @@ func (self *SXskyProviderFactory) GetName() string {
|
||||
}
|
||||
|
||||
func (self *SXskyProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
|
||||
client, err := xsky.NewXskyClient(cfg.Id, cfg.Name, cfg.URL, cfg.Account, cfg.Secret, false)
|
||||
client, err := xsky.NewXskyClient(
|
||||
objectstore.NewObjectStoreClientConfig(
|
||||
cfg.URL, cfg.Account, cfg.Secret,
|
||||
).CloudproviderConfig(cfg),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -42,7 +47,11 @@ func (self *SXskyProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig)
|
||||
}
|
||||
|
||||
func (self *SXskyProviderFactory) GetClientRC(url, account, secret string) (map[string]string, error) {
|
||||
client, err := xsky.NewXskyClient("", "", url, account, secret, false)
|
||||
client, err := xsky.NewXskyClient(
|
||||
objectstore.NewObjectStoreClientConfig(
|
||||
url, account, secret,
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -46,9 +46,14 @@ func parseAccount(account string) (user string, accessKey string) {
|
||||
return
|
||||
}
|
||||
|
||||
func NewXskyClient(providerId string, providerName string, endpoint string, account string, password string, isDebug bool) (*SXskyClient, error) {
|
||||
usrname, accessKey := parseAccount(account)
|
||||
adminApi := newXskyAdminApi(usrname, password, endpoint, isDebug)
|
||||
func NewXskyClient(cfg *objectstore.ObjectStoreClientConfig) (*SXskyClient, error) {
|
||||
usrname, accessKey := parseAccount(cfg.GetEndpoint())
|
||||
adminApi := newXskyAdminApi(
|
||||
usrname,
|
||||
cfg.GetAccessSecret(),
|
||||
cfg.GetEndpoint(),
|
||||
cfg.GetDebug(),
|
||||
)
|
||||
gwEp, err := adminApi.getS3GatewayEndpoint(context.Background())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "adminApi.getS3GatewayIP")
|
||||
@@ -68,7 +73,12 @@ func NewXskyClient(providerId string, providerName string, endpoint string, acco
|
||||
}
|
||||
}
|
||||
|
||||
s3store, err := objectstore.NewObjectStoreClientAndFetch(providerId, providerName, gwEp, accessKey, key.SecretKey, isDebug, false)
|
||||
s3store, err := objectstore.NewObjectStoreClientAndFetch(
|
||||
objectstore.NewObjectStoreClientConfig(
|
||||
gwEp, accessKey, key.SecretKey,
|
||||
).Debug(cfg.GetDebug()).CloudproviderConfig(cfg.GetCloudproviderConfig()),
|
||||
false,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "NewObjectStoreClient")
|
||||
}
|
||||
@@ -80,7 +90,7 @@ func NewXskyClient(providerId string, providerName string, endpoint string, acco
|
||||
}
|
||||
|
||||
if len(accessKey) > 0 {
|
||||
client.initAccount = account
|
||||
client.initAccount = cfg.GetAccessKey()
|
||||
}
|
||||
|
||||
client.SetVirtualObject(&client)
|
||||
|
||||
Reference in New Issue
Block a user