project.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 ksyun
  15. import (
  16. api "yunion.io/x/cloudmux/pkg/apis/compute"
  17. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. "yunion.io/x/cloudmux/pkg/multicloud"
  19. "yunion.io/x/pkg/errors"
  20. )
  21. type SProject struct {
  22. multicloud.SProjectBase
  23. client *SKsyunClient
  24. ProjectId string
  25. AccountId string
  26. ProjectName string
  27. ProjectDesc string
  28. Status string
  29. Krn string
  30. }
  31. func (self *SProject) GetGlobalId() string {
  32. return self.ProjectId
  33. }
  34. func (self *SProject) GetId() string {
  35. return self.ProjectId
  36. }
  37. func (self *SProject) GetName() string {
  38. return self.ProjectName
  39. }
  40. func (self *SProject) GetStatus() string {
  41. return api.EXTERNAL_PROJECT_STATUS_AVAILABLE
  42. }
  43. func (self *SKsyunClient) GetProjects() ([]SProject, error) {
  44. resp, err := self.iamRequest("", "GetAccountAllProjectList", nil)
  45. if err != nil {
  46. return nil, err
  47. }
  48. ret := []SProject{}
  49. err = resp.Unmarshal(&ret, "ProjectList")
  50. if err != nil {
  51. return nil, err
  52. }
  53. return ret, nil
  54. }
  55. func (self *SKsyunClient) GetSplitProjectIds() ([][]string, error) {
  56. projects, err := self.GetProjects()
  57. if err != nil {
  58. return nil, err
  59. }
  60. length := 100
  61. var result [][]string
  62. for i := 0; i < len(projects); i += length {
  63. end := i + length
  64. if end > len(projects) {
  65. end = len(projects)
  66. }
  67. part := []string{}
  68. for j := i; j < end; j++ {
  69. part = append(part, projects[j].ProjectId)
  70. }
  71. result = append(result, part)
  72. }
  73. return result, nil
  74. }
  75. func (self *SKsyunClient) GetIProjects() ([]cloudprovider.ICloudProject, error) {
  76. projects, err := self.GetProjects()
  77. if err != nil {
  78. return nil, err
  79. }
  80. ret := []cloudprovider.ICloudProject{}
  81. for i := range projects {
  82. projects[i].client = self
  83. ret = append(ret, &projects[i])
  84. }
  85. return ret, nil
  86. }
  87. func (cli *SKsyunClient) CreateProject(name string) (*SProject, error) {
  88. params := map[string]interface{}{
  89. "ProjectName": name,
  90. }
  91. _, err := cli.iamRequest("", "CreateProject", params)
  92. if err != nil {
  93. return nil, err
  94. }
  95. project, err := cli.GetProjects()
  96. if err != nil {
  97. return nil, err
  98. }
  99. for i := range project {
  100. project[i].client = cli
  101. if project[i].ProjectName == name {
  102. return &project[i], nil
  103. }
  104. }
  105. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "GetProject %s", name)
  106. }