keypaire.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 openstack
  15. import (
  16. "fmt"
  17. "net/url"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/pkg/util/rand"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. )
  22. type SKeypair struct {
  23. Fingerprint string
  24. Name string
  25. Type string
  26. PublicKey string
  27. }
  28. type SKeyPair struct {
  29. Keypair SKeypair
  30. }
  31. func (region *SRegion) GetKeypairs() ([]SKeyPair, error) {
  32. keypairs := []SKeyPair{}
  33. resource := "/os-keypairs"
  34. query := url.Values{}
  35. for {
  36. resp, err := region.ecsList(resource, query)
  37. if err != nil {
  38. return nil, errors.Wrap(err, "ecsList")
  39. }
  40. part := struct {
  41. Keypairs []SKeyPair
  42. KeypairsLinks SNextLinks
  43. }{}
  44. err = resp.Unmarshal(&part)
  45. if err != nil {
  46. return nil, errors.Wrap(err, "resp.Unmarshal")
  47. }
  48. keypairs = append(keypairs, part.Keypairs...)
  49. marker := part.KeypairsLinks.GetNextMark()
  50. if len(marker) == 0 {
  51. break
  52. }
  53. query.Set("marker", marker)
  54. }
  55. return keypairs, nil
  56. }
  57. func (region *SRegion) CreateKeypair(name, publicKey, Type string) (*SKeyPair, error) {
  58. params := map[string]map[string]string{
  59. "keypair": {
  60. "name": name,
  61. "public_key": publicKey,
  62. },
  63. }
  64. if len(Type) > 0 {
  65. params["keypair"]["type"] = Type
  66. }
  67. resp, err := region.ecsPost("/os-keypairs", params)
  68. if err != nil {
  69. return nil, errors.Wrap(err, "ecsPost")
  70. }
  71. keypair := &SKeyPair{}
  72. err = resp.Unmarshal(keypair)
  73. if err != nil {
  74. return nil, errors.Wrap(err, "resp.Unmarshal")
  75. }
  76. return keypair, nil
  77. }
  78. func (region *SRegion) DeleteKeypair(name string) error {
  79. _, err := region.ecsDelete("/os-keypairs/" + name)
  80. return err
  81. }
  82. func (region *SRegion) GetKeypair(name string) (*SKeyPair, error) {
  83. resp, err := region.ecsGet("/os-keypairs/" + name)
  84. if err != nil {
  85. return nil, errors.Wrap(err, "ecsGet")
  86. }
  87. keypair := &SKeyPair{}
  88. err = resp.Unmarshal(keypair)
  89. if err != nil {
  90. return nil, errors.Wrap(err, "resp.Unmarshal")
  91. }
  92. return keypair, nil
  93. }
  94. func (region *SRegion) syncKeypair(namePrefix, publicKey string) (string, error) {
  95. keypairs, err := region.GetKeypairs()
  96. if err != nil {
  97. return "", err
  98. }
  99. for _, keypair := range keypairs {
  100. if keypair.Keypair.PublicKey == publicKey {
  101. return keypair.Keypair.Name, nil
  102. }
  103. }
  104. randomString := func(prefix string, length int) string {
  105. return fmt.Sprintf("%s-%s", prefix, rand.String(length))
  106. }
  107. for i := 1; i < 10; i++ {
  108. name := randomString(namePrefix, i)
  109. if _, err := region.GetKeypair(name); err != nil {
  110. if errors.Cause(err) == cloudprovider.ErrNotFound {
  111. keypair, err := region.CreateKeypair(name, publicKey, "ssh")
  112. if err != nil {
  113. return "", errors.Wrapf(err, "CreateKeypair")
  114. }
  115. return keypair.Keypair.Name, nil
  116. }
  117. }
  118. }
  119. return "", fmt.Errorf("failed to find uniq name for keypair")
  120. }