mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-19 02:37:24 +08:00
Merge pull request #7328 from tb365/automated-cherry-pick-of-#7327-upstream-release-3.2
Automated cherry pick of #7327: huawei fetch regions&projects fix
This commit is contained in:
@@ -59,6 +59,12 @@ const (
|
||||
BATCH_HOST_REGISTER_QUANTITY_LIMITATION = 1000
|
||||
)
|
||||
|
||||
var (
|
||||
BatchHostRegisterTemplate = []string{HOST_MAC, HOST_NAME, HOST_IPMI_ADDR_OPTIONAL, HOST_IPMI_USERNAME_OPTIONAL, HOST_IPMI_PASSWORD_OPTIONAL}
|
||||
BatchHostISORegisterTemplate = []string{HOST_NAME, HOST_IPMI_ADDR, HOST_IPMI_USERNAME, HOST_IPMI_PASSWORD, HOST_MNG_IP_ADDR}
|
||||
BatchHostPXERegisterTemplate = []string{HOST_NAME, HOST_IPMI_ADDR, HOST_IPMI_USERNAME, HOST_IPMI_PASSWORD, HOST_MNG_IP_ADDR_OPTIONAL}
|
||||
)
|
||||
|
||||
func FetchSession(ctx context.Context, r *http.Request, apiVersion string) *mcclient.ClientSession {
|
||||
token := AppContextToken(ctx)
|
||||
session := auth.GetSession(ctx, token, FetchRegion(r), apiVersion)
|
||||
@@ -172,6 +178,25 @@ func (mh *MiscHandler) DoBatchHostRegister(ctx context.Context, w http.ResponseW
|
||||
return
|
||||
}
|
||||
|
||||
// check header line
|
||||
titlesOk := false
|
||||
for _, t := range [][]string{BatchHostRegisterTemplate, BatchHostISORegisterTemplate, BatchHostPXERegisterTemplate} {
|
||||
if len(t) == len(rows[0]) {
|
||||
for _, title := range rows[0] {
|
||||
if !utils.IsInStringArray(title, t) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
titlesOk = true
|
||||
}
|
||||
}
|
||||
|
||||
if !titlesOk {
|
||||
httperrors.InputParameterError(w, "template file is invalid.please check.")
|
||||
return
|
||||
}
|
||||
|
||||
paramKeys := []string{}
|
||||
i1 := -1
|
||||
i2 := -1
|
||||
@@ -406,21 +431,21 @@ func (mh *MiscHandler) getDownloadsHandler(ctx context.Context, w http.ResponseW
|
||||
var content bytes.Buffer
|
||||
switch template {
|
||||
case "BatchHostRegister":
|
||||
records := [][]string{{HOST_MAC, HOST_NAME, HOST_IPMI_ADDR_OPTIONAL, HOST_IPMI_USERNAME_OPTIONAL, HOST_IPMI_PASSWORD_OPTIONAL}}
|
||||
records := [][]string{BatchHostRegisterTemplate}
|
||||
content, err = writeXlsx("hosts", records)
|
||||
if err != nil {
|
||||
httperrors.InternalServerError(w, "internal server error")
|
||||
return
|
||||
}
|
||||
case "BatchHostISORegister":
|
||||
records := [][]string{{HOST_NAME, HOST_IPMI_ADDR, HOST_IPMI_USERNAME, HOST_IPMI_PASSWORD, HOST_MNG_IP_ADDR}}
|
||||
records := [][]string{BatchHostISORegisterTemplate}
|
||||
content, err = writeXlsx("hosts", records)
|
||||
if err != nil {
|
||||
httperrors.InternalServerError(w, "internal server error")
|
||||
return
|
||||
}
|
||||
case "BatchHostPXERegister":
|
||||
records := [][]string{{HOST_NAME, HOST_IPMI_ADDR, HOST_IPMI_USERNAME, HOST_IPMI_PASSWORD, HOST_MNG_IP_ADDR_OPTIONAL}}
|
||||
records := [][]string{BatchHostPXERegisterTemplate}
|
||||
content, err = writeXlsx("hosts", records)
|
||||
if err != nil {
|
||||
httperrors.InternalServerError(w, "internal server error")
|
||||
|
||||
@@ -204,9 +204,9 @@ func (self *SBaseManager) jsonRequest(request requests.IRequest) (http.Header, j
|
||||
|
||||
switch err := e.(type) {
|
||||
case *HuaweiClientError:
|
||||
if err.Code == 499 && retry > 0 && request.GetMethod() == "GET" {
|
||||
if (err.Code == 499 || err.Code == 429) && retry > 0 && request.GetMethod() == "GET" {
|
||||
retry -= 1
|
||||
time.Sleep(time.Second * time.Duration(MAX_RETRY-retry))
|
||||
time.Sleep(3 * time.Second * time.Duration(MAX_RETRY-retry))
|
||||
} else if (err.Code == 404 || strings.Contains(err.Details, "could not be found") || strings.Contains(err.Details, "does not exist")) && request.GetMethod() != "POST" {
|
||||
return h, b, errors.Wrap(cloudprovider.ErrNotFound, err.Error())
|
||||
} else {
|
||||
|
||||
@@ -90,6 +90,9 @@ type SHuaweiClient struct {
|
||||
|
||||
iregions []cloudprovider.ICloudRegion
|
||||
iBuckets []cloudprovider.ICloudBucket
|
||||
|
||||
projects []SProject
|
||||
regions []SRegion
|
||||
}
|
||||
|
||||
// 进行资源操作时参数account 对应数据库cloudprovider表中的account字段,由accessKey和projectID两部分组成,通过"/"分割。
|
||||
@@ -162,10 +165,14 @@ func (self *SHuaweiClient) newGeneralAPIClient() (*client.Client, error) {
|
||||
|
||||
func (self *SHuaweiClient) fetchRegions() error {
|
||||
huawei, _ := self.newGeneralAPIClient()
|
||||
regions := make([]SRegion, 0)
|
||||
err := doListAll(huawei.Regions.List, nil, ®ions)
|
||||
if err != nil {
|
||||
return err
|
||||
if self.regions == nil {
|
||||
regions := make([]SRegion, 0)
|
||||
err := doListAll(huawei.Regions.List, nil, ®ions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
self.regions = regions
|
||||
}
|
||||
|
||||
filtedRegions := make([]SRegion, 0)
|
||||
@@ -176,7 +183,7 @@ func (self *SHuaweiClient) fetchRegions() error {
|
||||
}
|
||||
|
||||
regionId := strings.Split(project.Name, "_")[0]
|
||||
for _, region := range regions {
|
||||
for _, region := range self.regions {
|
||||
if region.ID == regionId {
|
||||
filtedRegions = append(filtedRegions, region)
|
||||
}
|
||||
@@ -185,7 +192,7 @@ func (self *SHuaweiClient) fetchRegions() error {
|
||||
self.isMainProject = true
|
||||
}
|
||||
} else {
|
||||
filtedRegions = regions
|
||||
filtedRegions = self.regions
|
||||
}
|
||||
|
||||
self.iregions = make([]cloudprovider.ICloudRegion, len(filtedRegions))
|
||||
|
||||
@@ -47,9 +47,17 @@ func (self *SProject) GetHealthStatus() string {
|
||||
}
|
||||
|
||||
func (self *SHuaweiClient) fetchProjects() ([]SProject, error) {
|
||||
if self.projects != nil {
|
||||
return self.projects, nil
|
||||
}
|
||||
|
||||
huawei, _ := self.newGeneralAPIClient()
|
||||
projects := make([]SProject, 0)
|
||||
err := doListAll(huawei.Projects.List, nil, &projects)
|
||||
if err == nil {
|
||||
self.projects = projects
|
||||
}
|
||||
|
||||
return projects, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user