yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/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 hcso
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"yunion.io/x/jsonutils"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  )
    25  
    26  // https://support.huaweicloud.com/api-iam/zh-cn_topic_0057845625.html
    27  type SProject struct {
    28  	client *SHuaweiClient
    29  
    30  	IsDomain    bool   `json:"is_domain"`
    31  	Description string `json:"description"`
    32  	Enabled     bool   `json:"enabled"`
    33  	ID          string `json:"id"`
    34  	ParentID    string `json:"parent_id"`
    35  	DomainID    string `json:"domain_id"`
    36  	Name        string `json:"name"`
    37  }
    38  
    39  func (self *SProject) GetRegionID() string {
    40  	return strings.Split(self.Name, "_")[0]
    41  }
    42  
    43  func (self *SProject) GetHealthStatus() string {
    44  	if self.Enabled {
    45  		return api.CLOUD_PROVIDER_HEALTH_NORMAL
    46  	}
    47  
    48  	return api.CLOUD_PROVIDER_HEALTH_SUSPENDED
    49  }
    50  
    51  func (self *SHuaweiClient) fetchProjects() ([]SProject, error) {
    52  	if self.projects != nil {
    53  		return self.projects, nil
    54  	}
    55  
    56  	huawei, _ := self.newGeneralAPIClient()
    57  	projects := make([]SProject, 0)
    58  	err := doListAll(huawei.Projects.List, nil, &projects)
    59  	if err == nil {
    60  		self.projects = projects
    61  	}
    62  
    63  	return projects, err
    64  }
    65  
    66  // obs 权限必须赋予到mos project之上
    67  func (self *SHuaweiClient) GetMosProjectId() string {
    68  	projects, err := self.GetProjects()
    69  	if err != nil {
    70  		return ""
    71  	}
    72  	for i := range projects {
    73  		if strings.ToLower(projects[i].Name) == "mos" {
    74  			return projects[i].ID
    75  		}
    76  	}
    77  	return ""
    78  }
    79  
    80  func (self *SHuaweiClient) GetMosRoles(groupId string) ([]SRole, error) {
    81  	client, _ := self.newGeneralAPIClient()
    82  	resp, err := client.Projects.ListRoles(self.GetMosProjectId(), groupId)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	ret := []SRole{}
    87  	return ret, jsonutils.Update(&ret, resp.Data)
    88  }
    89  
    90  func (self *SHuaweiClient) GetProjectById(projectId string) (SProject, error) {
    91  	projects, err := self.fetchProjects()
    92  	if err != nil {
    93  		return SProject{}, err
    94  	}
    95  
    96  	for _, project := range projects {
    97  		if project.ID == projectId {
    98  			return project, nil
    99  		}
   100  	}
   101  	return SProject{}, fmt.Errorf("project %s not found", projectId)
   102  }
   103  
   104  func (self *SHuaweiClient) GetProjects() ([]SProject, error) {
   105  	return self.fetchProjects()
   106  }