services.py 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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 CloudService(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(CloudService):
  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, CloudService):
  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: List of ``object`` of :class:`.Instance`
  128. :return: A list of Instance objects matching the supplied attributes.
  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. Note that the
  146. maximum is not guaranteed to be honoured, and a lower
  147. maximum may be enforced depending on the provider. In
  148. such a case, the returned ResultList's is_truncated
  149. property can be used to determine whether more records
  150. are available.
  151. :type marker: ``str``
  152. :param marker: The marker is an opaque identifier used to assist
  153. in paging through very long lists of objects. It is
  154. returned on each invocation of the list method.
  155. :rtype: ``ResultList`` of :class:`.Instance`
  156. :return: A ResultList object containing a list of Instances
  157. """
  158. pass
  159. @abstractmethod
  160. def create(self, name, image, instance_type, subnet, zone=None,
  161. key_pair=None, security_groups=None, user_data=None,
  162. launch_config=None,
  163. **kwargs):
  164. """
  165. Creates a new virtual machine instance.
  166. :type name: ``str``
  167. :param name: The name of the virtual machine instance
  168. :type image: ``MachineImage`` or ``str``
  169. :param image: The MachineImage object or id to boot the virtual machine
  170. with
  171. :type instance_type: ``InstanceType`` or ``str``
  172. :param instance_type: The InstanceType or name, specifying the size of
  173. the instance to boot into
  174. :type subnet: ``Subnet`` or ``str``
  175. :param subnet: The subnet object or a subnet string ID with which the
  176. instance should be associated. The subnet is a mandatory
  177. parameter, and must be provided when launching an
  178. instance.
  179. Note: Older clouds (with classic networking), may not
  180. have proper subnet support and are not guaranteed to
  181. work. Some providers (e.g. OpenStack) support a null
  182. value but the behaviour is implementation specific.
  183. :type zone: ``Zone`` or ``str``
  184. :param zone: The Zone or its name, where the instance should be placed.
  185. This parameter is provided for legacy compatibility (with
  186. classic networks).
  187. The subnet's placement zone will take precedence over this
  188. parameter, but in its absence, this value will be used.
  189. :type key_pair: ``KeyPair`` or ``str``
  190. :param key_pair: The KeyPair object or its name, to set for the
  191. instance.
  192. :type security_groups: A ``list`` of ``SecurityGroup`` objects or a
  193. list of ``str`` object IDs
  194. :param security_groups: A list of ``SecurityGroup`` objects or a list
  195. of ``SecurityGroup`` IDs, which should be
  196. assigned to this instance.
  197. The security groups must be associated with the
  198. same network as the supplied subnet. Use
  199. ``network.security_groups`` to retrieve a list
  200. of security groups belonging to a network.
  201. :type user_data: ``str``
  202. :param user_data: An extra userdata object which is compatible with
  203. the provider.
  204. :type launch_config: ``LaunchConfig`` object
  205. :param launch_config: A ``LaunchConfig`` object which
  206. describes advanced launch configuration options for an instance.
  207. Currently, this includes only block_device_mappings. To
  208. construct a launch configuration object, call
  209. provider.compute.instances.create_launch_config()
  210. :rtype: ``object`` of :class:`.Instance`
  211. :return: an instance of Instance class
  212. """
  213. pass
  214. def create_launch_config(self):
  215. """
  216. Creates a ``LaunchConfig`` object which can be used
  217. to set additional options when launching an instance, such as
  218. block device mappings and network interfaces.
  219. :rtype: ``object`` of :class:`.LaunchConfig`
  220. :return: an instance of a LaunchConfig class
  221. """
  222. pass
  223. class VolumeService(PageableObjectMixin, CloudService):
  224. """
  225. Base interface for a Volume Service.
  226. """
  227. __metaclass__ = ABCMeta
  228. @abstractmethod
  229. def get(self, volume_id):
  230. """
  231. Returns a volume given its id.
  232. :rtype: ``object`` of :class:`.Volume`
  233. :return: a Volume object or ``None`` if the volume does not exist.
  234. """
  235. pass
  236. @abstractmethod
  237. def find(self, name, limit=None, marker=None):
  238. """
  239. Searches for a volume by a given list of attributes.
  240. :rtype: ``object`` of :class:`.Volume`
  241. :return: a Volume object or ``None`` if not found.
  242. """
  243. pass
  244. @abstractmethod
  245. def list(self, limit=None, marker=None):
  246. """
  247. List all volumes.
  248. :rtype: ``list`` of :class:`.Volume`
  249. :return: a list of Volume objects.
  250. """
  251. pass
  252. @abstractmethod
  253. def create(self, name, size, zone, snapshot=None, description=None):
  254. """
  255. Creates a new volume.
  256. :type name: ``str``
  257. :param name: The name of the volume.
  258. :type size: ``int``
  259. :param size: The size of the volume (in GB).
  260. :type zone: ``str`` or :class:`.PlacementZone` object
  261. :param zone: The availability zone in which the Volume will be created.
  262. :type snapshot: ``str`` or :class:`.Snapshot` object
  263. :param snapshot: An optional reference to a snapshot from which this
  264. volume should be created.
  265. :type description: ``str``
  266. :param description: An optional description that may be supported by
  267. some providers. Providers that do not support this
  268. property will return ``None``.
  269. :rtype: ``object`` of :class:`.Volume`
  270. :return: a newly created Volume object.
  271. """
  272. pass
  273. class SnapshotService(PageableObjectMixin, CloudService):
  274. """
  275. Base interface for a Snapshot Service.
  276. """
  277. __metaclass__ = ABCMeta
  278. @abstractmethod
  279. def get(self, volume_id):
  280. """
  281. Returns a snapshot given its id.
  282. :rtype: ``object`` of :class:`.Snapshot`
  283. :return: a Snapshot object or ``None`` if the snapshot does not exist.
  284. """
  285. pass
  286. @abstractmethod
  287. def find(self, name, limit=None, marker=None):
  288. """
  289. Searches for a snapshot by a given list of attributes.
  290. :rtype: list of :class:`.Snapshot`
  291. :return: a Snapshot object or an empty list if none found.
  292. """
  293. pass
  294. @abstractmethod
  295. def list(self, limit=None, marker=None):
  296. """
  297. List all snapshots.
  298. :rtype: ``list`` of :class:`.Snapshot`
  299. :return: a list of Snapshot objects.
  300. """
  301. pass
  302. @abstractmethod
  303. def create(self, name, volume, description=None):
  304. """
  305. Creates a new snapshot off a volume.
  306. :type name: ``str``
  307. :param name: The name of the snapshot
  308. :type volume: ``str`` or ``Volume``
  309. :param volume: The volume to create a snapshot of.
  310. :type description: ``str``
  311. :param description: An optional description that may be supported by
  312. some providers. Providers that do not support this
  313. property will return None.
  314. :rtype: ``object`` of :class:`.Snapshot`
  315. :return: a newly created Snapshot object.
  316. """
  317. pass
  318. class BlockStoreService(CloudService):
  319. """
  320. The Block Store Service interface provides access to block device services,
  321. such as volume and snapshot services in the provider.
  322. """
  323. __metaclass__ = ABCMeta
  324. @abstractproperty
  325. def volumes(self):
  326. """
  327. Provides access to volumes (i.e., block storage) for this provider.
  328. Example:
  329. .. code-block:: python
  330. # print all volumes
  331. for vol in provider.block_store.volumes:
  332. print(vol.id, vol.name)
  333. # find volume by name
  334. vol = provider.block_store.volumes.find(name='my_vol')[0]
  335. print(vol.id, vol.name)
  336. :rtype: :class:`.VolumeService`
  337. :return: a VolumeService object
  338. """
  339. pass
  340. @abstractproperty
  341. def snapshots(self):
  342. """
  343. Provides access to volume snapshots for this provider.
  344. Example:
  345. .. code-block:: python
  346. # print all snapshots
  347. for snap in provider.block_store.snapshots:
  348. print(snap.id, snap.name)
  349. # find snapshot by name
  350. snap = provider.block_store.snapshots.find(name='my_snap')[0]
  351. print(snap.id, snap.name)
  352. :rtype: :class:`.SnapshotService`
  353. :return: an SnapshotService object
  354. """
  355. pass
  356. class ImageService(PageableObjectMixin, CloudService):
  357. """
  358. Base interface for an Image Service
  359. """
  360. __metaclass__ = ABCMeta
  361. @abstractmethod
  362. def get(self, image_id):
  363. """
  364. Returns an Image given its id. Returns None if the Image does not
  365. exist.
  366. :rtype: ``object`` of :class:`.Image`
  367. :return: an Image instance
  368. """
  369. pass
  370. @abstractmethod
  371. def find(self, name, limit=None, marker=None):
  372. """
  373. Searches for an image by a given list of attributes
  374. :rtype: ``object`` of :class:`.Image`
  375. :return: an Image instance
  376. """
  377. pass
  378. @abstractmethod
  379. def list(self, limit=None, marker=None):
  380. """
  381. List all images.
  382. :rtype: ``list`` of :class:`.Image`
  383. :return: list of image objects
  384. """
  385. pass
  386. class NetworkService(PageableObjectMixin, CloudService):
  387. """
  388. Base interface for a Network Service.
  389. """
  390. __metaclass__ = ABCMeta
  391. @abstractmethod
  392. def get(self, network_id):
  393. """
  394. Returns a Network given its ID or ``None`` if not found.
  395. :type network_id: ``str``
  396. :param network_id: The ID of the network to retrieve.
  397. :rtype: ``object`` of :class:`.Network`
  398. :return: a Network object
  399. """
  400. pass
  401. @abstractmethod
  402. def list(self, limit=None, marker=None):
  403. """
  404. List all networks.
  405. :rtype: ``list`` of :class:`.Network`
  406. :return: list of Network objects
  407. """
  408. pass
  409. @abstractmethod
  410. def create(self, name=None):
  411. """
  412. Create a new network.
  413. :type name: ``str``
  414. :param name: An optional network name. The name will be set if the
  415. provider supports it.
  416. :rtype: ``object`` of :class:`.Network`
  417. :return: A Network object
  418. """
  419. pass
  420. @abstractmethod
  421. def delete(self, network_id):
  422. """
  423. Delete an existing Network.
  424. :type network_id: ``str``
  425. :param network_id: The ID of the network to be deleted.
  426. :rtype: ``bool``
  427. :return: ``True`` if the network does not exist, ``False`` otherwise.
  428. Note that this implies that the network may not have been
  429. deleted by this method but instead has not existed at all.
  430. """
  431. pass
  432. @abstractproperty
  433. def subnets(self):
  434. """
  435. Provides access to subnets.
  436. Example:
  437. .. code-block:: python
  438. # Print all subnets
  439. for s in provider.network.subnets:
  440. print(s.id, s.name)
  441. # Get subnet by ID
  442. s = provider.network.subnets.get('subnet-id')
  443. print(s.id, s.name)
  444. :rtype: :class:`.SubnetService`
  445. :return: a SubnetService object
  446. """
  447. pass
  448. @abstractmethod
  449. def floating_ips(self, network_id=None):
  450. """
  451. List floating (i.e., static) IP addresses.
  452. :type network_id: ``str``
  453. :param network_id: The ID of the network by which to filter the IPs.
  454. :rtype: ``list`` of :class:`FloatingIP`
  455. :return: list of floating IP objects
  456. """
  457. pass
  458. @abstractmethod
  459. def create_floating_ip(self):
  460. """
  461. Allocate a new floating (i.e., static) IP address.
  462. :type network_id: ``str``
  463. :param network_id: The ID of the network with which to associate the
  464. new IP address.
  465. :rtype: :class:`FloatingIP`
  466. :return: floating IP object
  467. """
  468. pass
  469. @abstractmethod
  470. def routers(self):
  471. """
  472. Get a list of available routers.
  473. :rtype: ``list`` of :class: `Router`
  474. :return: list of routers
  475. """
  476. pass
  477. @abstractmethod
  478. def create_router(self, name=None):
  479. """
  480. Create a new router/gateway.
  481. :type name: ``str``
  482. :param name: An optional router name. The name will be set if the
  483. provider supports it.
  484. :rtype: :class:`Router`
  485. :return: a newly created router object
  486. """
  487. pass
  488. class SubnetService(PageableObjectMixin, CloudService):
  489. """
  490. Base interface for a Subnet Service.
  491. """
  492. __metaclass__ = ABCMeta
  493. @abstractmethod
  494. def get(self, subnet_id):
  495. """
  496. Returns a Subnet given its ID or ``None`` if not found.
  497. :type network_id: :class:`.Network` object or ``str``
  498. :param network_id: The ID of the subnet to retrieve.
  499. :rtype: ``object`` of :class:`.Subnet`
  500. return: a Subnet object
  501. """
  502. pass
  503. @abstractmethod
  504. def list(self, network=None, limit=None, marker=None):
  505. """
  506. List all subnets or filter them by the supplied network ID.
  507. :type network: ``str``
  508. :param network: Network object or ID with which to filter the subnets.
  509. :rtype: ``list`` of :class:`.Subnet`
  510. :return: list of Subnet objects
  511. """
  512. pass
  513. @abstractmethod
  514. def create(self, network_id, cidr_block, name=None, zone=None):
  515. """
  516. Create a new subnet within the supplied network.
  517. :type network: :class:`.Network` object or ``str``
  518. :param network: Network object or ID under which to create the subnet.
  519. :type cidr_block: ``str``
  520. :param cidr_block: CIDR block within the Network to assign to the
  521. subnet.
  522. :type name: ``str``
  523. :param name: An optional subnet name. The name will be set if the
  524. provider supports it.
  525. :type zone: ``str``
  526. :param zone: An optional placement zone for the subnet. Some providers
  527. may not support this, in which case the value is ignored.
  528. :rtype: ``object`` of :class:`.Subnet`
  529. :return: A Subnet object
  530. """
  531. pass
  532. @abstractmethod
  533. def get_or_create_default(self, zone=None):
  534. """
  535. Return a default subnet for the account or create one if not found.
  536. A default network is one marked as such by the provider or matches the
  537. default name used by this library (e.g., CloudBridgeNet).
  538. If this method creates a new subnet, it will create one in each zone
  539. available from the provider.
  540. :type zone: ``str``
  541. :param zone: Placement zone where to look for the subnet. If not
  542. supplied, a subnet from random zone will be selected.
  543. :rtype: ``object`` of :class:`.Subnet`
  544. :return: A Subnet object
  545. """
  546. pass
  547. @abstractmethod
  548. def delete(self, subnet):
  549. """
  550. Delete an existing Subnet.
  551. :type subnet: :class:`.Subnet` object or ``str``
  552. :param subnet: Subnet object or ID of the subnet to delete.
  553. :rtype: ``bool``
  554. :return: ``True`` if the subnet does not exist, ``False`` otherwise.
  555. Note that this implies that the subnet may not have been
  556. deleted by this method but instead has not existed at all.
  557. """
  558. pass
  559. class ObjectStoreService(PageableObjectMixin, CloudService):
  560. """
  561. The Object Storage Service interface provides access to the underlying
  562. object store capabilities of this provider. This service is optional and
  563. the :func:`CloudProvider.has_service()` method should be used to verify its
  564. availability before using the service.
  565. """
  566. __metaclass__ = ABCMeta
  567. @abstractmethod
  568. def get(self, bucket_id):
  569. """
  570. Returns a bucket given its ID. Returns ``None`` if the bucket
  571. does not exist. On some providers, such as AWS and OpenStack,
  572. the bucket id is the same as its name.
  573. Example:
  574. .. code-block:: python
  575. bucket = provider.object_store.get('my_bucket_id')
  576. print(bucket.id, bucket.name)
  577. :rtype: :class:`.Bucket`
  578. :return: a Bucket instance
  579. """
  580. pass
  581. @abstractmethod
  582. def find(self, name):
  583. """
  584. Searches for a bucket by a given list of attributes.
  585. Example:
  586. .. code-block:: python
  587. buckets = provider.object_store.find(name='my_bucket_name')
  588. for bucket in buckets:
  589. print(bucket.id, bucket.name)
  590. :rtype: :class:`.Bucket`
  591. :return: a Bucket instance
  592. """
  593. pass
  594. @abstractmethod
  595. def list(self, limit=None, marker=None):
  596. """
  597. List all buckets.
  598. Example:
  599. .. code-block:: python
  600. buckets = provider.object_store.find(name='my_bucket_name')
  601. for bucket in buckets:
  602. print(bucket.id, bucket.name)
  603. :rtype: :class:`.Bucket`
  604. :return: list of bucket objects
  605. """
  606. pass
  607. @abstractmethod
  608. def create(self, name, location=None):
  609. """
  610. Create a new bucket.
  611. If a bucket with the specified name already exists, return a reference
  612. to that bucket.
  613. Example:
  614. .. code-block:: python
  615. bucket = provider.object_store.create('my_bucket_name')
  616. print(bucket.name)
  617. :type name: str
  618. :param name: The name of this bucket.
  619. :type location: ``object`` of :class:`.Region`
  620. :param location: The region in which to place this bucket.
  621. :return: a Bucket object
  622. :rtype: ``object`` of :class:`.Bucket`
  623. """
  624. pass
  625. class SecurityService(CloudService):
  626. """
  627. The security service interface can be used to access security related
  628. functions in the provider, such as firewall control and keypairs.
  629. """
  630. __metaclass__ = ABCMeta
  631. @abstractproperty
  632. def key_pairs(self):
  633. """
  634. Provides access to key pairs for this provider.
  635. Example:
  636. .. code-block:: python
  637. # print all keypairs
  638. for kp in provider.security.keypairs:
  639. print(kp.id, kp.name)
  640. # find keypair by name
  641. kp = provider.security.keypairs.find(name='my_key_pair')[0]
  642. print(kp.id, kp.name)
  643. :rtype: :class:`.KeyPairService`
  644. :return: a KeyPairService object
  645. """
  646. pass
  647. @abstractproperty
  648. def security_groups(self):
  649. """
  650. Provides access to security groups for this provider.
  651. Example:
  652. .. code-block:: python
  653. # print all security groups
  654. for sg in provider.security.security_groups:
  655. print(sg.id, sg.name)
  656. # find security group by name
  657. sg = provider.security.security_groups.find(name='my_sg')[0]
  658. print(sg.id, sg.name)
  659. :rtype: :class:`.SecurityGroupService`
  660. :return: a SecurityGroupService object
  661. """
  662. pass
  663. class KeyPairService(PageableObjectMixin, CloudService):
  664. """
  665. Base interface for key pairs.
  666. """
  667. __metaclass__ = ABCMeta
  668. @abstractmethod
  669. def get(self, key_pair_id):
  670. """
  671. Return a KeyPair given its ID or ``None`` if not found.
  672. On some providers, such as AWS and OpenStack, the KeyPair ID is
  673. the same as its name.
  674. Example:
  675. .. code-block:: python
  676. key_pair = provider.security.keypairs.get('my_key_pair_id')
  677. print(key_pair.id, key_pair.name)
  678. :rtype: :class:`.KeyPair`
  679. :return: a KeyPair instance
  680. """
  681. pass
  682. @abstractmethod
  683. def list(self, limit=None, marker=None):
  684. """
  685. List all key pairs associated with this account.
  686. :rtype: ``list`` of :class:`.KeyPair`
  687. :return: list of KeyPair objects
  688. """
  689. pass
  690. @abstractmethod
  691. def find(self, name, limit=None, marker=None):
  692. """
  693. Searches for a key pair by a given list of attributes.
  694. :rtype: ``object`` of :class:`.KeyPair`
  695. :return: a KeyPair object
  696. """
  697. pass
  698. @abstractmethod
  699. def create(self, name):
  700. """
  701. Create a new key pair or raise an exception if one already exists.
  702. :type name: str
  703. :param name: The name of the key pair to be created.
  704. :rtype: ``object`` of :class:`.KeyPair`
  705. :return: A keypair instance or ``None``.
  706. """
  707. pass
  708. @abstractmethod
  709. def delete(self, key_pair_id):
  710. """
  711. Delete an existing SecurityGroup.
  712. :type key_pair_id: str
  713. :param key_pair_id: The id of the key pair to be deleted.
  714. :rtype: ``bool``
  715. :return: ``True`` if the key does not exist, ``False`` otherwise. Note
  716. that this implies that the key may not have been deleted by
  717. this method but instead has not existed at all.
  718. """
  719. pass
  720. class SecurityGroupService(PageableObjectMixin, CloudService):
  721. """
  722. Base interface for security groups.
  723. """
  724. __metaclass__ = ABCMeta
  725. @abstractmethod
  726. def get(self, security_group_id):
  727. """
  728. Returns a SecurityGroup given its ID. Returns ``None`` if the
  729. SecurityGroup does not exist.
  730. Example:
  731. .. code-block:: python
  732. sg = provider.security.security_groups.get('my_sg_id')
  733. print(sg.id, sg.name)
  734. :rtype: :class:`.SecurityGroup`
  735. :return: a SecurityGroup instance
  736. """
  737. pass
  738. @abstractmethod
  739. def list(self, limit=None, marker=None):
  740. """
  741. List all security groups associated with this account.
  742. :rtype: ``list`` of :class:`.SecurityGroup`
  743. :return: list of SecurityGroup objects
  744. """
  745. pass
  746. @abstractmethod
  747. def create(self, name, description, network_id):
  748. """
  749. Create a new SecurityGroup.
  750. :type name: str
  751. :param name: The name of the new security group.
  752. :type description: str
  753. :param description: The description of the new security group.
  754. :type network_id: ``str``
  755. :param network_id: Network ID under which to create the security group.
  756. :rtype: ``object`` of :class:`.SecurityGroup`
  757. :return: A SecurityGroup instance or ``None`` if one was not created.
  758. """
  759. pass
  760. @abstractmethod
  761. def find(self, name, limit=None, marker=None):
  762. """
  763. Get security groups associated with your account filtered by name.
  764. :type name: str
  765. :param name: The name of the security group to retrieve.
  766. :rtype: list of :class:`SecurityGroup`
  767. :return: A list of SecurityGroup objects or an empty list if none
  768. found.
  769. """
  770. pass
  771. @abstractmethod
  772. def delete(self, group_id):
  773. """
  774. Delete an existing SecurityGroup.
  775. :type group_id: str
  776. :param group_id: The security group ID to be deleted.
  777. :rtype: ``bool``
  778. :return: ``True`` if the security group does not exist, ``False``
  779. otherwise. Note that this implies that the group may not have
  780. been deleted by this method but instead has not existed in
  781. the first place.
  782. """
  783. pass
  784. class InstanceTypesService(PageableObjectMixin, CloudService):
  785. __metaclass__ = ABCMeta
  786. @abstractmethod
  787. def get(self, instance_type_id):
  788. """
  789. Returns an InstanceType given its ID. Returns ``None`` if the
  790. InstanceType does not exist.
  791. Example:
  792. .. code-block:: python
  793. itype = provider.compute.instance_types.get('my_itype_id')
  794. print(itype.id, itype.name)
  795. :rtype: :class:`.InstanceType`
  796. :return: an InstanceType instance
  797. """
  798. pass
  799. @abstractmethod
  800. def list(self, limit=None, marker=None):
  801. """
  802. List all instance types.
  803. :rtype: ``list`` of :class:`.InstanceType`
  804. :return: list of InstanceType objects
  805. """
  806. pass
  807. @abstractmethod
  808. def find(self, **kwargs):
  809. """
  810. Searches for an instance by a given list of attributes.
  811. :rtype: ``object`` of :class:`.InstanceType`
  812. :return: an Instance object
  813. """
  814. pass
  815. class RegionService(PageableObjectMixin, CloudService):
  816. """
  817. Base interface for a Region service
  818. """
  819. __metaclass__ = ABCMeta
  820. @abstractproperty
  821. def current(self):
  822. """
  823. Returns the current region that this provider is connected to.
  824. If the current region cannot be discovered, return ``None``.
  825. :rtype: ``object`` of :class:`.Region`
  826. :return: a Region instance or ``None``
  827. """
  828. pass
  829. @abstractmethod
  830. def get(self, region_id):
  831. """
  832. Returns a region given its id. Returns None if the region
  833. does not exist.
  834. :rtype: ``object`` of :class:`.Region`
  835. :return: a Region instance
  836. """
  837. pass
  838. @abstractmethod
  839. def list(self, limit=None, marker=None):
  840. """
  841. List all regions.
  842. :rtype: ``list`` of :class:`.Region`
  843. :return: list of region objects
  844. """
  845. pass