project.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 aliyun
  15. import (
  16. "fmt"
  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. )
  24. type SResourceGroup struct {
  25. multicloud.SProjectBase
  26. AliyunTags
  27. client *SAliyunClient
  28. Status string
  29. DisplayName string
  30. Id string
  31. CreateDate time.Time
  32. Name string
  33. }
  34. func (self *SResourceGroup) GetGlobalId() string {
  35. return self.Id
  36. }
  37. func (self *SResourceGroup) GetId() string {
  38. return self.Id
  39. }
  40. func (self *SResourceGroup) GetName() string {
  41. if len(self.DisplayName) > 0 {
  42. return self.DisplayName
  43. }
  44. return self.Name
  45. }
  46. func (self *SResourceGroup) Refresh() error {
  47. group, err := self.client.GetResourceGroup(self.Id)
  48. if err != nil {
  49. return errors.Wrap(err, "GetResourceGroup")
  50. }
  51. return jsonutils.Update(self, group)
  52. }
  53. func (self *SResourceGroup) GetStatus() string {
  54. switch self.Status {
  55. case "Creating":
  56. return api.EXTERNAL_PROJECT_STATUS_CREATING
  57. case "OK":
  58. return api.EXTERNAL_PROJECT_STATUS_AVAILABLE
  59. case "Deleted", "Deleting", "PendingDelete":
  60. return api.EXTERNAL_PROJECT_STATUS_DELETING
  61. default:
  62. return api.EXTERNAL_PROJECT_STATUS_UNKNOWN
  63. }
  64. }
  65. func (self *SAliyunClient) GetResourceGroups(pageNumber int, pageSize int) ([]SResourceGroup, int, error) {
  66. if pageSize <= 0 || pageSize > 100 {
  67. pageSize = 10
  68. }
  69. if pageNumber <= 0 {
  70. pageNumber = 1
  71. }
  72. params := map[string]string{
  73. "PageNumber": fmt.Sprintf("%d", pageNumber),
  74. "PageSize": fmt.Sprintf("%d", pageSize),
  75. "IncludeTags": "true",
  76. }
  77. resp, err := self.rmRequest("ListResourceGroups", params)
  78. if err != nil {
  79. return nil, 0, errors.Wrap(err, "rmRequest.ListResourceGroups")
  80. }
  81. groups := []SResourceGroup{}
  82. err = resp.Unmarshal(&groups, "ResourceGroups", "ResourceGroup")
  83. if err != nil {
  84. return nil, 0, errors.Wrap(err, "resp.Unmarshal")
  85. }
  86. total, _ := resp.Int("TotalCount")
  87. return groups, int(total), nil
  88. }
  89. func (self *SAliyunClient) CreateIProject(name string) (cloudprovider.ICloudProject, error) {
  90. group, err := self.CreateResourceGroup(name)
  91. if err != nil {
  92. return nil, errors.Wrap(err, "CreateProject")
  93. }
  94. return group, nil
  95. }
  96. func (self *SAliyunClient) CreateResourceGroup(name string) (*SResourceGroup, error) {
  97. params := map[string]string{
  98. "DisplayName": name,
  99. "Name": name,
  100. }
  101. resp, err := self.rmRequest("CreateResourceGroup", params)
  102. if err != nil {
  103. return nil, errors.Wrap(err, "CreateResourceGroup")
  104. }
  105. group := &SResourceGroup{client: self}
  106. err = resp.Unmarshal(group, "ResourceGroup")
  107. if err != nil {
  108. return nil, errors.Wrap(err, "resp.Unmarshal")
  109. }
  110. err = cloudprovider.WaitStatus(group, api.EXTERNAL_PROJECT_STATUS_AVAILABLE, time.Second*5, time.Minute*3)
  111. if err != nil {
  112. return nil, errors.Wrap(err, "WaitStatus")
  113. }
  114. return group, nil
  115. }
  116. func (self *SAliyunClient) GetResourceGroup(id string) (*SResourceGroup, error) {
  117. params := map[string]string{
  118. "ResourceGroupId": id,
  119. }
  120. resp, err := self.rmRequest("GetResourceGroup", params)
  121. if err != nil {
  122. return nil, err
  123. }
  124. group := &SResourceGroup{client: self}
  125. err = resp.Unmarshal(group, "ResourceGroup")
  126. if err != nil {
  127. return nil, errors.Wrap(err, "resp.Unmarshal")
  128. }
  129. return group, nil
  130. }