project.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 google
  15. import (
  16. "time"
  17. "yunion.io/x/pkg/errors"
  18. api "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/cloudmux/pkg/multicloud"
  20. )
  21. type SProject struct {
  22. multicloud.SProjectBase
  23. GoogleTags
  24. Name string
  25. CreateTime time.Time
  26. LifecycleState string
  27. ProjectId string
  28. ProjectNumber string
  29. }
  30. func (cli *SGoogleClient) GetProject(id string) (*SProject, error) {
  31. project := &SProject{}
  32. resp, err := cli.managerGet(id)
  33. if err != nil {
  34. return nil, errors.Wrap(err, "managerGet")
  35. }
  36. err = resp.Unmarshal(project)
  37. if err != nil {
  38. return nil, errors.Wrap(err, "resp.Unmarshal")
  39. }
  40. return project, nil
  41. }
  42. func (cli *SGoogleClient) GetProjects() ([]SProject, error) {
  43. nextPageToken := ""
  44. params := map[string]string{}
  45. result := []SProject{}
  46. for {
  47. if len(nextPageToken) > 0 {
  48. params["pageToken"] = nextPageToken
  49. }
  50. resp, err := cli.managerList("projects", params)
  51. if err != nil {
  52. return nil, errors.Wrap(err, "managerList")
  53. }
  54. part := struct {
  55. Projects []SProject
  56. }{}
  57. err = resp.Unmarshal(&part)
  58. if err != nil {
  59. return nil, errors.Wrapf(err, "resp.Unmarshal")
  60. }
  61. result = append(result, part.Projects...)
  62. nextPageToken, _ = resp.GetString("nextPageToken")
  63. if len(nextPageToken) == 0 || len(part.Projects) == 0 {
  64. break
  65. }
  66. }
  67. return result, nil
  68. }
  69. func (p *SProject) GetName() string {
  70. return p.Name
  71. }
  72. func (p *SProject) GetId() string {
  73. return p.ProjectId
  74. }
  75. func (p *SProject) GetGlobalId() string {
  76. return p.ProjectId
  77. }
  78. func (p *SProject) GetStatus() string {
  79. return api.EXTERNAL_PROJECT_STATUS_AVAILABLE
  80. }
  81. func (p *SProject) Refresh() error {
  82. return nil
  83. }
  84. func (p *SProject) IsEmulated() bool {
  85. return false
  86. }