resources.py 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  1. """
  2. Specifications for data objects exposed through a ``provider`` or ``service``.
  3. """
  4. from abc import ABCMeta
  5. from abc import abstractmethod
  6. from abc import abstractproperty
  7. from enum import Enum
  8. class CloudServiceType(object):
  9. """
  10. Defines possible service types that are offered by providers.
  11. Providers can implement the ``has_service`` method and clients can check
  12. for the availability of a service with::
  13. if (provider.has_service(CloudServiceTypes.BUCKET))
  14. ...
  15. """
  16. COMPUTE = 'compute'
  17. IMAGE = 'image'
  18. SECURITY = 'security'
  19. VOLUME = 'storage.volumes'
  20. BUCKET = 'storage.buckets'
  21. DNS = 'dns'
  22. class CloudResource(object):
  23. """
  24. Base interface for any Resource supported by a provider.
  25. This interface has a _provider property that can be used to access the
  26. provider associated with the resource, which is only intended for use by
  27. subclasses. Every CloudBridge resource also has an id, a name and a
  28. label property. The id property is a unique identifier for the resource.
  29. The name is a more user-friendly version of an id, suitable for
  30. display to an end-user. However, it cannot be used in place of id. See
  31. @name documentation. The label property is a user-assignable
  32. identifier for the resource.
  33. """
  34. __metaclass__ = ABCMeta
  35. @abstractproperty
  36. def _provider(self):
  37. """
  38. Returns the provider instance associated with this resource.
  39. Intended for use by subclasses only.
  40. :rtype: :class:`.CloudProvider`
  41. :return: a CloudProvider object
  42. """
  43. pass
  44. @abstractproperty
  45. def id(self):
  46. """
  47. Get the resource identifier.
  48. The id property is used to uniquely identify the resource, and is an
  49. opaque value which should not be interpreted by CloudBridge clients,
  50. and is a value meaningful to the underlying cloud provider.
  51. :rtype: ``str``
  52. :return: ID for this resource as returned by the cloud middleware.
  53. """
  54. pass
  55. @abstractproperty
  56. def name(self):
  57. """
  58. Get the name id for the resource.
  59. The name property is typically a user-friendly id value for the
  60. resource. The name is different from the id property in the following
  61. ways:
  62. 1. The name property is often a more user-friendly value to
  63. display to the user than the id property.
  64. 2. The name may sometimes be the same as the id, but should never
  65. be used in place of the id.
  66. 3. The id is what will uniquely identify a resource, and will be used
  67. internally by CloudBridge for all get operations etc.
  68. 4. All resources have a name.
  69. 5. The name is read-only.
  70. 6. However, the name may not necessarily be unique, which is the
  71. reason why it should not be used for uniquely identifying a
  72. resource.
  73. Example:
  74. The AWS machine image name maps to a CloudBridge name. It is not
  75. editable and is a user friendly name such as 'Ubuntu 18.04' and
  76. corresponds to the ami-name. It is distinct from the ami-id, which
  77. maps to CloudBridge's id property. The ami-name cannot be edited, and
  78. is set at creation time. It is not necessarily unique.
  79. In Azure, the machine image's name corresponds to CloudBridge's name
  80. property. In Azure, it also happens to be the same as the id property.
  81. The name property and the label property share the same character
  82. restrictions. See :py:attr:`~LabeledCloudResource.label`.
  83. """
  84. pass
  85. @abstractmethod
  86. def to_json(self):
  87. """
  88. Returns a JSON representation of the CloudResource object.
  89. """
  90. pass
  91. class LabeledCloudResource(CloudResource):
  92. @abstractproperty
  93. def label(self):
  94. """
  95. Get the resource label.
  96. The label property is a user-defined, editable identifier for a
  97. resource. It will often correspond to a user editable resource label
  98. in the underlying cloud provider, or be simulated through tags/labels.
  99. The label property adheres to the following restrictions:
  100. * Must be at least 3 characters in length.
  101. * Cannot be longer than 63 characters.
  102. * May only contain ASCII characters comprising of lowercase letters,
  103. numeric characters, and dashes.
  104. * Must begin with an alphanumeric character and end with one
  105. (i.e. cannot begin or end with a dash)
  106. Some resources may not support labels, in which case, a
  107. ``NotImplementedError`` will be thrown.
  108. :rtype: ``str``
  109. :return: Label for this resource as returned by the cloud middleware.
  110. :raise: ``NotImplementedError`` if this resource does not support
  111. labels.
  112. """
  113. pass
  114. class Configuration(dict):
  115. """
  116. Represents a CloudBridge configuration object
  117. """
  118. @abstractproperty
  119. def default_result_limit(self):
  120. """
  121. Get the default maximum number of results to return for a list method.
  122. The default limit will be applied to most ``list()`` and ``find()``
  123. methods whenever an explicit limit is not specified.
  124. :rtype: ``int``
  125. :return: The maximum number of results to return
  126. """
  127. pass
  128. @property
  129. def default_wait_timeout(self):
  130. """
  131. Get the default wait timeout for ``LifeCycleObjects``.
  132. The default wait timeout is applied in ``wait_for()`` and
  133. ``wait_till_ready()`` methods if no explicit timeout is specified.
  134. :rtype: ``int``
  135. :return: The maximum length of time (in seconds) to wait for the object
  136. to change to desired state.
  137. """
  138. pass
  139. @property
  140. def default_wait_interval(self):
  141. """
  142. Get the default wait interval for ``LifeCycleObjects``.
  143. The default wait interval is applied in ``wait_for()`` and
  144. ``wait_till_ready()`` methods if no explicit interval is specified.
  145. :rtype: ``int``
  146. :return: How frequently to poll the object's state.
  147. """
  148. pass
  149. @abstractproperty
  150. def debug_mode(self):
  151. """
  152. A flag indicating whether CloudBridge is in debug mode.
  153. Setting this to ``True`` will cause the underlying provider's debug
  154. output to be turned on. The flag can be toggled by sending in the
  155. ``cb_debug`` value via the config dictionary, or setting the
  156. ``CB_DEBUG`` environment variable.
  157. :rtype: ``bool``
  158. :return: Whether debug mode is on.
  159. """
  160. class ObjectLifeCycleMixin(object):
  161. """
  162. A mixin for an object with a defined life-cycle.
  163. Object examples include an Instance, Volume, Image, or Snapshot. An object
  164. that supports ObjectLifeCycleMixin will always have a state, defining which
  165. point in its life cycle it is currently at.
  166. It also defines a ``wait_till_ready`` operation, which indicates that the
  167. object is in a state in its life cycle where it is ready to be used by an
  168. end-user.
  169. A ``refresh`` operation allows the object to synchronize its state with the
  170. service provider.
  171. """
  172. __metaclass__ = ABCMeta
  173. @abstractproperty
  174. def _provider(self):
  175. """
  176. Obtain the provider associated with this object.
  177. This property is used internally to access the provider config and get
  178. default timeouts/intervals.
  179. :rtype: :class:`.CloudProvider` :return: The provider associated with
  180. this Resource
  181. """
  182. pass
  183. @abstractproperty
  184. def state(self):
  185. """
  186. Get the current state of this object.
  187. :rtype: ``str``
  188. :return: The current state as a string.
  189. """
  190. pass
  191. @abstractmethod
  192. def refresh(self):
  193. """
  194. Refresh this object's state and synchronize it with the provider.
  195. """
  196. pass
  197. @abstractmethod
  198. def wait_for(self, target_states, terminal_states=None, timeout=None,
  199. interval=None):
  200. """
  201. Wait for for an object to reach a one of desired target states.
  202. If the object does not reach the desired state within the specified
  203. timeout, a ``WaitStateException`` will be raised. The optional
  204. ``terminal_states`` property can be used to specify an additional set
  205. of states which, should the object reach one, the object thereafter
  206. will not transition into the desired target state. Should this happen,
  207. a ``WaitStateException`` will be raised.
  208. Example:
  209. .. code-block:: python
  210. instance.wait_for(
  211. [InstanceState.DELETED, InstanceState.UNKNOWN],
  212. terminal_states=[InstanceState.ERROR])
  213. :type target_states: ``list`` of states
  214. :param target_states: The list of target states to wait for.
  215. :type terminal_states: ``list`` of states
  216. :param terminal_states: A list of terminal states after which the
  217. object will not transition into a target state.
  218. A WaitStateException will be raised if the
  219. object transition into a terminal state.
  220. :type timeout: ``int``
  221. :param timeout: The maximum length of time (in seconds) to wait for the
  222. object to changed to desired state. If no timeout is
  223. specified, the global default_wait_timeout defined in
  224. the provider config will apply.
  225. :type interval: ``int``
  226. :param interval: How frequently to poll the object's state (in
  227. seconds). If no interval is specified, the global
  228. default_wait_interval defined in the provider config
  229. will apply.
  230. :rtype: ``True``
  231. :return: Returns ``True`` if successful. A ``WaitStateException``
  232. exception may be thrown by the underlying service if the
  233. object cannot get into a ready state (e.g. if the object
  234. is in an error state).
  235. """
  236. pass
  237. @abstractmethod
  238. def wait_till_ready(self, timeout, interval):
  239. """
  240. Wait till the current object reaches its ready state.
  241. An object's ready state is any state where the end-user can
  242. successfully interact with the object. Will throw a
  243. ``WaitStateException`` if the object is not ready within the specified
  244. timeout.
  245. :type timeout: ``int``
  246. :param timeout: The maximum length of time (in seconds) to wait for the
  247. object to become ready.
  248. :type interval: ``int``
  249. :param interval: How frequently to poll the object's ready state (in
  250. seconds).
  251. :rtype: ``True``
  252. :return: Returns ``True`` if successful. A ``WaitStateException``
  253. exception may be thrown by the underlying service if the
  254. object cannot get into a ready state (e.g. if the object
  255. is in an error state).
  256. """
  257. pass
  258. class PageableObjectMixin(object):
  259. """
  260. A marker interface for objects which support paged iteration.
  261. The list of objects can be iterated over using the ``list(limit, marker)``
  262. method.
  263. """
  264. @abstractmethod
  265. def __iter__(self):
  266. """
  267. Enables iteration through this object.
  268. Typically, an implementation will call the ``list(limit, marker)``
  269. method to transparently page additional objects in as iteration
  270. progresses.
  271. """
  272. pass
  273. @abstractmethod
  274. def list(self, limit=None, marker=None):
  275. """
  276. Returns a list of objects up to a maximum limit.
  277. If a limit and marker are specified, the records will be fetched up to
  278. the limit starting from the marker onwards. The returned list is a list
  279. of class ResultList, which has extra properties like is_truncated,
  280. supports_total and total_records to provide extra information
  281. about record availability.
  282. If limit is not specified, the limit will default to the underlying
  283. provider's default limit. Therefore, you need to check the is_truncated
  284. property to determine whether more records are available.
  285. The total number of results can be determined through the total_results
  286. property. Not all provides will support returning the total_results
  287. property, so the supports_total property can be used to determine
  288. whether a total is supported.
  289. To iterate through all the records, it will be easier to iterate
  290. directly through the instances using __iter__ instead of calling
  291. the list method. The __iter__ method will automatically call the list
  292. method to fetch a batch of records at a time.
  293. Example:
  294. .. code-block:: python
  295. # get first page of results
  296. instlist = provider.compute.instances.list(limit=50)
  297. for instance in instlist:
  298. print("Instance Data: {0}", instance)
  299. if instlist.supports_total:
  300. print("Total results: {0}".format(instlist.total_results))
  301. else:
  302. print("Total records unknown,"
  303. "but has more data?: {0}".format(instlist.is_truncated))
  304. # Page to next set of results
  305. if (instlist.is_truncated)
  306. instlist = provider.compute.instances.list(limit=100,
  307. marker=instlist.marker)
  308. # Alternative: iterate through every available record
  309. for instance in provider.compute.instances:
  310. print(instance)
  311. """
  312. pass
  313. class ResultList(list):
  314. """
  315. Provide extra properties to aid with paging through a many results.
  316. This is a wrapper class around a standard Python :py:class:`list` class.
  317. Example:
  318. .. code-block:: python
  319. # get first page of results
  320. rl = provider.compute.instances.list(limit=50)
  321. for result in rl:
  322. print("Instance Data: {0}", result)
  323. if rl.supports_total:
  324. print("Total results: {0}".format(rl.total_results))
  325. else:
  326. print("Total records unknown,"
  327. "but has more data?: {0}."format(rl.is_truncated))
  328. # Page to next set of results
  329. if (rl.is_truncated)
  330. rl = provider.compute.instances.list(limit=100,
  331. marker=rl.marker)
  332. """
  333. __metaclass__ = ABCMeta
  334. @abstractproperty
  335. def marker(self):
  336. """
  337. An opaque identifier used in paging through very long lists of objects.
  338. This marker can be provided to the list method to get the next set of
  339. results.
  340. """
  341. pass
  342. @abstractproperty
  343. def is_truncated(self):
  344. """
  345. Indicate whether this result list has more results that can be paged.
  346. """
  347. pass
  348. @abstractproperty
  349. def supports_total(self):
  350. """
  351. Indicate whether can obtain the total number of available results.
  352. The ``supports_total`` property should be checked before accessing the
  353. ``total_results`` property. This is a provider-specific property.
  354. """
  355. pass
  356. @abstractproperty
  357. def total_results(self):
  358. """
  359. Indicate the total number of results for a particular query.
  360. The ``supports_total`` property should be used to check whether the
  361. provider supports returning the total number of results, before
  362. accessing this property, or the behavior is indeterminate.
  363. """
  364. pass
  365. @abstractproperty
  366. def supports_server_paging(self):
  367. """
  368. Indicate whether this ``ResultList`` supports server side paging.
  369. If server side paging is not supported, the result will use client side
  370. paging and the data property provides direct access to all available
  371. data.
  372. """
  373. pass
  374. @abstractproperty
  375. def data(self):
  376. pass
  377. class InstanceState(object):
  378. """
  379. Standard states for an instance.
  380. :cvar UNKNOWN: Instance state unknown.
  381. :cvar PENDING: Instance is pending
  382. :cvar CONFIGURING: Instance is being reconfigured in some way.
  383. :cvar RUNNING: Instance is running.
  384. :cvar REBOOTING: Instance is rebooting.
  385. :cvar DELETED: Instance is deleted. No further operations possible.
  386. :cvar STOPPED: Instance is stopped. Instance can be resumed.
  387. :cvar ERROR: Instance is in an error state. No further operations possible.
  388. """
  389. UNKNOWN = "unknown"
  390. PENDING = "pending"
  391. CONFIGURING = "configuring"
  392. RUNNING = "running"
  393. REBOOTING = "rebooting"
  394. DELETED = "deleted"
  395. STOPPED = "stopped"
  396. ERROR = "error"
  397. class Instance(ObjectLifeCycleMixin, LabeledCloudResource):
  398. __metaclass__ = ABCMeta
  399. @LabeledCloudResource.label.setter
  400. @abstractmethod
  401. def label(self, value):
  402. """
  403. Set the instance label.
  404. :type value: ``str``
  405. :param value: The value to set the label to.
  406. """
  407. pass
  408. @abstractproperty
  409. def public_ips(self):
  410. """
  411. Get all the public IP addresses for this instance.
  412. :rtype: ``list``
  413. :return: A list of public IP addresses associated with this instance.
  414. """
  415. pass
  416. @abstractproperty
  417. def private_ips(self):
  418. """
  419. Get all the private IP addresses for this instance.
  420. :rtype: ``list``
  421. :return: A list of private IP addresses associated with this instance.
  422. """
  423. pass
  424. @abstractproperty
  425. def vm_type_id(self):
  426. """
  427. Get the VM type id for this instance.
  428. This will typically be a string value like 'm1.large'. On OpenStack,
  429. this may be a number or UUID. To get the full :class:``.VMType``
  430. object, you can use the ``instance.vm_type`` property instead.
  431. :rtype: ``str``
  432. :return: VM type id for this instance (e.g., ``m1.large``)
  433. """
  434. pass
  435. @abstractproperty
  436. def vm_type(self):
  437. """
  438. Retrieve full VM type information for this instance.
  439. :rtype: :class:`.VMType`
  440. :return: VM type for this instance
  441. """
  442. pass
  443. @abstractmethod
  444. def reboot(self):
  445. """
  446. Reboot this instance (using the cloud middleware API).
  447. :rtype: ``bool``
  448. :return: ``True`` if the reboot was successful; ``False`` otherwise.
  449. """
  450. pass
  451. @abstractmethod
  452. def start(self):
  453. """
  454. Start this instance (using the cloud middleware API)
  455. :rtype: ``bool``
  456. :return: ``True`` if the starting was successful; ``False`` otherwise.
  457. """
  458. pass
  459. @abstractmethod
  460. def stop(self):
  461. """
  462. Stop this instance (using the cloud middleware API)
  463. :rtype: ``bool``
  464. :return: ``True`` if the stopping was successful; ``False`` otherwise.
  465. """
  466. pass
  467. @abstractmethod
  468. def delete(self):
  469. """
  470. Permanently delete this instance.
  471. """
  472. pass
  473. @abstractproperty
  474. def image_id(self):
  475. """
  476. Get the image ID for this instance.
  477. :rtype: ``str``
  478. :return: Image ID (i.e., AMI) this instance is using.
  479. """
  480. pass
  481. @abstractproperty
  482. def zone_id(self):
  483. """
  484. Get the placement zone ID where this instance is running.
  485. :rtype: ``str``
  486. :return: Region/zone/placement where this instance is running.
  487. """
  488. pass
  489. @abstractproperty
  490. def subnet_id(self):
  491. """
  492. Get the subnet ID where this instance is placed.
  493. :rtype: ``str``
  494. :return: Subnet ID to which this instance is connected.
  495. """
  496. pass
  497. # @abstractproperty
  498. # def mac_address(self):
  499. # """
  500. # Get the MAC address for this instance.
  501. #
  502. # :rtype: str
  503. # :return: MAC address for ths instance.
  504. # """
  505. # pass
  506. @abstractproperty
  507. def vm_firewalls(self):
  508. """
  509. Get the firewalls (security groups) associated with this instance.
  510. :rtype: list or :class:`.VMFirewall` objects
  511. :return: A list of VMFirewall objects associated with this instance.
  512. """
  513. pass
  514. @abstractproperty
  515. def vm_firewall_ids(self):
  516. """
  517. Get the IDs of the VM firewalls associated with this instance.
  518. :rtype: list or :class:``str``
  519. :return: A list of the VMFirewall IDs associated with this instance.
  520. """
  521. pass
  522. @abstractproperty
  523. def key_pair_id(self):
  524. """
  525. Get the id of the key pair associated with this instance.
  526. :rtype: ``str``
  527. :return: Id of the ssh key pair associated with this instance.
  528. """
  529. pass
  530. @abstractmethod
  531. def create_image(self, label):
  532. """
  533. Create a new image based on this instance.
  534. :rtype: :class:``.Image``
  535. :return: an Image object
  536. """
  537. pass
  538. @abstractmethod
  539. def add_floating_ip(self, floating_ip):
  540. """
  541. Add a public IP address to this instance.
  542. :type floating_ip: :class:``.FloatingIP`` or floating IP ID
  543. :param floating_ip: The FloatingIP object to associate with the
  544. instance. Note that is not the actual public IP
  545. address but the CloudBridge object encapsulating
  546. the IP or the respective provider ID that
  547. identifies the address.
  548. """
  549. pass
  550. @abstractmethod
  551. def remove_floating_ip(self, floating_ip):
  552. """
  553. Remove a public IP address from this instance.
  554. :type floating_ip: :class:``.FloatingIP`` or floating IP ID
  555. :param floating_ip: The FloatingIP object to remove from the
  556. instance. Note that is not the actual public IP
  557. address but the CloudBridge object encapsulating
  558. the IP or the respective provider ID that
  559. identifies the address.
  560. """
  561. pass
  562. @abstractmethod
  563. def add_vm_firewall(self, firewall):
  564. """
  565. Add a VM firewall to this instance
  566. :type firewall: :class:``.VMFirewall``
  567. :param firewall: The VMFirewall to associate with the instance.
  568. """
  569. pass
  570. @abstractmethod
  571. def remove_vm_firewall(self, firewall):
  572. """
  573. Remove a VM firewall from this instance
  574. :type firewall: ``VMFirewall``
  575. :param firewall: The VMFirewall to associate with the instance.
  576. """
  577. pass
  578. class MachineImageState(object):
  579. """
  580. Standard states for a machine image
  581. :cvar UNKNOWN: Image state unknown.
  582. :cvar PENDING: Image is pending
  583. :cvar AVAILABLE: Image is available
  584. :cvar ERROR: Image is in an error state. Not recoverable.
  585. """
  586. UNKNOWN = "unknown"
  587. PENDING = "pending"
  588. AVAILABLE = "available"
  589. ERROR = "error"
  590. class LaunchConfig(object):
  591. """
  592. Represents an advanced launch configuration object.
  593. This object can contain information such as ``BlockDeviceMappings``
  594. configurations and other advanced options, which may be useful when
  595. launching an instance.
  596. Example:
  597. .. code-block:: python
  598. lc = provider.compute.instances.create_launch_config()
  599. lc.add_block_device(...)
  600. inst = provider.compute.instances.create(
  601. 'MyVM', image, vm_type, subnet, launch_config=lc)
  602. """
  603. @abstractmethod
  604. def add_ephemeral_device(self):
  605. """
  606. Add a new ephemeral block device mapping to the boot configuration.
  607. This can be used to add existing ephemeral devices to the instance
  608. (the total number of ephemeral devices available for a particular
  609. ``VMType`` can be determined by querying the ``VMType`` service).
  610. Note that on some providers, such as AWS, ephemeral devices must be
  611. added in as a device mapping at instance creation time and cannot be
  612. added afterwards.
  613. Note that the device name, such as */dev/sda1*, cannot be selected at
  614. present, since this tends to be provider and VM type specific.
  615. However, the order of device addition coupled with device type will
  616. generally determine naming order, with devices added first getting
  617. lower letters than instances added later.
  618. Example:
  619. .. code-block:: python
  620. lc = provider.compute.instances.create_launch_config()
  621. # 1. Add all available ephemeral devices
  622. vm_type = provider.compute.vm_types.find(name='m1.tiny')[0]
  623. for i in range(vm_type.num_ephemeral_disks):
  624. lc.add_ephemeral_device()
  625. """
  626. pass
  627. @abstractmethod
  628. def add_volume_device(self, source=None, is_root=None, size=None,
  629. delete_on_terminate=None):
  630. """
  631. Add a new volume based block device mapping to the boot configuration.
  632. The volume can be based on a snapshot, image, existing volume or
  633. be a blank new volume, and is specified by the source parameter.
  634. The property ``is_root`` can be set to ``True`` to override any
  635. existing root device mappings. Otherwise, the default behavior is to
  636. add new block devices to the instance.
  637. Note that the device name, such as */dev/sda1*, cannot be selected at
  638. present since this tends to be provider and VM type specific. However,
  639. the order of device addition coupled with device type will generally
  640. determine naming order, with devices added first getting lower letters
  641. than instances added later (except when ``is_root`` is set).
  642. Example:
  643. .. code-block:: python
  644. lc = provider.compute.instances.create_launch_config()
  645. # 1. Create and attach an empty volume of size 100GB
  646. lc.add_volume_device(size=100, delete_on_terminate=True)
  647. # 2. Create and attach a volume based on a snapshot
  648. snap = provider.storage.snapshots.get('<my_snapshot_id>')
  649. lc.add_volume_device(source=snap)
  650. # 3. Create+attach a volume based on an image and set it as root
  651. img = provider.compute.images.get('<my_image_id>')
  652. lc.add_volume_device(source=img, size=100, is_root=True)
  653. :type source: ``Volume``, ``Snapshot``, ``Image`` or None.
  654. :param source: The source ``block_device`` to add. If ``Volume``, the
  655. volume will be attached directly to the instance.
  656. If ``Snapshot``, a volume will be created based on the
  657. Snapshot and attached to the instance. If ``Image``,
  658. a volume based on the Image will be attached to the
  659. instance. If ``None``, the source is assumed to be
  660. a blank volume.
  661. :type is_root: ``bool``
  662. :param is_root: Determines which device will serve as the root device.
  663. If more than one device is defined as root, an
  664. ``InvalidConfigurationException`` will be thrown.
  665. :type size: ``int``
  666. :param size: The size of the volume to create. An implementation may
  667. ignore this parameter for certain sources like 'Volume'.
  668. :type delete_on_terminate: ``bool``
  669. :param delete_on_terminate: Determines whether to delete or keep the
  670. volume on instance termination.
  671. """
  672. pass
  673. class MachineImage(ObjectLifeCycleMixin, LabeledCloudResource):
  674. __metaclass__ = ABCMeta
  675. @abstractproperty
  676. def description(self):
  677. """
  678. Get the image description.
  679. :rtype: ``str``
  680. :return: Description for this image as returned by the cloud
  681. middleware.
  682. """
  683. pass
  684. @abstractproperty
  685. def min_disk(self):
  686. """
  687. Return the minimum size of the disk that's required to boot this image.
  688. Value returned is in gigabytes.
  689. :rtype: ``int``
  690. :return: The minimum disk size needed by this image.
  691. """
  692. pass
  693. @abstractmethod
  694. def delete(self):
  695. """
  696. Delete this image.
  697. :rtype: ``bool``
  698. :return: ``True`` if the operation succeeded.
  699. """
  700. pass
  701. class NetworkState(object):
  702. """
  703. Standard states for a network.
  704. :cvar UNKNOWN: Network state unknown.
  705. :cvar PENDING: Network is being created.
  706. :cvar AVAILABLE: Network is available.
  707. :cvar DOWN: Network is not operational.
  708. :cvar ERROR: Network is in error state.
  709. """
  710. UNKNOWN = "unknown"
  711. PENDING = "pending"
  712. AVAILABLE = "available"
  713. DOWN = "down"
  714. ERROR = "error"
  715. class Network(ObjectLifeCycleMixin, LabeledCloudResource):
  716. """
  717. Represents a software-defined network, like the Virtual Private Cloud.
  718. """
  719. __metaclass__ = ABCMeta
  720. @LabeledCloudResource.label.setter
  721. @abstractmethod
  722. def label(self, value):
  723. """
  724. Set the resource label.
  725. :type value: ``str``
  726. :param value: The value to set the label to.
  727. """
  728. pass
  729. @abstractproperty
  730. def external(self):
  731. """
  732. A flag to indicate if this network is capable of Internet-connectivity.
  733. :rtype: ``bool``
  734. :return: ``True`` if the network can be connected to the Internet.
  735. """
  736. pass
  737. @abstractproperty
  738. def state(self):
  739. """
  740. The state of the network.
  741. :rtype: ``str``
  742. :return: One of ``unknown``, ``pending``, ``available``, ``down`` or
  743. ``error``.
  744. """
  745. pass
  746. @abstractproperty
  747. def cidr_block(self):
  748. """
  749. A CIDR block for this network.
  750. .. note:: OpenStack does not define a CIDR block for networks.
  751. :rtype: ``str``
  752. :return: A CIDR block string.
  753. """
  754. pass
  755. @abstractmethod
  756. def delete(self):
  757. """
  758. Delete this network.
  759. :rtype: ``bool``
  760. :return: ``True`` if successful.
  761. """
  762. pass
  763. @abstractproperty
  764. def subnets(self):
  765. """
  766. The associated subnets.
  767. :rtype: ``list`` of :class:`.Subnet`
  768. :return: List of subnets associated with this network.
  769. """
  770. pass
  771. @abstractproperty
  772. def gateways(self):
  773. """
  774. Provides access to the internet gateways attached to this network.
  775. :rtype: :class:`.GatewaySubService`
  776. :return: A GatewaySubService object
  777. """
  778. pass
  779. class SubnetState(object):
  780. """
  781. Standard states for a subnet.
  782. :cvar UNKNOWN: Subnet state unknown.
  783. :cvar PENDING: Subnet is being created.
  784. :cvar AVAILABLE: Subnet is available.
  785. :cvar DOWN: Subnet is not operational.
  786. :cvar ERROR: Subnet is in error state.
  787. """
  788. UNKNOWN = "unknown"
  789. PENDING = "pending"
  790. AVAILABLE = "available"
  791. DOWN = "down"
  792. ERROR = "error"
  793. class Subnet(ObjectLifeCycleMixin, LabeledCloudResource):
  794. """
  795. Represents a subnet, as part of a Network.
  796. """
  797. __metaclass__ = ABCMeta
  798. @LabeledCloudResource.label.setter
  799. @abstractmethod
  800. def label(self, value):
  801. """
  802. Set the resource label.
  803. :type value: ``str``
  804. :param value: The value to set the label to.
  805. """
  806. pass
  807. @abstractproperty
  808. def cidr_block(self):
  809. """
  810. A CIDR block for this subnet.
  811. :rtype: ``str``
  812. :return: A CIDR block string.
  813. """
  814. pass
  815. @abstractproperty
  816. def network_id(self):
  817. """
  818. ID of the network associated with this this subnet.
  819. :rtype: ``str``
  820. :return: Network ID.
  821. """
  822. pass
  823. @abstractproperty
  824. def network(self):
  825. """
  826. The parent network object associated with this this subnet.
  827. :rtype: ``Network``
  828. :return: `Network` object
  829. """
  830. pass
  831. @abstractproperty
  832. def zone(self):
  833. """
  834. Placement zone of the subnet.
  835. If the provider does not support subnet placement, return ``None``.
  836. :rtype: :class:`.PlacementZone` object
  837. :return: Placement zone of the subnet, or ``None`` if not defined.
  838. """
  839. pass
  840. @abstractmethod
  841. def delete(self):
  842. """
  843. Delete this subnet.
  844. :rtype: ``bool``
  845. :return: ``True`` if successful.
  846. """
  847. pass
  848. class FloatingIpState(object):
  849. """
  850. Standard states for a floating ip.
  851. :cvar UNKNOWN: Floating IP state unknown.
  852. :cvar AVAILABLE: Floating IP is available.
  853. :cvar IN_USE: Floating IP is attached to a device.
  854. :cvar ERROR: Floating IP is in an error state.
  855. """
  856. UNKNOWN = "unknown"
  857. AVAILABLE = "available"
  858. IN_USE = "in_use"
  859. ERROR = "error"
  860. class FloatingIP(ObjectLifeCycleMixin, CloudResource):
  861. """
  862. Represents a floating (i.e., static) IP address.
  863. """
  864. __metaclass__ = ABCMeta
  865. @abstractproperty
  866. def public_ip(self):
  867. """
  868. Public IP address.
  869. :rtype: ``str``
  870. :return: IP address.
  871. """
  872. pass
  873. @abstractproperty
  874. def private_ip(self):
  875. """
  876. Private IP address this address is attached to.
  877. :rtype: ``str``
  878. :return: IP address or ``None``.
  879. """
  880. pass
  881. @abstractproperty
  882. def in_use(self):
  883. """
  884. Whether the address is in use or not.
  885. :rtype: ``bool``
  886. :return: ``True`` if the address is attached to an instance.
  887. """
  888. pass
  889. @abstractmethod
  890. def delete(self):
  891. """
  892. Delete this address.
  893. :rtype: ``bool``
  894. :return: ``True`` if successful.
  895. """
  896. pass
  897. class RouterState(object):
  898. """
  899. Standard states for a router.
  900. :cvar UNKNOWN: Router state unknown.
  901. :cvar ATTACHED: Router is attached to a network and should be operational.
  902. :cvar DETACHED: Router is detached from a network.
  903. """
  904. UNKNOWN = "unknown"
  905. ATTACHED = "attached"
  906. DETACHED = "detached"
  907. class Router(LabeledCloudResource):
  908. """
  909. Represents a private network router.
  910. This logical router is meant to roughly mimic the properties of a physical
  911. router. Therefore, attaching a subnet can be thought of as plugging in a
  912. network cable to enable routing to/from that subnet. Attaching a gateway
  913. can be thought of as plugging in an upstream link.
  914. """
  915. __metaclass__ = ABCMeta
  916. @LabeledCloudResource.label.setter
  917. @abstractmethod
  918. def label(self, value):
  919. """
  920. Set the resource label.
  921. :type value: ``str``
  922. :param value: The value to set the label to.
  923. """
  924. pass
  925. @abstractproperty
  926. def state(self):
  927. """
  928. Router state: attached or detached to a network.
  929. :rtype: ``str``
  930. :return: ``attached`` or ``detached``.
  931. """
  932. pass
  933. @abstractproperty
  934. def network_id(self):
  935. """
  936. ID of the network to which the router is attached.
  937. :rtype: ``str``
  938. :return: ID for the attached network or ``None``.
  939. """
  940. pass
  941. @abstractmethod
  942. def delete(self):
  943. """
  944. Delete this router.
  945. :rtype: ``bool``
  946. :return: ``True`` if successful.
  947. """
  948. pass
  949. @abstractmethod
  950. def attach_subnet(self, subnet):
  951. """
  952. Attach this router to a subnet.
  953. :type subnet: ``Subnet`` or ``str``
  954. :param subnet: The subnet to which to attach this router.
  955. :rtype: ``bool``
  956. :return: ``True`` if successful.
  957. """
  958. pass
  959. @abstractmethod
  960. def detach_subnet(self, subnet):
  961. """
  962. Detach this subnet from a network.
  963. :type subnet: ``Subnet`` or ``str``
  964. :param subnet: The subnet to detach from this router.
  965. :rtype: ``bool``
  966. :return: ``True`` if successful.
  967. """
  968. pass
  969. @abstractproperty
  970. def subnets(self):
  971. """
  972. List of subnets attached to this router.
  973. :rtype: ``list`` of :class:`.Subnet` objects
  974. :return: A list of subnets associated with this router.
  975. """
  976. pass
  977. @abstractmethod
  978. def attach_gateway(self, gateway):
  979. """
  980. Attach a gateway to this router.
  981. :type gateway: ``Gateway``
  982. :param gateway: The Gateway to attach to this router.
  983. :rtype: ``bool``
  984. :return: ``True`` if successful.
  985. """
  986. pass
  987. @abstractmethod
  988. def detach_gateway(self, gateway):
  989. """
  990. Detach this router from a gateway.
  991. :rtype: ``bool``
  992. :return: ``True`` if successful.
  993. """
  994. pass
  995. class GatewayState(object):
  996. """
  997. Standard states for a gateway.
  998. :cvar UNKNOWN: Gateway state unknown.
  999. :cvar CONFIGURING: Gateway is being configured
  1000. :cvar AVAILABLE: Gateway is ready
  1001. :cvar ERROR: Gateway is ready
  1002. """
  1003. UNKNOWN = "unknown"
  1004. CONFIGURING = "configuring"
  1005. AVAILABLE = "available"
  1006. ERROR = "error"
  1007. class Gateway(CloudResource):
  1008. """
  1009. Represents a gateway resource.
  1010. """
  1011. __metaclass__ = ABCMeta
  1012. @abstractproperty
  1013. def network_id(self):
  1014. """
  1015. ID of the network to which the gateway is attached.
  1016. :rtype: ``str``
  1017. :return: ID for the attached network or ``None``.
  1018. """
  1019. pass
  1020. @abstractmethod
  1021. def delete(self):
  1022. """
  1023. Delete this gateway. On some providers, if the gateway
  1024. is public/a singleton, this operation will do nothing.
  1025. """
  1026. pass
  1027. @abstractproperty
  1028. def floating_ips(self):
  1029. """
  1030. Provides access to floating IPs connected to this internet gateway.
  1031. :rtype: :class:`.FloatingIPSubService`
  1032. :return: A FloatingIPSubService object
  1033. """
  1034. pass
  1035. class InternetGateway(ObjectLifeCycleMixin, Gateway):
  1036. """
  1037. Represents an Internet gateway resource.
  1038. """
  1039. __metaclass__ = ABCMeta
  1040. class DnsZone(CloudResource):
  1041. """
  1042. Represents a dns host zone.
  1043. A host zone represents a top level domain (e.g. cloudve.org) in which
  1044. multiple dns records (e.g. A, CNAME. MX etc.) are contained.
  1045. """
  1046. __metaclass__ = ABCMeta
  1047. @property
  1048. def admin_email(self):
  1049. """
  1050. Email address of this zone's administrator. Some cloud providers do not
  1051. support this field, and therefore, it may be stored in an extra field
  1052. such as description or not supported at all. (This field is mandatory
  1053. in OpenStack)
  1054. :return: Administrator's email as a string
  1055. """
  1056. pass
  1057. @abstractmethod
  1058. def delete(self):
  1059. """
  1060. Delete this zone.
  1061. """
  1062. pass
  1063. @abstractproperty
  1064. def records(self):
  1065. """
  1066. List of DNS records in this zone.
  1067. :rtype: ``list`` of :class:`.DnsRecord` objects
  1068. :return: A list of DnsRecords associated with this zone.
  1069. """
  1070. pass
  1071. class DnsRecordType(object):
  1072. """
  1073. DNS record types.
  1074. """
  1075. A = 'A'
  1076. AAAA = 'AAAA'
  1077. CNAME = 'CNAME'
  1078. MX = 'MX'
  1079. NS = 'NS'
  1080. PTR = 'PTR'
  1081. SPF = 'SPF'
  1082. SRV = 'SRV'
  1083. SSHFP = 'SSHFP'
  1084. TXT = 'TXT'
  1085. class DnsRecord(CloudResource):
  1086. """
  1087. Represents a dns record.
  1088. A dns record belongs to a host zone and can contain
  1089. records of varous types such as A, CNAME. MX etc.
  1090. """
  1091. __metaclass__ = ABCMeta
  1092. @abstractproperty
  1093. def zone_id(self):
  1094. """
  1095. The containing zone for this dns record
  1096. :rtype: ``str``
  1097. :return: The ID of the zone for this dns record
  1098. """
  1099. pass
  1100. @abstractproperty
  1101. def type(self):
  1102. """
  1103. Dns Record type which could be A, CNAME, MX, AAAA, PTR
  1104. :rtype: ``DnsRecordType``
  1105. :return: An enum representing the DNS record type.
  1106. """
  1107. pass
  1108. @abstractproperty
  1109. def data(self):
  1110. """
  1111. Dns Record data
  1112. :rtype: ``str``
  1113. :return: A string containing this DNS record's data.
  1114. """
  1115. pass
  1116. @abstractproperty
  1117. def ttl(self):
  1118. """
  1119. ttl for this record
  1120. :rtype: ``int``
  1121. :return: The ttl (in seconds) for this record.
  1122. """
  1123. pass
  1124. class AttachmentInfo(object):
  1125. """
  1126. Contains attachment information for a volume.
  1127. """
  1128. @abstractproperty
  1129. def volume(self):
  1130. """
  1131. Get the volume instance related to this attachment.
  1132. :rtype: ``Volume``
  1133. :return: Volume object that this attachment info belongs to
  1134. """
  1135. pass
  1136. @abstractproperty
  1137. def instance_id(self):
  1138. """
  1139. Get the instance_id related to this attachment.
  1140. :rtype: ``str``
  1141. :return: Instance id that this attachment info belongs to
  1142. """
  1143. pass
  1144. @abstractproperty
  1145. def device(self):
  1146. """
  1147. Get the device the volume is mapped as.
  1148. :rtype: ``str``
  1149. :return: Device that the volume is mapped as
  1150. """
  1151. pass
  1152. class VolumeState(object):
  1153. """
  1154. Standard states for a volume
  1155. :cvar UNKNOWN: Volume state unknown.
  1156. :cvar CREATING: Volume is being created.
  1157. :cvar CONFIGURING: Volume is being configured in some way.
  1158. :cvar AVAILABLE: Volume is available and can be attached to an instance.
  1159. :cvar IN_USE: Volume is attached and in-use.
  1160. :cvar DELETED: Volume has been deleted. No further operations possible.
  1161. :cvar ERROR: Volume is in an error state. No further operations possible.
  1162. """
  1163. UNKNOWN = "unknown"
  1164. CREATING = "creating"
  1165. CONFIGURING = "configuring"
  1166. AVAILABLE = "available"
  1167. IN_USE = "in-use"
  1168. DELETED = "deleted"
  1169. ERROR = "error"
  1170. class Volume(ObjectLifeCycleMixin, LabeledCloudResource):
  1171. """
  1172. Represents a block storage device (aka volume).
  1173. """
  1174. __metaclass__ = ABCMeta
  1175. @LabeledCloudResource.label.setter
  1176. @abstractmethod
  1177. def label(self, value):
  1178. """
  1179. Set the volume label.
  1180. """
  1181. pass
  1182. @abstractproperty
  1183. def description(self):
  1184. """
  1185. Get the volume description.
  1186. Some cloud providers may not support this property, and will return the
  1187. volume label instead.
  1188. :rtype: ``str``
  1189. :return: Description for this volume as returned by the cloud
  1190. middleware.
  1191. """
  1192. pass
  1193. @description.setter
  1194. @abstractmethod
  1195. def description(self, value):
  1196. """
  1197. Set the volume description.
  1198. Some cloud providers may not support this property, and setting the
  1199. description may have no effect (providers that do not support this
  1200. property will always return the volume label as the description).
  1201. """
  1202. pass
  1203. @abstractproperty
  1204. def size(self):
  1205. """
  1206. Get the volume size (in GB).
  1207. :rtype: ``int``
  1208. :return: Size for this volume as returned by the cloud middleware.
  1209. """
  1210. pass
  1211. @abstractproperty
  1212. def create_time(self):
  1213. """
  1214. Get the creation data and time for this volume.
  1215. :rtype: ``DateTime``
  1216. :return: Creation time for this volume as returned by the cloud
  1217. middleware.
  1218. """
  1219. pass
  1220. @abstractproperty
  1221. def zone_id(self):
  1222. """
  1223. Get the placement zone id that this volume belongs to.
  1224. :rtype: ``str``
  1225. :return: PlacementZone for this volume as returned by the cloud
  1226. middleware.
  1227. """
  1228. pass
  1229. @abstractproperty
  1230. def source(self):
  1231. """
  1232. If available, get the source that this volume is based on.
  1233. This can be a ``Snapshot``, an ``Image``, or ``None`` if no source.
  1234. :rtype: ``Snapshot``, ``Image``, or ``None``
  1235. :return: Snapshot or Image source for this volume as returned by the
  1236. cloud middleware.
  1237. """
  1238. pass
  1239. @abstractproperty
  1240. def attachments(self):
  1241. """
  1242. Get attachment information for this volume.
  1243. :rtype: ``AttachmentInfo``
  1244. :return: Returns an AttachmentInfo object.
  1245. """
  1246. pass
  1247. @abstractmethod
  1248. def attach(self, instance, device):
  1249. """
  1250. Attach this volume to an instance.
  1251. :type instance: ``str`` or :class:`.Instance` object
  1252. :param instance: The ID of an instance or an ``Instance`` object to
  1253. which this volume will be attached.
  1254. :type device: ``str``
  1255. :param device: The device on the instance through which the
  1256. volume will be exposed (e.g. /dev/sdh).
  1257. :rtype: ``bool``
  1258. :return: ``True`` if successful.
  1259. """
  1260. pass
  1261. @abstractmethod
  1262. def detach(self, force=False):
  1263. """
  1264. Detach this volume from an instance.
  1265. :type force: ``bool``
  1266. :param force: Forces detachment if the previous detachment attempt
  1267. did not occur cleanly. This option is supported on select
  1268. clouds only. This option can lead to data loss or a
  1269. corrupted file system. Use this option only as a last
  1270. resort to detach a volume from a failed instance. The
  1271. instance will not have an opportunity to flush file
  1272. system caches nor file system meta data. If you
  1273. use this option, you must perform file system check and
  1274. repair procedures.
  1275. :rtype: ``bool``
  1276. :return: ``True`` if successful.
  1277. """
  1278. pass
  1279. @abstractmethod
  1280. def create_snapshot(self, label, description=None):
  1281. """
  1282. Create a snapshot of this Volume.
  1283. :type label: ``str``
  1284. :param label: The label for this snapshot.
  1285. :type description: ``str``
  1286. :param description: A description of the snapshot.
  1287. Limited to 256 characters.
  1288. :rtype: :class:`.Snapshot`
  1289. :return: The created Snapshot object.
  1290. """
  1291. pass
  1292. @abstractmethod
  1293. def delete(self):
  1294. """
  1295. Delete this volume.
  1296. :rtype: ``bool``
  1297. :return: ``True`` if successful.
  1298. """
  1299. pass
  1300. class SnapshotState(object):
  1301. """
  1302. Standard states for a snapshot
  1303. :cvar UNKNOWN: Snapshot state unknown.
  1304. :cvar PENDING: Snapshot is pending.
  1305. :cvar CONFIGURING: Snapshot is being configured in some way.
  1306. :cvar AVAILABLE: Snapshot has been completed and is ready for use.
  1307. :cvar ERROR: Snapshot is in an error state. No further operations possible.
  1308. """
  1309. UNKNOWN = "unknown"
  1310. PENDING = "pending"
  1311. CONFIGURING = "configuring"
  1312. AVAILABLE = "available"
  1313. ERROR = "error"
  1314. class Snapshot(ObjectLifeCycleMixin, LabeledCloudResource):
  1315. """
  1316. Represents a snapshot of a block storage device.
  1317. """
  1318. __metaclass__ = ABCMeta
  1319. @LabeledCloudResource.label.setter
  1320. @abstractmethod
  1321. def label(self, value):
  1322. """
  1323. Set the snapshot label.
  1324. """
  1325. pass
  1326. @abstractproperty
  1327. def description(self):
  1328. """
  1329. Get the snapshot description.
  1330. Some cloud providers may not support this property, and will return the
  1331. snapshot label instead.
  1332. :rtype: ``str``
  1333. :return: Description for this snapshot as returned by the cloud
  1334. middleware.
  1335. """
  1336. pass
  1337. @description.setter
  1338. @abstractmethod
  1339. def description(self, value):
  1340. """
  1341. Set the snapshot description.
  1342. Some cloud providers may not support this property, and setting the
  1343. description may have no effect (providers that do not support this
  1344. property will always return the snapshot label as the description).
  1345. :type value: ``str``
  1346. :param value: The value for the snapshot description.
  1347. """
  1348. pass
  1349. @abstractproperty
  1350. def size(self):
  1351. """
  1352. Get the snapshot size (in GB).
  1353. :rtype: ``int``
  1354. :return: Size for this snapshot as returned by the cloud middleware.
  1355. """
  1356. pass
  1357. @abstractproperty
  1358. def volume_id(self):
  1359. """
  1360. Get the id of the volume that this snapshot is based on.
  1361. This method may return ``None`` if the source volume no longer exists.
  1362. :rtype: ``int``
  1363. :return: Id of the volume that this snapshot is based on
  1364. """
  1365. pass
  1366. @abstractproperty
  1367. def create_time(self):
  1368. """
  1369. Get the creation data and time for this snapshot.
  1370. :rtype: ``DateTime``
  1371. :return: Creation time for this snapshot as returned by the cloud
  1372. middleware.
  1373. """
  1374. pass
  1375. @abstractmethod
  1376. def create_volume(self, size=None, volume_type=None, iops=None):
  1377. """
  1378. Create a new Volume from this Snapshot.
  1379. :type size: ``int``
  1380. :param size: The size of the new volume, in GiB (optional). Defaults to
  1381. the size of the snapshot.
  1382. :type volume_type: ``str``
  1383. :param volume_type: The type of the volume (optional). Availability and
  1384. valid values depend on the provider.
  1385. :type iops: ``int``
  1386. :param iops: The provisioned IOPs you want to associate with
  1387. this volume (optional). Availability depends on the
  1388. provider.
  1389. :rtype: :class:`.Volume`
  1390. :return: An instance of the created Volume.
  1391. """
  1392. pass
  1393. # @abstractmethod
  1394. # def share(self, user_ids=None):
  1395. # """
  1396. # Share this Snapshot.
  1397. #
  1398. # :type user_ids: list of strings
  1399. # :param user_ids: A list of cloud provider compatible user IDs. If no
  1400. # IDs are specified, the snapshot is made public.
  1401. #
  1402. # :rtype: bool
  1403. # :return: ``True`` if successful.
  1404. # """
  1405. # pass
  1406. #
  1407. # @abstractmethod
  1408. # def unshare(self, user_ids=None):
  1409. # """
  1410. # Unshare this Snapshot.
  1411. #
  1412. # :type user_ids: list of strings
  1413. # :param user_ids: A list of cloud provider compatible user IDs. If no
  1414. # IDs are specified, the snapshot is made private.
  1415. #
  1416. # :rtype: bool
  1417. # :return: ``True`` if successful.
  1418. # """
  1419. # pass
  1420. @abstractmethod
  1421. def delete(self):
  1422. """
  1423. Delete this snapshot.
  1424. :rtype: ``bool``
  1425. :return: ``True`` if successful.
  1426. """
  1427. pass
  1428. class KeyPair(CloudResource):
  1429. """
  1430. Represents an ssh key pair.
  1431. """
  1432. __metaclass__ = ABCMeta
  1433. @abstractproperty
  1434. def material(self):
  1435. """
  1436. Unencrypted private key.
  1437. :rtype: ``str``
  1438. :return: Unencrypted private key or ``None`` if not available.
  1439. """
  1440. pass
  1441. @abstractmethod
  1442. def delete(self):
  1443. """
  1444. Delete this key pair.
  1445. :rtype: ``bool``
  1446. :return: ``True`` if successful.
  1447. """
  1448. pass
  1449. class Region(CloudResource):
  1450. """
  1451. Represents a cloud region.
  1452. A cloud region is typically a separate geographic area and will contain at
  1453. least one placement zone.
  1454. """
  1455. __metaclass__ = ABCMeta
  1456. @abstractproperty
  1457. def zones(self):
  1458. """
  1459. Access information about placement zones within this region.
  1460. :rtype: Iterable
  1461. :return: Iterable of available placement zones in this region.
  1462. """
  1463. pass
  1464. class PlacementZone(CloudResource):
  1465. """
  1466. Represents a placement zone.
  1467. A placement zone is contained within a Region.
  1468. """
  1469. __metaclass__ = ABCMeta
  1470. @abstractproperty
  1471. def region_name(self):
  1472. """
  1473. A region this placement zone is associated with.
  1474. :rtype: ``str``
  1475. :return: The id of the region the zone is associated with.
  1476. """
  1477. pass
  1478. class VMType(CloudResource):
  1479. """
  1480. A VM type object.
  1481. """
  1482. __metaclass__ = ABCMeta
  1483. @abstractproperty
  1484. def family(self):
  1485. """
  1486. The family/group that this VM type belongs to.
  1487. For example, General Purpose Instances or High-Memory Instances. If
  1488. the provider does not support such a grouping, it may return ``None``.
  1489. :rtype: ``str``
  1490. :return: Name of the instance family or ``None``.
  1491. """
  1492. pass
  1493. @abstractproperty
  1494. def vcpus(self):
  1495. """
  1496. The number of VCPUs supported by this VM type.
  1497. :rtype: ``int``
  1498. :return: Number of VCPUs.
  1499. """
  1500. pass
  1501. @abstractproperty
  1502. def ram(self):
  1503. """
  1504. The amount of RAM (in GB) supported by this VM type.
  1505. :rtype: ``float``
  1506. :return: Total RAM (in GB).
  1507. """
  1508. pass
  1509. @abstractproperty
  1510. def size_root_disk(self):
  1511. """
  1512. The size of this VM types's root disk (in GB).
  1513. :rtype: ``int``
  1514. :return: Size of root disk (in GB).
  1515. """
  1516. pass
  1517. @abstractproperty
  1518. def size_ephemeral_disks(self):
  1519. """
  1520. The size of this VM types's total ephemeral storage (in GB).
  1521. :rtype: ``int``
  1522. :return: Size of ephemeral disks (in GB).
  1523. """
  1524. pass
  1525. @abstractproperty
  1526. def num_ephemeral_disks(self):
  1527. """
  1528. The total number of ephemeral disks on this VM type.
  1529. :rtype: ``int``
  1530. :return: Number of ephemeral disks available.
  1531. """
  1532. pass
  1533. @abstractproperty
  1534. def size_total_disk(self):
  1535. """
  1536. The total disk space available on this VM type
  1537. (root_disk + ephemeral).
  1538. :rtype: ``int``
  1539. :return: Size of total disk space (in GB).
  1540. """
  1541. pass
  1542. @abstractproperty
  1543. def extra_data(self):
  1544. """
  1545. A dictionary of extra data about this instance. May contain
  1546. nested dictionaries, but all key value pairs are strings or integers.
  1547. :rtype: ``dict``
  1548. :return: Extra attributes for this VM type.
  1549. """
  1550. pass
  1551. class VMFirewall(LabeledCloudResource):
  1552. """
  1553. Represents a firewall resource applied to virtual machines.
  1554. This is in contrast to a firewall for a network, for example.
  1555. """
  1556. __metaclass__ = ABCMeta
  1557. @LabeledCloudResource.label.setter
  1558. @abstractmethod
  1559. def label(self, value):
  1560. """
  1561. Set the resource label.
  1562. """
  1563. pass
  1564. @abstractproperty
  1565. def description(self):
  1566. """
  1567. Return the description of this VM firewall.
  1568. :rtype: ``str``
  1569. :return: A description of this VM firewall.
  1570. """
  1571. pass
  1572. @abstractproperty
  1573. def network_id(self):
  1574. """
  1575. Network ID with which this VM firewall is associated.
  1576. :rtype: ``str``
  1577. :return: Provider-supplied network ID or ``None`` is not available.
  1578. """
  1579. pass
  1580. @abstractproperty
  1581. def rules(self):
  1582. """
  1583. Get access to the rules belonging to this VM firewall.
  1584. This object can be used for further operations on rules, such as get,
  1585. list, create, etc.
  1586. :rtype: An object of :class:`.VMFirewallRuleSubService`
  1587. :return: A VMFirewallRuleSubService for further operations
  1588. """
  1589. pass
  1590. class TrafficDirection(Enum):
  1591. """
  1592. Direction of data flow in a firewall.
  1593. """
  1594. INBOUND = 'inbound'
  1595. OUTBOUND = 'outbound'
  1596. class VMFirewallRule(CloudResource):
  1597. """
  1598. Represents a VM firewall rule.
  1599. """
  1600. __metaclass__ = ABCMeta
  1601. @abstractproperty
  1602. def direction(self):
  1603. """
  1604. Direction of traffic to which this rule applies.
  1605. Either ``TrafficDirection.INBOUND`` or ``TrafficDirection.OUTBOUND``.
  1606. :rtype: ``str``
  1607. :return: Direction of traffic to which this rule applies.
  1608. """
  1609. pass
  1610. @abstractproperty
  1611. def protocol(self):
  1612. """
  1613. IP protocol used. Either ``tcp`` | ``udp`` | ``icmp``.
  1614. :rtype: ``str``
  1615. :return: Active protocol.
  1616. """
  1617. pass
  1618. @abstractproperty
  1619. def from_port(self):
  1620. """
  1621. Lowest port number opened as part of this rule.
  1622. :rtype: ``int``
  1623. :return: Lowest port number or 0 if not set.
  1624. """
  1625. pass
  1626. @abstractproperty
  1627. def to_port(self):
  1628. """
  1629. Highest port number opened as part of this rule.
  1630. :rtype: ``int``
  1631. :return: Highest port number or 0 if not set.
  1632. """
  1633. pass
  1634. @abstractproperty
  1635. def cidr(self):
  1636. """
  1637. CIDR block this VM firewall is providing access to.
  1638. :rtype: ``str``
  1639. :return: CIDR block.
  1640. """
  1641. pass
  1642. @abstractproperty
  1643. def src_dest_fw_id(self):
  1644. """
  1645. VM firewall id given access permissions by this rule.
  1646. :rtype: ``str``
  1647. :return: The VM firewall granted access.
  1648. """
  1649. pass
  1650. @abstractproperty
  1651. def src_dest_fw(self):
  1652. """
  1653. VM firewall given access permissions by this rule.
  1654. :rtype: :class:``.VMFirewall``
  1655. :return: The VM firewall granted access.
  1656. """
  1657. pass
  1658. @abstractmethod
  1659. def delete(self):
  1660. """
  1661. Delete this rule.
  1662. """
  1663. pass
  1664. class BucketObject(CloudResource):
  1665. """
  1666. Represents an object stored within a bucket.
  1667. """
  1668. __metaclass__ = ABCMeta
  1669. @abstractproperty
  1670. def name(self):
  1671. """
  1672. Retrieve the name of the current object.
  1673. The bucket object name adheres to a naming requirement that is more
  1674. relaxed than the naming requirement enforced across CloudBridge. More
  1675. details are available here: http://docs.aws.amazon.com/AmazonS3/latest/
  1676. dev/UsingMetadata.html#object-key-guidelines
  1677. :rtype: ``str``
  1678. :return: Name for this object as returned by the cloud middleware.
  1679. """
  1680. pass
  1681. @abstractproperty
  1682. def size(self):
  1683. """
  1684. Get this object's size.
  1685. :rtype: ``int``
  1686. :return: Size of this object in bytes.
  1687. """
  1688. pass
  1689. @abstractproperty
  1690. def last_modified(self):
  1691. """
  1692. Get the date and time this object was last modified.
  1693. :rtype: ``str``
  1694. :return: Date and time formatted string %Y-%m-%dT%H:%M:%S.%f
  1695. """
  1696. pass
  1697. @abstractmethod
  1698. def iter_content(self):
  1699. """
  1700. Returns this object's content as an iterable.
  1701. :rtype: Iterable
  1702. :return: An iterable of the file contents
  1703. """
  1704. pass
  1705. @abstractmethod
  1706. def save_content(self, target_stream):
  1707. """
  1708. Save this object and write its contents to the ``target_stream``.
  1709. """
  1710. pass
  1711. @abstractmethod
  1712. def upload(self, source_stream):
  1713. """
  1714. Set the contents of the object to the data read from the source stream.
  1715. :rtype: ``bool``
  1716. :return: ``True`` if successful.
  1717. """
  1718. pass
  1719. @abstractmethod
  1720. def upload_from_file(self, path):
  1721. """
  1722. Store the contents of the file pointed by the "path" variable.
  1723. :type path: ``str``
  1724. :param path: Absolute path to the file to be uploaded to S3.
  1725. """
  1726. pass
  1727. @abstractmethod
  1728. def delete(self):
  1729. """
  1730. Delete this object.
  1731. :rtype: ``bool``
  1732. :return: ``True`` if successful.
  1733. """
  1734. pass
  1735. @abstractmethod
  1736. def generate_url(self, expires_in, writable=False):
  1737. """
  1738. Generate a signed URL to this object.
  1739. A signed URL associated with an object gives time-limited read access
  1740. to that specific object. Anyone in possession of the URL has the access
  1741. granted by the URL.
  1742. :type expires_in: ``int``
  1743. :param expires_in: Time to live of the generated URL in seconds.
  1744. :type writable: ``bool``
  1745. :param writable: Write permission for this signed URL. Users with the URL
  1746. will be able to upload to this object, but they will NOT be able to
  1747. read from it.
  1748. :rtype: ``str``
  1749. :return: A URL to access the object.
  1750. """
  1751. pass
  1752. @abstractmethod
  1753. def refresh(self):
  1754. """
  1755. Refresh this object's state and synchronize it with the underlying
  1756. service provider.
  1757. """
  1758. pass
  1759. class Bucket(CloudResource):
  1760. """
  1761. Represents a namespace for objects (eg, object store bucket or container).
  1762. """
  1763. __metaclass__ = ABCMeta
  1764. @abstractproperty
  1765. def name(self):
  1766. """
  1767. Retrieve the name of the current bucket.
  1768. :rtype: ``str``
  1769. :return: Name for this instance as returned by the cloud middleware.
  1770. """
  1771. pass
  1772. @abstractproperty
  1773. def objects(self):
  1774. """
  1775. Get a container for the objects belonging to this Bucket.
  1776. This object can be used to iterate through bucket objects, as well as
  1777. perform further operations on buckets, such as ``get``, ``list``,
  1778. ``create``, etc.
  1779. .. code-block:: python
  1780. # Show all objects in bucket
  1781. print(list(bucket.objects))
  1782. # Find an object by name
  1783. print(bucket.objects.find(name='my_obj.txt'))
  1784. # Get first page of bucket list
  1785. print(bucket.objects.list())
  1786. # Create a new object within this bucket
  1787. obj = bucket.objects.create('my_obj.txt')
  1788. :rtype: :class:`.BucketContainer`
  1789. :return: A BucketContainer for further operations.
  1790. """
  1791. pass
  1792. @abstractmethod
  1793. def delete(self, delete_contents=False):
  1794. """
  1795. Delete this bucket.
  1796. :type delete_contents: ``bool``
  1797. :param delete_contents: If ``True``, all objects within the bucket
  1798. will be deleted.
  1799. :rtype: ``bool``
  1800. :return: ``True`` if successful.
  1801. """
  1802. pass