ucloud: use UcloudClientConfig

This commit is contained in:
Yousong Zhou
2020-03-18 19:35:48 +08:00
parent e277ed65ad
commit bd228b2e5b
7 changed files with 55 additions and 27 deletions
+6 -5
View File
@@ -86,11 +86,12 @@ func newClient(options *BaseOptions) (*ucloud.SRegion, error) {
return nil, fmt.Errorf("Missing secret")
}
cli, err := ucloud.NewUcloudClient("", "",
options.AccessKey,
options.Secret,
options.ProjectId,
options.Debug)
cli, err := ucloud.NewUcloudClient(
ucloud.NewUcloudClientConfig(
options.AccessKey,
options.Secret,
).ProjectId(options.ProjectId).Debug(options.Debug),
)
if err != nil {
return nil, err
}
+1 -1
View File
@@ -174,7 +174,7 @@ func jsonRequest(client *SUcloudClient, params SParams) (jsonutils.JSONObject, e
UCLOUD_API_HOST,
nil,
BuildParams(params, client.accessKeySecret),
client.Debug)
client.debug)
if err == nil {
return parseUcloudResponse(params, resp)
+2 -2
View File
@@ -37,11 +37,11 @@ type SHost struct {
}
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 {
+5 -1
View File
@@ -84,7 +84,11 @@ func parseAccount(account string) (accessKey string, projectId string) {
func (self *SUcloudProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
accessKey, projectId := parseAccount(cfg.Account)
client, err := ucloud.NewUcloudClient(cfg.Id, cfg.Name, accessKey, cfg.Secret, projectId, false)
client, err := ucloud.NewUcloudClient(
ucloud.NewUcloudClientConfig(
accessKey, cfg.Secret,
).ProjectId(projectId).CloudproviderConfig(cfg),
)
if err != nil {
return nil, err
}
+3 -3
View File
@@ -30,15 +30,15 @@ type SStorage struct {
}
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) GetStatus() string {
+3 -3
View File
@@ -61,15 +61,15 @@ func GetBucketName(regionId string, imageId string) string {
}
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) 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) GetStatus() string {
+35 -12
View File
@@ -56,30 +56,53 @@ const (
UCLOUD_API_VERSION = "2019-02-28"
)
type SUcloudClient struct {
providerId string
providerName string
type UcloudClientConfig struct {
cpcfg cloudprovider.ProviderConfig
accessKeyId string
accessKeySecret string
projectId string
debug bool
}
func NewUcloudClientConfig(accessKeyId, accessKeySecret string) *UcloudClientConfig {
cfg := &UcloudClientConfig{
accessKeyId: accessKeyId,
accessKeySecret: accessKeySecret,
}
return cfg
}
func (cfg *UcloudClientConfig) CloudproviderConfig(cpcfg cloudprovider.ProviderConfig) *UcloudClientConfig {
cfg.cpcfg = cpcfg
return cfg
}
func (cfg *UcloudClientConfig) ProjectId(projectId string) *UcloudClientConfig {
cfg.projectId = projectId
return cfg
}
func (cfg *UcloudClientConfig) Debug(debug bool) *UcloudClientConfig {
cfg.debug = debug
return cfg
}
type SUcloudClient struct {
*UcloudClientConfig
iregions []cloudprovider.ICloudRegion
iBuckets []cloudprovider.ICloudBucket
httpClient *http.Client
Debug bool
}
// 进行资源操作时参数account 对应数据库cloudprovider表中的account字段,由accessKey和projectID两部分组成,通过"/"分割。
// 初次导入Subaccount时,参数account对应cloudaccounts表中的account字段,即accesskey。此时projectID为空,只能进行同步子账号(项目)、查询region列表等projectId无关的操作。
func NewUcloudClient(providerId string, providerName string, accessKey string, secret string, projectId string, isDebug bool) (*SUcloudClient, error) {
func NewUcloudClient(cfg *UcloudClientConfig) (*SUcloudClient, error) {
client := SUcloudClient{
providerId: providerId,
providerName: providerName,
accessKeyId: accessKey,
accessKeySecret: secret,
projectId: projectId,
Debug: isDebug,
UcloudClientConfig: cfg,
}
err := client.fetchRegions()
@@ -243,7 +266,7 @@ func (self *SUcloudClient) GetSubAccounts() ([]cloudprovider.SSubAccount, error)
subAccounts := make([]cloudprovider.SSubAccount, 0)
for _, project := range projects {
subAccount := cloudprovider.SSubAccount{}
subAccount.Name = fmt.Sprintf("%s-%s", self.providerName, project.ProjectName)
subAccount.Name = fmt.Sprintf("%s-%s", self.cpcfg.Name, project.ProjectName)
// ucloud账号ID中可能包含/。因此使用::作为分割符号
subAccount.Account = fmt.Sprintf("%s::%s", self.accessKeyId, project.ProjectID)
subAccount.HealthStatus = api.CLOUD_PROVIDER_HEALTH_NORMAL