base.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 k8s
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/onecloud/pkg/mcclient/options"
  18. )
  19. type ClusterBaseOptions struct {
  20. Cluster string `default:"$K8S_CLUSTER" help:"Kubernetes cluster name"`
  21. }
  22. func (o ClusterBaseOptions) Params() *jsonutils.JSONDict {
  23. ret := jsonutils.NewDict()
  24. if o.Cluster != "" {
  25. ret.Add(jsonutils.NewString(o.Cluster), "cluster")
  26. }
  27. return ret
  28. }
  29. type ClusterResourceBaseOptions struct {
  30. Cluster string `default:"$K8S_CLUSTER" help:"Kubernetes cluster name"`
  31. NAME string `help:"Name of resource"`
  32. }
  33. func (o ClusterResourceBaseOptions) GetId() string {
  34. return o.NAME
  35. }
  36. func (o ClusterResourceBaseOptions) Params() (jsonutils.JSONObject, error) {
  37. params := jsonutils.NewDict()
  38. params.Add(jsonutils.NewString(o.Cluster), "cluster")
  39. params.Add(jsonutils.NewString(o.NAME), "name")
  40. return params, nil
  41. }
  42. type BaseListOptions struct {
  43. options.BaseListOptions
  44. Name string `help:"List by name"`
  45. }
  46. func (o BaseListOptions) Params() (*jsonutils.JSONDict, error) {
  47. params, err := o.BaseListOptions.Params()
  48. if err != nil {
  49. return nil, err
  50. }
  51. if o.Name != "" {
  52. params.Add(jsonutils.NewString(o.Name), "name")
  53. }
  54. return params, nil
  55. }
  56. type ResourceListOptions struct {
  57. ClusterBaseOptions
  58. BaseListOptions
  59. }
  60. func (o ResourceListOptions) Params() (jsonutils.JSONObject, error) {
  61. params, err := o.BaseListOptions.Params()
  62. if err != nil {
  63. return nil, err
  64. }
  65. params.Update(o.ClusterBaseOptions.Params())
  66. return params, nil
  67. }
  68. type ResourceGetOptions struct {
  69. ClusterBaseOptions
  70. NAME string `help:"Name ident of the resource"`
  71. }
  72. func (o ResourceGetOptions) Params() (jsonutils.JSONObject, error) {
  73. params := o.ClusterBaseOptions.Params()
  74. return params, nil
  75. }
  76. func (o ResourceGetOptions) GetId() string {
  77. return o.NAME
  78. }
  79. type ResourceDeleteOptions struct {
  80. ClusterBaseOptions
  81. NAME []string `help:"Name ident of the resources"`
  82. }
  83. func (o ResourceDeleteOptions) GetIds() []string {
  84. return o.NAME
  85. }
  86. func (o ResourceDeleteOptions) QueryParams() (jsonutils.JSONObject, error) {
  87. return nil, nil
  88. }
  89. func (o ResourceDeleteOptions) Params() (jsonutils.JSONObject, error) {
  90. params := o.ClusterBaseOptions.Params()
  91. return params, nil
  92. }
  93. type ResourceIdsOptions struct {
  94. ID []string `help:"Resource id"`
  95. }
  96. func (o ResourceIdsOptions) GetIds() []string {
  97. return o.ID
  98. }
  99. func (o ResourceIdsOptions) Params() (jsonutils.JSONObject, error) {
  100. return nil, nil
  101. }
  102. type NamespaceResourceListOptions struct {
  103. ResourceListOptions
  104. Namespace string `help:"Namespace of this resource"`
  105. }
  106. func (o NamespaceResourceListOptions) Params() (jsonutils.JSONObject, error) {
  107. params, err := o.ResourceListOptions.Params()
  108. if err != nil {
  109. return nil, err
  110. }
  111. if o.Namespace != "" {
  112. params.(*jsonutils.JSONDict).Add(jsonutils.NewString(o.Namespace), "namespace")
  113. }
  114. return params, nil
  115. }
  116. type NamespaceOptions struct {
  117. Namespace string `help:"Namespace of this resource"`
  118. }
  119. func (o NamespaceOptions) Params() *jsonutils.JSONDict {
  120. params := jsonutils.NewDict()
  121. if o.Namespace != "" {
  122. params.Add(jsonutils.NewString(o.Namespace), "namespace")
  123. }
  124. return params
  125. }
  126. type NamespaceResourceGetOptions struct {
  127. ResourceGetOptions
  128. NamespaceOptions
  129. }
  130. func (o NamespaceResourceGetOptions) Params() (jsonutils.JSONObject, error) {
  131. params, err := o.ResourceGetOptions.Params()
  132. if err != nil {
  133. return nil, err
  134. }
  135. params.(*jsonutils.JSONDict).Update(o.NamespaceOptions.Params())
  136. return params, nil
  137. }
  138. type NamespaceResourceDeleteOptions struct {
  139. ResourceDeleteOptions
  140. NamespaceOptions
  141. }
  142. func (o NamespaceResourceDeleteOptions) QueryParams() (jsonutils.JSONObject, error) {
  143. return nil, nil
  144. }
  145. func (o NamespaceResourceDeleteOptions) Params() (jsonutils.JSONObject, error) {
  146. params, err := o.ResourceDeleteOptions.Params()
  147. if err != nil {
  148. return nil, err
  149. }
  150. params.(*jsonutils.JSONDict).Update(o.NamespaceOptions.Params())
  151. return params, nil
  152. }
  153. type NamespaceWithClusterOptions struct {
  154. NamespaceOptions
  155. ClusterBaseOptions
  156. }
  157. func (o NamespaceWithClusterOptions) Params() *jsonutils.JSONDict {
  158. params := o.ClusterBaseOptions.Params()
  159. params.Update(o.NamespaceOptions.Params())
  160. return params
  161. }