project.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2023 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 volcengine
  15. import (
  16. "fmt"
  17. "time"
  18. api "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/cloudmux/pkg/multicloud"
  21. "yunion.io/x/jsonutils"
  22. "yunion.io/x/pkg/errors"
  23. )
  24. type SProject struct {
  25. multicloud.SProjectBase
  26. VolcEngineTags
  27. client *SVolcEngineClient
  28. AccountId int
  29. ProjectName string
  30. ParentProjectName string
  31. Path string
  32. DisplayName string
  33. Description string
  34. CreateDate string
  35. UpdateDate string
  36. Status string
  37. }
  38. func (project *SProject) GetGlobalId() string {
  39. return project.ProjectName
  40. }
  41. func (project *SProject) GetId() string {
  42. return project.ProjectName
  43. }
  44. func (project *SProject) GetName() string {
  45. if len(project.DisplayName) > 0 {
  46. return project.DisplayName
  47. }
  48. return project.ProjectName
  49. }
  50. func (project *SProject) Refresh() error {
  51. group, err := project.client.GetProject(project.ProjectName)
  52. if err != nil {
  53. return errors.Wrap(err, "GetProject")
  54. }
  55. return jsonutils.Update(project, group)
  56. }
  57. func (project *SProject) GetStatus() string {
  58. switch project.Status {
  59. case "active":
  60. return api.EXTERNAL_PROJECT_STATUS_AVAILABLE
  61. default:
  62. return api.EXTERNAL_PROJECT_STATUS_UNKNOWN
  63. }
  64. }
  65. func (client *SVolcEngineClient) GetProject(name string) (*SProject, error) {
  66. params := map[string]string{
  67. "ProjectName": name,
  68. }
  69. body, err := client.iam20210801Request("", "GetProject", params)
  70. if err != nil {
  71. return nil, err
  72. }
  73. project := &SProject{client: client}
  74. err = body.Unmarshal(project)
  75. if err != nil {
  76. return nil, errors.Wrap(err, "resp.Unmarshal")
  77. }
  78. return project, nil
  79. }
  80. func (client *SVolcEngineClient) ListProjects(limit int, offset int) ([]SProject, int, error) {
  81. if limit > 50 || limit <= 0 {
  82. limit = 50
  83. }
  84. params := map[string]string{
  85. "Limit": fmt.Sprintf("%d", limit),
  86. "Offset": fmt.Sprintf("%d", offset),
  87. }
  88. resp, err := client.iam20210801Request("", "ListProjects", params)
  89. if err != nil {
  90. return nil, 0, errors.Wrap(err, "iamRequest.ListProjects")
  91. }
  92. projects := []SProject{}
  93. err = resp.Unmarshal(&projects, "Projects")
  94. if err != nil {
  95. return nil, 0, errors.Wrap(err, "resp.Unmarshal")
  96. }
  97. total, _ := resp.Int("Total")
  98. return projects, int(total), nil
  99. }
  100. func (client *SVolcEngineClient) CreateIProject(name string) (cloudprovider.ICloudProject, error) {
  101. group, err := client.CreateProject(name)
  102. if err != nil {
  103. return nil, errors.Wrap(err, "CreateProject")
  104. }
  105. return group, nil
  106. }
  107. func (client *SVolcEngineClient) CreateProject(name string) (*SProject, error) {
  108. params := map[string]string{
  109. "DisplayName": name,
  110. "ProjectName": name,
  111. }
  112. resp, err := client.iam20210801Request("", "CreateProject", params)
  113. if err != nil {
  114. return nil, errors.Wrap(err, "CreateProject")
  115. }
  116. group := &SProject{client: client}
  117. err = resp.Unmarshal(group, "Project")
  118. if err != nil {
  119. return nil, errors.Wrap(err, "resp.Unmarshal")
  120. }
  121. err = cloudprovider.WaitStatus(group, api.EXTERNAL_PROJECT_STATUS_AVAILABLE, time.Second*5, time.Minute*3)
  122. if err != nil {
  123. return nil, errors.Wrap(err, "WaitStatus")
  124. }
  125. return group, nil
  126. }