project.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 huawei
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/pkg/errors"
  19. )
  20. type SProject struct {
  21. client *SHuaweiClient
  22. IsDomain bool
  23. Description string
  24. Enabled bool
  25. Id string
  26. ParentId string
  27. DomainId string
  28. Name string
  29. }
  30. func (self *SProject) GetRegionId() string {
  31. return strings.Split(self.Name, "_")[0]
  32. }
  33. func (self *SHuaweiClient) GetProjects() ([]SProject, error) {
  34. if len(self.projects) > 0 {
  35. ret := []SProject{}
  36. for _, project := range self.projects {
  37. ret = append(ret, project)
  38. }
  39. return ret, nil
  40. }
  41. self.projects = map[string]SProject{}
  42. projects := []SProject{}
  43. resp, err := self.list(SERVICE_IAM_V3, "", "auth/projects", nil)
  44. if err != nil {
  45. return nil, errors.Wrapf(err, "list projects")
  46. }
  47. err = resp.Unmarshal(&projects, "projects")
  48. if err != nil {
  49. return nil, errors.Wrapf(err, "Unmarshal")
  50. }
  51. for _, project := range projects {
  52. self.projects[project.Name] = project
  53. }
  54. return projects, nil
  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) GetProjectById(projectId string) (SProject, error) {
  70. projects, err := self.GetProjects()
  71. if err != nil {
  72. return SProject{}, err
  73. }
  74. for _, project := range projects {
  75. if project.Id == projectId {
  76. return project, nil
  77. }
  78. }
  79. return SProject{}, fmt.Errorf("project %s not found", projectId)
  80. }