resources.py 58 KB

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