mod_dbinstanceaccounts.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 compute
  15. import (
  16. "github.com/pkg/errors"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/utils"
  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. var (
  25. DBInstanceAccounts SDBInstanceAccountManager
  26. )
  27. type SDBInstanceAccountManager struct {
  28. modulebase.ResourceManager
  29. }
  30. func (this *SDBInstanceAccountManager) GetLoginInfo(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  31. data, err := this.Get(s, id, nil)
  32. if err != nil {
  33. return nil, err
  34. }
  35. account := struct {
  36. Id string
  37. Name string
  38. Secret string
  39. }{}
  40. err = data.Unmarshal(&account)
  41. if err != nil {
  42. return nil, errors.Wrap(err, "data.Unmarshal")
  43. }
  44. if len(account.Secret) == 0 {
  45. return nil, httperrors.NewNotFoundError("No login secret found")
  46. }
  47. password, err := utils.DescryptAESBase64(account.Id, account.Secret)
  48. if err != nil {
  49. return nil, errors.Wrap(err, "Descrypt")
  50. }
  51. return jsonutils.Marshal(map[string]string{
  52. "account": account.Name,
  53. "password": password,
  54. }), nil
  55. }
  56. func init() {
  57. DBInstanceAccounts = SDBInstanceAccountManager{modules.NewComputeManager("dbinstanceaccount", "dbinstanceaccounts",
  58. []string{"ID", "Name", "DBInstance_id", "Status", "Dbinstanceprivileges"},
  59. []string{})}
  60. modules.RegisterCompute(&DBInstanceAccounts)
  61. }