services.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. """
  2. Specifications for services available through a provider
  3. """
  4. from abc import ABCMeta, abstractmethod, abstractproperty
  5. from cloudbridge.cloud.interfaces.resources import PageableObjectMixin
  6. class ProviderService(object):
  7. """
  8. Base interface for any service supported by a provider. This interface
  9. has a provider property that can be used to access the provider associated
  10. with this service.
  11. """
  12. __metaclass__ = ABCMeta
  13. @abstractproperty
  14. def provider(self):
  15. """
  16. Returns the provider instance associated with this service.
  17. :rtype: :class:`.CloudProvider`
  18. :return: a CloudProvider object
  19. """
  20. pass
  21. class ComputeService(ProviderService):
  22. """
  23. The compute service interface is a collection of services that provides
  24. access to the underlying compute related services in a provider. For
  25. example, the compute.instances service can be used to launch a new
  26. instance, and the compute.images service can be used to list available
  27. machine images.
  28. """
  29. __metaclass__ = ABCMeta
  30. @abstractproperty
  31. def images(self):
  32. """
  33. Provides access to all Image related services in this provider.
  34. (e.g. Glance in Openstack)
  35. Example:
  36. .. code-block:: python
  37. # print all images
  38. for image in provider.compute.images:
  39. print(image.id, image.name)
  40. # print only first 50 images
  41. for image in provider.compute.images.list(limit=50):
  42. print(image.id, image.name)
  43. # find image by name
  44. image = provider.compute.images.find(name='Ubuntu 14.04')
  45. print(image.id, image.name)
  46. :rtype: :class:`.ImageService`
  47. :return: an ImageService object
  48. """
  49. pass
  50. @abstractproperty
  51. def instance_types(self):
  52. """
  53. Provides access to all Instance type related services in this provider.
  54. Example:
  55. .. code-block:: python
  56. # list all instance sizes
  57. for inst_type in provider.compute.instance_types:
  58. print(inst_type.id, inst_type.name)
  59. # find a specific size by name
  60. inst_type = provider.compute.instance_types.find(name='m1.small')
  61. print(inst_type.vcpus)
  62. :rtype: :class:`.InstanceTypeService`
  63. :return: an InstanceTypeService object
  64. """
  65. pass
  66. @abstractproperty
  67. def instances(self):
  68. """
  69. Provides access to all Instance related services in this provider.
  70. Example:
  71. .. code-block:: python
  72. # launch a new instance
  73. image = provider.compute.images.find(name='Ubuntu 14.04')[0]
  74. size = provider.compute.instance_types.find(name='m1.small')
  75. instance = provider.compute.instances.create('Hello', image, size)
  76. print(instance.id, instance.name)
  77. :rtype: :class:`.InstanceService`
  78. :return: an InstanceService object
  79. """
  80. pass
  81. @abstractproperty
  82. def regions(self):
  83. """
  84. Provides access to all Region related services in this provider.
  85. Example:
  86. .. code-block:: python
  87. for region in provider.compute.regions:
  88. print("Region: ", region.name)
  89. for zone in region.zones:
  90. print("\\tZone: ", zone.name)
  91. :rtype: :class:`.RegionService`
  92. :return: a RegionService object
  93. """
  94. pass
  95. class InstanceService(PageableObjectMixin, ProviderService):
  96. """
  97. Provides access to instances in a provider, including creating,
  98. listing and deleting instances.
  99. """
  100. __metaclass__ = ABCMeta
  101. @abstractmethod
  102. def __iter__(self):
  103. """
  104. Iterate through the list of instances.
  105. Example:
  106. ```
  107. for instance in provider.compute.instances:
  108. print(instance.name)
  109. ```
  110. :rtype: ``object`` of :class:`.Instance`
  111. :return: an Instance object
  112. """
  113. pass
  114. @abstractmethod
  115. def get(self, instance_id):
  116. """
  117. Returns an instance given its id. Returns None
  118. if the object does not exist.
  119. :rtype: ``object`` of :class:`.Instance`
  120. :return: an Instance object
  121. """
  122. pass
  123. @abstractmethod
  124. def find(self, name):
  125. """
  126. Searches for an instance by a given list of attributes.
  127. :rtype: ``object`` of :class:`.Instance`
  128. :return: an Instance object
  129. """
  130. pass
  131. @abstractmethod
  132. def list(self, limit=None, marker=None):
  133. """
  134. List available instances.
  135. The returned results can be limited with limit and marker. If not
  136. specified, the limit defaults to a global default.
  137. See :func:`~interfaces.resources.PageableObjectMixin.list`
  138. for more information on how to page through returned results.
  139. example::
  140. # List instances
  141. instlist = provider.compute.instances.list()
  142. for instance in instlist:
  143. print("Instance Data: {0}", instance)
  144. :type limit: ``int``
  145. :param limit: The maximum number of objects to return
  146. :type marker: ``str``
  147. :param marker: The marker is an opaque identifier used to assist
  148. in paging through very long lists of objects. It is returned on each
  149. invocation of the list method.
  150. :rtype: ``ResultList`` of :class:`.Instance`
  151. :return: A ResultList object containing a list of Instances
  152. """
  153. pass
  154. @abstractmethod
  155. def create(self, name, image, instance_type, zone=None,
  156. keypair=None, security_groups=None, user_data=None,
  157. launch_config=None,
  158. **kwargs):
  159. """
  160. Creates a new virtual machine instance.
  161. :type name: ``str``
  162. :param name: The name of the virtual machine instance
  163. :type image: ``MachineImage`` or ``str``
  164. :param image: The MachineImage object or id to boot the virtual machine
  165. with
  166. :type instance_type: ``InstanceType`` or ``str``
  167. :param instance_type: The InstanceType or name, specifying the size of
  168. the instance to boot into
  169. :type zone: ``Zone`` or ``str``
  170. :param zone: The Zone or its name, where the instance should be placed.
  171. :type keypair: ``KeyPair`` or ``str``
  172. :param keypair: The KeyPair object or its name, to set for the
  173. instance.
  174. :type security_groups: A ``list`` of ``SecurityGroup`` objects or a
  175. list of ``str`` names
  176. :param security_groups: A list of ``SecurityGroup`` objects or a list
  177. of ``SecurityGroup`` names, which should be
  178. assigned to this instance.
  179. :type user_data: ``str``
  180. :param user_data: An extra userdata object which is compatible with
  181. the provider.
  182. :type launch_config: ``LaunchConfig`` object
  183. :param launch_config: A ``LaunchConfig`` object which
  184. describes advanced launch configuration options for an instance.
  185. This includes block_device_mappings and network_interfaces. To
  186. construct a launch configuration object, call
  187. provider.compute.instances.create_launch_config()
  188. :rtype: ``object`` of :class:`.Instance`
  189. :return: an instance of Instance class
  190. """
  191. pass
  192. def create_launch_config(self):
  193. """
  194. Creates a ``LaunchConfig`` object which can be used
  195. to set additional options when launching an instance, such as
  196. block device mappings and network interfaces.
  197. :rtype: ``object`` of :class:`.LaunchConfig`
  198. :return: an instance of a LaunchConfig class
  199. """
  200. pass
  201. class VolumeService(PageableObjectMixin, ProviderService):
  202. """
  203. Base interface for a Volume Service
  204. """
  205. __metaclass__ = ABCMeta
  206. @abstractmethod
  207. def get(self, volume_id):
  208. """
  209. Returns a volume given its id. Returns None if the volume
  210. does not exist.
  211. :rtype: ``object`` of :class:`.Volume`
  212. :return: a Volume object
  213. """
  214. pass
  215. @abstractmethod
  216. def find(self, name, limit=None, marker=None):
  217. """
  218. Searches for a volume by a given list of attributes.
  219. :rtype: ``object`` of :class:`.Volume`
  220. :return: a Volume object or ``None`` if not found
  221. """
  222. pass
  223. @abstractmethod
  224. def list(self, limit=None, marker=None):
  225. """
  226. List all volumes.
  227. :rtype: ``list`` of :class:`.Volume`
  228. :return: a list of Volume objects
  229. """
  230. pass
  231. @abstractmethod
  232. def create(self, name, size, zone, snapshot=None, description=None):
  233. """
  234. Creates a new volume.
  235. :type name: ``str``
  236. :param name: The name of the volume
  237. :type size: ``int``
  238. :param size: The size of the volume (in GB)
  239. :type zone: ``str`` or ``PlacementZone``
  240. :param zone: The availability zone in which the Volume will be created.
  241. :type description: ``str``
  242. :param description: An optional description that may be supported by
  243. some providers. Providers that do not support this property will return
  244. None.
  245. :rtype: ``object`` of :class:`.Volume`
  246. :return: a newly created Volume object
  247. """
  248. pass
  249. class SnapshotService(PageableObjectMixin, ProviderService):
  250. """
  251. Base interface for a Snapshot Service
  252. """
  253. __metaclass__ = ABCMeta
  254. @abstractmethod
  255. def get(self, volume_id):
  256. """
  257. Returns a snapshot given its id. Returns None if the snapshot
  258. does not exist.
  259. :rtype: ``object`` of :class:`.Snapshot`
  260. :return: a Snapshot object
  261. """
  262. pass
  263. @abstractmethod
  264. def find(self, name, limit=None, marker=None):
  265. """
  266. Searches for a snapshot by a given list of attributes.
  267. :rtype: ``object`` of :class:`.Snapshot`
  268. :return: a Snapshot object or ``None`` if not found
  269. """
  270. pass
  271. @abstractmethod
  272. def list(self, limit=None, marker=None):
  273. """
  274. List all snapshots.
  275. :rtype: ``list`` of :class:`.Snapshot`
  276. :return: a list of Snapshot objects
  277. """
  278. pass
  279. @abstractmethod
  280. def create(self, name, volume, description=None):
  281. """
  282. Creates a new snapshot off a volume.
  283. :type name: ``str``
  284. :param name: The name of the snapshot
  285. :type volume: ``str`` or ``Volume``
  286. :param volume: The volume to create a snapshot of.
  287. :type description: ``str``
  288. :param description: An optional description that may be supported by
  289. some providers. Providers that do not support this property will return
  290. None.
  291. :rtype: ``object`` of :class:`.Snapshot`
  292. :return: a newly created Snapshot object
  293. """
  294. pass
  295. class BlockStoreService(ProviderService):
  296. """
  297. The Block Store Service interface provides access to block device services,
  298. such as volume and snapshot services in the provider.
  299. """
  300. __metaclass__ = ABCMeta
  301. @abstractproperty
  302. def volumes(self):
  303. """
  304. Provides access to volumes (i.e., block storage) for this provider.
  305. Example:
  306. .. code-block:: python
  307. # print all volumes
  308. for vol in provider.block_store.volumes:
  309. print(vol.id, vol.name)
  310. # find volume by name
  311. vol = provider.block_store.volumes.find(name='my_vol')[0]
  312. print(vol.id, vol.name)
  313. :rtype: :class:`.VolumeService`
  314. :return: a VolumeService object
  315. """
  316. pass
  317. @abstractproperty
  318. def snapshots(self):
  319. """
  320. Provides access to volume snapshots for this provider.
  321. Example:
  322. .. code-block:: python
  323. # print all snapshots
  324. for snap in provider.block_store.snapshots:
  325. print(snap.id, snap.name)
  326. # find snapshot by name
  327. snap = provider.block_store.snapshots.find(name='my_snap')[0]
  328. print(snap.id, snap.name)
  329. :rtype: :class:`.SnapshotService`
  330. :return: an SnapshotService object
  331. """
  332. pass
  333. class ImageService(PageableObjectMixin, ProviderService):
  334. """
  335. Base interface for an Image Service
  336. """
  337. __metaclass__ = ABCMeta
  338. @abstractmethod
  339. def get(self, image_id):
  340. """
  341. Returns an Image given its id. Returns None if the Image does not
  342. exist.
  343. :rtype: ``object`` of :class:`.Image`
  344. :return: an Image instance
  345. """
  346. pass
  347. @abstractmethod
  348. def find(self, name, limit=None, marker=None):
  349. """
  350. Searches for an image by a given list of attributes
  351. :rtype: ``object`` of :class:`.Image`
  352. :return: an Image instance
  353. """
  354. pass
  355. @abstractmethod
  356. def list(self, limit=None, marker=None):
  357. """
  358. List all images.
  359. :rtype: ``list`` of :class:`.Image`
  360. :return: list of image objects
  361. """
  362. pass
  363. class ObjectStoreService(PageableObjectMixin, ProviderService):
  364. """
  365. The Object Storage Service interface provides access to the underlying
  366. object store capabilities of this provider. This service is optional and
  367. the :func:`CloudProvider.has_service()` method should be used to verify its
  368. availability before using the service.
  369. """
  370. __metaclass__ = ABCMeta
  371. @abstractmethod
  372. def get(self, bucket_id):
  373. """
  374. Returns a bucket given its ID. Returns ``None`` if the bucket
  375. does not exist. On some providers, such as AWS and Openstack,
  376. the bucket id is the same as its name.
  377. Example:
  378. .. code-block:: python
  379. bucket = provider.object_store.get('my_bucket_id')
  380. print(bucket.id, bucket.name)
  381. :rtype: :class:`.Bucket`
  382. :return: a Bucket instance
  383. """
  384. pass
  385. @abstractmethod
  386. def find(self, name):
  387. """
  388. Searches for a bucket by a given list of attributes.
  389. Example:
  390. .. code-block:: python
  391. buckets = provider.object_store.find(name='my_bucket_name')
  392. for bucket in buckets:
  393. print(bucket.id, bucket.name)
  394. :rtype: :class:`.Bucket`
  395. :return: a Bucket instance
  396. """
  397. pass
  398. @abstractmethod
  399. def list(self, limit=None, marker=None):
  400. """
  401. List all buckets.
  402. Example:
  403. .. code-block:: python
  404. buckets = provider.object_store.find(name='my_bucket_name')
  405. for bucket in buckets:
  406. print(bucket.id, bucket.name)
  407. :rtype: :class:`.Bucket`
  408. :return: list of bucket objects
  409. """
  410. pass
  411. @abstractmethod
  412. def create(self, name, location=None):
  413. """
  414. Create a new bucket.
  415. Example:
  416. .. code-block:: python
  417. bucket = provider.object_store.create('my_bucket_name')
  418. print(bucket.name)
  419. :type name: str
  420. :param name: The name of this bucket.
  421. :type location: ``object`` of :class:`.Region`
  422. :param location: The region in which to place this bucket.
  423. :return: a Bucket object
  424. :rtype: ``object`` of :class:`.Bucket`
  425. """
  426. pass
  427. class SecurityService(ProviderService):
  428. """
  429. The security service interface can be used to access security related
  430. functions in the provider, such as firewall control and keypairs.
  431. """
  432. __metaclass__ = ABCMeta
  433. @abstractproperty
  434. def key_pairs(self):
  435. """
  436. Provides access to key pairs for this provider.
  437. Example:
  438. .. code-block:: python
  439. # print all keypairs
  440. for kp in provider.security.keypairs:
  441. print(kp.id, kp.name)
  442. # find keypair by name
  443. kp = provider.security.keypairs.find(name='my_key_pair')[0]
  444. print(kp.id, kp.name)
  445. :rtype: :class:`.KeyPairService`
  446. :return: a KeyPairService object
  447. """
  448. pass
  449. @abstractproperty
  450. def security_groups(self):
  451. """
  452. Provides access to security groups for this provider.
  453. Example:
  454. .. code-block:: python
  455. # print all security groups
  456. for sg in provider.security.security_groups:
  457. print(sg.id, sg.name)
  458. # find security group by name
  459. sg = provider.security.security_groups.find(name='my_sg')[0]
  460. print(sg.id, sg.name)
  461. :rtype: :class:`.SecurityGroupService`
  462. :return: a SecurityGroupService object
  463. """
  464. pass
  465. class KeyPairService(PageableObjectMixin, ProviderService):
  466. """
  467. Base interface for key pairs.
  468. """
  469. __metaclass__ = ABCMeta
  470. @abstractmethod
  471. def get(self, keypair_id):
  472. """
  473. Returns a KeyPair given its ID. Returns ``None`` if the KeyPair
  474. does not exist. On some providers, such as AWS and Openstack,
  475. the KeyPair id is the same as its name.
  476. Example:
  477. .. code-block:: python
  478. keypair = provider.security.keypairs.get('my_keypair_id')
  479. print(keypair.id, keypair.name)
  480. :rtype: :class:`.KeyPair`
  481. :return: a KeyPair instance
  482. """
  483. pass
  484. @abstractmethod
  485. def list(self, limit=None, marker=None):
  486. """
  487. List all key pairs associated with this account.
  488. :rtype: ``list`` of :class:`.KeyPair`
  489. :return: list of KeyPair objects
  490. """
  491. pass
  492. @abstractmethod
  493. def find(self, name, limit=None, marker=None):
  494. """
  495. Searches for a key pair by a given list of attributes.
  496. :rtype: ``object`` of :class:`.KeyPair`
  497. :return: a KeyPair object
  498. """
  499. pass
  500. @abstractmethod
  501. def create(self, name):
  502. """
  503. Create a new keypair or return an existing one by the same name.
  504. :type name: str
  505. :param name: The name of the key pair to be created.
  506. :rtype: ``object`` of :class:`.KeyPair`
  507. :return: A keypair instance
  508. """
  509. pass
  510. @abstractmethod
  511. def delete(self, keypair_id):
  512. """
  513. Delete an existing SecurityGroup.
  514. :type keypair_id: str
  515. :param keypair_id: The id of the key pair to be deleted.
  516. :rtype: ``bool``
  517. :return: ``True`` if the key does not exist, ``False`` otherwise. Note
  518. that this implies that the key may not have been deleted by
  519. this method but instead has not existed at all.
  520. """
  521. pass
  522. class SecurityGroupService(PageableObjectMixin, ProviderService):
  523. """
  524. Base interface for security groups.
  525. """
  526. __metaclass__ = ABCMeta
  527. @abstractmethod
  528. def list(self, limit=None, marker=None):
  529. """
  530. List all security groups associated with this account.
  531. :rtype: ``list`` of :class:`.SecurityGroup`
  532. :return: list of SecurityGroup objects
  533. """
  534. pass
  535. @abstractmethod
  536. def create(self, name, description):
  537. """
  538. Create a new SecurityGroup.
  539. :type name: str
  540. :param name: The name of the new security group.
  541. :type description: str
  542. :param description: The description of the new security group.
  543. :rtype: ``object`` of :class:`.SecurityGroup`
  544. :return: A SecurityGroup instance or ``None`` if one was not created.
  545. """
  546. pass
  547. @abstractmethod
  548. def get(self, group_names=None, group_ids=None):
  549. """
  550. Get all security groups associated with your account.
  551. :type group_names: list
  552. :param group_names: A list of the names of security groups to retrieve.
  553. If not provided, all security groups will be
  554. returned.
  555. :type group_ids: list
  556. :param group_ids: A list of IDs of security groups to retrieve.
  557. If not provided, all security groups will be
  558. returned.
  559. :rtype: list of :class:`SecurityGroup`
  560. :return: A list of SecurityGroup objects or an empty list if none
  561. found.
  562. """
  563. pass
  564. @abstractmethod
  565. def delete(self, group_id):
  566. """
  567. Delete an existing SecurityGroup.
  568. :type group_id: str
  569. :param group_id: The security group ID to be deleted.
  570. :rtype: ``bool``
  571. :return: ``True`` if the security group does not exist, ``False``
  572. otherwise. Note that this implies that the group may not have
  573. been deleted by this method but instead has not existed in
  574. the first place.
  575. """
  576. pass
  577. class InstanceTypesService(PageableObjectMixin, ProviderService):
  578. __metaclass__ = ABCMeta
  579. @abstractmethod
  580. def list(self, limit=None, marker=None):
  581. """
  582. List all instance types.
  583. :rtype: ``list`` of :class:`.InstanceType`
  584. :return: list of InstanceType objects
  585. """
  586. pass
  587. @abstractmethod
  588. def find(self, **kwargs):
  589. """
  590. Searches for an instance by a given list of attributes.
  591. :rtype: ``object`` of :class:`.InstanceType`
  592. :return: an Instance object
  593. """
  594. pass
  595. class RegionService(PageableObjectMixin, ProviderService):
  596. """
  597. Base interface for a Region service
  598. """
  599. __metaclass__ = ABCMeta
  600. @abstractmethod
  601. def get(self, region_id):
  602. """
  603. Returns a region given its id. Returns None if the region
  604. does not exist.
  605. :rtype: ``object`` of :class:`.Region`
  606. :return: a Region instance
  607. """
  608. pass
  609. @abstractmethod
  610. def list(self, limit=None, marker=None):
  611. """
  612. List all regions.
  613. :rtype: ``list`` of :class:`.Region`
  614. :return: list of region objects
  615. """
  616. pass