project.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 apsara
  15. import (
  16. "fmt"
  17. "time"
  18. "yunion.io/x/pkg/errors"
  19. api "yunion.io/x/cloudmux/pkg/apis/compute"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. )
  23. type DepartmentInfo struct {
  24. Department string
  25. DepartmentName string
  26. ResourceGroup string
  27. ResourceGroupName string
  28. ResourceGroupId string
  29. }
  30. func (self DepartmentInfo) GetProjectId() string {
  31. return self.ResourceGroup
  32. }
  33. type SResourceGroup struct {
  34. multicloud.SProjectBase
  35. ApsaraTags
  36. client *SApsaraClient
  37. Creator string
  38. GmtCreated string
  39. Id string
  40. OrganizationId string
  41. OrganizationName string
  42. ResourceGroupName string
  43. RsId string
  44. }
  45. func (self *SResourceGroup) GetGlobalId() string {
  46. return self.Id
  47. }
  48. func (self *SResourceGroup) GetId() string {
  49. return self.Id
  50. }
  51. func (self *SResourceGroup) GetName() string {
  52. return self.ResourceGroupName
  53. }
  54. func (self *SResourceGroup) Refresh() error {
  55. return nil
  56. }
  57. func (self *SResourceGroup) GetStatus() string {
  58. return api.EXTERNAL_PROJECT_STATUS_AVAILABLE
  59. }
  60. func (self *SApsaraClient) GetResourceGroups(pageNumber int, pageSize int) ([]SResourceGroup, int, error) {
  61. if pageSize <= 0 || pageSize > 100 {
  62. pageSize = 10
  63. }
  64. if pageNumber <= 0 {
  65. pageNumber = 1
  66. }
  67. params := map[string]string{
  68. "PageNumber": fmt.Sprintf("%d", pageNumber),
  69. "PageSize": fmt.Sprintf("%d", pageSize),
  70. }
  71. resp, err := self.ascmRequest("ListResourceGroup", params)
  72. if err != nil {
  73. return nil, 0, errors.Wrap(err, "rmRequest.ListResourceGroup")
  74. }
  75. groups := []SResourceGroup{}
  76. err = resp.Unmarshal(&groups, "data")
  77. if err != nil {
  78. return nil, 0, errors.Wrap(err, "resp.Unmarshal")
  79. }
  80. total, _ := resp.Int("pageInfo", "total")
  81. return groups, int(total), nil
  82. }
  83. func (self *SApsaraClient) CreateIProject(name string) (cloudprovider.ICloudProject, error) {
  84. group, err := self.CreateResourceGroup(name)
  85. if err != nil {
  86. return nil, errors.Wrap(err, "CreateProject")
  87. }
  88. return group, nil
  89. }
  90. func (self *SApsaraClient) CreateResourceGroup(name string) (*SResourceGroup, error) {
  91. params := map[string]string{
  92. "DisplayName": name,
  93. "Name": name,
  94. }
  95. resp, err := self.ascmRequest("CreateResourceGroup", params)
  96. if err != nil {
  97. return nil, errors.Wrap(err, "CreateResourceGroup")
  98. }
  99. group := &SResourceGroup{client: self}
  100. err = resp.Unmarshal(group, "ResourceGroup")
  101. if err != nil {
  102. return nil, errors.Wrap(err, "resp.Unmarshal")
  103. }
  104. err = cloudprovider.WaitStatus(group, api.EXTERNAL_PROJECT_STATUS_AVAILABLE, time.Second*5, time.Minute*3)
  105. if err != nil {
  106. return nil, errors.Wrap(err, "WaitStatus")
  107. }
  108. return group, nil
  109. }
  110. func (self *SApsaraClient) GetResourceGroup(id string) (*SResourceGroup, error) {
  111. params := map[string]string{
  112. "ResourceGroupId": id,
  113. }
  114. resp, err := self.ascmRequest("GetResourceGroup", params)
  115. if err != nil {
  116. return nil, err
  117. }
  118. group := &SResourceGroup{client: self}
  119. err = resp.Unmarshal(group, "ResourceGroup")
  120. if err != nil {
  121. return nil, errors.Wrap(err, "resp.Unmarshal")
  122. }
  123. return group, nil
  124. }