aliyun clientcfg

This commit is contained in:
Yousong Zhou
2020-03-18 12:22:56 +08:00
parent 55eb57fd60
commit 361cbd7454
14 changed files with 72 additions and 56 deletions
+5 -1
View File
@@ -84,7 +84,11 @@ func newClient(options *BaseOptions) (*aliyun.SRegion, error) {
return nil, fmt.Errorf("Missing secret")
}
cli, err := aliyun.NewAliyunClient("", "", options.AccessKey, options.Secret, options.Debug)
cli, err := aliyun.NewAliyunClient(
aliyun.NewAliyunClientConfig(
options.AccessKey, options.Secret,
).Debug(options.Debug),
)
if err != nil {
return nil, err
}
+39 -28
View File
@@ -53,28 +53,48 @@ const (
ALIYUN_API_VERION_RDS = "2014-08-15"
)
type SAliyunClient struct {
providerId string
providerName string
type AliyunClientConfig struct {
cpcfg cloudprovider.ProviderConfig
accessKey string
secret string
accessSecret string
debug bool
}
func NewAliyunClientConfig(accessKey, accessSecret string) *AliyunClientConfig {
cfg := &AliyunClientConfig{
accessKey: accessKey,
accessSecret: accessSecret,
}
return cfg
}
func (cfg *AliyunClientConfig) CloudproviderConfig(cpcfg cloudprovider.ProviderConfig) *AliyunClientConfig {
cfg.cpcfg = cpcfg
return cfg
}
func (cfg *AliyunClientConfig) Debug(debug bool) *AliyunClientConfig {
cfg.debug = debug
return cfg
}
func (cfg AliyunClientConfig) Copy() AliyunClientConfig {
return cfg
}
type SAliyunClient struct {
*AliyunClientConfig
ownerId string
ownerName string
iregions []cloudprovider.ICloudRegion
iBuckets []cloudprovider.ICloudBucket
Debug bool
}
func NewAliyunClient(providerId string, providerName string, accessKey string, secret string, isDebug bool) (*SAliyunClient, error) {
func NewAliyunClient(cfg *AliyunClientConfig) (*SAliyunClient, error) {
client := SAliyunClient{
providerId: providerId,
providerName: providerName,
accessKey: accessKey,
secret: secret,
Debug: isDebug,
AliyunClientConfig: cfg,
}
err := client.fetchRegions()
if err != nil {
@@ -84,7 +104,7 @@ func NewAliyunClient(providerId string, providerName string, accessKey string, s
if err != nil {
return nil, errors.Wrap(err, "fetchBuckets")
}
if client.Debug {
if client.debug {
log.Debugf("ClientID: %s ClientName: %s", client.ownerId, client.ownerName)
}
return &client, nil
@@ -165,18 +185,9 @@ func _jsonRequest(client *sdk.Client, domain string, version string, apiName str
return body, nil
}
func (self *SAliyunClient) UpdateAccount(accessKey, secret string) error {
if self.accessKey != accessKey || self.secret != secret {
self.accessKey = accessKey
self.secret = secret
return self.fetchRegions()
} else {
return nil
}
}
func (self *SAliyunClient) getDefaultClient() (*sdk.Client, error) {
return sdk.NewClientWithAccessKey(ALIYUN_DEFAULT_REGION, self.accessKey, self.secret)
return sdk.NewClientWithAccessKey(ALIYUN_DEFAULT_REGION,
self.accessKey, self.accessSecret)
}
func (self *SAliyunClient) ecsRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
@@ -184,7 +195,7 @@ func (self *SAliyunClient) ecsRequest(apiName string, params map[string]string)
if err != nil {
return nil, err
}
return jsonRequest(cli, "ecs.aliyuncs.com", ALIYUN_API_VERSION, apiName, params, self.Debug)
return jsonRequest(cli, "ecs.aliyuncs.com", ALIYUN_API_VERSION, apiName, params, self.debug)
}
func (self *SAliyunClient) trialRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
@@ -192,7 +203,7 @@ func (self *SAliyunClient) trialRequest(apiName string, params map[string]string
if err != nil {
return nil, err
}
return jsonRequest(cli, "actiontrail.cn-hangzhou.aliyuncs.com", ALIYUN_API_VERSION_TRIAL, apiName, params, self.Debug)
return jsonRequest(cli, "actiontrail.cn-hangzhou.aliyuncs.com", ALIYUN_API_VERSION_TRIAL, apiName, params, self.debug)
}
func (self *SAliyunClient) fetchRegions() error {
@@ -241,7 +252,7 @@ func (client *SAliyunClient) getOssClient(regionId string) (*oss.Client, error)
oss.HTTPClient(httputils.GetAdaptiveTimeoutClient()),
}
ep := getOSSExternalDomain(regionId)
cli, err := oss.New(ep, client.accessKey, client.secret, cliOpts...)
cli, err := oss.New(ep, client.accessKey, client.accessSecret, cliOpts...)
if err != nil {
return nil, errors.Wrap(err, "oss.New")
}
@@ -320,7 +331,7 @@ func (self *SAliyunClient) GetSubAccounts() ([]cloudprovider.SSubAccount, error)
return nil, err
}
subAccount := cloudprovider.SSubAccount{}
subAccount.Name = self.providerName
subAccount.Name = self.cpcfg.Name
subAccount.Account = self.accessKey
subAccount.HealthStatus = api.CLOUD_PROVIDER_HEALTH_NORMAL
return []cloudprovider.SSubAccount{subAccount}, nil
+2 -2
View File
@@ -291,7 +291,7 @@ func (b *SBucket) UploadPart(ctx context.Context, key string, uploadId string, p
if err != nil {
return "", errors.Wrap(err, "bucket.UploadPart")
}
if b.region.client.Debug {
if b.region.client.debug {
log.Debugf("upload part key:%s uploadId:%s partIndex:%d etag:%s", key, uploadId, partIndex, part.ETag)
}
return part.ETag, nil
@@ -322,7 +322,7 @@ func (b *SBucket) CompleteMultipartUpload(ctx context.Context, key string, uploa
if err != nil {
return errors.Wrap(err, "bucket.CompleteMultipartUpload")
}
if b.region.client.Debug {
if b.region.client.debug {
log.Debugf("CompleteMultipartUpload bucket:%s key:%s etag:%s location:%s", result.Bucket, result.Key, result.ETag, result.Location)
}
return nil
+1 -1
View File
@@ -29,7 +29,7 @@ func (self *SAliyunClient) businessRequest(apiName string, params map[string]str
if err != nil {
return nil, err
}
return jsonRequest(cli, "business.aliyuncs.com", ALIYUN_BSS_API_VERSION, apiName, params, self.Debug)
return jsonRequest(cli, "business.aliyuncs.com", ALIYUN_BSS_API_VERSION, apiName, params, self.debug)
}
type SAccountBalance struct {
+3 -3
View File
@@ -88,15 +88,15 @@ func (self *SHost) GetIVMById(gid string) (cloudprovider.ICloudVM, error) {
}
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) IsEmulated() bool {
+1 -1
View File
@@ -31,7 +31,7 @@ func (r *SRegion) metricsRequest(action string, params map[string]string) (jsonu
if err != nil {
return nil, errors.Wrap(err, "r.getSdkClient")
}
return jsonRequest(client, "metrics.aliyuncs.com", ALIYUN_API_VERSION_METRICS, action, params, r.client.Debug)
return jsonRequest(client, "metrics.aliyuncs.com", ALIYUN_API_VERSION_METRICS, action, params, r.client.debug)
}
type SResourceLabel struct {
+1 -1
View File
@@ -207,7 +207,7 @@ func (self *SRegion) GetNatGateways(vpcId string, natGwId string, offset, limit
return nil, 0, err
}
if self.client.Debug {
if self.client.debug {
log.Debugf("%s", body.PrettyString())
}
+2 -2
View File
@@ -94,7 +94,7 @@ func (self *SRegion) GetSNATEntries(tableId string, offset, limit int) ([]SSNATT
return nil, 0, err
}
if self.client.Debug {
if self.client.debug {
log.Debugf("%s", body.PrettyString())
}
@@ -120,7 +120,7 @@ func (self *SRegion) GetSNATEntry(tableID, SNATEntryID string) (SSNATTableEntry,
return SSNATTableEntry{}, err
}
if self.client.Debug {
if self.client.debug {
log.Debugf("%s", body.PrettyString())
}
+4 -1
View File
@@ -73,7 +73,10 @@ func (self *SAliyunProviderFactory) ValidateUpdateCloudaccountCredential(ctx con
func (self *SAliyunProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
client, err := aliyun.NewAliyunClient(
cfg.Id, cfg.Name, cfg.Account, cfg.Secret, true,
aliyun.NewAliyunClientConfig(
cfg.Account,
cfg.Secret,
).CloudproviderConfig(cfg),
)
if err != nil {
return nil, err
+1 -1
View File
@@ -28,7 +28,7 @@ func (self *SAliyunClient) ramRequest(apiName string, params map[string]string)
if err != nil {
return nil, err
}
return jsonRequest(cli, "ram.aliyuncs.com", ALIYUN_RAM_API_VERSION, apiName, params, self.Debug)
return jsonRequest(cli, "ram.aliyuncs.com", ALIYUN_RAM_API_VERSION, apiName, params, self.debug)
}
type SRole struct {
+6 -8
View File
@@ -41,8 +41,6 @@ type SRegion struct {
sdkClient *sdk.Client
ossClient *oss.Client
Debug bool
RegionId string
LocalName string
@@ -75,7 +73,7 @@ func (self *SRegion) GetMetadata() *jsonutils.JSONDict {
func (self *SRegion) getSdkClient() (*sdk.Client, error) {
if self.sdkClient == nil {
cli, err := sdk.NewClientWithAccessKey(self.RegionId, self.client.accessKey, self.client.secret)
cli, err := sdk.NewClientWithAccessKey(self.RegionId, self.client.accessKey, self.client.accessSecret)
if err != nil {
return nil, err
}
@@ -108,7 +106,7 @@ func (self *SRegion) ecsRequest(apiName string, params map[string]string) (jsonu
if err != nil {
return nil, err
}
return jsonRequest(client, "ecs.aliyuncs.com", ALIYUN_API_VERSION, apiName, params, self.client.Debug)
return jsonRequest(client, "ecs.aliyuncs.com", ALIYUN_API_VERSION, apiName, params, self.client.debug)
}
func (self *SRegion) rdsRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
@@ -116,7 +114,7 @@ func (self *SRegion) rdsRequest(apiName string, params map[string]string) (jsonu
if err != nil {
return nil, err
}
return jsonRequest(client, "rds.aliyuncs.com", ALIYUN_API_VERION_RDS, apiName, params, self.client.Debug)
return jsonRequest(client, "rds.aliyuncs.com", ALIYUN_API_VERION_RDS, apiName, params, self.client.debug)
}
func (self *SRegion) vpcRequest(action string, params map[string]string) (jsonutils.JSONObject, error) {
@@ -124,7 +122,7 @@ func (self *SRegion) vpcRequest(action string, params map[string]string) (jsonut
if err != nil {
return nil, err
}
return jsonRequest(client, "vpc.aliyuncs.com", ALIYUN_API_VERSION_VPC, action, params, self.client.Debug)
return jsonRequest(client, "vpc.aliyuncs.com", ALIYUN_API_VERSION_VPC, action, params, self.client.debug)
}
func (self *SRegion) kvsRequest(action string, params map[string]string) (jsonutils.JSONObject, error) {
@@ -132,7 +130,7 @@ func (self *SRegion) kvsRequest(action string, params map[string]string) (jsonut
if err != nil {
return nil, err
}
return jsonRequest(client, "r-kvstore.aliyuncs.com", ALIYUN_API_VERSION_KVS, action, params, self.client.Debug)
return jsonRequest(client, "r-kvstore.aliyuncs.com", ALIYUN_API_VERSION_KVS, action, params, self.client.debug)
}
type LBRegion struct {
@@ -182,7 +180,7 @@ func (self *SRegion) lbRequest(apiName string, params map[string]string) (jsonut
}
func (self *SRegion) _lbRequest(client *sdk.Client, apiName string, domain string, params map[string]string) (jsonutils.JSONObject, error) {
return jsonRequest(client, domain, ALIYUN_API_VERSION_LB, apiName, params, self.Debug)
return jsonRequest(client, domain, ALIYUN_API_VERSION_LB, apiName, params, self.client.debug)
}
/////////////////////////////////////////////////////////////////////////////
+3 -3
View File
@@ -49,15 +49,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(), self.storageType)
return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId(), self.storageType)
}
func (self *SStorage) 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 *SStorage) 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 *SStorage) IsEmulated() bool {
+3 -3
View File
@@ -46,11 +46,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 {
@@ -62,7 +62,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 {
+1 -1
View File
@@ -246,7 +246,7 @@ func (self *SRegion) GetVSwitchAttributes(idstr string) (*SVSwitch, error) {
log.Errorf("DescribeVSwitchAttributes fail %s", err)
return nil, err
}
if self.client.Debug {
if self.client.debug {
log.Debugf("%s", body.PrettyString())
}
switches := SVSwitch{}