services.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. """
  2. Specifications for services available through a provider
  3. """
  4. class ProviderService(object):
  5. """
  6. Base interface for any service supported by a provider
  7. """
  8. def provider(self):
  9. """
  10. Returns the provider instance associated with this service.
  11. :rtype: ``object`` of :class:`.CloudProvider`
  12. :return: a Provider object
  13. """
  14. raise NotImplementedError(
  15. 'ComputeService.Provider not implemented by this provider')
  16. class ComputeService(ProviderService):
  17. """
  18. Base interface for compute service supported by a provider
  19. """
  20. def get_instance(self, instance_id):
  21. """
  22. Returns an instance given its id. Returns None
  23. if the object does not exist.
  24. :rtype: ``object`` of :class:`.Instance`
  25. :return: an Instance object
  26. """
  27. raise NotImplementedError(
  28. 'get_instance not implemented by this provider')
  29. def find_instance(self, name):
  30. """
  31. Searches for an instance by a given list of attributes.
  32. :rtype: ``object`` of :class:`.Instance`
  33. :return: an Instance object
  34. """
  35. raise NotImplementedError(
  36. 'find_instance not implemented by this provider')
  37. def list_instances(self):
  38. """
  39. List all instances.
  40. :rtype: ``list`` of :class:`.Instance`
  41. :return: list of Instance objects
  42. """
  43. raise NotImplementedError(
  44. 'list_instances not implemented by this provider')
  45. def instance_types(self):
  46. """
  47. Provides access to all Instance type related services in this provider.
  48. :rtype: ``object`` of :class:`.InstanceTypeService`
  49. :return: an InstanceTypeService object
  50. """
  51. raise NotImplementedError(
  52. 'instance_types not implemented by this provider')
  53. def list_regions(self):
  54. """
  55. List all data center regions for this provider.
  56. :rtype: ``list`` of :class:`.Region`
  57. :return: list of Region objects
  58. """
  59. raise NotImplementedError(
  60. 'list_regions not implemented by this provider')
  61. def create_instance(self, name, image, instance_type, zone=None,
  62. keypair=None, security_groups=None, user_data=None,
  63. block_device_mapping=None, network_interfaces=None,
  64. **kwargs):
  65. """
  66. Creates a new virtual machine instance.
  67. :type name: ``str``
  68. :param name: The name of the virtual machine instance
  69. :type image: ``MachineImage`` or ``str``
  70. :param image: The MachineImage object or id to boot the virtual machine
  71. with
  72. :type instance_type: ``InstanceType`` or ``str``
  73. :param instance_type: The InstanceType or name, specifying the size of
  74. the instance to boot into
  75. :type zone: ``Zone`` or ``str``
  76. :param zone: The Zone or its name, where the instance should be placed.
  77. :type keypair: ``KeyPair`` or ``str``
  78. :param keypair: The KeyPair object or its name, to set for the
  79. instance.
  80. :type security_groups: A ``list`` of ``SecurityGroup`` objects or a
  81. list of ``str`` names
  82. :param security_groups: A list of ``SecurityGroup`` objects or a list
  83. of ``SecurityGroup`` names, which should be
  84. assigned to this instance.
  85. :type user_data: ``str``
  86. :param user_data: An extra userdata object which is compatible with
  87. the provider.
  88. :type block_device_mapping: ``BlockDeviceMapping`` object
  89. :param block_device_mapping: A ``BlockDeviceMapping`` object which
  90. describes additional block device mappings
  91. for this instance.
  92. :type network_interfaces: ``NetworkInterfaceList`` object
  93. :param network_interfaces: A ``NetworkInterfaceList`` object which
  94. describes network interfaces for this
  95. instance.
  96. :rtype: `object`` of :class:`.Instance`
  97. :return: an instance of Instance class
  98. """
  99. raise NotImplementedError(
  100. 'create_instance not implemented by this provider')
  101. class VolumeService(ProviderService):
  102. """
  103. Base interface for a Volume Service
  104. """
  105. def get_volume(self, volume_id):
  106. """
  107. Returns a volume given its id. Returns None if the volume
  108. does not exist.
  109. :rtype: ``object`` of :class:`.Volume`
  110. :return: a Volume object
  111. """
  112. raise NotImplementedError(
  113. 'get_volume not implemented by this provider')
  114. def find_volume(self, name):
  115. """
  116. Searches for a volume by a given list of attributes.
  117. :rtype: ``object`` of :class:`.Volume`
  118. :return: a Volume object or ``None`` if not found
  119. """
  120. raise NotImplementedError(
  121. 'find_volume not implemented by this provider')
  122. def list_volumes(self):
  123. """
  124. List all volumes.
  125. :rtype: ``list`` of :class:`.Volume`
  126. :return: a list of Volume objects
  127. """
  128. raise NotImplementedError(
  129. 'list_volumes not implemented by this provider')
  130. def create_volume(self, name, size, zone, snapshot=None, description=None):
  131. """
  132. Creates a new volume.
  133. :type name: ``str``
  134. :param name: The name of the volume
  135. :type size: ``int``
  136. :param size: The size of the volume (in GB)
  137. :type zone: ``str`` or ``PlacementZone``
  138. :param zone: The availability zone in which the Volume will be created.
  139. :type description: ``str``
  140. :param description: An optional description that may be supported by
  141. some providers. Providers that do not support this property will return
  142. None.
  143. :rtype: ``object`` of :class:`.Volume`
  144. :return: a newly created Volume object
  145. """
  146. raise NotImplementedError(
  147. 'create_volume not implemented by this provider')
  148. class SnapshotService(ProviderService):
  149. """
  150. Base interface for a Snapshot Service
  151. """
  152. def get_snapshot(self, volume_id):
  153. """
  154. Returns a snapshot given its id. Returns None if the snapshot
  155. does not exist.
  156. :rtype: ``object`` of :class:`.Snapshot`
  157. :return: a Snapshot object
  158. """
  159. raise NotImplementedError(
  160. 'get_snapshot not implemented by this provider')
  161. def find_snapshot(self, name):
  162. """
  163. Searches for a snapshot by a given list of attributes.
  164. :rtype: ``object`` of :class:`.Snapshot`
  165. :return: a Snapshot object or ``None`` if not found
  166. """
  167. raise NotImplementedError(
  168. 'find_snapshot not implemented by this provider')
  169. def list_snapshots(self):
  170. """
  171. List all snapshots.
  172. :rtype: ``list`` of :class:`.Snapshot`
  173. :return: a list of Snapshot objects
  174. """
  175. raise NotImplementedError(
  176. 'list_snapshots not implemented by this provider')
  177. def create_snapshot(self, name, volume, description=None):
  178. """
  179. Creates a new snapshot off a volume.
  180. :type name: ``str``
  181. :param name: The name of the snapshot
  182. :type volume: ``str`` or ``Volume``
  183. :param volume: The volume to create a snapshot of.
  184. :type description: ``str``
  185. :param description: An optional description that may be supported by
  186. some providers. Providers that do not support this property will return
  187. None.
  188. :rtype: ``object`` of :class:`.Snapshot`
  189. :return: a newly created Snapshot object
  190. """
  191. raise NotImplementedError(
  192. 'create_snapshot not implemented by this provider')
  193. class BlockStoreService(ProviderService):
  194. """
  195. Base interface for a Block Store Service
  196. """
  197. @property
  198. def volumes(self):
  199. """
  200. Provides access to the volumes (i.e., block storage) for this provider.
  201. :rtype: ``object`` of :class:`.VolumeService`
  202. :return: a VolumeService object
  203. """
  204. raise NotImplementedError(
  205. 'CloudProvider.block_store not implemented by this provider')
  206. @property
  207. def snapshots(self):
  208. """
  209. Provides access to volume snapshots for this provider.
  210. :rtype: ``object`` of :class:`.SnapshotService`
  211. :return: an SnapshotService object
  212. """
  213. raise NotImplementedError(
  214. 'CloudProvider.object_store not implemented by this provider')
  215. class ImageService(ProviderService):
  216. """
  217. Base interface for an Image Service
  218. """
  219. def get_image(self, image_id):
  220. """
  221. Returns an Image given its id. Returns None if the Image does not
  222. exist.
  223. :rtype: ``object`` of :class:`.Image`
  224. :return: an Image instance
  225. """
  226. raise NotImplementedError(
  227. 'get_image implemented by this provider')
  228. def find_image(self, name):
  229. """
  230. Searches for an image by a given list of attributes
  231. :rtype: ``object`` of :class:`.Image`
  232. :return: an Image instance
  233. """
  234. raise NotImplementedError(
  235. 'find_image not implemented by this provider')
  236. def list_images(self):
  237. """
  238. List all images.
  239. :rtype: ``list`` of :class:`.Image`
  240. :return: list of image objects
  241. """
  242. raise NotImplementedError(
  243. 'list_images not implemented by this provider')
  244. class ObjectStoreService(ProviderService):
  245. """
  246. Base interface for an Object Storage Service
  247. """
  248. def get_container(self, container_id):
  249. """
  250. Returns a container given its id. Returns None if the container
  251. does not exist.
  252. :rtype: ``object`` of :class:`.Container`
  253. :return: a Container instance
  254. """
  255. raise NotImplementedError(
  256. 'get_container implemented by this provider')
  257. def find_container(self, name):
  258. """
  259. Searches for a container by a given list of attributes
  260. :rtype: ``object`` of :class:`.Container`
  261. :return: a Container instance
  262. """
  263. raise NotImplementedError(
  264. 'find_container not implemented by this provider')
  265. def list_containers(self):
  266. """
  267. List all containers.
  268. :rtype: ``list`` of :class:`.Container`
  269. :return: list of container objects
  270. """
  271. raise NotImplementedError(
  272. 'list_containers not implemented by this provider')
  273. def create_container(self, name, location=None):
  274. """
  275. Create a new container.
  276. :type name: str
  277. :param name: The name of this container
  278. :type location: ``object`` of :class:`.Region`
  279. :param location: The region in which to place this container
  280. :return: a Container object
  281. :rtype: ``object`` of :class:`.Container`
  282. """
  283. raise NotImplementedError(
  284. 'create_container not implemented by this provider')
  285. class SecurityService(ProviderService):
  286. """
  287. Base interface for a Security Service.
  288. """
  289. @property
  290. def key_pairs(self):
  291. """
  292. Provides access to key pairs for this provider.
  293. :rtype: ``object`` of :class:`.KeyPairService`
  294. :return: a KeyPairService object
  295. """
  296. return self._key_pairs
  297. @property
  298. def security_groups(self):
  299. """
  300. Provides access to security groups for this provider.
  301. :rtype: ``object`` of :class:`.SecurityGroupService`
  302. :return: a SecurityGroupService object
  303. """
  304. return self._security_groups
  305. class KeyPairService(ProviderService):
  306. """
  307. Base interface for key pairs.
  308. """
  309. def list(self):
  310. """
  311. List all key pairs associated with this account.
  312. :rtype: ``list`` of :class:`.KeyPair`
  313. :return: list of KeyPair objects
  314. """
  315. raise NotImplementedError(
  316. 'list_key_pairs not implemented by this provider')
  317. def create(self, key_name):
  318. """
  319. Create a new keypair.
  320. :type key_name: str
  321. :param key_name: The name of the key pair to be created.
  322. :rtype: ``object`` of :class:`.KeyPair`
  323. :return: A keypair instance
  324. """
  325. raise NotImplementedError(
  326. 'create_key_pair not implemented by this provider')
  327. def delete(self, key_name):
  328. """
  329. Delete an existing SecurityGroup.
  330. :type key_name: str
  331. :param key_name: The name of the key pair to be deleted.
  332. :rtype: ``bool``
  333. :return: ``True`` if the key does not exist, ``False`` otherwise. Note
  334. that this implies that the key may not have been deleted by
  335. this method but instead has not existed at all.
  336. """
  337. raise NotImplementedError(
  338. 'delete not implemented by this provider')
  339. class SecurityGroupService(ProviderService):
  340. """
  341. Base interface for security groups.
  342. """
  343. def list(self):
  344. """
  345. List all security groups associated with this account.
  346. :rtype: ``list`` of :class:`.SecurityGroup`
  347. :return: list of SecurityGroup objects
  348. """
  349. raise NotImplementedError(
  350. 'list not implemented by this provider')
  351. def create(self, name, description):
  352. """
  353. Create a new SecurityGroup.
  354. :type name: str
  355. :param name: The name of the new security group.
  356. :type description: str
  357. :param description: The description of the new security group.
  358. :rtype: ``object`` of :class:`.SecurityGroup`
  359. :return: A SecurityGroup instance or ``None`` if one was not created.
  360. """
  361. raise NotImplementedError(
  362. 'create not implemented by this provider')
  363. def get(self, groupnames=None, group_ids=None):
  364. """
  365. Get a security group.
  366. :type groupnames: list
  367. :param groupnames: A list of the names of security groups to retrieve.
  368. If not provided, all security groups will be
  369. returned.
  370. :type group_ids: list
  371. :param group_ids: A list of IDs of security groups to retrieve.
  372. If not provided, all security groups will be
  373. returned.
  374. :rtype: list of :class:`SecurityGroup`
  375. :return: A list of SecurityGroup objects or an empty list if none found.
  376. """
  377. raise NotImplementedError(
  378. 'get not implemented by this provider')
  379. def delete(self, group_id):
  380. """
  381. Delete an existing SecurityGroup.
  382. :type group_id: str
  383. :param group_id: The security group ID to be deleted.
  384. :rtype: ``bool``
  385. :return: ``True`` if the security group does not exist, ``False``
  386. otherwise. Note that this implies that the group may not have
  387. been deleted by this method but instead has not existed in
  388. the first place.
  389. """
  390. raise NotImplementedError(
  391. 'delete not implemented by this provider')
  392. class InstanceTypesService(object):
  393. def list(self):
  394. """
  395. List all instance types.
  396. :rtype: ``list`` of :class:`.InstanceType`
  397. :return: list of InstanceType objects
  398. """
  399. raise NotImplementedError(
  400. 'InstanceTypesService.list not implemented by this provider')
  401. def find_by_name(self, name):
  402. """
  403. Searches for an instance by a given list of attributes.
  404. :rtype: ``object`` of :class:`.InstanceType`
  405. :return: an Instance object
  406. """
  407. raise NotImplementedError(
  408. 'InstanceTypesService.find_instance not implemented by this'
  409. 'provider')