resources.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 "fmt"
  16. type SAliyunResource struct {
  17. ResourceGroupId string
  18. ResourceId string
  19. Service string
  20. ResourceType string
  21. RegionId string
  22. CreateDate string
  23. }
  24. func (self *SAliyunClient) ListResources(service, resourceType string) ([]SAliyunResource, error) {
  25. params := map[string]string{
  26. "PageSize": "100",
  27. }
  28. if len(service) > 0 {
  29. params["Service"] = service
  30. }
  31. if len(resourceType) > 0 {
  32. params["ResourceType"] = resourceType
  33. }
  34. pageNumber := 1
  35. ret := []SAliyunResource{}
  36. for {
  37. params["PageNumber"] = fmt.Sprintf("%d", pageNumber)
  38. resp, err := self.rmRequest("ListResources", params)
  39. if err != nil {
  40. return nil, err
  41. }
  42. part := struct {
  43. Resources struct {
  44. Resource []SAliyunResource
  45. }
  46. TotalCount int
  47. }{}
  48. err = resp.Unmarshal(&part)
  49. if err != nil {
  50. return nil, err
  51. }
  52. ret = append(ret, part.Resources.Resource...)
  53. if len(ret) >= part.TotalCount || len(part.Resources.Resource) == 0 {
  54. break
  55. }
  56. pageNumber++
  57. }
  58. return ret, nil
  59. }