project.go 2.7 KB

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