keypair.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 ctyun
  15. import (
  16. "fmt"
  17. "strconv"
  18. "time"
  19. "github.com/aokoli/goutils"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/pkg/errors"
  22. )
  23. type SKeypair struct {
  24. Fingerprint string
  25. KeyPairName string
  26. KeyPairId string
  27. PublicKey string
  28. }
  29. func (self *SRegion) GetKeypairs(name string) ([]SKeypair, error) {
  30. pageNo := 1
  31. params := map[string]interface{}{
  32. "pageNo": pageNo,
  33. "pageSize": 50,
  34. }
  35. if len(name) > 0 {
  36. params["queryContent"] = name
  37. }
  38. ret := []SKeypair{}
  39. for {
  40. resp, err := self.post(SERVICE_ECS, "/v4/ecs/keypair/details", params)
  41. if err != nil {
  42. return nil, err
  43. }
  44. part := struct {
  45. ReturnObj struct {
  46. Results []SKeypair
  47. }
  48. TotalCount int
  49. }{}
  50. err = resp.Unmarshal(&part)
  51. if err != nil {
  52. return nil, errors.Wrapf(err, "Unmarshal")
  53. }
  54. ret = append(ret, part.ReturnObj.Results...)
  55. if len(ret) >= part.TotalCount || len(part.ReturnObj.Results) == 0 {
  56. break
  57. }
  58. pageNo++
  59. params["pageNo"] = pageNo
  60. }
  61. return ret, nil
  62. }
  63. func (self *SRegion) lookUpKeypair(publicKey string) (*SKeypair, error) {
  64. keypairs, err := self.GetKeypairs("")
  65. if err != nil {
  66. return nil, err
  67. }
  68. for i := range keypairs {
  69. if keypairs[i].PublicKey == publicKey {
  70. return &keypairs[i], nil
  71. }
  72. }
  73. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "by public key")
  74. }
  75. func (self *SRegion) ImportKeypair(name, publicKey string) (*SKeypair, error) {
  76. params := map[string]interface{}{
  77. "keyPairName": name,
  78. "publicKey": publicKey,
  79. }
  80. _, err := self.post(SERVICE_ECS, "/v4/ecs/keypair/import-keypair", params)
  81. if err != nil {
  82. return nil, err
  83. }
  84. keypairs, err := self.GetKeypairs(name)
  85. if err != nil {
  86. return nil, err
  87. }
  88. for i := range keypairs {
  89. if keypairs[i].KeyPairName == name {
  90. return &keypairs[i], nil
  91. }
  92. }
  93. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "after create %s", name)
  94. }
  95. func (self *SRegion) importKeypair(publicKey string) (*SKeypair, error) {
  96. prefix, err := goutils.RandomAlphabetic(6)
  97. if err != nil {
  98. return nil, fmt.Errorf("publicKey error %s", err)
  99. }
  100. name := prefix + strconv.FormatInt(time.Now().Unix(), 10)
  101. return self.ImportKeypair(name, publicKey)
  102. }
  103. func (self *SRegion) syncKeypair(publicKey string) (*SKeypair, error) {
  104. key, err := self.lookUpKeypair(publicKey)
  105. if err == nil {
  106. return key, nil
  107. }
  108. return self.importKeypair(publicKey)
  109. }