enterpriceprojects.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. "strings"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. "yunion.io/x/cloudmux/pkg/multicloud/huawei"
  24. )
  25. // https://support.huaweicloud.com/api-em/zh-cn_topic_0121230880.html
  26. type SEnterpriseProject struct {
  27. multicloud.SProjectBase
  28. huawei.HuaweiTags
  29. Id string
  30. Name string
  31. Description string
  32. Status int
  33. CreatedAt time.Time
  34. UpdatedAt time.Time
  35. }
  36. func (self *SHuaweiClient) GetEnterpriseProjects() ([]SEnterpriseProject, error) {
  37. projects := []SEnterpriseProject{}
  38. client, err := self.newGeneralAPIClient()
  39. if err != nil {
  40. return nil, errors.Wrap(err, "newGeneralAPIClient")
  41. }
  42. err = doListAllWithOffset(client.EnterpriseProjects.List, map[string]string{}, &projects)
  43. if err != nil {
  44. return nil, errors.Wrap(err, "doListAllWithOffset")
  45. }
  46. return projects, nil
  47. }
  48. func (ep *SEnterpriseProject) GetId() string {
  49. return ep.Id
  50. }
  51. func (ep *SEnterpriseProject) GetGlobalId() string {
  52. return ep.Id
  53. }
  54. func (ep *SEnterpriseProject) GetStatus() string {
  55. if ep.Status == 1 {
  56. return api.EXTERNAL_PROJECT_STATUS_AVAILABLE
  57. }
  58. return api.EXTERNAL_PROJECT_STATUS_UNAVAILABLE
  59. }
  60. func (ep *SEnterpriseProject) GetName() string {
  61. return ep.Name
  62. }
  63. func (self *SHuaweiClient) CreateExterpriseProject(name, desc string) (*SEnterpriseProject, error) {
  64. client, err := self.newGeneralAPIClient()
  65. if err != nil {
  66. return nil, errors.Wrap(err, "newGeneralAPIClient")
  67. }
  68. params := map[string]string{
  69. "name": name,
  70. }
  71. if len(desc) > 0 {
  72. params["description"] = desc
  73. }
  74. resp, err := client.EnterpriseProjects.Create(jsonutils.Marshal(params))
  75. if err != nil {
  76. if strings.Contains(err.Error(), "EPS.0004") {
  77. return nil, cloudprovider.ErrNotSupported
  78. }
  79. return nil, errors.Wrap(err, "EnterpriseProjects.Create")
  80. }
  81. project := &SEnterpriseProject{}
  82. err = resp.Unmarshal(&project)
  83. if err != nil {
  84. return nil, errors.Wrap(err, "resp.Unmarshal")
  85. }
  86. return project, nil
  87. }
  88. func (self *SHuaweiClient) CreateIProject(name string) (cloudprovider.ICloudProject, error) {
  89. return self.CreateExterpriseProject(name, "")
  90. }
  91. func (self *SHuaweiClient) GetIProjects() ([]cloudprovider.ICloudProject, error) {
  92. projects, err := self.GetEnterpriseProjects()
  93. if err != nil {
  94. return nil, errors.Wrap(err, "GetProjects")
  95. }
  96. ret := []cloudprovider.ICloudProject{}
  97. for i := range projects {
  98. ret = append(ret, &projects[i])
  99. }
  100. return ret, nil
  101. }