project.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 qcloud
  15. import (
  16. "fmt"
  17. "strings"
  18. "time"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. )
  24. type SProject struct {
  25. multicloud.SProjectBase
  26. QcloudTags
  27. client *SQcloudClient
  28. ProjectName string `json:"projectName"`
  29. ProjectId string `json:"projectId"`
  30. CreateTime time.Time `json:"createTime"`
  31. CreateorUin int `json:"creatorUin"`
  32. ProjectInfo string `json:"projectInfo"`
  33. }
  34. func (p *SProject) GetId() string {
  35. var pId string
  36. pos := strings.Index(p.ProjectId, ".")
  37. if pos >= 0 {
  38. pId = p.ProjectId[:pos]
  39. } else {
  40. pId = p.ProjectId
  41. }
  42. return pId
  43. }
  44. func (p *SProject) GetGlobalId() string {
  45. return p.GetId()
  46. }
  47. func (p *SProject) GetName() string {
  48. return p.ProjectName
  49. }
  50. func (p *SProject) GetStatus() string {
  51. return api.EXTERNAL_PROJECT_STATUS_AVAILABLE
  52. }
  53. func (p *SProject) IsEmulated() bool {
  54. return false
  55. }
  56. func (p *SProject) Refresh() error {
  57. return nil
  58. }
  59. func (client *SQcloudClient) CreateIProject(name string) (cloudprovider.ICloudProject, error) {
  60. return client.CreateProject(name, "")
  61. }
  62. func (client *SQcloudClient) GetProjects(offset, limit int) ([]SProject, int, error) {
  63. if limit < 1 || limit > 1000 {
  64. limit = 1000
  65. }
  66. params := map[string]string{"AllList": "0"}
  67. params["Limit"] = fmt.Sprintf("%d", limit)
  68. params["Offset"] = fmt.Sprintf("%d", offset)
  69. resp, err := client.tagRequest("DescribeProjects", params)
  70. if err != nil {
  71. return nil, 0, errors.Wrapf(err, "DescribeProjects")
  72. }
  73. projects := []SProject{}
  74. err = resp.Unmarshal(&projects, "Projects")
  75. if err != nil {
  76. return nil, 0, errors.Wrap(err, "resp.Unmarshal")
  77. }
  78. total, _ := resp.Float("Total")
  79. return projects, int(total), nil
  80. }
  81. func (client *SQcloudClient) CreateProject(name, desc string) (*SProject, error) {
  82. params := map[string]string{
  83. "ProjectName": name,
  84. }
  85. if len(desc) > 0 {
  86. params["Info"] = desc
  87. }
  88. body, err := client.tagRequest("AddProject", params)
  89. if err != nil {
  90. return nil, errors.Wrap(err, "AddProject")
  91. }
  92. projectId, _ := body.GetString("ProjectId")
  93. if len(projectId) == 0 {
  94. return nil, fmt.Errorf("empty project reture")
  95. }
  96. return &SProject{
  97. client: client,
  98. ProjectName: name,
  99. ProjectId: projectId,
  100. ProjectInfo: desc,
  101. }, nil
  102. }