services.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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/pkg/util/shellutils"
  20. "yunion.io/x/onecloud/pkg/mcclient"
  21. modules "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  22. "yunion.io/x/onecloud/pkg/util/fileutils2"
  23. )
  24. func init() {
  25. type ServiceListOptions struct {
  26. Limit int64 `help:"Limit, default 0, i.e. no limit" default:"20"`
  27. Offset int64 `help:"Offset, default 0, i.e. no offset"`
  28. Name string `help:"Search by name"`
  29. Type string `help:"Search by type"`
  30. Search string `help:"search any fields"`
  31. }
  32. R(&ServiceListOptions{}, "service-list", "List services", func(s *mcclient.ClientSession, args *ServiceListOptions) error {
  33. query := jsonutils.NewDict()
  34. if args.Limit > 0 {
  35. query.Add(jsonutils.NewInt(args.Limit), "limit")
  36. }
  37. if args.Offset > 0 {
  38. query.Add(jsonutils.NewInt(args.Offset), "offset")
  39. }
  40. if len(args.Name) > 0 {
  41. query.Add(jsonutils.NewString(args.Name), "name")
  42. }
  43. if len(args.Type) > 0 {
  44. query.Add(jsonutils.NewString(args.Type), "type")
  45. }
  46. if len(args.Search) > 0 {
  47. query.Add(jsonutils.NewString(args.Search), "search")
  48. }
  49. result, err := modules.ServicesV3.List(s, query)
  50. if err != nil {
  51. return err
  52. }
  53. printList(result, modules.ServicesV3.GetColumns(s))
  54. return nil
  55. })
  56. type ServiceShowOptions struct {
  57. ID string `help:"ID of service"`
  58. }
  59. R(&ServiceShowOptions{}, "service-show", "Show details of a service", func(s *mcclient.ClientSession, args *ServiceShowOptions) error {
  60. srvId, err := modules.ServicesV3.GetId(s, args.ID, nil)
  61. if err != nil {
  62. return err
  63. }
  64. result, err := modules.ServicesV3.Get(s, srvId, nil)
  65. if err != nil {
  66. return err
  67. }
  68. printObject(result)
  69. return nil
  70. })
  71. R(&ServiceShowOptions{}, "service-delete", "Delete a service", func(s *mcclient.ClientSession, args *ServiceShowOptions) error {
  72. srvId, err := modules.ServicesV3.GetId(s, args.ID, nil)
  73. if err != nil {
  74. return err
  75. }
  76. result, err := modules.ServicesV3.Delete(s, srvId, nil)
  77. if err != nil {
  78. return err
  79. }
  80. printObject(result)
  81. return nil
  82. })
  83. type ServiceCreateOptions struct {
  84. TYPE string `help:"Service type"`
  85. NAME string `help:"Service name"`
  86. Desc string `help:"Description"`
  87. Enabled bool `help:"Enabeld"`
  88. Disabled bool `help:"Disabled"`
  89. }
  90. R(&ServiceCreateOptions{}, "service-create", "Create a service", func(s *mcclient.ClientSession, args *ServiceCreateOptions) error {
  91. params := jsonutils.NewDict()
  92. params.Add(jsonutils.NewString(args.TYPE), "type")
  93. params.Add(jsonutils.NewString(args.NAME), "name")
  94. if len(args.Desc) > 0 {
  95. params.Add(jsonutils.NewString(args.Desc), "description")
  96. }
  97. if args.Enabled && !args.Disabled {
  98. params.Add(jsonutils.JSONTrue, "enabled")
  99. } else if !args.Enabled && args.Disabled {
  100. params.Add(jsonutils.JSONFalse, "enabled")
  101. }
  102. srv, err := modules.ServicesV3.Create(s, params)
  103. if err != nil {
  104. return err
  105. }
  106. printObject(srv)
  107. return nil
  108. })
  109. type ServiceUpdateOptions struct {
  110. ID string `help:"ID or name of the service"`
  111. Type string `help:"Service type"`
  112. Name string `help:"Service name"`
  113. Desc string `help:"Description"`
  114. Enabled bool `help:"Enabeld"`
  115. Disabled bool `help:"Disabled"`
  116. }
  117. R(&ServiceUpdateOptions{}, "service-update", "Update a service", func(s *mcclient.ClientSession, args *ServiceUpdateOptions) error {
  118. srvId, err := modules.ServicesV3.GetId(s, args.ID, nil)
  119. if err != nil {
  120. return err
  121. }
  122. params := jsonutils.NewDict()
  123. if len(args.Type) > 0 {
  124. params.Add(jsonutils.NewString(args.Type), "type")
  125. }
  126. if len(args.Name) > 0 {
  127. params.Add(jsonutils.NewString(args.Name), "name")
  128. }
  129. if len(args.Desc) > 0 {
  130. params.Add(jsonutils.NewString(args.Desc), "description")
  131. }
  132. if args.Enabled && !args.Disabled {
  133. params.Add(jsonutils.JSONTrue, "enabled")
  134. } else if !args.Enabled && args.Disabled {
  135. params.Add(jsonutils.JSONFalse, "enabled")
  136. }
  137. srv, err := modules.ServicesV3.Patch(s, srvId, params)
  138. if err != nil {
  139. return err
  140. }
  141. printObject(srv)
  142. return nil
  143. })
  144. type ServiceConfigShowOptions struct {
  145. SERVICE string `help:"service name or id"`
  146. }
  147. R(&ServiceConfigShowOptions{}, "service-config-show", "Show configs of a service", func(s *mcclient.ClientSession, args *ServiceConfigShowOptions) error {
  148. conf, err := modules.ServicesV3.GetSpecific(s, args.SERVICE, "config", nil)
  149. if err != nil {
  150. return err
  151. }
  152. fmt.Println(conf.PrettyString())
  153. return nil
  154. })
  155. type ServiceConfigShow2Options struct {
  156. TYPE string `help:"service type"`
  157. }
  158. R(&ServiceConfigShow2Options{}, "service-config-show2", "Show configs of a service", func(s *mcclient.ClientSession, args *ServiceConfigShow2Options) error {
  159. conf, err := modules.ServicesV3.GetConfig(s, args.TYPE)
  160. if err != nil {
  161. return err
  162. }
  163. fmt.Println(conf.PrettyString())
  164. return nil
  165. })
  166. type ServiceConfigOptions struct {
  167. SERVICE string `help:"service name or id"`
  168. Config []string `help:"config options, can be a JSON, a YAML or a key=value pair, e.g:\n * JSON\n '{\"default\":{\"password_expiration_seconds\":300}}'\n * YAML\n default:\n password_expiration_seconds: 300\n * A key=value pair (under default section)\n password_expiration_seconds=300\n"`
  169. Remove bool `help:"remove config"`
  170. }
  171. R(&ServiceConfigOptions{}, "service-config", "Add config to service", func(s *mcclient.ClientSession, args *ServiceConfigOptions) error {
  172. config := jsonutils.NewDict()
  173. if args.Remove {
  174. config.Add(jsonutils.NewString("remove"), "action")
  175. } else {
  176. config.Add(jsonutils.NewString("update"), "action")
  177. }
  178. for _, c := range args.Config {
  179. json, _ := jsonutils.ParseString(c)
  180. if json != nil {
  181. if _, ok := json.(*jsonutils.JSONDict); ok {
  182. subconf := jsonutils.NewDict()
  183. subconf.Add(json, "config")
  184. config.Update(subconf)
  185. continue
  186. }
  187. }
  188. yaml, _ := jsonutils.ParseYAML(c)
  189. if yaml != nil {
  190. if _, ok := yaml.(*jsonutils.JSONDict); ok {
  191. subconf := jsonutils.NewDict()
  192. subconf.Add(yaml, "config")
  193. config.Update(subconf)
  194. continue
  195. }
  196. }
  197. pos := strings.IndexByte(c, '=')
  198. if pos < 0 {
  199. return fmt.Errorf("%s is not a key=value pair", c)
  200. }
  201. key := strings.TrimSpace(c[:pos])
  202. value := strings.TrimSpace(c[pos+1:])
  203. var v jsonutils.JSONObject
  204. if value == "true" || value == "false" {
  205. v = jsonutils.NewBool(value == "true")
  206. } else {
  207. v = jsonutils.NewString(value)
  208. }
  209. config.Add(v, "config", "default", key)
  210. }
  211. nconf, err := modules.ServicesV3.PerformAction(s, args.SERVICE, "config", config)
  212. if err != nil {
  213. return err
  214. }
  215. fmt.Println(nconf.PrettyString())
  216. return nil
  217. })
  218. type ServiceConfigYamlOptions struct {
  219. SERVICE string `help:"service name or id"`
  220. YAML string `help:"config yaml file"`
  221. }
  222. R(&ServiceConfigYamlOptions{}, "service-config-yaml", "Config service with a yaml file", func(s *mcclient.ClientSession, args *ServiceConfigYamlOptions) error {
  223. content, err := fileutils2.FileGetContents(args.YAML)
  224. if err != nil {
  225. return err
  226. }
  227. yamlJson, err := jsonutils.ParseYAML(content)
  228. if err != nil {
  229. return err
  230. }
  231. config := jsonutils.NewDict()
  232. config.Add(yamlJson, "config", "default")
  233. nconf, err := modules.ServicesV3.PerformAction(s, args.SERVICE, "config", config)
  234. if err != nil {
  235. return err
  236. }
  237. fmt.Println(nconf.PrettyString())
  238. return nil
  239. })
  240. type ServiceConfigEditOptions struct {
  241. SERVICE string `help:"service name or id"`
  242. }
  243. R(&ServiceConfigEditOptions{}, "service-config-edit", "Edit config yaml of a service", func(s *mcclient.ClientSession, args *ServiceConfigEditOptions) error {
  244. conf, err := modules.ServicesV3.GetSpecific(s, args.SERVICE, "config", nil)
  245. if err != nil {
  246. return err
  247. }
  248. confJson, err := conf.Get("config")
  249. if err != nil {
  250. return err
  251. }
  252. content, err := shellutils.Edit(confJson.YAMLString())
  253. if err != nil {
  254. return err
  255. }
  256. yamlJson, err := jsonutils.ParseYAML(content)
  257. if err != nil {
  258. return err
  259. }
  260. config := jsonutils.NewDict()
  261. config.Add(yamlJson, "config")
  262. nconf, err := modules.ServicesV3.PerformAction(s, args.SERVICE, "config", config)
  263. if err != nil {
  264. return err
  265. }
  266. fmt.Println(nconf.PrettyString())
  267. return nil
  268. })
  269. }