resources.py 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. """
  2. Specifications for data objects exposed through a provider or service
  3. """
  4. from abc import ABCMeta, abstractmethod, abstractproperty
  5. class CloudServiceType(object):
  6. """
  7. Defines possible service types that are offered by providers.
  8. Providers can implement the ``has_service`` method and clients can check
  9. for the availability of a service with::
  10. if (provider.has_service(CloudServiceTypes.OBJECTSTORE))
  11. ...
  12. """
  13. COMPUTE = 'compute'
  14. IMAGE = 'image'
  15. SECURITY = 'security'
  16. VOLUME = 'volume'
  17. BLOCKSTORE = 'block_store'
  18. OBJECTSTORE = 'object_store'
  19. class CloudResource(object):
  20. """
  21. Base interface for any Resource supported by a provider. This interface
  22. has an _provider property that can be used to access the provider
  23. associated with the resource, which is only intended for use by subclasses.
  24. """
  25. __metaclass__ = ABCMeta
  26. @abstractproperty
  27. def _provider(self):
  28. """
  29. Returns the provider instance associated with this resource. Intended
  30. for use by subclasses only.
  31. :rtype: :class:`.CloudProvider`
  32. :return: a CloudProvider object
  33. """
  34. pass
  35. class CloudBridgeBaseException(Exception):
  36. """
  37. Base class for all CloudBridge exceptions
  38. """
  39. pass
  40. class WaitStateException(CloudBridgeBaseException):
  41. """
  42. Marker interface for object wait exceptions.
  43. Thrown when a timeout or errors occurs waiting for an object does not reach
  44. the expected state within a specified time limit.
  45. """
  46. pass
  47. class InvalidConfigurationException(CloudBridgeBaseException):
  48. """
  49. Marker interface for invalid launch configurations.
  50. Thrown when a combination of parameters in a LaunchConfig
  51. object results in an illegal state.
  52. """
  53. pass
  54. class Configuration(dict):
  55. """
  56. Represents a cloudbridge configuration object
  57. """
  58. @abstractproperty
  59. def default_result_limit(self):
  60. """
  61. Get the default maximum number of results to return for a
  62. list method. The default limit will be applied to most list()
  63. and find() methods whenever an explicit limit is not specified.
  64. :rtype: ``int``
  65. :return: The maximum number of results to return
  66. """
  67. pass
  68. @property
  69. def default_wait_timeout(self):
  70. """
  71. Gets the default wait timeout for LifeCycleObjects. The default
  72. wait timeout is applied in wait_for() and wait_till_ready() methods
  73. if no explicit timeout is specified.
  74. :rtype: ``int``
  75. :return: The maximum length of time (in seconds) to wait for the
  76. object to change to desired state.
  77. """
  78. pass
  79. @property
  80. def default_wait_interval(self):
  81. """
  82. Gets the default wait interval for LifeCycleObjects. The default
  83. wait interval is applied in wait_for() and wait_till_ready() methods
  84. if no explicit interval is specified.
  85. :rtype: ``int``
  86. :return: How frequently to poll the object's state
  87. """
  88. pass
  89. @abstractproperty
  90. def debug_mode(self):
  91. """
  92. A flag indicating whether CloudBridge is in debug mode. Setting
  93. this to True will cause the underlying provider's debug
  94. output to be turned on.
  95. The flag can be toggled by sending in the cb_debug value via
  96. the config dictionary, or setting the CB_DEBUG environment variable.
  97. :rtype: ``bool``
  98. :return: Whether debug mode is on.
  99. """
  100. class ObjectLifeCycleMixin(object):
  101. """
  102. A mixin for an object with a defined life-cycle, such as an Instance,
  103. Volume, Image or Snapshot. An object that supports ObjectLifeCycleMixin
  104. will always have a state, defining which point in its lifecycle it is
  105. currently at.
  106. It also defines a wait_till_ready operation, which indicates that the
  107. object is in a state in its lifecycle where it is ready to be used by an
  108. end-user.
  109. A refresh operation allows the object to synchronise its state with the
  110. service provider.
  111. """
  112. __metaclass__ = ABCMeta
  113. @abstractproperty
  114. def _provider(self):
  115. """
  116. Obtain the provider associated with this object. Used internally
  117. to access the provider config and get default timeouts/intervals.
  118. :rtype: :class:``.CloudProvider``
  119. :return: The provider associated with this Resource
  120. """
  121. pass
  122. @abstractproperty
  123. def state(self):
  124. """
  125. Get the current state of this object.
  126. :rtype: ``str``
  127. :return: The current state as a string.
  128. """
  129. pass
  130. @abstractmethod
  131. def refresh(self):
  132. """
  133. Refreshs this object's state and synchronize it with the underlying
  134. service provider.
  135. """
  136. pass
  137. @abstractmethod
  138. def wait_for(self, target_states, terminal_states=None, timeout=None,
  139. interval=None):
  140. """
  141. Wait for a specified timeout for an object to reach a set of desired
  142. target states. If the object does not reach the desired state within
  143. the specified timeout, a ``WaitStateException`` will be raised.
  144. The optional terminal_states property can be used to specify an
  145. additional set of states which, should the object reach one,
  146. the object thereafter will not transition into the desired target
  147. state. Should this happen, a ``WaitStateException`` will be raised.
  148. Example:
  149. .. code-block:: python
  150. instance.wait_for(
  151. [InstanceState.TERMINATED, InstanceState.UNKNOWN],
  152. terminal_states=[InstanceState.ERROR])
  153. :type target_states: ``list`` of states
  154. :param target_states: The list of target states to wait for.
  155. :type terminal_states: ``list`` of states
  156. :param terminal_states: A list of terminal states after which the
  157. object will not transition into a target state. A WaitStateException
  158. will be raised if the object transition into a terminal state.
  159. :type timeout: int
  160. :param timeout: The maximum length of time (in seconds) to wait for the
  161. object to changed to desired state. If no timeout is
  162. specified, the global default_wait_timeout defined in
  163. the provider config will apply.
  164. :type interval: int
  165. :param interval: How frequently to poll the object's state (in
  166. seconds). If no interval is specified, the global
  167. default_wait_interval defined in the provider config
  168. will apply.
  169. :rtype: ``True``
  170. :return: Returns ``True`` if successful. A ``WaitStateException``
  171. exception may be thrown by the underlying service if the
  172. object cannot get into a ready state (e.g. if the object
  173. is in an error state).
  174. """
  175. pass
  176. @abstractmethod
  177. def wait_till_ready(self, timeout, interval):
  178. """
  179. A convenience method to wait till the current object reaches a state
  180. which is ready for use, which is any state where the end-user can
  181. successfully interact with the object.
  182. Will throw a ``WaitStateException`` if the object is not ready within
  183. the specified timeout.
  184. :type timeout: int
  185. :param timeout: The maximum length of time (in seconds) to wait for the
  186. object to become ready.
  187. :type interval: int
  188. :param interval: How frequently to poll the object's ready state (in
  189. seconds).
  190. :rtype: ``True``
  191. :return: Returns ``True`` if successful. A ``WaitStateException``
  192. exception may be thrown by the underlying service if the
  193. object cannot get into a ready state (e.g. if the object
  194. is in an error state).
  195. """
  196. pass
  197. class PageableObjectMixin(object):
  198. """
  199. A marker interface for objects which support paged iteration through
  200. a list of objects with a list(limit, marker) method.
  201. """
  202. @abstractmethod
  203. def __iter__(self):
  204. """
  205. Enables iteration through this object. Typically, an implementation
  206. will call the list(limit, marker) method to transparently page
  207. additional objects in as iteration progresses.
  208. """
  209. pass
  210. @abstractmethod
  211. def list(self, limit=None, marker=None):
  212. """
  213. Returns a list of objects up to a maximum limit.
  214. If a limit and marker are specified, the records will be fetched up to
  215. the limit starting from the marker onwards. The returned list is a list
  216. of class ResultList, which has extra properties like is_truncated,
  217. supports_total and total_records to provide extra information
  218. about record availability.
  219. If limit is not specified, the limit will default to the underlying
  220. provider's default limit. Therefore, you need to check the is_truncated
  221. property to determine whether more records are available.
  222. The total number of results can be determined through the total_results
  223. property. Not all provides will support returning the total_results
  224. property, so the supports_total property can be used to determine
  225. whether a total is supported.
  226. To iterate through all the records, it will be easier to iterate
  227. directly through the instances using __iter__ instead of calling
  228. the list method. The __iter__ method will automatically call the list
  229. method to fetch a batch of records at a time.
  230. Example:
  231. .. code-block:: python
  232. # get first page of results
  233. instlist = provider.compute.instances.list(limit=50)
  234. for instance in instlist:
  235. print("Instance Data: {0}", instance)
  236. if instlist.supports_total:
  237. print("Total results: {0}".format(instlist.total_results))
  238. else:
  239. print("Total records unknown,"
  240. "but has more data?: {0}".format(instlist.is_truncated))
  241. # Page to next set of results
  242. if (instlist.is_truncated)
  243. instlist = provider.compute.instances.list(limit=100,
  244. marker=instlist.marker)
  245. # Alternative: iterate through every available record
  246. for instance in provider.compute.instances:
  247. print(instance)
  248. """
  249. pass
  250. class ResultList(list):
  251. """
  252. This is a wrapper class around a standard Python :py:class:`list` class
  253. and provides some extra properties to aid with paging through a large
  254. number of results.
  255. Example:
  256. .. code-block:: python
  257. # get first page of results
  258. rl = provider.compute.instances.list(limit=50)
  259. for result in rl:
  260. print("Instance Data: {0}", result)
  261. if rl.supports_total:
  262. print("Total results: {0}".format(rl.total_results))
  263. else:
  264. print("Total records unknown,"
  265. "but has more data?: {0}."format(rl.is_truncated))
  266. # Page to next set of results
  267. if (rl.is_truncated)
  268. rl = provider.compute.instances.list(limit=100,
  269. marker=rl.marker)
  270. """
  271. __metaclass__ = ABCMeta
  272. @abstractproperty
  273. def marker(self):
  274. """
  275. This is an opaque identifier used to assist in paging through very long
  276. lists of objects. This marker can be provided to the list method to get
  277. the next set of results.
  278. """
  279. pass
  280. @abstractproperty
  281. def is_truncated(self):
  282. """
  283. Indicates whether this result list has more results
  284. that can be paged in.
  285. """
  286. pass
  287. @abstractproperty
  288. def supports_total(self):
  289. """
  290. Indicates whether the provider supports returning the total number of
  291. available results. The supports_total property should be checked
  292. before accessing the total_results property.
  293. """
  294. pass
  295. @abstractproperty
  296. def total_results(self):
  297. """
  298. Indicates the total number of results for a particular query. The
  299. supports_total property should be used to check whether the provider
  300. supports returning the total number of results, before accessing this
  301. property, or the behaviour is indeterminate.
  302. """
  303. pass
  304. @abstractproperty
  305. def supports_server_paging(self):
  306. """
  307. Indicates whether this ResultList supports client side paging or server
  308. side paging. If server side paging is not supported, the data property
  309. provides direct access to all available data.
  310. """
  311. pass
  312. @abstractproperty
  313. def data(self):
  314. pass
  315. class InstanceState(object):
  316. """
  317. Standard states for a node
  318. :cvar UNKNOWN: Instance state unknown.
  319. :cvar PENDING: Instance is pending
  320. :cvar CONFIGURING: Instance is being reconfigured in some way.
  321. :cvar RUNNING: Instance is running.
  322. :cvar REBOOTING: Instance is rebooting.
  323. :cvar TERMINATED: Instance is terminated. No further operations possible.
  324. :cvar STOPPED: Instance is stopped. Instance can be resumed.
  325. :cvar ERROR: Instance is in an error state. No further operations possible.
  326. """
  327. UNKNOWN = "unknown"
  328. PENDING = "pending"
  329. CONFIGURING = "configuring"
  330. RUNNING = "running"
  331. REBOOTING = "rebooting"
  332. TERMINATED = "terminated"
  333. STOPPED = "stopped"
  334. ERROR = "error"
  335. class Instance(ObjectLifeCycleMixin, CloudResource):
  336. __metaclass__ = ABCMeta
  337. @abstractproperty
  338. def id(self):
  339. """
  340. Get the instance identifier.
  341. :rtype: str
  342. :return: ID for this instance as returned by the cloud middleware.
  343. """
  344. pass
  345. @abstractproperty
  346. def name(self):
  347. """
  348. Get the instance name.
  349. :rtype: str
  350. :return: Name for this instance as returned by the cloud middleware.
  351. """
  352. pass
  353. @name.setter
  354. @abstractmethod
  355. def name(self, value):
  356. """
  357. Set the instance name.
  358. """
  359. pass
  360. @abstractproperty
  361. def public_ips(self):
  362. """
  363. Get all the public IP addresses for this instance.
  364. :rtype: list
  365. :return: A list of public IP addresses associated with this instance.
  366. """
  367. pass
  368. @abstractproperty
  369. def private_ips(self):
  370. """
  371. Get all the private IP addresses for this instance.
  372. :rtype: list
  373. :return: A list of private IP addresses associated with this instance.
  374. """
  375. pass
  376. @abstractproperty
  377. def instance_type(self):
  378. """
  379. Get the instance type.
  380. :rtype: :class:``.InstanceType``
  381. :return: API type of this instance (e.g., ``m1.large``)
  382. """
  383. pass
  384. @abstractmethod
  385. def reboot(self):
  386. """
  387. Reboot this instance (using the cloud middleware API).
  388. :rtype: bool
  389. :return: ``True`` if the reboot was succesful; ``False`` otherwise.
  390. """
  391. pass
  392. @abstractmethod
  393. def terminate(self):
  394. """
  395. Permanently terminate this instance.
  396. :rtype: bool
  397. :return: ``True`` if the termination of the instance was succesfully
  398. initiated; ``False`` otherwise.
  399. """
  400. pass
  401. @abstractproperty
  402. def image_id(self):
  403. """
  404. Get the image ID for this insance.
  405. :rtype: str
  406. :return: Image ID (i.e., AMI) this instance is using.
  407. """
  408. pass
  409. @abstractproperty
  410. def placement_zone(self):
  411. """
  412. Get the placement zone where this instance is running.
  413. :rtype: str
  414. :return: Region/zone/placement where this instance is running.
  415. """
  416. pass
  417. # @abstractproperty
  418. # def mac_address(self):
  419. # """
  420. # Get the MAC address for this instance.
  421. #
  422. # :rtype: str
  423. # :return: MAC address for ths instance.
  424. # """
  425. # pass
  426. @abstractproperty
  427. def security_groups(self):
  428. """
  429. Get the security groups associated with this instance.
  430. :rtype: list or :class:``SecurityGroup`` objects
  431. :return: A list of SecurityGroup objects associated with this instance.
  432. """
  433. pass
  434. @abstractproperty
  435. def key_pair_name(self):
  436. """
  437. Get the name of the key pair associated with this instance.
  438. :rtype: str
  439. :return: Name of the ssh key pair associated with this instance.
  440. """
  441. pass
  442. @abstractmethod
  443. def create_image(self, name):
  444. """
  445. Create a new image based on this instance.
  446. :rtype: :class:``.Image``
  447. :return: an Image object
  448. """
  449. pass
  450. @abstractmethod
  451. def add_floating_ip(self, ip_address):
  452. """
  453. Add a public IP address to this instance.
  454. :type ip_address: str
  455. :param ip_address: The IP address to associate with the instance.
  456. """
  457. pass
  458. @abstractmethod
  459. def remove_floating_ip(self, ip_address):
  460. """
  461. Remove a public IP address from this instance.
  462. :type ip_address: str
  463. :param ip_address: The IP address to remove from the instance.
  464. """
  465. pass
  466. class MachineImageState(object):
  467. """
  468. Standard states for a machine image
  469. :cvar UNKNOWN: Image state unknown.
  470. :cvar PENDING: Image is pending
  471. :cvar AVAILABLE: Image is available
  472. :cvar ERROR: Image is in an error state. Not recoverable.
  473. """
  474. UNKNOWN = "unknown"
  475. PENDING = "pending"
  476. AVAILABLE = "available"
  477. ERROR = "error"
  478. class LaunchConfig(object):
  479. """
  480. Represents an advanced launch configuration object, containing
  481. information such as BlockDeviceMappings, NetworkInterface configurations,
  482. and other advanced options which may be useful when launching an instance.
  483. Example:
  484. .. code-block:: python
  485. lc = provider.compute.instances.create_launch_config()
  486. lc.add_block_device(...)
  487. lc.add_network_interface(...)
  488. inst = provider.compute.instances.create(name, image, instance_type,
  489. launch_config=lc)
  490. """
  491. @abstractmethod
  492. def add_ephemeral_device(self):
  493. """
  494. Adds a new ephemeral block device mapping to the boot configuration.
  495. This can be used to add existing ephemeral devices to the instance.
  496. (The total number of ephemeral devices available for a particular
  497. InstanceType can be determined by querying the InstanceTypes service).
  498. Note that on some services, such as AWS, ephemeral devices must be
  499. added in as a device mapping at instance creation time, and cannot be
  500. added afterwards.
  501. Note that the device name, such as /dev/sda1, cannot be selected at
  502. present, since this tends to be provider and instance type specific.
  503. However, the order of device addition coupled with device type will
  504. generally determine naming order, with devices added first getting
  505. lower letters than instances added later.
  506. Example:
  507. .. code-block:: python
  508. lc = provider.compute.instances.create_launch_config()
  509. # 1. Add all available ephemeral devices
  510. inst_type = provider.compute.instance_types.find(name='m1.tiny')[0]
  511. for i in range(inst_type.num_ephemeral_disks):
  512. lc.add_ephemeral_device()
  513. """
  514. pass
  515. @abstractmethod
  516. def add_volume_device(self, source=None, is_root=None, size=None,
  517. delete_on_terminate=None):
  518. """
  519. Adds a new volume based block device mapping to the boot configuration.
  520. The volume can be based on a snapshot, image, existing volume or
  521. be a blank new volume, and is specified by the source parameter.
  522. The property is_root can be set to True to override any existing root
  523. device mappings. Otherwise, the default behaviour is to add new block
  524. devices to the instance.
  525. Note that the device name, such as /dev/sda1, cannot be selected at
  526. present, since this tends to be provider and instance type specific.
  527. However, the order of device addition coupled with device type will
  528. generally determine naming order, with devices added first getting
  529. lower letters than instances added later (except when is_root is set).
  530. Example:
  531. .. code-block:: python
  532. lc = provider.compute.instances.create_launch_config()
  533. # 1. Create and attach an empty volume of size 100GB
  534. lc.add_volume_device(size=100, delete_on_terminate=True)
  535. # 2. Create and attach a volume based on a snapshot
  536. snap = provider.block_store.snapshots.get('<my_snapshot_id>')
  537. lc.add_volume_device(source=snap)
  538. # 3. Create+attach a volume based on an image and set it as root
  539. img = provider.compute.images.get('<my_image_id>')
  540. lc.add_volume_device(source=img, size=100, is_root=True)
  541. :type source: ``Volume``, ``Snapshot``, ``Image`` or None.
  542. :param source: The source ``block_device`` to add. If ``Volume``, the
  543. volume will be attached directly to the instance.
  544. If ``Snapshot``, a volume will be created based on the
  545. Snapshot and attached to the instance. If ``Image``,
  546. a volume based on the Image will be attached to the
  547. instance. If ``None``, the source is assumed to be
  548. a blank volume.
  549. :type is_root: ``bool``
  550. :param is_root: Determines which device will serve as the root device.
  551. If more than one device is defined as root, an
  552. ``InvalidConfigurationException`` will be thrown.
  553. :type size: ``int``
  554. :param size: The size of the volume to create. An implementation may
  555. ignore this parameter for certain sources like 'Volume'.
  556. :type delete_on_terminate: ``bool``
  557. :param delete_on_terminate: Determines whether to delete or keep the
  558. volume on instance termination.
  559. """
  560. pass
  561. @abstractmethod
  562. def add_network_interface(self, net_id):
  563. """
  564. Add a private network info to the launch configuration.
  565. Example:
  566. .. code-block:: python
  567. lc = provider.compute.instances.create_launch_config()
  568. # 1. Add a VPC subnet for use with AWS
  569. lc.add_network_interface('subnet-c24aeaff')
  570. # 2. Add a network ID for use with OpenStack
  571. lc.add_network_interface('5820c766-75fe-4fc6-96ef-798f67623238')
  572. :type net_id: str
  573. :param net_id: Network ID to launch an instance into. This is a
  574. preliminary implementation (pending full private cloud
  575. support within cloudbridge) so native network IDs need
  576. to be supplied. For OpenStack, this is the Neutron
  577. network ID. For AWS, this is a VPC subnet ID. For the
  578. time being, only a single network interface can be
  579. supplied.
  580. """
  581. pass
  582. class MachineImage(ObjectLifeCycleMixin, CloudResource):
  583. __metaclass__ = ABCMeta
  584. @abstractproperty
  585. def id(self):
  586. """
  587. Get the image identifier.
  588. :rtype: ``str``
  589. :return: ID for this instance as returned by the cloud middleware.
  590. """
  591. pass
  592. @abstractproperty
  593. def name(self):
  594. """
  595. Get the image name.
  596. :rtype: ``str``
  597. :return: Name for this image as returned by the cloud middleware.
  598. """
  599. pass
  600. @abstractproperty
  601. def description(self):
  602. """
  603. Get the image description.
  604. :rtype: ``str``
  605. :return: Description for this image as returned by the cloud
  606. middleware.
  607. """
  608. pass
  609. @abstractmethod
  610. def delete(self):
  611. """
  612. Delete this image
  613. :rtype: ``bool``
  614. :return: ``True`` if the operation succeeded.
  615. """
  616. pass
  617. class VolumeState(object):
  618. """
  619. Standard states for a volume
  620. :cvar UNKNOWN: Volume state unknown.
  621. :cvar CREATING: Volume is being created.
  622. :cvar CONFIGURING: Volume is being configured in some way.
  623. :cvar AVAILABLE: Volume is available and can be attached to an instance.
  624. :cvar IN_USE: Volume is attached and in-use.
  625. :cvar DELETED: Volume has been deleted. No further operations possible.
  626. :cvar ERROR: Volume is in an error state. No further operations possible.
  627. """
  628. UNKNOWN = "unknown"
  629. CREATING = "creating"
  630. CONFIGURING = "configuring"
  631. AVAILABLE = "available"
  632. IN_USE = "in-use"
  633. DELETED = "deleted"
  634. ERROR = "error"
  635. class Volume(ObjectLifeCycleMixin, CloudResource):
  636. __metaclass__ = ABCMeta
  637. @abstractproperty
  638. def id(self):
  639. """
  640. Get the volume identifier.
  641. :rtype: ``str``
  642. :return: ID for this volume. Will generally correspond to the cloud
  643. middleware's ID, but should be treated as an opaque value.
  644. """
  645. pass
  646. @abstractproperty
  647. def name(self):
  648. """
  649. Get the volume name.
  650. :rtype: ``str``
  651. :return: Name for this volume as returned by the cloud middleware.
  652. """
  653. pass
  654. @name.setter
  655. @abstractmethod
  656. def name(self, value):
  657. """
  658. Set the volume name.
  659. """
  660. pass
  661. @abstractmethod
  662. def attach(self, instance, device):
  663. """
  664. Attach this volume to an instance.
  665. :type instance: str or :class:``.Instance`` object
  666. :param instance: The ID of an instance or an ``Instance`` object to
  667. which this volume will be attached.
  668. :type device: str
  669. :param device: The device on the instance through which the
  670. volume will be exposed (e.g. /dev/sdh).
  671. :rtype: bool
  672. :return: ``True`` if successful.
  673. """
  674. pass
  675. @abstractmethod
  676. def detach(self, force=False):
  677. """
  678. Detach this volume from an instance.
  679. :type force: bool
  680. :param force: Forces detachment if the previous detachment attempt
  681. did not occur cleanly. This option is supported on select
  682. clouds only. This option can lead to data loss or a
  683. corrupted file system. Use this option only as a last
  684. resort to detach a volume from a failed instance. The
  685. instance will not have an opportunity to flush file
  686. system caches nor file system meta data. If you
  687. use this option, you must perform file system check and
  688. repair procedures.
  689. :rtype: bool
  690. :return: ``True`` if successful.
  691. """
  692. pass
  693. @abstractmethod
  694. def create_snapshot(self, name, description=None):
  695. """
  696. Create a snapshot of this Volume.
  697. :type name: str
  698. :param name: The name of this snapshot.
  699. :type description: str
  700. :param description: A description of the snapshot.
  701. Limited to 256 characters.
  702. :rtype: :class:``.Snapshot``
  703. :return: The created Snapshot object.
  704. """
  705. pass
  706. @abstractmethod
  707. def delete(self):
  708. """
  709. Delete this volume.
  710. :rtype: bool
  711. :return: ``True`` if successful.
  712. """
  713. pass
  714. class SnapshotState(object):
  715. """
  716. Standard states for a snapshot
  717. :cvar UNKNOWN: Snapshot state unknown.
  718. :cvar PENDING: Snapshot is pending.
  719. :cvar CONFIGURING: Snapshot is being configured in some way.
  720. :cvar AVAILABLE: Snapshot has been completed and is ready for use.
  721. :cvar ERROR: Snapshot is in an error state. No further operations possible.
  722. """
  723. UNKNOWN = "unknown"
  724. PENDING = "pending"
  725. CONFIGURING = "configuring"
  726. AVAILABLE = "available"
  727. ERROR = "error"
  728. class Snapshot(ObjectLifeCycleMixin, CloudResource):
  729. __metaclass__ = ABCMeta
  730. @abstractproperty
  731. def id(self):
  732. """
  733. Get the snapshot identifier.
  734. :rtype: ``str``
  735. :return: ID for this snapshot. Will generally correspond to the cloud
  736. middleware's ID, but should be treated as an opaque value.
  737. """
  738. pass
  739. @abstractproperty
  740. def name(self):
  741. """
  742. Get the snapshot name.
  743. """
  744. pass
  745. @name.setter
  746. @abstractmethod
  747. def name(self, value):
  748. """
  749. set the snapshot name.
  750. """
  751. pass
  752. @abstractmethod
  753. def create_volume(self, placement, size=None, volume_type=None, iops=None):
  754. """
  755. Create a new Volume from this Snapshot.
  756. :type zone: str
  757. :param zone: The availability zone in which the Volume will be created.
  758. :type size: int
  759. :param size: The size of the new volume, in GiB (optional). Defaults to
  760. the size of the snapshot.
  761. :type volume_type: str
  762. :param volume_type: The type of the volume (optional). Availability and
  763. valid values depend on the provider.
  764. :type iops: int
  765. :param iops: The provisioned IOPs you want to associate with
  766. this volume (optional). Availability depends on the
  767. provider.
  768. :rtype: :class:`.Volume`
  769. :return: An instance of the created Volume.
  770. """
  771. pass
  772. # @abstractmethod
  773. # def share(self, user_ids=None):
  774. # """
  775. # Share this Snapshot.
  776. #
  777. # :type user_ids: list of strings
  778. # :param user_ids: A list of cloud provider compatible user IDs. If no
  779. # IDs are specified, the snapshot is made public.
  780. #
  781. # :rtype: bool
  782. # :return: ``True`` if successful.
  783. # """
  784. # pass
  785. #
  786. # @abstractmethod
  787. # def unshare(self, user_ids=None):
  788. # """
  789. # Unshare this Snapshot.
  790. #
  791. # :type user_ids: list of strings
  792. # :param user_ids: A list of cloud provider compatible user IDs. If no
  793. # IDs are specified, the snapshot is made private.
  794. #
  795. # :rtype: bool
  796. # :return: ``True`` if successful.
  797. # """
  798. # pass
  799. @abstractmethod
  800. def delete(self):
  801. """
  802. Delete this snapshot.
  803. :rtype: bool
  804. :return: ``True`` if successful.
  805. """
  806. pass
  807. class KeyPair(CloudResource):
  808. __metaclass__ = ABCMeta
  809. @abstractproperty
  810. def id(self):
  811. """
  812. Return the id of this key pair.
  813. :rtype: ``str``
  814. :return: ID for this snapshot. Will generally correspond to the cloud
  815. middleware's name, but should be treated as an opaque value.
  816. """
  817. pass
  818. @abstractproperty
  819. def name(self):
  820. """
  821. Return the name of this key pair.
  822. :rtype: str
  823. :return: A name of this ssh key pair.
  824. """
  825. pass
  826. @abstractproperty
  827. def material(self):
  828. """
  829. Unencrypted private key.
  830. :rtype: str
  831. :return: Unencrypted private key or ``None`` if not available.
  832. """
  833. pass
  834. @abstractmethod
  835. def delete(self):
  836. """
  837. Delete this key pair.
  838. :rtype: bool
  839. :return: ``True`` is successful.
  840. """
  841. pass
  842. class Region(CloudResource):
  843. """
  844. Represents a cloud region, typically a separate geographic area and will
  845. contain at least one placement zone.
  846. """
  847. __metaclass__ = ABCMeta
  848. @abstractproperty
  849. def id(self):
  850. """
  851. The id for this region
  852. :rtype: str
  853. :return: ID of the region.
  854. """
  855. pass
  856. @abstractproperty
  857. def name(self):
  858. """
  859. Name of the region.
  860. :rtype: str
  861. :return: Name of the region.
  862. """
  863. pass
  864. @abstractproperty
  865. def zones(self):
  866. """
  867. Accesss information about placement zones within this region.
  868. :rtype: iterable
  869. :return: Iterable of available placement zones in this region.
  870. """
  871. pass
  872. class PlacementZone(CloudResource):
  873. """
  874. Represents a placement zone. A placement zone is contained within a Region.
  875. """
  876. __metaclass__ = ABCMeta
  877. @abstractproperty
  878. def id(self):
  879. """
  880. Name of the placement zone.
  881. :rtype: str
  882. :return: Name of the placement zone.
  883. """
  884. pass
  885. @abstractproperty
  886. def name(self):
  887. """
  888. Name of the placement zone.
  889. :rtype: str
  890. :return: Name of the placement zone.
  891. """
  892. pass
  893. @abstractproperty
  894. def region(self):
  895. """
  896. A region this placement zone is associated with.
  897. :rtype: str
  898. :return: The name of the region the zone is associated with.
  899. """
  900. pass
  901. class InstanceType(CloudResource):
  902. """
  903. An instance type object.
  904. """
  905. __metaclass__ = ABCMeta
  906. @abstractproperty
  907. def id(self):
  908. pass
  909. @abstractproperty
  910. def name(self):
  911. pass
  912. @abstractproperty
  913. def family(self):
  914. """
  915. The family/group that this instance type belongs to.
  916. For example, General Purpose Instances or High-Memory Instances. If
  917. the provider does not support such a grouping, it may return ``None``.
  918. :rtype: str
  919. :return: Name of the instance family or ``None``.
  920. """
  921. pass
  922. @abstractproperty
  923. def vcpus(self):
  924. """
  925. The number of VCPUs supported by this instance type.
  926. :rtype: int
  927. :return: Number of VCPUs.
  928. """
  929. pass
  930. @abstractproperty
  931. def ram(self):
  932. """
  933. The amount of RAM (in mb) supported by this instance type.
  934. :rtype: int
  935. :return: Total RAM (in MB).
  936. """
  937. pass
  938. @abstractproperty
  939. def size_root_disk(self):
  940. """
  941. The size of this instance types's root disk (in GB).
  942. :rtype: int
  943. :return: Size of root disk (in GB).
  944. """
  945. pass
  946. @abstractproperty
  947. def size_ephemeral_disks(self):
  948. """
  949. The size of this instance types's total ephemeral storage (in GB).
  950. :rtype: int
  951. :return: Size of ephemeral disks (in GB).
  952. """
  953. pass
  954. @abstractproperty
  955. def num_ephemeral_disks(self):
  956. """
  957. The total number of ephemeral disks on this instance type.
  958. :rtype: int
  959. :return: Number of ephemeral disks available.
  960. """
  961. pass
  962. @abstractproperty
  963. def size_total_disk(self):
  964. """
  965. The total disk space available on this instance type
  966. (root_disk + ephemeral).
  967. :rtype: int
  968. :return: Size of total disk space (in GB).
  969. """
  970. pass
  971. @abstractproperty
  972. def extra_data(self):
  973. """
  974. A dictionary of extra data about this instance. May contain
  975. nested dictionaries, but all key value pairs are strings or integers.
  976. :rtype: dict
  977. :return: Extra attributes for this instance type.
  978. """
  979. pass
  980. class SecurityGroup(CloudResource):
  981. __metaclass__ = ABCMeta
  982. @abstractproperty
  983. def id(self):
  984. """
  985. Get the ID of this security group.
  986. :rtype: str
  987. :return: Security group ID.
  988. """
  989. pass
  990. @abstractproperty
  991. def name(self):
  992. """
  993. Return the name of this security group.
  994. :rtype: str
  995. :return: A name of this security group.
  996. """
  997. pass
  998. @abstractproperty
  999. def description(self):
  1000. """
  1001. Return the description of this security group.
  1002. :rtype: str
  1003. :return: A description of this security group.
  1004. """
  1005. pass
  1006. @abstractproperty
  1007. def rules(self):
  1008. """
  1009. Get the list of rules for this security group.
  1010. :rtype: list of :class:``.SecurityGroupRule``
  1011. :return: A list of security group rule objects.
  1012. """
  1013. pass
  1014. @abstractmethod
  1015. def delete(self):
  1016. """
  1017. Delete this security group.
  1018. :rtype: bool
  1019. :return: ``True`` if successful.
  1020. """
  1021. pass
  1022. @abstractmethod
  1023. def add_rule(self, ip_protocol=None, from_port=None, to_port=None,
  1024. cidr_ip=None, src_group=None):
  1025. """
  1026. Create a security group rule.
  1027. You need to pass in either ``src_group`` OR ``ip_protocol``,
  1028. ``from_port``, ``to_port``, and ``cidr_ip``. In other words, either
  1029. you are authorizing another group or you are authorizing some
  1030. ip-based rule.
  1031. :type ip_protocol: str
  1032. :param ip_protocol: Either ``tcp`` | ``udp`` | ``icmp``.
  1033. :type from_port: int
  1034. :param from_port: The beginning port number you are enabling.
  1035. :type to_port: int
  1036. :param to_port: The ending port number you are enabling.
  1037. :type cidr_ip: str or list of strings
  1038. :param cidr_ip: The CIDR block you are providing access to.
  1039. :type src_group: :class:``.SecurityGroup``
  1040. :param src_group: The Security Group object you are granting access to.
  1041. :rtype: bool
  1042. :return: ``True`` if successful.
  1043. """
  1044. pass
  1045. class SecurityGroupRule(CloudResource):
  1046. """
  1047. Represents a security group rule.
  1048. """
  1049. __metaclass__ = ABCMeta
  1050. @abstractproperty
  1051. def ip_protocol(self):
  1052. """
  1053. IP protocol used. Either ``tcp`` | ``udp`` | ``icmp``.
  1054. """
  1055. pass
  1056. @abstractproperty
  1057. def from_port(self):
  1058. """
  1059. Lowest port number opened as part of this rule.
  1060. """
  1061. pass
  1062. @abstractproperty
  1063. def to_port(self):
  1064. """
  1065. Highest port number opened as part of this rule.
  1066. """
  1067. pass
  1068. @abstractproperty
  1069. def cidr_ip(self):
  1070. """
  1071. CIDR block this security group is providing access to.
  1072. """
  1073. pass
  1074. @abstractproperty
  1075. def group(self):
  1076. """
  1077. Security group given access permissions by this rule.
  1078. :rtype: :class:``.SecurityGroup``
  1079. :return: The Security Group with granting access.
  1080. """
  1081. pass
  1082. class BucketObject(CloudResource):
  1083. """
  1084. Represents an object stored within a bucket.
  1085. """
  1086. __metaclass__ = ABCMeta
  1087. @abstractproperty
  1088. def id(self):
  1089. """
  1090. Get this object's id.
  1091. :rtype: id
  1092. :return: id of this object as returned by the cloud middleware.
  1093. """
  1094. pass
  1095. @abstractproperty
  1096. def name(self):
  1097. """
  1098. Get this object's name.
  1099. :rtype: str
  1100. :return: Name of this object as returned by the cloud middleware.
  1101. """
  1102. pass
  1103. @abstractmethod
  1104. def download(self, target_stream):
  1105. """
  1106. Download this object and write its contents to the ``target_stream``.
  1107. :rtype: bool
  1108. :return: ``True`` if successful.
  1109. """
  1110. pass
  1111. @abstractmethod
  1112. def upload(self, source_stream):
  1113. """
  1114. Set the contents of this object to the data read from the source
  1115. stream.
  1116. :rtype: bool
  1117. :return: ``True`` if successful.
  1118. """
  1119. pass
  1120. @abstractmethod
  1121. def delete(self):
  1122. """
  1123. Delete this object.
  1124. :rtype: bool
  1125. :return: ``True`` if successful.
  1126. """
  1127. pass
  1128. class Bucket(PageableObjectMixin, CloudResource):
  1129. __metaclass__ = ABCMeta
  1130. @abstractproperty
  1131. def id(self):
  1132. """
  1133. Get this bucket's id.
  1134. :rtype: id
  1135. :return: id of this bucket as returned by the cloud middleware.
  1136. """
  1137. pass
  1138. @abstractproperty
  1139. def name(self):
  1140. """
  1141. Get this bucket's name.
  1142. :rtype: str
  1143. :return: Name of this bucket as returned by the cloud middleware.
  1144. """
  1145. pass
  1146. @abstractmethod
  1147. def get(self, key):
  1148. """
  1149. Retrieve a given object from this bucket.
  1150. :type key: str
  1151. :param key: the identifier of the object to retrieve
  1152. :rtype: :class:``.BucketObject``
  1153. :return: The BucketObject or ``None`` if it cannot be found.
  1154. """
  1155. pass
  1156. @abstractmethod
  1157. def list(self, limit=None, marker=None):
  1158. """
  1159. List all objects within this bucket.
  1160. :rtype: :class:``.BucketObject``
  1161. :return: List of all available BucketObjects within this bucket.
  1162. """
  1163. pass
  1164. @abstractmethod
  1165. def delete(self, delete_contents=False):
  1166. """
  1167. Delete this bucket.
  1168. :type delete_contents: bool
  1169. :param delete_contents: If ``True``, all objects within the bucket
  1170. will be deleted.
  1171. :rtype: bool
  1172. :return: ``True`` if successful.
  1173. """
  1174. pass