mod_sshkeypairs.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/util/printutils"
  21. "yunion.io/x/onecloud/pkg/mcclient"
  22. "yunion.io/x/onecloud/pkg/mcclient/auth"
  23. "yunion.io/x/onecloud/pkg/mcclient/models"
  24. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  25. "yunion.io/x/onecloud/pkg/mcclient/modules"
  26. )
  27. type SSshkeypairManager struct {
  28. modulebase.ResourceManager
  29. }
  30. func (this *SSshkeypairManager) List(s *mcclient.ClientSession, params jsonutils.JSONObject) (*printutils.ListResult, error) {
  31. url := "/sshkeypairs"
  32. if params != nil {
  33. if queryStr := params.QueryString(); queryStr != "" {
  34. url = fmt.Sprintf("%s?%s", url, queryStr)
  35. }
  36. }
  37. body, err := modulebase.Get(this.ResourceManager, s, url, "sshkeypair")
  38. if err != nil {
  39. return nil, err
  40. }
  41. result := printutils.ListResult{Data: []jsonutils.JSONObject{body}}
  42. return &result, nil
  43. }
  44. func (this *SSshkeypairManager) FetchPrivateKey(ctx context.Context, userCred mcclient.TokenCredential) (string, error) {
  45. s := auth.GetSession(ctx, userCred, "")
  46. return this.FetchPrivateKeyBySession(ctx, s)
  47. }
  48. func (this *SSshkeypairManager) FetchPrivateKeyBySession(ctx context.Context, s *mcclient.ClientSession) (string, error) {
  49. kp, err := this.FetchKeypairBySession(ctx, s)
  50. if err != nil {
  51. return "", errors.Wrap(err, "FetchKeypairBySession")
  52. }
  53. return kp.PrivateKey, nil
  54. }
  55. func (this *SSshkeypairManager) FetchKeypairBySession(ctx context.Context, s *mcclient.ClientSession) (*models.SshKeypair, error) {
  56. userCred := s.GetToken()
  57. jd := jsonutils.NewDict()
  58. var jr jsonutils.JSONObject
  59. if userCred.HasSystemAdminPrivilege() {
  60. jd.Set("admin", jsonutils.JSONTrue)
  61. r, err := Sshkeypairs.List(s, jd)
  62. if err != nil {
  63. return nil, errors.Wrap(err, "get admin ssh key")
  64. }
  65. jr = r.Data[0]
  66. } else {
  67. r, err := Sshkeypairs.GetById(s, userCred.GetProjectId(), jd)
  68. if err != nil {
  69. return nil, errors.Wrap(err, "get project ssh key")
  70. }
  71. jr = r
  72. }
  73. kp := &models.SshKeypair{}
  74. if err := jr.Unmarshal(kp); err != nil {
  75. return nil, errors.Wrap(err, "unmarshal ssh key")
  76. }
  77. return kp, nil
  78. }
  79. var (
  80. Sshkeypairs SSshkeypairManager
  81. )
  82. func init() {
  83. Sshkeypairs = SSshkeypairManager{modules.NewComputeManager("sshkeypair", "sshkeypairs",
  84. []string{},
  85. []string{})}
  86. modules.RegisterCompute(&Sshkeypairs)
  87. }