yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/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 hcs 16 17 import ( 18 "fmt" 19 "strings" 20 21 "yunion.io/x/pkg/errors" 22 23 api "yunion.io/x/cloudmux/pkg/apis/compute" 24 "yunion.io/x/cloudmux/pkg/cloudprovider" 25 ) 26 27 type SProject struct { 28 Id string 29 Name string 30 DomainId string 31 Description bool 32 Enabled bool 33 ParentId string 34 IsDomain bool 35 } 36 37 func (self *SProject) GetHealthStatus() string { 38 if self.Enabled { 39 return api.CLOUD_PROVIDER_HEALTH_NORMAL 40 } 41 return api.CLOUD_PROVIDER_HEALTH_SUSPENDED 42 } 43 44 func (self *SHcsClient) GetProjects() ([]SProject, error) { 45 if len(self.projects) > 0 { 46 return self.projects, nil 47 } 48 resp, err := self.iamGet("v3/projects", nil) 49 if err != nil { 50 return nil, err 51 } 52 self.projects = []SProject{} 53 return self.projects, resp.Unmarshal(&self.projects, "projects") 54 } 55 56 func (self *SHcsClient) GetSubAccounts() ([]cloudprovider.SSubAccount, error) { 57 projects, err := self.GetProjects() 58 if err != nil { 59 return nil, err 60 } 61 62 ret := make([]cloudprovider.SSubAccount, 0) 63 for i := range projects { 64 project := projects[i] 65 // name 为MOS的project是华为云内部的一个特殊project。不需要同步到本地 66 if strings.ToLower(project.Name) == "mos" { 67 continue 68 } 69 s := cloudprovider.SSubAccount{ 70 Name: fmt.Sprintf("%s-%s", self.cpcfg.Name, project.Name), 71 Account: fmt.Sprintf("%s/%s", self.accessKey, project.Id), 72 HealthStatus: project.GetHealthStatus(), 73 DefaultProjectId: "0", 74 } 75 for j := range self.regions { 76 region := self.regions[j] 77 if strings.Contains(project.Name, region.Id) { 78 s.Desc = region.Locales.ZhCN 79 break 80 } 81 } 82 ret = append(ret, s) 83 } 84 return ret, nil 85 } 86 87 func (self *SHcsClient) GetProjectById(id string) (*SProject, error) { 88 projects, err := self.GetProjects() 89 if err != nil { 90 return nil, err 91 } 92 for i := range projects { 93 if projects[i].Id == id { 94 return &projects[i], nil 95 } 96 } 97 return nil, errors.Wrapf(cloudprovider.ErrNotFound, id) 98 }