mod_services.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. "yunion.io/x/jsonutils"
  17. "yunion.io/x/pkg/errors"
  18. identity_api "yunion.io/x/onecloud/pkg/apis/identity"
  19. "yunion.io/x/onecloud/pkg/httperrors"
  20. "yunion.io/x/onecloud/pkg/mcclient"
  21. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  22. "yunion.io/x/onecloud/pkg/mcclient/modules"
  23. )
  24. type SServiceManager struct {
  25. modulebase.ResourceManager
  26. }
  27. var (
  28. Services modulebase.ResourceManager
  29. ServicesV3 SServiceManager
  30. )
  31. func (manager *SServiceManager) GetConfig(s *mcclient.ClientSession, typeStr string) (*jsonutils.JSONDict, error) {
  32. opts := struct {
  33. Type string
  34. Scope string
  35. }{
  36. Type: s.GetServiceName(typeStr),
  37. Scope: "system",
  38. }
  39. list, err := manager.List(s, jsonutils.Marshal(opts))
  40. if err != nil {
  41. return nil, errors.Wrap(err, "List")
  42. }
  43. if len(list.Data) == 0 {
  44. return nil, errors.Wrapf(httperrors.ErrNotFound, "type %s not found", typeStr)
  45. }
  46. srv := identity_api.SService{}
  47. err = list.Data[0].Unmarshal(&srv)
  48. if err != nil {
  49. return nil, errors.Wrap(err, "Unmarshal service")
  50. }
  51. configObj, err := manager.GetSpecific(s, srv.Id, "config", nil)
  52. if err != nil {
  53. return nil, errors.Wrap(err, "GetSpecific config")
  54. }
  55. if configObj.Contains("config") {
  56. configObj, _ = configObj.Get("config")
  57. }
  58. return configObj.(*jsonutils.JSONDict), nil
  59. }
  60. func init() {
  61. Services = modules.NewIdentityManager("OS-KSADM:service",
  62. "OS-KSADM:services",
  63. []string{},
  64. []string{"ID", "Name", "Type", "Description"})
  65. modules.Register(&Services)
  66. ServicesV3 = SServiceManager{
  67. ResourceManager: modules.NewIdentityV3Manager("service",
  68. "services",
  69. []string{},
  70. []string{"ID", "Name", "Type", "Description"}),
  71. }
  72. modules.Register(&ServicesV3)
  73. }