project.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 openstack
  15. import (
  16. "fmt"
  17. "net/url"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/util/httputils"
  21. api "yunion.io/x/cloudmux/pkg/apis/compute"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. "yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli"
  24. )
  25. type SProject struct {
  26. multicloud.SProjectBase
  27. OpenStackTags
  28. client *SOpenStackClient
  29. Description string
  30. Enabled bool
  31. Id string
  32. Name string
  33. }
  34. func (p *SProject) GetId() string {
  35. return p.Id
  36. }
  37. func (p *SProject) GetGlobalId() string {
  38. return p.GetId()
  39. }
  40. func (p *SProject) GetName() string {
  41. return p.Name
  42. }
  43. func (p *SProject) GetStatus() string {
  44. if !p.Enabled {
  45. return api.EXTERNAL_PROJECT_STATUS_UNKNOWN
  46. }
  47. _, err := p.getToken()
  48. if err != nil {
  49. log.Errorf("get project %s token error: %v %T", p.Name, err, err)
  50. return api.EXTERNAL_PROJECT_STATUS_UNKNOWN
  51. }
  52. return api.EXTERNAL_PROJECT_STATUS_AVAILABLE
  53. }
  54. func (p *SProject) getToken() (oscli.TokenCredential, error) {
  55. return p.client.getProjectToken(p.Id, p.Name)
  56. }
  57. func (p *SProject) IsEmulated() bool {
  58. return false
  59. }
  60. func (p *SProject) Refresh() error {
  61. return nil
  62. }
  63. type SProjectLinks struct {
  64. Next string
  65. Previous string
  66. Self string
  67. }
  68. func (link SProjectLinks) GetNextMark() string {
  69. if len(link.Next) == 0 || link.Next == "null" {
  70. return ""
  71. }
  72. next, err := url.Parse(link.Next)
  73. if err != nil {
  74. log.Errorf("parse next link %s error: %v", link.Next, err)
  75. return ""
  76. }
  77. return next.Query().Get("marker")
  78. }
  79. func (cli *SOpenStackClient) GetProjects() ([]SProject, error) {
  80. resource := "/v3/projects"
  81. projects := []SProject{}
  82. query := url.Values{}
  83. for {
  84. resp, err := cli.iamRequest("", httputils.GET, resource, query, nil)
  85. if err != nil {
  86. return nil, errors.Wrap(err, "iamRequest")
  87. }
  88. part := struct {
  89. Projects []SProject
  90. Links SProjectLinks
  91. }{}
  92. err = resp.Unmarshal(&part)
  93. if err != nil {
  94. return nil, errors.Wrap(err, "iamRequest")
  95. }
  96. projects = append(projects, part.Projects...)
  97. marker := part.Links.GetNextMark()
  98. if len(marker) == 0 {
  99. break
  100. }
  101. query.Set("marker", marker)
  102. }
  103. return projects, nil
  104. }
  105. func (cli *SOpenStackClient) DeleteProject(projectId string) error {
  106. resource := fmt.Sprintf("/v3/projects/%s", projectId)
  107. _, err := cli.iamRequest(cli.getDefaultRegionName(), httputils.DELETE, resource, nil, nil)
  108. return err
  109. }