keypair.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 ksyun
  15. import "time"
  16. type SKeypair struct {
  17. KeyName string
  18. KeyId string
  19. IsChecked bool
  20. CreateTime time.Time
  21. PublicKey string
  22. }
  23. func (cli *SKsyunClient) GetKeypairs() ([]SKeypair, error) {
  24. params := map[string]interface{}{
  25. "MaxResults": "1000",
  26. }
  27. ret := []SKeypair{}
  28. for {
  29. body, err := cli.sksRequest("", "DescribeKeys", params)
  30. if err != nil {
  31. return nil, err
  32. }
  33. part := struct {
  34. KeySet []SKeypair
  35. NextToken string
  36. }{}
  37. err = body.Unmarshal(&part)
  38. if err != nil {
  39. return nil, err
  40. }
  41. ret = append(ret, part.KeySet...)
  42. if len(part.NextToken) == 0 {
  43. break
  44. }
  45. params["NextToken"] = part.NextToken
  46. }
  47. return ret, nil
  48. }
  49. func (cli *SKsyunClient) CreateKeypair(name, publicKey string) (*SKeypair, error) {
  50. params := map[string]interface{}{
  51. "KeyName": name,
  52. "PublicKey": publicKey,
  53. "IsCheck": "true",
  54. }
  55. resp, err := cli.sksRequest("", "ImportKey", params)
  56. if err != nil {
  57. return nil, err
  58. }
  59. ret := SKeypair{}
  60. err = resp.Unmarshal(&ret, "Key")
  61. if err != nil {
  62. return nil, err
  63. }
  64. return &ret, nil
  65. }