metadata.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 options
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. )
  20. type MetadataListOptions struct {
  21. Resources []string `help:"list of resource e.g server、disk、eip、snapshot, empty will show all metadata"`
  22. Service string `help:"service type"`
  23. SysMeta *bool `help:"Show sys metadata only"`
  24. CloudMeta *bool `help:"Show cloud metadata olny"`
  25. UserMeta *bool `help:"Show user metadata olny"`
  26. Admin *bool `help:"Show all metadata"`
  27. WithSysMeta *bool `help:"Show sys metadata"`
  28. WithCloudMeta *bool `help:"Show cloud metadata"`
  29. WithUserMeta *bool `help:"Show user metadata"`
  30. Key []string `help:"key"`
  31. Value []string `help:"value"`
  32. Limit *int `help:"limit"`
  33. Offset *int `help:"offset"`
  34. KeyOnly *bool `help:"show key only"`
  35. }
  36. type TagListOptions MetadataListOptions
  37. type TagValuePairsOptions struct {
  38. KeyOnly bool `help:"show key only"`
  39. SysMeta bool `help:"show system tags only"`
  40. CloudMeta bool `help:"show cloud tags only"`
  41. UserMeta bool `help:"show user tags only"`
  42. }
  43. func (opts *TagValuePairsOptions) Params() (jsonutils.JSONObject, error) {
  44. params := jsonutils.NewDict()
  45. if opts.KeyOnly {
  46. params.Add(jsonutils.JSONTrue, "key_only")
  47. }
  48. if opts.UserMeta {
  49. params.Add(jsonutils.JSONTrue, "user_meta")
  50. }
  51. if opts.CloudMeta {
  52. params.Add(jsonutils.JSONTrue, "cloud_meta")
  53. }
  54. if opts.SysMeta {
  55. params.Add(jsonutils.JSONTrue, "sys_meta")
  56. }
  57. return params, nil
  58. }
  59. func (opts *TagValuePairsOptions) Property() string {
  60. return "tag-value-pairs"
  61. }
  62. type ProjectTagValuePairsOptions struct {
  63. TagValuePairsOptions
  64. }
  65. func (opts *ProjectTagValuePairsOptions) Property() string {
  66. return "project-tag-value-pairs"
  67. }
  68. type DomainTagValuePairsOptions struct {
  69. TagValuePairsOptions
  70. }
  71. func (opts *DomainTagValuePairsOptions) Property() string {
  72. return "domain-tag-value-pairs"
  73. }
  74. type TagValueTreeOptions struct {
  75. Key []string `help:"the sequence of key to construct the tree"`
  76. ShowMap bool `help:"show map only"`
  77. }
  78. func (opts *TagValueTreeOptions) Params() (jsonutils.JSONObject, error) {
  79. params := jsonutils.NewDict()
  80. if len(opts.Key) > 0 {
  81. params.Add(jsonutils.NewStringArray(opts.Key), "keys")
  82. }
  83. if opts.ShowMap {
  84. params.Add(jsonutils.JSONTrue, "show_map")
  85. }
  86. return params, nil
  87. }
  88. func (opts *TagValueTreeOptions) Property() string {
  89. return "tag-value-tree"
  90. }
  91. type ProjectTagValueTreeOptions struct {
  92. TagValueTreeOptions
  93. }
  94. func (opts *ProjectTagValueTreeOptions) Property() string {
  95. return "project-tag-value-tree"
  96. }
  97. type DomainTagValueTreeOptions struct {
  98. TagValueTreeOptions
  99. }
  100. func (opts *DomainTagValueTreeOptions) Property() string {
  101. return "domain-tag-value-tree"
  102. }
  103. type ResourceIdOptions struct {
  104. ID string `help:"ID or name of the resource" json:"-"`
  105. }
  106. func (o *ResourceIdOptions) GetId() string {
  107. return o.ID
  108. }
  109. func (o *ResourceIdOptions) Params() (jsonutils.JSONObject, error) {
  110. return nil, nil
  111. }
  112. type ResourceMetadataOptions struct {
  113. ID string `help:"ID or name of resources" json:"-"`
  114. TAGS []string `help:"Tags info, eg: hypervisor=aliyun、os_type=Linux、os_version"`
  115. }
  116. func (opts *ResourceMetadataOptions) GetId() string {
  117. return opts.ID
  118. }
  119. func (opts *ResourceMetadataOptions) Params() (jsonutils.JSONObject, error) {
  120. params := jsonutils.NewDict()
  121. for _, tag := range opts.TAGS {
  122. sep := "="
  123. if strings.Index(tag, sep) < 0 {
  124. sep = ":"
  125. }
  126. info := strings.Split(tag, sep)
  127. if len(info) == 2 {
  128. if len(info[0]) == 0 {
  129. return nil, fmt.Errorf("invalidate tag info %s", tag)
  130. }
  131. params.Add(jsonutils.NewString(info[1]), info[0])
  132. } else if len(info) == 1 {
  133. params.Add(jsonutils.NewString(info[0]), info[0])
  134. } else {
  135. return nil, fmt.Errorf("invalidate tag info %s", tag)
  136. }
  137. }
  138. return params, nil
  139. }