OpenStack Compute (Nova) Client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

keypairs.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright 2011 OpenStack Foundation
  2. # All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. """
  16. Keypair interface (1.1 extension).
  17. """
  18. from novaclient import base
  19. class Keypair(base.Resource):
  20. """
  21. A keypair is a ssh key that can be injected into a server on launch.
  22. """
  23. def __repr__(self):
  24. return "<Keypair: %s>" % self.id
  25. def _add_details(self, info):
  26. dico = 'keypair' in info and \
  27. info['keypair'] or info
  28. for (k, v) in dico.items():
  29. # NOTE(rpodolyaka): keypair name allows us to uniquely identify
  30. # a specific keypair, while its id attribute
  31. # is nothing more than an implementation
  32. # detail. We can safely omit the id attribute
  33. # here to ensure setattr() won't raise
  34. # AttributeError trying to set read-only
  35. # property id
  36. if k != 'id':
  37. setattr(self, k, v)
  38. @property
  39. def id(self):
  40. return self.name
  41. def delete(self):
  42. self.manager.delete(self)
  43. class KeypairManager(base.ManagerWithFind):
  44. resource_class = Keypair
  45. keypair_prefix = "os-keypairs"
  46. is_alphanum_id_allowed = True
  47. def get(self, keypair):
  48. """
  49. Get a keypair.
  50. :param keypair: The ID of the keypair to get.
  51. :rtype: :class:`Keypair`
  52. """
  53. return self._get("/%s/%s" % (self.keypair_prefix, base.getid(keypair)),
  54. "keypair")
  55. def create(self, name, public_key=None):
  56. """
  57. Create a keypair
  58. :param name: name for the keypair to create
  59. :param public_key: existing public key to import
  60. """
  61. body = {'keypair': {'name': name}}
  62. if public_key:
  63. body['keypair']['public_key'] = public_key
  64. return self._create('/%s' % self.keypair_prefix, body, 'keypair')
  65. def delete(self, key):
  66. """
  67. Delete a keypair
  68. :param key: The :class:`Keypair` (or its ID) to delete.
  69. """
  70. self._delete('/%s/%s' % (self.keypair_prefix, base.getid(key)))
  71. def list(self):
  72. """
  73. Get a list of keypairs.
  74. """
  75. return self._list('/%s' % self.keypair_prefix, 'keypairs')