services.py 16 KB

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