organization.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 identity
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/onecloud/pkg/apis"
  20. "yunion.io/x/onecloud/pkg/util/tagutils"
  21. )
  22. const (
  23. OrganizationLabelSeparator = "/"
  24. OrganizationRootParent = "<-root-org-node->"
  25. OrganizationStatusInit = "init"
  26. OrganizationStatusReady = "ready"
  27. OrganizationStatusSync = "sync"
  28. OrganizationStatusSyncFailed = "sync_failed"
  29. )
  30. type TOrgType string
  31. const (
  32. OrgTypeProject = TOrgType("project")
  33. OrgTypeDomain = TOrgType("domain")
  34. OrgTypeObject = TOrgType("object")
  35. )
  36. var (
  37. OrganizationTypes = []TOrgType{
  38. OrgTypeProject,
  39. OrgTypeDomain,
  40. OrgTypeObject,
  41. }
  42. )
  43. func IsValidOrgType(orgType TOrgType) bool {
  44. for _, t := range OrganizationTypes {
  45. if t == orgType {
  46. return true
  47. }
  48. }
  49. return false
  50. }
  51. type OrganizationListInput struct {
  52. EnabledIdentityBaseResourceListInput
  53. apis.SharableResourceBaseListInput
  54. apis.StatusResourceBaseListInput
  55. // apis.EnabledStatusInfrasResourceBaseListInput
  56. Type []TOrgType `json:"type"`
  57. Key string `json:"key"`
  58. }
  59. type OrganizationCreateInput struct {
  60. EnabledIdentityBaseResourceCreateInput
  61. apis.SharableResourceBaseCreateInput
  62. apis.StatusBaseResourceCreateInput
  63. // apis.EnabledStatusInfrasResourceBaseCreateInput
  64. Type TOrgType `json:"type"`
  65. // swagger:ignore
  66. Level int `json:"level,omitzero"`
  67. // key
  68. Key []string `json:"key"`
  69. // keys
  70. // swagger:ignore
  71. Keys string `json:"keys"`
  72. }
  73. type SOrganizationInfo struct {
  74. Keys []string `json:"levels,omitempty"`
  75. Tags map[string]string `json:"tags,omitempty"`
  76. }
  77. func (info *SOrganizationInfo) IsZero() bool {
  78. return len(info.Keys) == 0 && len(info.Tags) == 0
  79. }
  80. func (info *SOrganizationInfo) String() string {
  81. return jsonutils.Marshal(info).String()
  82. }
  83. type OrganizationUpdateInput struct {
  84. EnabledIdentityBaseUpdateInput
  85. // apis.SharableResourceBaseUpdateInput
  86. // apis.StatusResourceBaseUpdateInput
  87. // apis.EnabledStatusInfrasResourceBaseUpdateInput
  88. }
  89. type OrganizationPerformAddLevelsInput struct {
  90. Key []string `json:"key" help:"add keys"`
  91. OrganizationPerformAddNodeInput
  92. }
  93. type OrganizationPerformAddNodeInput struct {
  94. Tags map[string]string `json:"tags"`
  95. Weight int `json:"weight"`
  96. Description string `json:"description"`
  97. }
  98. type OrganizationPerformSyncInput struct {
  99. ResourceType string `json:"resource_type"`
  100. Reset *bool `json:"reset"`
  101. }
  102. type OrganizationNodePerformBindInput struct {
  103. TargetId []string `json:"target_id"`
  104. ResourceType string `json:"resource_type"`
  105. }
  106. func IsValidLabel(val string) bool {
  107. return !strings.Contains(val, OrganizationLabelSeparator)
  108. }
  109. func trimLabel(label string) string {
  110. label = strings.Trim(label, OrganizationLabelSeparator+" ")
  111. label = strings.ReplaceAll(label, "/", "\\/")
  112. return label
  113. }
  114. func JoinLabels(seg ...string) string {
  115. if len(seg) == 0 {
  116. return ""
  117. }
  118. buf := strings.Builder{}
  119. buf.WriteString(trimLabel(seg[0]))
  120. for _, s := range seg[1:] {
  121. buf.WriteString(OrganizationLabelSeparator)
  122. buf.WriteString(trimLabel(s))
  123. }
  124. return buf.String()
  125. }
  126. func SplitLabel(label string) []string {
  127. parts := strings.Split(label, OrganizationLabelSeparator)
  128. ret := make([]string, 0)
  129. for _, p := range parts {
  130. p = trimLabel(p)
  131. if len(p) > 0 {
  132. if len(ret) > 0 && strings.HasSuffix(ret[len(ret)-1], "\\") {
  133. pref := ret[len(ret)-1]
  134. ret[len(ret)-1] = fmt.Sprintf("%s/%s", pref[:len(pref)-1], p)
  135. } else {
  136. ret = append(ret, p)
  137. }
  138. }
  139. }
  140. return ret
  141. }
  142. type OrganizationNodeUpdateInput struct {
  143. apis.StandaloneResourceBaseUpdateInput
  144. Weight *int `json:"weight"`
  145. }
  146. type OrganizationNodeListInput struct {
  147. apis.StandaloneResourceListInput
  148. OrgId string `json:"org_id"`
  149. OrgType TOrgType `json:"org_type"`
  150. Level int `json:"level"`
  151. // domain tags filter imposed by policy
  152. PolicyDomainTags tagutils.TTagSetList `json:"policy_domain_tags"`
  153. // project tags filter imposed by policy
  154. PolicyProjectTags tagutils.TTagSetList `json:"policy_project_tags"`
  155. // object tags filter imposed by policy
  156. PolicyObjectTags tagutils.TTagSetList `json:"policy_object_tags"`
  157. }
  158. type SProjectOrganization struct {
  159. Id string `json:"id"`
  160. Name string `json:"name"`
  161. Keys []string `json:"keys"`
  162. Nodes []SProjectOrganizationNode `json:"nodes"`
  163. }
  164. type SProjectOrganizationNode struct {
  165. Id string `json:"id"`
  166. Labels []string `json:"labels"`
  167. }
  168. type SOrganizationNodeDetails struct {
  169. apis.StandaloneResourceDetails
  170. SOrganizationNode
  171. Tags tagutils.TTagSet `json:"tags"`
  172. Organization string `json:"organization"`
  173. Type TOrgType `json:"type"`
  174. }
  175. type SOrganizationDetails struct {
  176. EnabledIdentityBaseResourceDetails
  177. apis.SharableResourceBaseInfo
  178. SOrganization
  179. }
  180. type OrganizationPerformCleanInput struct {
  181. }