qcloud: use QcloudClientConfig

This commit is contained in:
Yousong Zhou
2020-03-18 19:19:52 +08:00
parent 0faa2bfb9c
commit 70b78835d3
9 changed files with 87 additions and 61 deletions
+6 -5
View File
@@ -85,11 +85,12 @@ func newClient(options *BaseOptions) (*qcloud.SRegion, error) {
return nil, fmt.Errorf("Missing SecretID")
}
if cli, err := qcloud.NewQcloudClient("", "",
options.SecretID,
options.SecretKey,
options.AppID,
options.Debug); err != nil {
if cli, err := qcloud.NewQcloudClient(
qcloud.NewQcloudClientConfig(
options.SecretID,
options.SecretKey,
).AppId(options.AppID).Debug(options.Debug),
); err != nil {
return nil, err
} else if region := cli.GetRegion(options.RegionId); region == nil {
return nil, fmt.Errorf("No such region %s", options.RegionId)
+3 -3
View File
@@ -135,7 +135,7 @@ func (b *SBucket) SetAcl(aclStr cloudprovider.TBucketACLType) error {
}
func (b *SBucket) getFullName() string {
return fmt.Sprintf("%s-%s", b.Name, b.region.client.AppID)
return fmt.Sprintf("%s-%s", b.Name, b.region.client.appId)
}
func (b *SBucket) getBucketUrlHost() string {
@@ -393,8 +393,8 @@ func (b *SBucket) GetTempUrl(method string, key string, expire time.Duration) (s
return "", errors.Wrap(err, "GetCosClient")
}
url, err := coscli.Object.GetPresignedURL(context.Background(), method, key,
b.region.client.SecretID,
b.region.client.SecretKey,
b.region.client.secretId,
b.region.client.secretKey,
expire, nil)
if err != nil {
return "", errors.Wrap(err, "coscli.Object.GetPresignedURL")
+3 -3
View File
@@ -37,15 +37,15 @@ func (self *SHost) GetMetadata() *jsonutils.JSONDict {
}
func (self *SHost) GetId() string {
return fmt.Sprintf("%s-%s", self.zone.region.client.providerId, self.zone.GetId())
return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId())
}
func (self *SHost) GetName() string {
return fmt.Sprintf("%s-%s", self.zone.region.client.providerName, self.zone.GetId())
return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Name, self.zone.GetId())
}
func (self *SHost) GetGlobalId() string {
return fmt.Sprintf("%s-%s", self.zone.region.client.providerId, self.zone.GetId())
return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId())
}
func (self *SHost) GetInstanceById(instanceId string) (*SInstance, error) {
+3 -3
View File
@@ -35,15 +35,15 @@ func (self *SLocalStorage) GetMetadata() *jsonutils.JSONDict {
}
func (self *SLocalStorage) GetId() string {
return fmt.Sprintf("%s-%s-%s", self.zone.region.client.providerId, self.zone.GetId(), self.storageType)
return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId(), self.storageType)
}
func (self *SLocalStorage) GetName() string {
return fmt.Sprintf("%s-%s-%s", self.zone.region.client.providerName, self.zone.GetId(), self.storageType)
return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Name, self.zone.GetId(), self.storageType)
}
func (self *SLocalStorage) GetGlobalId() string {
return fmt.Sprintf("%s-%s-%s", self.zone.region.client.providerId, self.zone.GetGlobalId(), self.storageType)
return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetGlobalId(), self.storageType)
}
func (self *SLocalStorage) IsEmulated() bool {
+1 -1
View File
@@ -53,7 +53,7 @@ func (r *SRegion) metricsRequest(action string, params map[string]string) (jsonu
if err != nil {
return nil, err
}
return monitorRequest(cli, action, params, client.Debug)
return monitorRequest(cli, action, params, client.debug)
}
func (r *SRegion) GetMonitorData(name string, ns string, since time.Time, until time.Time,
+5 -1
View File
@@ -105,7 +105,11 @@ func (self *SQcloudProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig
secretId = tmp[0]
appId = tmp[1]
}
client, err := qcloud.NewQcloudClient(cfg.Id, cfg.Name, secretId, cfg.Secret, appId, false)
client, err := qcloud.NewQcloudClient(
qcloud.NewQcloudClientConfig(
secretId, cfg.Secret,
).AppId(appId).CloudproviderConfig(cfg),
)
if err != nil {
return nil, err
}
+58 -37
View File
@@ -52,30 +52,51 @@ const (
QCLOUD_AUDIT_API_VERSION = "2019-03-19"
)
type SQcloudClient struct {
providerId string
providerName string
AppID string
SecretID string
SecretKey string
type QcloudClientConfig struct {
cpcfg cloudprovider.ProviderConfig
secretId string
secretKey string
appId string
debug bool
}
func NewQcloudClientConfig(secretId, secretKey string) *QcloudClientConfig {
cfg := &QcloudClientConfig{
secretId: secretId,
secretKey: secretKey,
}
return cfg
}
func (cfg *QcloudClientConfig) CloudproviderConfig(cpcfg cloudprovider.ProviderConfig) *QcloudClientConfig {
cfg.cpcfg = cpcfg
return cfg
}
func (cfg *QcloudClientConfig) AppId(appId string) *QcloudClientConfig {
cfg.appId = appId
return cfg
}
func (cfg *QcloudClientConfig) Debug(debug bool) *QcloudClientConfig {
cfg.debug = debug
return cfg
}
type SQcloudClient struct {
*QcloudClientConfig
ownerId string
ownerName string
iregions []cloudprovider.ICloudRegion
ibuckets []cloudprovider.ICloudBucket
Debug bool
}
func NewQcloudClient(providerId string, providerName string, secretID string, secretKey string, appID string, isDebug bool) (*SQcloudClient, error) {
func NewQcloudClient(cfg *QcloudClientConfig) (*SQcloudClient, error) {
client := SQcloudClient{
providerId: providerId,
providerName: providerName,
SecretID: secretID,
SecretKey: secretKey,
AppID: appID,
Debug: isDebug,
QcloudClientConfig: cfg,
}
err := client.fetchRegions()
if err != nil {
@@ -89,7 +110,7 @@ func NewQcloudClient(providerId string, providerName string, secretID string, se
if err != nil {
return nil, errors.Wrap(err, "fetchBuckets")
}
if isDebug {
if client.debug {
log.Debugf("ownerID: %s ownerName: %s", client.ownerId, client.ownerName)
}
return &client, nil
@@ -404,7 +425,7 @@ func (client *SQcloudClient) GetRegions() []SRegion {
}
func (client *SQcloudClient) getDefaultClient() (*common.Client, error) {
return common.NewClientWithSecretId(client.SecretID, client.SecretKey, QCLOUD_DEFAULT_REGION)
return common.NewClientWithSecretId(client.secretId, client.secretKey, QCLOUD_DEFAULT_REGION)
}
func (client *SQcloudClient) vpcRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
@@ -412,7 +433,7 @@ func (client *SQcloudClient) vpcRequest(apiName string, params map[string]string
if err != nil {
return nil, err
}
return vpcRequest(cli, apiName, params, client.Debug)
return vpcRequest(cli, apiName, params, client.debug)
}
func (client *SQcloudClient) auditRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
@@ -420,7 +441,7 @@ func (client *SQcloudClient) auditRequest(apiName string, params map[string]stri
if err != nil {
return nil, err
}
return auditRequest(cli, apiName, params, client.Debug)
return auditRequest(cli, apiName, params, client.debug)
}
func (client *SQcloudClient) cbsRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
@@ -428,7 +449,7 @@ func (client *SQcloudClient) cbsRequest(apiName string, params map[string]string
if err != nil {
return nil, err
}
return cbsRequest(cli, apiName, params, client.Debug)
return cbsRequest(cli, apiName, params, client.debug)
}
func (client *SQcloudClient) accountRequestRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
@@ -436,7 +457,7 @@ func (client *SQcloudClient) accountRequestRequest(apiName string, params map[st
if err != nil {
return nil, err
}
return accountRequest(cli, apiName, params, client.Debug)
return accountRequest(cli, apiName, params, client.debug)
}
func (client *SQcloudClient) clbRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
@@ -444,7 +465,7 @@ func (client *SQcloudClient) clbRequest(apiName string, params map[string]string
if err != nil {
return nil, err
}
return clbRequest(cli, apiName, params, client.Debug)
return clbRequest(cli, apiName, params, client.debug)
}
func (client *SQcloudClient) lbRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
@@ -452,7 +473,7 @@ func (client *SQcloudClient) lbRequest(apiName string, params map[string]string)
if err != nil {
return nil, err
}
return lbRequest(cli, apiName, params, client.Debug)
return lbRequest(cli, apiName, params, client.debug)
}
func (client *SQcloudClient) wssRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
@@ -460,7 +481,7 @@ func (client *SQcloudClient) wssRequest(apiName string, params map[string]string
if err != nil {
return nil, err
}
return wssRequest(cli, apiName, params, client.Debug)
return wssRequest(cli, apiName, params, client.debug)
}
func (client *SQcloudClient) vpc2017Request(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
@@ -468,7 +489,7 @@ func (client *SQcloudClient) vpc2017Request(apiName string, params map[string]st
if err != nil {
return nil, err
}
return vpc2017Request(cli, apiName, params, client.Debug)
return vpc2017Request(cli, apiName, params, client.debug)
}
func (client *SQcloudClient) billingRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
@@ -476,7 +497,7 @@ func (client *SQcloudClient) billingRequest(apiName string, params map[string]st
if err != nil {
return nil, err
}
return billingRequest(cli, apiName, params, client.Debug)
return billingRequest(cli, apiName, params, client.debug)
}
func (client *SQcloudClient) jsonRequest(apiName string, params map[string]string, retry bool) (jsonutils.JSONObject, error) {
@@ -484,7 +505,7 @@ func (client *SQcloudClient) jsonRequest(apiName string, params map[string]strin
if err != nil {
return nil, err
}
return jsonRequest(cli, apiName, params, client.Debug, retry)
return jsonRequest(cli, apiName, params, client.debug, retry)
}
func (client *SQcloudClient) fetchRegions() error {
@@ -520,13 +541,13 @@ func (client *SQcloudClient) getCosClient(bucket *SBucket) (*cos.Client, error)
baseUrl,
&http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: client.SecretID,
SecretKey: client.SecretKey,
SecretID: client.secretId,
SecretKey: client.secretKey,
Transport: &debug.DebugRequestTransport{
RequestHeader: client.Debug,
RequestBody: client.Debug,
ResponseHeader: client.Debug,
ResponseBody: client.Debug,
RequestHeader: client.debug,
RequestBody: client.debug,
ResponseHeader: client.debug,
ResponseBody: client.debug,
},
},
},
@@ -614,11 +635,11 @@ func (client *SQcloudClient) GetSubAccounts() ([]cloudprovider.SSubAccount, erro
return nil, err
}
subAccount := cloudprovider.SSubAccount{}
subAccount.Name = client.providerName
subAccount.Account = client.SecretID
subAccount.Name = client.cpcfg.Name
subAccount.Account = client.secretId
subAccount.HealthStatus = api.CLOUD_PROVIDER_HEALTH_NORMAL
if len(client.AppID) > 0 {
subAccount.Account = fmt.Sprintf("%s/%s", client.SecretID, client.AppID)
if len(client.appId) > 0 {
subAccount.Account = fmt.Sprintf("%s/%s", client.secretId, client.appId)
}
return []cloudprovider.SSubAccount{subAccount}, nil
}
+3 -3
View File
@@ -37,15 +37,15 @@ func (self *SStorage) GetMetadata() *jsonutils.JSONDict {
}
func (self *SStorage) GetId() string {
return fmt.Sprintf("%s-%s-%s", self.zone.region.client.providerId, self.zone.GetId(), strings.ToLower(self.storageType))
return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId(), strings.ToLower(self.storageType))
}
func (self *SStorage) GetName() string {
return fmt.Sprintf("%s-%s-%s", self.zone.region.client.providerName, self.zone.GetId(), strings.ToLower(self.storageType))
return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Name, self.zone.GetId(), strings.ToLower(self.storageType))
}
func (self *SStorage) GetGlobalId() string {
return fmt.Sprintf("%s-%s-%s", self.zone.region.client.providerId, self.zone.GetGlobalId(), strings.ToLower(self.storageType))
return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetGlobalId(), strings.ToLower(self.storageType))
}
func (self *SStorage) IsEmulated() bool {
+5 -5
View File
@@ -44,11 +44,11 @@ func (self *SStoragecache) GetMetadata() *jsonutils.JSONDict {
}
func (self *SStoragecache) GetId() string {
return fmt.Sprintf("%s-%s", self.region.client.providerId, self.region.GetId())
return fmt.Sprintf("%s-%s", self.region.client.cpcfg.Id, self.region.GetId())
}
func (self *SStoragecache) GetName() string {
return fmt.Sprintf("%s-%s", self.region.client.providerName, self.region.GetId())
return fmt.Sprintf("%s-%s", self.region.client.cpcfg.Name, self.region.GetId())
}
func (self *SStoragecache) GetStatus() string {
@@ -60,7 +60,7 @@ func (self *SStoragecache) Refresh() error {
}
func (self *SStoragecache) GetGlobalId() string {
return fmt.Sprintf("%s-%s", self.region.client.providerId, self.region.GetGlobalId())
return fmt.Sprintf("%s-%s", self.region.client.cpcfg.Id, self.region.GetGlobalId())
}
func (self *SStoragecache) IsEmulated() bool {
@@ -153,8 +153,8 @@ func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.To
}
func (self *SRegion) getCosUrl(bucket, object string) string {
//signature := cosauth.NewSignature(self.client.AppID, bucket, self.client.SecretID, time.Now().Add(time.Minute*30).String(), time.Now().String(), "yunion", object).SignOnce(self.client.SecretKey)
return fmt.Sprintf("http://%s-%s.cos.%s.myqcloud.com/%s", bucket, self.client.AppID, self.Region, object)
//signature := cosauth.NewSignature(self.client.AppID, bucket, self.client.secretId, time.Now().Add(time.Minute*30).String(), time.Now().String(), "yunion", object).SignOnce(self.client.secretKey)
return fmt.Sprintf("http://%s-%s.cos.%s.myqcloud.com/%s", bucket, self.client.appId, self.Region, object)
}
func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, isForce bool) (string, error) {