yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/project.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package huawei
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    22  )
    23  
    24  // https://support.huaweicloud.com/api-iam/zh-cn_topic_0057845625.html
    25  type SProject struct {
    26  	client *SHuaweiClient
    27  
    28  	IsDomain    bool   `json:"is_domain"`
    29  	Description string `json:"description"`
    30  	Enabled     bool   `json:"enabled"`
    31  	ID          string `json:"id"`
    32  	ParentID    string `json:"parent_id"`
    33  	DomainID    string `json:"domain_id"`
    34  	Name        string `json:"name"`
    35  }
    36  
    37  func (self *SProject) GetRegionID() string {
    38  	return strings.Split(self.Name, "_")[0]
    39  }
    40  
    41  func (self *SProject) GetHealthStatus() string {
    42  	if self.Enabled {
    43  		return api.CLOUD_PROVIDER_HEALTH_NORMAL
    44  	}
    45  
    46  	return api.CLOUD_PROVIDER_HEALTH_SUSPENDED
    47  }
    48  
    49  func (self *SHuaweiClient) fetchProjects() ([]SProject, error) {
    50  	if self.projects != nil {
    51  		return self.projects, nil
    52  	}
    53  
    54  	huawei, _ := self.newGeneralAPIClient()
    55  	projects := make([]SProject, 0)
    56  	err := doListAll(huawei.Projects.List, nil, &projects)
    57  	if err == nil {
    58  		self.projects = projects
    59  	}
    60  
    61  	return projects, err
    62  }
    63  
    64  // obs 权限必须赋予到mos project之上
    65  func (self *SHuaweiClient) GetMosProjectId() string {
    66  	projects, err := self.GetProjects()
    67  	if err != nil {
    68  		return ""
    69  	}
    70  	for i := range projects {
    71  		if strings.ToLower(projects[i].Name) == "mos" {
    72  			return projects[i].ID
    73  		}
    74  	}
    75  	return ""
    76  }
    77  
    78  func (self *SHuaweiClient) GetProjectById(projectId string) (SProject, error) {
    79  	projects, err := self.fetchProjects()
    80  	if err != nil {
    81  		return SProject{}, err
    82  	}
    83  
    84  	for _, project := range projects {
    85  		if project.ID == projectId {
    86  			return project, nil
    87  		}
    88  	}
    89  	return SProject{}, fmt.Errorf("project %s not found", projectId)
    90  }
    91  
    92  func (self *SHuaweiClient) GetProjects() ([]SProject, error) {
    93  	return self.fetchProjects()
    94  }