enterpriseprojects.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. "net/url"
  18. "strings"
  19. "time"
  20. "yunion.io/x/pkg/errors"
  21. api "yunion.io/x/cloudmux/pkg/apis/compute"
  22. "yunion.io/x/cloudmux/pkg/cloudprovider"
  23. "yunion.io/x/cloudmux/pkg/multicloud"
  24. )
  25. type SEnterpriseProject struct {
  26. multicloud.SProjectBase
  27. HuaweiTags
  28. Id string
  29. Name string
  30. Description string
  31. Status int
  32. CreatedAt time.Time
  33. UpdatedAt time.Time
  34. }
  35. func (self *SHuaweiClient) GetEnterpriseProjects() ([]SEnterpriseProject, error) {
  36. ret := []SEnterpriseProject{}
  37. query := url.Values{}
  38. for {
  39. resp, err := self.list(SERVICE_EPS, "", "enterprise-projects", query)
  40. if err != nil {
  41. return nil, errors.Wrapf(err, "list")
  42. }
  43. part := struct {
  44. EnterpriseProjects []SEnterpriseProject
  45. TotalCount int
  46. }{}
  47. err = resp.Unmarshal(&part)
  48. if err != nil {
  49. return nil, err
  50. }
  51. ret = append(ret, part.EnterpriseProjects...)
  52. if len(part.EnterpriseProjects) == 0 || len(ret) > part.TotalCount {
  53. break
  54. }
  55. query.Set("offset", fmt.Sprintf("%d", len(ret)))
  56. }
  57. return ret, nil
  58. }
  59. func (ep *SEnterpriseProject) GetId() string {
  60. return ep.Id
  61. }
  62. func (ep *SEnterpriseProject) GetGlobalId() string {
  63. return ep.Id
  64. }
  65. func (ep *SEnterpriseProject) GetStatus() string {
  66. if ep.Status == 1 {
  67. return api.EXTERNAL_PROJECT_STATUS_AVAILABLE
  68. }
  69. return api.EXTERNAL_PROJECT_STATUS_UNAVAILABLE
  70. }
  71. func (ep *SEnterpriseProject) GetName() string {
  72. return ep.Name
  73. }
  74. func (self *SHuaweiClient) CreateExterpriseProject(name, desc string) (*SEnterpriseProject, error) {
  75. params := map[string]interface{}{
  76. "name": name,
  77. }
  78. if len(desc) > 0 {
  79. params["description"] = desc
  80. }
  81. resp, err := self.post(SERVICE_EPS, "", "enterprise-projects", params)
  82. if err != nil {
  83. if strings.Contains(err.Error(), "EPS.0004") {
  84. return nil, cloudprovider.ErrNotSupported
  85. }
  86. if strings.Contains(err.Error(), "EPS.0039") {
  87. return nil, errors.Wrapf(cloudprovider.ErrForbidden, "%s", err.Error())
  88. }
  89. return nil, errors.Wrap(err, "EnterpriseProjects.Create")
  90. }
  91. ret := &SEnterpriseProject{}
  92. err = resp.Unmarshal(&ret, "enterprise_project")
  93. if err != nil {
  94. return nil, errors.Wrap(err, "resp.Unmarshal")
  95. }
  96. return ret, nil
  97. }
  98. func (self *SHuaweiClient) CreateIProject(name string) (cloudprovider.ICloudProject, error) {
  99. return self.CreateExterpriseProject(name, "")
  100. }
  101. func (self *SHuaweiClient) GetIProjects() ([]cloudprovider.ICloudProject, error) {
  102. projects, err := self.GetEnterpriseProjects()
  103. if err != nil {
  104. return nil, errors.Wrap(err, "GetProjects")
  105. }
  106. ret := []cloudprovider.ICloudProject{}
  107. for i := range projects {
  108. ret = append(ret, &projects[i])
  109. }
  110. return ret, nil
  111. }