OpenStack Orchestration (Heat)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

resource.py 104KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633
  1. #
  2. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  3. # not use this file except in compliance with the License. You may obtain
  4. # a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  10. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  11. # License for the specific language governing permissions and limitations
  12. # under the License.
  13. import collections
  14. import contextlib
  15. import datetime as dt
  16. import itertools
  17. import pydoc
  18. import tenacity
  19. import weakref
  20. from oslo_config import cfg
  21. from oslo_log import log as logging
  22. from oslo_utils import excutils
  23. from oslo_utils import reflection
  24. import six
  25. from heat.common import exception
  26. from heat.common.i18n import _
  27. from heat.common import identifier
  28. from heat.common import short_id
  29. from heat.common import timeutils
  30. from heat.engine import attributes
  31. from heat.engine.cfn import template as cfn_tmpl
  32. from heat.engine import clients
  33. from heat.engine.clients import default_client_plugin
  34. from heat.engine import environment
  35. from heat.engine import event
  36. from heat.engine import function
  37. from heat.engine.hot import template as hot_tmpl
  38. from heat.engine import node_data
  39. from heat.engine import properties
  40. from heat.engine import resources
  41. from heat.engine import rsrc_defn
  42. from heat.engine import scheduler
  43. from heat.engine import status
  44. from heat.engine import support
  45. from heat.engine import sync_point
  46. from heat.engine import template
  47. from heat.objects import resource as resource_objects
  48. from heat.objects import resource_data as resource_data_objects
  49. from heat.objects import resource_properties_data as rpd_objects
  50. from heat.rpc import client as rpc_client
  51. cfg.CONF.import_opt('action_retry_limit', 'heat.common.config')
  52. cfg.CONF.import_opt('observe_on_update', 'heat.common.config')
  53. cfg.CONF.import_opt('error_wait_time', 'heat.common.config')
  54. LOG = logging.getLogger(__name__)
  55. datetime = dt.datetime
  56. def _register_class(resource_type, resource_class):
  57. resources.global_env().register_class(resource_type, resource_class)
  58. # Attention developers about to move/delete this: STOP IT!!!
  59. UpdateReplace = exception.UpdateReplace
  60. # Attention developers about to move this: STOP IT!!!
  61. class NoActionRequired(Exception):
  62. """Exception raised when a signal is ignored.
  63. Resource subclasses should raise this exception from handle_signal() to
  64. suppress recording of an event corresponding to the signal.
  65. """
  66. def __init__(self, res_name='Unknown', reason=''):
  67. msg = (_("The resource %(res)s could not perform "
  68. "scaling action: %(reason)s") %
  69. {'res': res_name, 'reason': reason})
  70. super(Exception, self).__init__(six.text_type(msg))
  71. class PollDelay(Exception):
  72. """Exception to delay polling of the resource.
  73. This exception may be raised by a Resource subclass's check_*_complete()
  74. methods to indicate that it need not be polled again immediately. If this
  75. exception is raised, the check_*_complete() method will not be called
  76. again until the nth time that the resource becomes eligible for polling.
  77. A PollDelay period of 1 is equivalent to returning False.
  78. """
  79. def __init__(self, period):
  80. assert period >= 1
  81. self.period = period
  82. @six.python_2_unicode_compatible
  83. class Resource(status.ResourceStatus):
  84. BASE_ATTRIBUTES = (SHOW, ) = (attributes.SHOW_ATTR, )
  85. LOCK_ACTIONS = (
  86. LOCK_NONE, LOCK_ACQUIRE, LOCK_RELEASE, LOCK_RESPECT,
  87. ) = (
  88. None, 1, -1, 0,
  89. )
  90. # If True, this resource must be created before it can be referenced.
  91. strict_dependency = True
  92. # Resource implementation set this to the subset of resource properties
  93. # supported for handle_update, used by update_template_diff_properties
  94. update_allowed_properties = ()
  95. # Resource implementations set this to the name: description dictionary
  96. # that describes the appropriate resource attributes
  97. attributes_schema = {}
  98. # Resource implementations set this to update policies
  99. update_policy_schema = {}
  100. # Default entity of resource, which is used for during resolving
  101. # show attribute
  102. entity = None
  103. # Description dictionary, that describes the common attributes for all
  104. # resources
  105. base_attributes_schema = {
  106. SHOW: attributes.Schema(
  107. _("Detailed information about resource."),
  108. cache_mode=attributes.Schema.CACHE_NONE,
  109. type=attributes.Schema.MAP
  110. )
  111. }
  112. # If True, this resource may perform authenticated API requests
  113. # throughout its lifecycle
  114. requires_deferred_auth = False
  115. # Limit to apply to physical_resource_name() size reduction algorithm.
  116. # If set to None no limit will be applied.
  117. physical_resource_name_limit = 255
  118. support_status = support.SupportStatus()
  119. # Default name to use for calls to self.client()
  120. default_client_name = None
  121. # Required service extension for this resource
  122. required_service_extension = None
  123. # no signal actions
  124. no_signal_actions = (status.ResourceStatus.SUSPEND,
  125. status.ResourceStatus.DELETE)
  126. # Whether all other resources need a metadata_update() after
  127. # a signal to this resource
  128. signal_needs_metadata_updates = True
  129. def __new__(cls, name, definition, stack):
  130. """Create a new Resource of the appropriate class for its type."""
  131. assert isinstance(definition, rsrc_defn.ResourceDefinition)
  132. if cls != Resource:
  133. # Call is already for a subclass, so pass it through
  134. ResourceClass = cls
  135. else:
  136. registry = stack.env.registry
  137. ResourceClass = registry.get_class_to_instantiate(
  138. definition.resource_type,
  139. resource_name=name)
  140. assert issubclass(ResourceClass, Resource)
  141. return super(Resource, cls).__new__(ResourceClass)
  142. @classmethod
  143. def _validate_service_availability(cls, context, resource_type):
  144. try:
  145. (svc_available, reason) = cls.is_service_available(context)
  146. except Exception as exc:
  147. LOG.exception("Resource type %s unavailable",
  148. resource_type)
  149. ex = exception.ResourceTypeUnavailable(
  150. resource_type=resource_type,
  151. service_name=cls.default_client_name,
  152. reason=six.text_type(exc))
  153. raise ex
  154. else:
  155. if not svc_available:
  156. ex = exception.ResourceTypeUnavailable(
  157. resource_type=resource_type,
  158. service_name=cls.default_client_name,
  159. reason=reason)
  160. LOG.info(six.text_type(ex))
  161. raise ex
  162. def __init__(self, name, definition, stack):
  163. def _validate_name(res_name):
  164. if '/' in res_name:
  165. message = _('Resource name may not contain "/"')
  166. raise exception.StackValidationFailed(message=message)
  167. _validate_name(name)
  168. self.stack = stack
  169. self.context = stack.context
  170. self.name = name
  171. self.t = definition
  172. self.reparse(client_resolve=False)
  173. self.update_policy = self.t.update_policy(self.update_policy_schema,
  174. self.context)
  175. self._update_allowed_properties = self.calc_update_allowed(
  176. self.properties)
  177. self.attributes_schema.update(self.base_attributes_schema)
  178. self.attributes = attributes.Attributes(self.name,
  179. self.attributes_schema,
  180. self._make_resolver(
  181. weakref.ref(self)))
  182. self.abandon_in_progress = False
  183. self.resource_id = None
  184. # if the stack is being deleted, assume we've already been deleted.
  185. # or if the resource has not been created yet, and the stack was
  186. # rollback, we set the resource to rollback
  187. if stack.action == stack.DELETE or stack.action == stack.ROLLBACK:
  188. self.action = stack.action
  189. else:
  190. self.action = self.INIT
  191. self.status = self.COMPLETE
  192. self.status_reason = ''
  193. self.id = None
  194. self.uuid = None
  195. self._data = None
  196. self._attr_data_id = None
  197. self._rsrc_metadata = None
  198. self._rsrc_prop_data_id = None
  199. self._stored_properties_data = None
  200. self.created_time = stack.created_time
  201. self.updated_time = stack.updated_time
  202. self._rpc_client = None
  203. self.requires = set()
  204. self.replaces = None
  205. self.replaced_by = None
  206. self.current_template_id = None
  207. self.old_template_id = None
  208. self.root_stack_id = None
  209. self._calling_engine_id = None
  210. self._atomic_key = None
  211. self.converge = False
  212. if not self.stack.in_convergence_check:
  213. resource = stack.db_resource_get(name)
  214. if resource:
  215. self._load_data(resource)
  216. else:
  217. proxy = self.stack.defn[self.name]
  218. node_data = proxy._resource_data
  219. if node_data is not None:
  220. self.action, self.status = proxy.state
  221. self.id = node_data.primary_key
  222. self.uuid = node_data.uuid
  223. def rpc_client(self):
  224. """Return a client for making engine RPC calls."""
  225. if not self._rpc_client:
  226. self._rpc_client = rpc_client.EngineClient()
  227. return self._rpc_client
  228. def _load_data(self, resource):
  229. """Load the resource state from its DB representation."""
  230. self.resource_id = resource.physical_resource_id
  231. self.action = resource.action
  232. self.status = resource.status
  233. self.status_reason = resource.status_reason
  234. self.id = resource.id
  235. self.uuid = resource.uuid
  236. try:
  237. self._data = resource_data_objects.ResourceData.get_all(
  238. self, resource.data)
  239. except exception.NotFound:
  240. self._data = {}
  241. self.attributes.cached_attrs = resource.attr_data or None
  242. self._attr_data_id = resource.attr_data_id
  243. self._rsrc_metadata = resource.rsrc_metadata
  244. self._stored_properties_data = resource.properties_data
  245. self._rsrc_prop_data_id = resource.rsrc_prop_data_id
  246. self.created_time = resource.created_at
  247. self.updated_time = resource.updated_at
  248. self.requires = set(resource.requires)
  249. self.replaces = resource.replaces
  250. self.replaced_by = resource.replaced_by
  251. self.current_template_id = resource.current_template_id
  252. self.root_stack_id = resource.root_stack_id
  253. self._atomic_key = resource.atomic_key
  254. @property
  255. def external_id(self):
  256. return self.t.external_id()
  257. @classmethod
  258. def getdoc(cls):
  259. if cls.__doc__ is None:
  260. return _('No description available')
  261. return pydoc.getdoc(cls)
  262. @property
  263. def stack(self):
  264. stack = self._stackref()
  265. assert stack is not None, "Need a reference to the Stack object"
  266. return stack
  267. @stack.setter
  268. def stack(self, stack):
  269. self._stackref = weakref.ref(stack)
  270. @classmethod
  271. def load(cls, context, resource_id, current_traversal, is_update, data):
  272. """Load a specified resource from the database to check.
  273. Returns a tuple of the Resource, the StackDefinition corresponding to
  274. the resource's ResourceDefinition (i.e. the one the resource was last
  275. updated to if it has already been created, or the one it will be
  276. created with if it hasn't been already), and the Stack containing the
  277. latest StackDefinition (i.e. the one that the latest traversal is
  278. updating to.
  279. The latter two must remain in-scope, because the Resource holds weak
  280. references to them.
  281. """
  282. from heat.engine import stack as stack_mod
  283. db_res = resource_objects.Resource.get_obj(context, resource_id)
  284. curr_stack = stack_mod.Stack.load(context, stack_id=db_res.stack_id,
  285. cache_data=data)
  286. initial_stk_defn = latest_stk_defn = curr_stack.defn
  287. current_template_id = db_res.current_template_id
  288. using_new_template = (current_template_id != curr_stack.t.id and
  289. current_template_id is not None)
  290. will_create = (db_res.action == cls.INIT and
  291. is_update and
  292. current_traversal == curr_stack.current_traversal)
  293. if using_new_template and not will_create:
  294. # load the definition associated with the resource's template
  295. current_template = template.Template.load(context,
  296. current_template_id)
  297. initial_stk_defn = curr_stack.defn.clone_with_new_template(
  298. current_template,
  299. curr_stack.identifier())
  300. curr_stack.defn = initial_stk_defn
  301. res_defn = initial_stk_defn.resource_definition(db_res.name)
  302. res_type = initial_stk_defn.env.registry.get_class_to_instantiate(
  303. res_defn.resource_type, resource_name=db_res.name)
  304. # If the resource type has changed and the new one is a valid
  305. # substitution, use that as the class to instantiate.
  306. if is_update and (latest_stk_defn is not initial_stk_defn):
  307. try:
  308. new_res_defn = latest_stk_defn.resource_definition(db_res.name)
  309. except KeyError:
  310. pass
  311. else:
  312. new_registry = latest_stk_defn.env.registry
  313. new_res_type = new_registry.get_class_to_instantiate(
  314. new_res_defn.resource_type, resource_name=db_res.name)
  315. if res_type.check_is_substituted(new_res_type):
  316. res_type = new_res_type
  317. # Load only the resource in question; don't load all resources
  318. # by invoking stack.resources. Maintain light-weight stack.
  319. resource = res_type(db_res.name, res_defn, curr_stack)
  320. resource._load_data(db_res)
  321. curr_stack.defn = latest_stk_defn
  322. return resource, initial_stk_defn, curr_stack
  323. def make_replacement(self, new_tmpl_id, requires):
  324. """Create a replacement resource in the database.
  325. Returns the DB ID of the new resource, or None if the new resource
  326. cannot be created (generally because the template ID does not exist).
  327. Raises UpdateInProgress if another traversal has already locked the
  328. current resource.
  329. """
  330. # 1. create the replacement with "replaces" = self.id
  331. # Don't set physical_resource_id so that a create is triggered.
  332. rs = {'stack_id': self.stack.id,
  333. 'name': self.name,
  334. 'rsrc_prop_data_id': None,
  335. 'needed_by': [],
  336. 'requires': sorted(requires, reverse=True),
  337. 'replaces': self.id,
  338. 'action': self.INIT,
  339. 'status': self.COMPLETE,
  340. 'current_template_id': new_tmpl_id,
  341. 'stack_name': self.stack.name,
  342. 'root_stack_id': self.root_stack_id}
  343. update_data = {'status': self.COMPLETE}
  344. # Retry in case a signal has updated the atomic_key
  345. attempts = max(cfg.CONF.client_retry_limit, 0) + 1
  346. def prepare_attempt(fn, attempt):
  347. if attempt > 1:
  348. res_obj = resource_objects.Resource.get_obj(
  349. self.context, self.id)
  350. if (res_obj.engine_id is not None or
  351. res_obj.updated_at != self.updated_time):
  352. raise exception.UpdateInProgress(resource_name=self.name)
  353. self._atomic_key = res_obj.atomic_key
  354. @tenacity.retry(
  355. stop=tenacity.stop_after_attempt(attempts),
  356. retry=tenacity.retry_if_exception_type(
  357. exception.UpdateInProgress),
  358. before=prepare_attempt,
  359. wait=tenacity.wait_random(max=2),
  360. reraise=True)
  361. def create_replacement():
  362. return resource_objects.Resource.replacement(self.context,
  363. self.id,
  364. update_data,
  365. rs,
  366. self._atomic_key)
  367. new_rs = create_replacement()
  368. if new_rs is None:
  369. return None
  370. self._incr_atomic_key(self._atomic_key)
  371. self.replaced_by = new_rs.id
  372. return new_rs.id
  373. def reparse(self, client_resolve=True):
  374. """Reparse the resource properties.
  375. Optional translate flag for property translation and
  376. client_resolve flag for resolving properties by doing
  377. client lookup.
  378. """
  379. self.properties = self.t.properties(self.properties_schema,
  380. self.context)
  381. self.translate_properties(self.properties, client_resolve)
  382. def calc_update_allowed(self, props):
  383. update_allowed_set = set(self.update_allowed_properties)
  384. for (psk, psv) in six.iteritems(props.props):
  385. if psv.update_allowed():
  386. update_allowed_set.add(psk)
  387. return update_allowed_set
  388. def __eq__(self, other):
  389. """Allow == comparison of two resources."""
  390. # For the purposes of comparison, we declare two resource objects
  391. # equal if their names and resolved templates are the same
  392. if isinstance(other, Resource):
  393. return ((self.name == other.name) and
  394. (self.t.freeze() == other.t.freeze()))
  395. return NotImplemented
  396. def __ne__(self, other):
  397. """Allow != comparison of two resources."""
  398. result = self.__eq__(other)
  399. if result is NotImplemented:
  400. return result
  401. return not result
  402. def __hash__(self):
  403. return id(self)
  404. def metadata_get(self, refresh=False):
  405. if refresh:
  406. self._rsrc_metadata = None
  407. if self.id is None or self.action == self.INIT:
  408. return self.t.metadata()
  409. if self._rsrc_metadata is not None:
  410. return self._rsrc_metadata
  411. rs = resource_objects.Resource.get_obj(self.stack.context, self.id,
  412. refresh=True,
  413. fields=('rsrc_metadata', ))
  414. self._rsrc_metadata = rs.rsrc_metadata
  415. return rs.rsrc_metadata
  416. @resource_objects.retry_on_conflict
  417. def metadata_set(self, metadata, merge_metadata=None):
  418. """Write new metadata to the database.
  419. The caller may optionally provide a merge_metadata() function, which
  420. takes two arguments - the metadata passed to metadata_set() and the
  421. current metadata of the resource - and returns the merged metadata to
  422. write. If merge_metadata is not provided, the metadata passed to
  423. metadata_set() is written verbatim, overwriting any existing metadata.
  424. If a race condition is detected, the write will be retried with the new
  425. result of merge_metadata() (if it is supplied) or the verbatim data (if
  426. it is not).
  427. """
  428. if self.id is None or self.action == self.INIT:
  429. raise exception.ResourceNotAvailable(resource_name=self.name)
  430. refresh = merge_metadata is not None
  431. db_res = resource_objects.Resource.get_obj(
  432. self.stack.context, self.id, refresh=refresh,
  433. fields=('name', 'rsrc_metadata', 'atomic_key', 'engine_id',
  434. 'action', 'status'))
  435. if db_res.action == self.DELETE:
  436. self._db_res_is_deleted = True
  437. LOG.debug("resource %(name)s, id: %(id)s is DELETE_%(st)s, "
  438. "not setting metadata",
  439. {'name': self.name, 'id': self.id, 'st': db_res.status})
  440. raise exception.ResourceNotAvailable(resource_name=self.name)
  441. LOG.debug('Setting metadata for %s', six.text_type(self))
  442. if refresh:
  443. metadata = merge_metadata(metadata, db_res.rsrc_metadata)
  444. if db_res.update_metadata(metadata):
  445. self._incr_atomic_key(db_res.atomic_key)
  446. self._rsrc_metadata = metadata
  447. def handle_metadata_reset(self):
  448. """Default implementation; should be overridden by resources.
  449. Now we override this method to reset the metadata for scale-policy
  450. and scale-group resources, because their metadata might hang in a
  451. wrong state ('scaling_in_progress' is always True) if engine restarts
  452. while scaling.
  453. """
  454. pass
  455. def _break_if_required(self, action, hook):
  456. """Block the resource until the hook is cleared if there is one."""
  457. if self.stack.env.registry.matches_hook(self.name, hook):
  458. self.trigger_hook(hook)
  459. self._add_event(self.action, self.status,
  460. "%(a)s paused until Hook %(h)s is cleared"
  461. % {'a': action, 'h': hook})
  462. LOG.info('Reached hook on %s', self)
  463. while self.has_hook(hook):
  464. try:
  465. yield
  466. except BaseException as exc:
  467. self.clear_hook(hook)
  468. self._add_event(
  469. self.action, self.status,
  470. "Failure occurred while waiting.")
  471. if (isinstance(exc, AssertionError) or
  472. not isinstance(exc, Exception)):
  473. raise
  474. def has_nested(self):
  475. """Return True if the resource has an existing nested stack.
  476. For most resource types, this will always return False. StackResource
  477. subclasses return True when appropriate. Resource subclasses that may
  478. return True must also provide a nested_identifier() method to return
  479. the identifier of the nested stack, and a nested() method to return a
  480. Stack object for the nested stack.
  481. """
  482. return False
  483. def get_nested_parameters_stack(self):
  484. """Return the nested stack for schema validation.
  485. Regular resources don't have such a thing.
  486. """
  487. return
  488. def has_hook(self, hook):
  489. # Clear the cache to make sure the data is up to date:
  490. self._data = None
  491. return self.data().get(hook) == "True"
  492. def trigger_hook(self, hook):
  493. self.data_set(hook, "True")
  494. def clear_hook(self, hook):
  495. self.data_delete(hook)
  496. def type(self):
  497. return self.t.resource_type
  498. def has_interface(self, resource_type):
  499. """Check if resource is mapped to resource_type or is "resource_type".
  500. Check to see if this resource is either mapped to resource_type
  501. or is a "resource_type".
  502. """
  503. if self.type() == resource_type:
  504. return True
  505. try:
  506. ri = self.stack.env.get_resource_info(self.type(),
  507. self.name)
  508. except exception.EntityNotFound:
  509. return False
  510. else:
  511. return ri.name == resource_type
  512. def identifier(self):
  513. """Return an identifier for this resource."""
  514. return identifier.ResourceIdentifier(resource_name=self.name,
  515. **self.stack.identifier())
  516. def frozen_definition(self):
  517. """Return a frozen ResourceDefinition with stored property values.
  518. The returned definition will contain the property values read from the
  519. database, and will have all intrinsic functions resolved (note that
  520. this makes it useless for calculating dependencies).
  521. """
  522. if self._stored_properties_data is not None:
  523. args = {'properties': self._stored_properties_data}
  524. else:
  525. args = {}
  526. return self.t.freeze(**args)
  527. @contextlib.contextmanager
  528. def frozen_properties(self):
  529. """Context manager to use the frozen property values from the database.
  530. The live property values are always substituted back when the context
  531. ends.
  532. """
  533. live_props = self.properties
  534. props = self.frozen_definition().properties(self.properties_schema,
  535. self.context)
  536. try:
  537. self.properties = props
  538. yield props
  539. finally:
  540. self.properties = live_props
  541. def update_template_diff(self, after, before):
  542. """Returns the difference between the before and after json snippets.
  543. If something has been removed in after which exists in before we set it
  544. to None.
  545. """
  546. return after - before
  547. def update_template_diff_properties(self, after_props, before_props):
  548. """Return changed Properties between the before and after properties.
  549. If any property having immutable as True is updated, raises
  550. NotSupported error.
  551. If any properties have changed which are not in
  552. update_allowed_properties, raises UpdateReplace.
  553. """
  554. update_allowed_set = self.calc_update_allowed(after_props)
  555. immutable_set = set()
  556. for (psk, psv) in six.iteritems(after_props.props):
  557. if psv.immutable():
  558. immutable_set.add(psk)
  559. def prop_changed(key):
  560. try:
  561. before = before_props.get(key)
  562. except (TypeError, ValueError) as exc:
  563. # We shouldn't get here usually, but there is a known issue
  564. # with template resources and new parameters in non-convergence
  565. # stacks (see bug 1543685). The error should be harmless
  566. # because we're on the before properties, which have presumably
  567. # already been validated.
  568. LOG.warning('Ignoring error in old property value '
  569. '%(prop_name)s: %(msg)s',
  570. {'prop_name': key, 'msg': six.text_type(exc)})
  571. return True
  572. return before != after_props.get(key)
  573. # Create a set of keys which differ (or are missing/added)
  574. changed_properties_set = set(k for k in after_props if prop_changed(k))
  575. # Create a list of updated properties offending property immutability
  576. update_replace_forbidden = [k for k in changed_properties_set
  577. if k in immutable_set]
  578. if update_replace_forbidden:
  579. msg = _("Update to properties %(props)s of %(name)s (%(res)s)"
  580. ) % {'props': ", ".join(sorted(update_replace_forbidden)),
  581. 'res': self.type(), 'name': self.name}
  582. raise exception.NotSupported(feature=msg)
  583. if changed_properties_set and self.needs_replace_with_prop_diff(
  584. changed_properties_set,
  585. after_props,
  586. before_props):
  587. raise UpdateReplace(self)
  588. if not changed_properties_set.issubset(update_allowed_set):
  589. raise UpdateReplace(self.name)
  590. return dict((k, after_props.get(k)) for k in changed_properties_set)
  591. def __str__(self):
  592. class_name = reflection.get_class_name(self, fully_qualified=False)
  593. if self.stack.id is not None:
  594. if self.resource_id is not None:
  595. text = '%s "%s" [%s] %s' % (class_name, self.name,
  596. self.resource_id,
  597. six.text_type(self.stack))
  598. else:
  599. text = '%s "%s" %s' % (class_name, self.name,
  600. six.text_type(self.stack))
  601. else:
  602. text = '%s "%s"' % (class_name, self.name)
  603. return six.text_type(text)
  604. def add_explicit_dependencies(self, deps):
  605. """Add all dependencies explicitly specified in the template.
  606. The deps parameter is a Dependencies object to which dependency pairs
  607. are added.
  608. """
  609. for dep in self.t.dependencies(self.stack):
  610. deps += (self, dep)
  611. deps += (self, None)
  612. def add_dependencies(self, deps):
  613. """Add implicit dependencies specific to the resource type.
  614. Some resource types may have implicit dependencies on other resources
  615. in the same stack that are not linked by a property value (that would
  616. be set using get_resource or get_attr for example, thus creating an
  617. explicit dependency). Such dependencies are opaque to the user and
  618. should be avoided wherever possible, however in some circumstances they
  619. are required due to magic in the underlying API.
  620. The deps parameter is a Dependencies object to which dependency pairs
  621. may be added.
  622. """
  623. return
  624. def required_by(self):
  625. """List of resources that require this one as a dependency.
  626. Returns a list of names of resources that depend on this resource
  627. directly.
  628. """
  629. try:
  630. reqd_by = self.stack.dependencies.required_by(self)
  631. except KeyError:
  632. if self.stack.convergence:
  633. # for convergence, fall back to building from node graph
  634. needed_by_ids = self.stack.dependent_resource_ids(self.id)
  635. reqd_by = [r for r in self.stack.resources.values()
  636. if r.id in needed_by_ids]
  637. else:
  638. LOG.error('Getting required_by list for Resource not in '
  639. 'dependency graph.')
  640. return []
  641. return [r.name for r in reqd_by]
  642. def client(self, name=None, version=None):
  643. client_name = name or self.default_client_name
  644. assert client_name, "Must specify client name"
  645. return self.stack.clients.client(client_name, version)
  646. def client_plugin(self, name=None):
  647. client_name = name or self.default_client_name
  648. assert client_name, "Must specify client name"
  649. return self.stack.clients.client_plugin(client_name)
  650. def _default_client_plugin(self):
  651. """Always return a client plugin.
  652. This will be the client_plugin if the resource has defined a
  653. default_client_name, or a no-op plugin if it does not. Thus, the
  654. result of this call always has e.g. is_not_found() and is_conflict()
  655. methods.
  656. """
  657. cp = None
  658. if self.default_client_name:
  659. cp = self.client_plugin()
  660. if cp is None:
  661. cp = default_client_plugin.DefaultClientPlugin(self.context)
  662. return cp
  663. @classmethod
  664. def is_service_available(cls, context):
  665. # NOTE(kanagaraj-manickam): return True to satisfy the cases like
  666. # resource does not have endpoint, such as RandomString, OS::Heat
  667. # resources as they are implemented within the engine.
  668. if cls.default_client_name is None:
  669. return (True, None)
  670. client_plugin = clients.Clients(context).client_plugin(
  671. cls.default_client_name)
  672. if not client_plugin:
  673. raise exception.ClientNotAvailable(
  674. client_name=cls.default_client_name)
  675. service_types = client_plugin.service_types
  676. if not service_types:
  677. return (True, None)
  678. # NOTE(kanagaraj-manickam): if one of the service_type does
  679. # exist in the keystone, then considered it as available.
  680. for service_type in service_types:
  681. endpoint_exists = client_plugin.does_endpoint_exist(
  682. service_type=service_type,
  683. service_name=cls.default_client_name)
  684. if endpoint_exists:
  685. req_extension = cls.required_service_extension
  686. is_ext_available = (
  687. not req_extension or client_plugin.has_extension(
  688. req_extension))
  689. if is_ext_available:
  690. return (True, None)
  691. else:
  692. reason = _('Required extension {0} in {1} service '
  693. 'is not available.')
  694. reason = reason.format(req_extension,
  695. cls.default_client_name)
  696. else:
  697. reason = _('{0} {1} endpoint is not in service catalog.')
  698. reason = reason.format(cls.default_client_name, service_type)
  699. return (False, reason)
  700. def keystone(self):
  701. return self.client('keystone')
  702. def nova(self):
  703. return self.client('nova')
  704. def swift(self):
  705. return self.client('swift')
  706. def neutron(self):
  707. return self.client('neutron')
  708. def cinder(self):
  709. return self.client('cinder')
  710. def trove(self):
  711. return self.client('trove')
  712. def heat(self):
  713. return self.client('heat')
  714. def glance(self):
  715. return self.client('glance')
  716. def _incr_atomic_key(self, last_key):
  717. if last_key is None:
  718. self._atomic_key = 1
  719. else:
  720. self._atomic_key = last_key + 1
  721. def _should_lock_on_action(self, action):
  722. """Return whether we should take a resource-level lock for an action.
  723. In the legacy path, we always took a lock at the Stack level and never
  724. at the Resource level. In convergence, we lock at the Resource level
  725. for most operations. However, there are currently some exceptions:
  726. the SUSPEND, RESUME, SNAPSHOT, and CHECK actions, and stack abandon.
  727. """
  728. return (self.stack.convergence and
  729. not self.abandon_in_progress and
  730. action in {self.ADOPT,
  731. self.CREATE,
  732. self.UPDATE,
  733. self.ROLLBACK,
  734. self.DELETE})
  735. @contextlib.contextmanager
  736. def _action_recorder(self, action, expected_exceptions=tuple()):
  737. """Return a context manager to record the progress of an action.
  738. Upon entering the context manager, the state is set to IN_PROGRESS.
  739. Upon exiting, the state will be set to COMPLETE if no exception was
  740. raised, or FAILED otherwise. Non-exit exceptions will be translated
  741. to ResourceFailure exceptions.
  742. Expected exceptions are re-raised, with the Resource moved to the
  743. COMPLETE state.
  744. """
  745. attempts = 1
  746. first_iter = [True] # work around no nonlocal in py27
  747. if self.stack.convergence:
  748. if self._should_lock_on_action(action):
  749. lock_acquire = self.LOCK_ACQUIRE
  750. lock_release = self.LOCK_RELEASE
  751. else:
  752. lock_acquire = lock_release = self.LOCK_RESPECT
  753. if action != self.CREATE:
  754. attempts += max(cfg.CONF.client_retry_limit, 0)
  755. else:
  756. lock_acquire = lock_release = self.LOCK_NONE
  757. # retry for convergence DELETE or UPDATE if we get the usual
  758. # lock-acquire exception of exception.UpdateInProgress
  759. @tenacity.retry(
  760. stop=tenacity.stop_after_attempt(attempts),
  761. retry=tenacity.retry_if_exception_type(
  762. exception.UpdateInProgress),
  763. wait=tenacity.wait_random(max=2),
  764. reraise=True)
  765. def set_in_progress():
  766. if not first_iter[0]:
  767. res_obj = resource_objects.Resource.get_obj(
  768. self.context, self.id)
  769. self._atomic_key = res_obj.atomic_key
  770. else:
  771. first_iter[0] = False
  772. self.state_set(action, self.IN_PROGRESS, lock=lock_acquire)
  773. try:
  774. set_in_progress()
  775. yield
  776. except exception.UpdateInProgress as ex:
  777. with excutils.save_and_reraise_exception():
  778. LOG.info('Update in progress for %s', self.name)
  779. except expected_exceptions as ex:
  780. with excutils.save_and_reraise_exception():
  781. self.state_set(action, self.COMPLETE, six.text_type(ex),
  782. lock=lock_release)
  783. LOG.debug('%s', six.text_type(ex))
  784. except Exception as ex:
  785. LOG.info('%(action)s: %(info)s',
  786. {"action": action,
  787. "info": six.text_type(self)},
  788. exc_info=True)
  789. failure = exception.ResourceFailure(ex, self, action)
  790. self.state_set(action, self.FAILED, six.text_type(failure),
  791. lock=lock_release)
  792. raise failure
  793. except BaseException as exc:
  794. with excutils.save_and_reraise_exception():
  795. try:
  796. reason = six.text_type(exc)
  797. msg = '%s aborted' % action
  798. if reason:
  799. msg += ' (%s)' % reason
  800. self.state_set(action, self.FAILED, msg,
  801. lock=lock_release)
  802. except Exception:
  803. LOG.exception('Error marking resource as failed')
  804. else:
  805. self.state_set(action, self.COMPLETE, lock=lock_release)
  806. def action_handler_task(self, action, args=None, action_prefix=None):
  807. """A task to call the Resource subclass's handler methods for action.
  808. Calls the handle_<ACTION>() method for the given action and then calls
  809. the check_<ACTION>_complete() method with the result in a loop until it
  810. returns True. If the methods are not provided, the call is omitted.
  811. Any args provided are passed to the handler.
  812. If a prefix is supplied, the handler method handle_<PREFIX>_<ACTION>()
  813. is called instead.
  814. """
  815. args = args or []
  816. handler_action = action.lower()
  817. check = getattr(self, 'check_%s_complete' % handler_action, None)
  818. if action_prefix:
  819. handler_action = '%s_%s' % (action_prefix.lower(), handler_action)
  820. handler = getattr(self, 'handle_%s' % handler_action, None)
  821. if callable(handler):
  822. try:
  823. handler_data = handler(*args)
  824. except StopIteration:
  825. raise RuntimeError('Plugin method raised StopIteration')
  826. yield
  827. if callable(check):
  828. try:
  829. while True:
  830. try:
  831. done = check(handler_data)
  832. except PollDelay as delay:
  833. yield delay.period
  834. else:
  835. if done:
  836. break
  837. else:
  838. yield
  839. except StopIteration:
  840. raise RuntimeError('Plugin method raised StopIteration')
  841. except Exception:
  842. raise
  843. except: # noqa
  844. with excutils.save_and_reraise_exception():
  845. canceller = getattr(
  846. self,
  847. 'handle_%s_cancel' % handler_action,
  848. None
  849. )
  850. if callable(canceller):
  851. try:
  852. canceller(handler_data)
  853. except Exception:
  854. LOG.exception(
  855. 'Error cancelling resource %s',
  856. action
  857. )
  858. @scheduler.wrappertask
  859. def _do_action(self, action, pre_func=None, resource_data=None):
  860. """Perform a transition to a new state via a specified action.
  861. Action should be e.g self.CREATE, self.UPDATE etc, we set
  862. status based on this, the transition is handled by calling the
  863. corresponding handle_* and check_*_complete functions
  864. Note pre_func is an optional function reference which will
  865. be called before the handle_<action> function
  866. If the resource does not declare a check_$action_complete function,
  867. we declare COMPLETE status as soon as the handle_$action call has
  868. finished, and if no handle_$action function is declared, then we do
  869. nothing, useful e.g if the resource requires no action for a given
  870. state transition
  871. """
  872. assert action in self.ACTIONS, 'Invalid action %s' % action
  873. with self._action_recorder(action):
  874. if callable(pre_func):
  875. pre_func()
  876. handler_args = [resource_data] if resource_data is not None else []
  877. yield self.action_handler_task(action, args=handler_args)
  878. def _update_stored_properties(self):
  879. old_props = self._stored_properties_data
  880. self._stored_properties_data = function.resolve(self.properties.data)
  881. if self._stored_properties_data != old_props:
  882. self._rsrc_prop_data_id = None
  883. self.attributes.reset_resolved_values()
  884. def referenced_attrs(self, stk_defn=None,
  885. in_resources=True, in_outputs=True,
  886. load_all=False):
  887. """Return the set of all attributes referenced in the template.
  888. This enables the resource to calculate which of its attributes will
  889. be used. By default, attributes referenced in either other resources
  890. or outputs will be included. Either can be excluded by setting the
  891. `in_resources` or `in_outputs` parameters to False. To limit to a
  892. subset of outputs, pass an iterable of the output names to examine
  893. for the `in_outputs` parameter.
  894. The set of referenced attributes is calculated from the
  895. StackDefinition object provided, or from the stack's current
  896. definition if none is passed.
  897. """
  898. if stk_defn is None:
  899. stk_defn = self.stack.defn
  900. def get_dep_attrs(source):
  901. return set(itertools.chain.from_iterable(s.dep_attrs(self.name,
  902. load_all)
  903. for s in source))
  904. refd_attrs = set()
  905. if in_resources:
  906. enabled_resources = stk_defn.enabled_rsrc_names()
  907. refd_attrs |= get_dep_attrs(stk_defn.resource_definition(r_name)
  908. for r_name in enabled_resources)
  909. subset_outputs = isinstance(in_outputs, collections.Iterable)
  910. if subset_outputs or in_outputs:
  911. if not subset_outputs:
  912. in_outputs = stk_defn.enabled_output_names()
  913. refd_attrs |= get_dep_attrs(stk_defn.output_definition(op_name)
  914. for op_name in in_outputs)
  915. if attributes.ALL_ATTRIBUTES in refd_attrs:
  916. refd_attrs.remove(attributes.ALL_ATTRIBUTES)
  917. refd_attrs |= (set(self.attributes) - {self.SHOW})
  918. return refd_attrs
  919. def node_data(self, stk_defn=None, for_resources=True, for_outputs=False):
  920. """Return a NodeData object representing the resource.
  921. The NodeData object returned contains basic data about the resource,
  922. including its name, ID and state, as well as its reference ID and any
  923. attribute values that are used.
  924. By default, those attribute values that are referenced by other
  925. resources are included. These can be ignored by setting the
  926. for_resources parameter to False. If the for_outputs parameter is
  927. True, those attribute values that are referenced by stack outputs are
  928. included. If the for_outputs parameter is an iterable of output names,
  929. only those attribute values referenced by the specified stack outputs
  930. are included.
  931. The set of referenced attributes is calculated from the
  932. StackDefinition object provided, or from the stack's current
  933. definition if none is passed.
  934. After calling this method, the resource's attribute cache is
  935. populated with any cacheable attribute values referenced by stack
  936. outputs, even if they are not also referenced by other resources.
  937. """
  938. def get_attrs(attrs, cacheable_only=False):
  939. for attr in attrs:
  940. path = (attr,) if isinstance(attr, six.string_types) else attr
  941. if (cacheable_only and
  942. (self.attributes.get_cache_mode(path[0]) ==
  943. attributes.Schema.CACHE_NONE)):
  944. continue
  945. if self.action == self.INIT:
  946. if (path[0] in self.attributes or
  947. (type(self).get_attribute != Resource.get_attribute or
  948. type(self).FnGetAtt != Resource.FnGetAtt)):
  949. # TODO(ricolin) make better placeholder values here
  950. yield attr, None
  951. else:
  952. try:
  953. yield attr, self.FnGetAtt(*path)
  954. except exception.InvalidTemplateAttribute as ita:
  955. # Attribute doesn't exist, so don't store it. Whatever
  956. # tries to access it will get another
  957. # InvalidTemplateAttribute exception at that point
  958. LOG.info('%s', ita)
  959. except Exception as exc:
  960. # Store the exception that occurred. It will be
  961. # re-raised when something tries to access it, or when
  962. # we try to serialise the NodeData.
  963. yield attr, exc
  964. load_all = not self.stack.in_convergence_check
  965. dep_attrs = self.referenced_attrs(stk_defn,
  966. in_resources=for_resources,
  967. in_outputs=for_outputs,
  968. load_all=load_all)
  969. # Ensure all attributes referenced in outputs get cached
  970. if for_outputs is False and self.stack.convergence:
  971. out_attrs = self.referenced_attrs(stk_defn, in_resources=False,
  972. load_all=load_all)
  973. for e in get_attrs(out_attrs - dep_attrs, cacheable_only=True):
  974. pass
  975. # Calculate attribute values *before* reference ID, to potentially
  976. # save an extra RPC call in TemplateResource
  977. attribute_values = dict(get_attrs(dep_attrs))
  978. return node_data.NodeData(self.id, self.name, self.uuid,
  979. self.FnGetRefId(), attribute_values,
  980. self.action, self.status)
  981. def preview(self):
  982. """Default implementation of Resource.preview.
  983. This method should be overridden by child classes for specific
  984. behavior.
  985. """
  986. return self
  987. def create_convergence(self, template_id, requires, engine_id,
  988. timeout, progress_callback=None):
  989. """Creates the resource by invoking the scheduler TaskRunner."""
  990. self._calling_engine_id = engine_id
  991. self.requires = requires
  992. self.current_template_id = template_id
  993. if self.stack.adopt_stack_data is None:
  994. runner = scheduler.TaskRunner(self.create)
  995. else:
  996. adopt_data = self.stack._adopt_kwargs(self)
  997. runner = scheduler.TaskRunner(self.adopt, **adopt_data)
  998. runner(timeout=timeout, progress_callback=progress_callback)
  999. def validate_external(self):
  1000. if self.external_id is not None:
  1001. try:
  1002. self.resource_id = self.external_id
  1003. self._show_resource()
  1004. except Exception as ex:
  1005. if self._default_client_plugin().is_not_found(ex):
  1006. error_message = (_("Invalid external resource: Resource "
  1007. "%(external_id)s (%(type)s) can not "
  1008. "be found.") %
  1009. {'external_id': self.external_id,
  1010. 'type': self.type()})
  1011. raise exception.StackValidationFailed(
  1012. message="%s" % error_message)
  1013. raise
  1014. @scheduler.wrappertask
  1015. def create(self):
  1016. """Create the resource.
  1017. Subclasses should provide a handle_create() method to customise
  1018. creation.
  1019. """
  1020. action = self.CREATE
  1021. if (self.action, self.status) != (self.INIT, self.COMPLETE):
  1022. exc = exception.Error(_('State %s invalid for create')
  1023. % six.text_type(self.state))
  1024. raise exception.ResourceFailure(exc, self, action)
  1025. if self.external_id is not None:
  1026. yield self._do_action(self.ADOPT,
  1027. resource_data={
  1028. 'resource_id': self.external_id})
  1029. self.check()
  1030. return
  1031. # This method can be called when we replace a resource, too. In that
  1032. # case, a hook has already been dealt with in `Resource.update` so we
  1033. # shouldn't do it here again:
  1034. if self.stack.action == self.stack.CREATE:
  1035. yield self._break_if_required(
  1036. self.CREATE, environment.HOOK_PRE_CREATE)
  1037. LOG.info('creating %s', self)
  1038. # Re-resolve the template, since if the resource Ref's
  1039. # the StackId pseudo parameter, it will change after
  1040. # the parser.Stack is stored (which is after the resources
  1041. # are __init__'d, but before they are create()'d). We also
  1042. # do client lookups for RESOLVE translation rules here.
  1043. self.reparse()
  1044. self._update_stored_properties()
  1045. count = {self.CREATE: 0, self.DELETE: 0}
  1046. retry_limit = max(cfg.CONF.action_retry_limit, 0)
  1047. first_failure = None
  1048. while (count[self.CREATE] <= retry_limit and
  1049. count[self.DELETE] <= retry_limit):
  1050. pre_func = None
  1051. if count[action] > 0:
  1052. delay = timeutils.retry_backoff_delay(count[action],
  1053. jitter_max=2.0)
  1054. waiter = scheduler.TaskRunner(self.pause)
  1055. yield waiter.as_task(timeout=delay)
  1056. elif action == self.CREATE:
  1057. # Only validate properties in first create call.
  1058. pre_func = self.properties.validate
  1059. try:
  1060. yield self._do_action(action, pre_func)
  1061. if action == self.CREATE:
  1062. first_failure = None
  1063. break
  1064. else:
  1065. action = self.CREATE
  1066. except exception.ResourceFailure as failure:
  1067. exc = failure.exc
  1068. if isinstance(exc, exception.StackValidationFailed):
  1069. path = [self.t.name]
  1070. path.extend(exc.path)
  1071. raise exception.ResourceFailure(
  1072. exception_or_error=exception.StackValidationFailed(
  1073. error=exc.error,
  1074. path=path,
  1075. message=exc.error_message
  1076. ),
  1077. resource=failure.resource,
  1078. action=failure.action
  1079. )
  1080. if not (isinstance(exc, exception.ResourceInError) or
  1081. self._default_client_plugin().is_conflict(exc)):
  1082. raise failure
  1083. count[action] += 1
  1084. if action == self.CREATE:
  1085. action = self.DELETE
  1086. count[action] = 0
  1087. if first_failure is None:
  1088. # Save the first exception
  1089. first_failure = failure
  1090. if first_failure:
  1091. raise first_failure
  1092. if self.stack.action == self.stack.CREATE:
  1093. yield self._break_if_required(
  1094. self.CREATE, environment.HOOK_POST_CREATE)
  1095. @staticmethod
  1096. def pause():
  1097. try:
  1098. while True:
  1099. yield
  1100. except scheduler.Timeout:
  1101. return
  1102. def prepare_abandon(self):
  1103. self.abandon_in_progress = True
  1104. return {
  1105. 'name': self.name,
  1106. 'resource_id': self.resource_id,
  1107. 'type': self.type(),
  1108. 'action': self.action,
  1109. 'status': self.status,
  1110. 'metadata': self.metadata_get(),
  1111. 'resource_data': self.data()
  1112. }
  1113. def adopt(self, resource_data):
  1114. """Adopt the existing resource.
  1115. Resource subclasses can provide a handle_adopt() method to customise
  1116. adopt.
  1117. """
  1118. self._update_stored_properties()
  1119. return self._do_action(self.ADOPT, resource_data=resource_data)
  1120. def handle_adopt(self, resource_data=None):
  1121. resource_id, data, metadata = self._get_resource_info(resource_data)
  1122. if not resource_id:
  1123. exc = Exception(_('Resource ID was not provided.'))
  1124. failure = exception.ResourceFailure(exc, self)
  1125. raise failure
  1126. # set resource id
  1127. self.resource_id_set(resource_id)
  1128. # save the resource data
  1129. if data and isinstance(data, dict):
  1130. for key, value in six.iteritems(data):
  1131. self.data_set(key, value)
  1132. # save the resource metadata
  1133. self.metadata_set(metadata)
  1134. def translation_rules(self, properties):
  1135. """Return specified rules for resource."""
  1136. return []
  1137. def translate_properties(self, properties,
  1138. client_resolve=True, ignore_resolve_error=False):
  1139. """Set resource specific rules for properties translation.
  1140. The properties parameter is a properties object and the
  1141. optional client_resolve flag is to specify whether to
  1142. do 'RESOLVE' translation with client lookup.
  1143. """
  1144. rules = self.translation_rules(properties) or []
  1145. properties.update_translation(
  1146. rules, client_resolve=client_resolve,
  1147. ignore_resolve_error=ignore_resolve_error)
  1148. def cancel_grace_period(self):
  1149. canceller = getattr(self,
  1150. 'handle_%s_cancel' % self.action.lower(),
  1151. None)
  1152. if callable(canceller):
  1153. return None
  1154. return cfg.CONF.error_wait_time
  1155. def _get_resource_info(self, resource_data):
  1156. if not resource_data:
  1157. return None, None, None
  1158. return (resource_data.get('resource_id'),
  1159. resource_data.get('resource_data'),
  1160. resource_data.get('metadata'))
  1161. def needs_replace(self, after_props):
  1162. """Mandatory replace based on certain properties."""
  1163. return False
  1164. def needs_replace_with_prop_diff(self, changed_properties_set,
  1165. after_props, before_props):
  1166. """Needs replace based on prop_diff."""
  1167. return False
  1168. def needs_replace_with_tmpl_diff(self, tmpl_diff):
  1169. """Needs replace based on tmpl_diff."""
  1170. return False
  1171. def needs_replace_failed(self):
  1172. """Needs replace if resource is in ``*_FAILED``."""
  1173. return True
  1174. def _needs_update(self, after, before, after_props, before_props,
  1175. prev_resource, check_init_complete=True):
  1176. if self.status == self.FAILED:
  1177. # always replace when a resource is in CHECK_FAILED
  1178. if self.action == self.CHECK or self.needs_replace_failed():
  1179. raise UpdateReplace(self)
  1180. if self.state == (self.DELETE, self.COMPLETE):
  1181. raise UpdateReplace(self)
  1182. if (check_init_complete and
  1183. self.state == (self.INIT, self.COMPLETE)):
  1184. raise UpdateReplace(self)
  1185. if self.needs_replace(after_props):
  1186. raise UpdateReplace(self)
  1187. if before != after.freeze():
  1188. return True
  1189. try:
  1190. return before_props != after_props
  1191. except ValueError:
  1192. return True
  1193. def _check_for_convergence_replace(self, restricted_actions):
  1194. if 'replace' in restricted_actions:
  1195. ex = exception.ResourceActionRestricted(action='replace')
  1196. failure = exception.ResourceFailure(ex, self, self.UPDATE)
  1197. self._add_event(self.UPDATE, self.FAILED, six.text_type(ex))
  1198. raise failure
  1199. else:
  1200. raise UpdateReplace(self.name)
  1201. def update_convergence(self, template_id, new_requires, engine_id,
  1202. timeout, new_stack, progress_callback=None):
  1203. """Update the resource synchronously.
  1204. Persist the resource's current_template_id to template_id and
  1205. resource's requires to list of the required resource ids from the given
  1206. resource_data and existing resource's requires, then updates the
  1207. resource by invoking the scheduler TaskRunner.
  1208. """
  1209. self._calling_engine_id = engine_id
  1210. # Check that the resource type matches. If the type has changed by a
  1211. # legitimate substitution, the load()ed resource will already be of
  1212. # the new type.
  1213. registry = new_stack.env.registry
  1214. new_res_def = new_stack.defn.resource_definition(self.name)
  1215. new_res_type = registry.get_class_to_instantiate(
  1216. new_res_def.resource_type, resource_name=self.name)
  1217. if type(self) is not new_res_type:
  1218. restrictions = registry.get_rsrc_restricted_actions(self.name)
  1219. self._check_for_convergence_replace(restrictions)
  1220. action_rollback = self.stack.action == self.stack.ROLLBACK
  1221. status_in_progress = self.stack.status == self.stack.IN_PROGRESS
  1222. if action_rollback and status_in_progress and self.replaced_by:
  1223. try:
  1224. self.restore_prev_rsrc(convergence=True)
  1225. except Exception as e:
  1226. failure = exception.ResourceFailure(e, self, self.action)
  1227. self.state_set(self.UPDATE, self.FAILED,
  1228. six.text_type(failure))
  1229. raise failure
  1230. self.replaced_by = None
  1231. runner = scheduler.TaskRunner(self.update, new_res_def,
  1232. new_template_id=template_id,
  1233. new_requires=new_requires)
  1234. runner(timeout=timeout, progress_callback=progress_callback)
  1235. def preview_update(self, after, before, after_props, before_props,
  1236. prev_resource, check_init_complete=False):
  1237. """Simulates update without actually updating the resource.
  1238. Raises UpdateReplace, if replacement is required or returns True,
  1239. if in-place update is required.
  1240. """
  1241. if self._needs_update(after, before, after_props, before_props,
  1242. prev_resource, check_init_complete):
  1243. tmpl_diff = self.update_template_diff(after.freeze(), before)
  1244. if tmpl_diff and self.needs_replace_with_tmpl_diff(tmpl_diff):
  1245. raise UpdateReplace(self)
  1246. self.update_template_diff_properties(after_props, before_props)
  1247. return True
  1248. else:
  1249. return False
  1250. def _check_restricted_actions(self, actions, after, before,
  1251. after_props, before_props,
  1252. prev_resource):
  1253. """Checks for restricted actions.
  1254. Raises ResourceActionRestricted, if the resource requires update
  1255. or replace and the required action is restricted.
  1256. Else, Raises UpdateReplace, if replacement is required or returns
  1257. True, if in-place update is required.
  1258. """
  1259. try:
  1260. if self.preview_update(after, before, after_props, before_props,
  1261. prev_resource, check_init_complete=True):
  1262. if 'update' in actions:
  1263. raise exception.ResourceActionRestricted(action='update')
  1264. return True
  1265. except UpdateReplace:
  1266. if 'replace' in actions:
  1267. raise exception.ResourceActionRestricted(action='replace')
  1268. raise
  1269. return False
  1270. def _prepare_update_props(self, after, before):
  1271. before_props = before.properties(self.properties_schema,
  1272. self.context)
  1273. # Regenerate the schema, else validation would fail
  1274. self.regenerate_info_schema(after)
  1275. after.set_translation_rules(self.translation_rules(self.properties))
  1276. after_props = after.properties(self.properties_schema,
  1277. self.context)
  1278. self.translate_properties(after_props)
  1279. self.translate_properties(before_props, ignore_resolve_error=True)
  1280. if (cfg.CONF.observe_on_update or self.converge) and before_props:
  1281. if not self.resource_id:
  1282. raise UpdateReplace(self)
  1283. try:
  1284. resource_reality = self.get_live_state(before_props)
  1285. if resource_reality:
  1286. self._update_properties_with_live_state(before_props,
  1287. resource_reality)
  1288. except exception.EntityNotFound:
  1289. raise UpdateReplace(self)
  1290. except Exception as ex:
  1291. LOG.warning("Resource cannot be updated with it's "
  1292. "live state in case of next "
  1293. "error: %s", ex)
  1294. return after_props, before_props
  1295. def _prepare_update_replace_handler(self, action):
  1296. """Return the handler method for preparing to replace a resource.
  1297. This may be either restore_prev_rsrc() (in the case of a legacy
  1298. rollback) or, more typically, prepare_for_replace().
  1299. If the plugin has not overridden the method, then None is returned in
  1300. place of the default method (which is empty anyway).
  1301. """
  1302. if (self.stack.action == 'ROLLBACK' and
  1303. self.stack.status == 'IN_PROGRESS' and
  1304. not self.stack.convergence):
  1305. # handle case, when it's rollback and we should restore
  1306. # old resource
  1307. if self.restore_prev_rsrc != Resource.restore_prev_rsrc:
  1308. return self.restore_prev_rsrc
  1309. else:
  1310. if self.prepare_for_replace != Resource.prepare_for_replace:
  1311. return self.prepare_for_replace
  1312. return None
  1313. def _prepare_update_replace(self, action):
  1314. handler = self._prepare_update_replace_handler(action)
  1315. if handler is None:
  1316. return
  1317. try:
  1318. handler()
  1319. except Exception as e:
  1320. # if any exception happen, we should set the resource to
  1321. # FAILED, then raise ResourceFailure
  1322. failure = exception.ResourceFailure(e, self, action)
  1323. self.state_set(action, self.FAILED, six.text_type(failure))
  1324. raise failure
  1325. @classmethod
  1326. def check_is_substituted(cls, new_res_type):
  1327. support_status = getattr(cls, 'support_status', None)
  1328. if support_status:
  1329. is_substituted = support_status.is_substituted(new_res_type)
  1330. return is_substituted
  1331. return False
  1332. def _persist_update_no_change(self, new_template_id):
  1333. """Persist an update where the resource is unchanged."""
  1334. if new_template_id is not None:
  1335. self.current_template_id = new_template_id
  1336. lock = (self.LOCK_RESPECT if self.stack.convergence
  1337. else self.LOCK_NONE)
  1338. if self.status == self.FAILED:
  1339. status_reason = _('Update status to COMPLETE for '
  1340. 'FAILED resource neither update '
  1341. 'nor replace.')
  1342. self.state_set(self.action, self.COMPLETE,
  1343. status_reason, lock=lock)
  1344. elif new_template_id is not None:
  1345. self.store(lock=lock)
  1346. @scheduler.wrappertask
  1347. def update(self, after, before=None, prev_resource=None,
  1348. new_template_id=None, new_requires=None):
  1349. """Return a task to update the resource.
  1350. Subclasses should provide a handle_update() method to customise update,
  1351. the base-class handle_update will fail by default.
  1352. """
  1353. action = self.UPDATE
  1354. assert isinstance(after, rsrc_defn.ResourceDefinition)
  1355. if before is None:
  1356. before = self.frozen_definition()
  1357. after_external_id = after.external_id()
  1358. if self.external_id != after_external_id:
  1359. msg = _("Update to property %(prop)s of %(name)s (%(res)s)"
  1360. ) % {'prop': hot_tmpl.HOTemplate20161014.RES_EXTERNAL_ID,
  1361. 'res': self.type(), 'name': self.name}
  1362. exc = exception.NotSupported(feature=msg)
  1363. raise exception.ResourceFailure(exc, self, action)
  1364. elif after_external_id is not None:
  1365. LOG.debug("Skip update on external resource.")
  1366. self._persist_update_no_change(new_template_id)
  1367. return
  1368. after_props, before_props = self._prepare_update_props(after, before)
  1369. yield self._break_if_required(
  1370. self.UPDATE, environment.HOOK_PRE_UPDATE)
  1371. try:
  1372. registry = self.stack.env.registry
  1373. restr_actions = registry.get_rsrc_restricted_actions(self.name)
  1374. if restr_actions:
  1375. needs_update = self._check_restricted_actions(restr_actions,
  1376. after, before,
  1377. after_props,
  1378. before_props,
  1379. prev_resource)
  1380. else:
  1381. needs_update = self._needs_update(after, before,
  1382. after_props, before_props,
  1383. prev_resource)
  1384. except UpdateReplace:
  1385. with excutils.save_and_reraise_exception():
  1386. if self._prepare_update_replace_handler(action) is not None:
  1387. with self.lock(self._calling_engine_id):
  1388. self._prepare_update_replace(action)
  1389. except exception.ResourceActionRestricted as ae:
  1390. failure = exception.ResourceFailure(ae, self, action)
  1391. self._add_event(action, self.FAILED, six.text_type(ae))
  1392. raise failure
  1393. if not needs_update:
  1394. self._persist_update_no_change(new_template_id)
  1395. return
  1396. if not self.stack.convergence:
  1397. if (self.action, self.status) in (
  1398. (self.CREATE, self.IN_PROGRESS),
  1399. (self.UPDATE, self.IN_PROGRESS),
  1400. (self.ADOPT, self.IN_PROGRESS)):
  1401. exc = Exception(_('Resource update already requested'))
  1402. raise exception.ResourceFailure(exc, self, action)
  1403. LOG.info('updating %s', self)
  1404. self.updated_time = datetime.utcnow()
  1405. if new_requires is not None:
  1406. self.requires = self.requires | new_requires
  1407. with self._action_recorder(action, UpdateReplace):
  1408. after_props.validate()
  1409. self.properties = before_props
  1410. tmpl_diff = self.update_template_diff(after.freeze(), before)
  1411. self.old_template_id = self.current_template_id
  1412. try:
  1413. if tmpl_diff and self.needs_replace_with_tmpl_diff(tmpl_diff):
  1414. raise UpdateReplace(self)
  1415. prop_diff = self.update_template_diff_properties(after_props,
  1416. before_props)
  1417. if new_template_id is not None:
  1418. self.current_template_id = new_template_id
  1419. yield self.action_handler_task(action,
  1420. args=[after, tmpl_diff,
  1421. prop_diff])
  1422. except UpdateReplace:
  1423. with excutils.save_and_reraise_exception():
  1424. self.current_template_id = self.old_template_id
  1425. self.old_template_id = None
  1426. self._prepare_update_replace(action)
  1427. self.t = after
  1428. self.reparse()
  1429. self._update_stored_properties()
  1430. if new_requires is not None:
  1431. self.requires = new_requires
  1432. yield self._break_if_required(
  1433. self.UPDATE, environment.HOOK_POST_UPDATE)
  1434. def prepare_for_replace(self):
  1435. """Prepare resource for replacing.
  1436. Some resources requires additional actions before replace them.
  1437. If resource need to be changed before replacing, this method should
  1438. be implemented in resource class.
  1439. """
  1440. pass
  1441. def restore_prev_rsrc(self, convergence=False):
  1442. """Restore resource after rollback.
  1443. Some resources requires additional actions after rollback.
  1444. If resource need to be changed during rollback, this method should
  1445. be implemented in resource class.
  1446. """
  1447. pass
  1448. def check(self):
  1449. """Checks that the physical resource is in its expected state.
  1450. Gets the current status of the physical resource and updates the
  1451. database accordingly. If check is not supported by the resource,
  1452. default action is to fail and revert the resource's status to its
  1453. original state with the added message that check was not performed.
  1454. """
  1455. action = self.CHECK
  1456. LOG.info('Checking %s', self)
  1457. if hasattr(self, 'handle_%s' % action.lower()):
  1458. if self.state == (self.INIT, self.COMPLETE):
  1459. reason = _('Can not check %s, resource not '
  1460. 'created yet.') % self.name
  1461. self.state_set(action, self.FAILED, reason)
  1462. exc = Exception(_('Resource %s not created yet.') % self.name)
  1463. failure = exception.ResourceFailure(exc, self, action)
  1464. raise failure
  1465. with self.frozen_properties():
  1466. return self._do_action(action)
  1467. else:
  1468. if self.state == (self.INIT, self.COMPLETE):
  1469. # No need to store status; better to leave the resource in
  1470. # INIT_COMPLETE than imply that we've checked and it exists.
  1471. return
  1472. reason = '%s not supported for %s' % (action, self.type())
  1473. self.state_set(action, self.COMPLETE, reason)
  1474. def _verify_check_conditions(self, checks):
  1475. def valid(check):
  1476. if isinstance(check['expected'], list):
  1477. return check['current'] in check['expected']
  1478. else:
  1479. return check['current'] == check['expected']
  1480. msg = _("'%(attr)s': expected '%(expected)s', got '%(current)s'")
  1481. invalid_checks = [
  1482. msg % check
  1483. for check in checks
  1484. if not valid(check)
  1485. ]
  1486. if invalid_checks:
  1487. raise exception.Error('; '.join(invalid_checks))
  1488. def suspend(self):
  1489. """Return a task to suspend the resource.
  1490. Subclasses should provide a handle_suspend() method to implement
  1491. suspend.
  1492. """
  1493. action = self.SUSPEND
  1494. # Don't try to suspend the resource unless it's in a stable state
  1495. # or if the previous suspend failed
  1496. if (self.action == self.DELETE or
  1497. (self.action != self.SUSPEND and
  1498. self.status != self.COMPLETE)):
  1499. exc = exception.Error(_('State %s invalid for suspend')
  1500. % six.text_type(self.state))
  1501. raise exception.ResourceFailure(exc, self, action)
  1502. LOG.info('suspending %s', self)
  1503. with self.frozen_properties():
  1504. return self._do_action(action)
  1505. def resume(self):
  1506. """Return a task to resume the resource.
  1507. Subclasses should provide a handle_resume() method to implement resume.
  1508. """
  1509. action = self.RESUME
  1510. # Allow resume a resource if it's SUSPEND_COMPLETE
  1511. # or RESUME_FAILED or RESUME_COMPLETE. Recommend to check
  1512. # the real state of physical resource in handle_resume()
  1513. if self.state not in ((self.SUSPEND, self.COMPLETE),
  1514. (self.RESUME, self.FAILED),
  1515. (self.RESUME, self.COMPLETE)):
  1516. exc = exception.Error(_('State %s invalid for resume')
  1517. % six.text_type(self.state))
  1518. raise exception.ResourceFailure(exc, self, action)
  1519. LOG.info('resuming %s', self)
  1520. with self.frozen_properties():
  1521. return self._do_action(action)
  1522. def snapshot(self):
  1523. """Snapshot the resource and return the created data, if any."""
  1524. LOG.info('snapshotting %s', self)
  1525. with self.frozen_properties():
  1526. return self._do_action(self.SNAPSHOT)
  1527. @scheduler.wrappertask
  1528. def delete_snapshot(self, data):
  1529. yield self.action_handler_task('delete_snapshot', args=[data])
  1530. def physical_resource_name(self):
  1531. if self.id is None or self.action == self.INIT:
  1532. return None
  1533. name = '%s-%s-%s' % (self.stack.name.rstrip('*'),
  1534. self.name,
  1535. short_id.get_id(self.uuid))
  1536. if self.physical_resource_name_limit:
  1537. name = self.reduce_physical_resource_name(
  1538. name, self.physical_resource_name_limit)
  1539. return name
  1540. @staticmethod
  1541. def reduce_physical_resource_name(name, limit):
  1542. """Reduce length of physical resource name to a limit.
  1543. The reduced name will consist of the following:
  1544. * the first 2 characters of the name
  1545. * a hyphen
  1546. * the end of the name, truncated on the left to bring
  1547. the name length within the limit
  1548. :param name: The name to reduce the length of
  1549. :param limit: The max length limit
  1550. :returns: A name whose length is less than or equal to the limit
  1551. """
  1552. if len(name) <= limit:
  1553. return name
  1554. if limit < 4:
  1555. raise ValueError(_('limit cannot be less than 4'))
  1556. postfix_length = limit - 3
  1557. return name[0:2] + '-' + name[-postfix_length:]
  1558. def validate(self):
  1559. """Validate the resource.
  1560. This may be overridden by resource plugins to add extra
  1561. validation logic specific to the resource implementation.
  1562. """
  1563. LOG.info('Validating %s', self)
  1564. return self.validate_template()
  1565. def validate_template(self):
  1566. """Validate structural/syntax aspects of the resource definition.
  1567. Resource plugins should not override this, because this interface
  1568. is expected to be called pre-create so things normally valid
  1569. in an overridden validate() such as accessing properties
  1570. may not work.
  1571. """
  1572. self._validate_service_availability(
  1573. self.stack.context,
  1574. self.t.resource_type
  1575. )
  1576. try:
  1577. self.t.validate()
  1578. self.validate_deletion_policy(self.t.deletion_policy())
  1579. self.t.update_policy(self.update_policy_schema,
  1580. self.context).validate()
  1581. validate = self.properties.validate(
  1582. with_value=self.stack.strict_validate)
  1583. except exception.StackValidationFailed as ex:
  1584. path = [self.stack.t.RESOURCES, self.t.name]
  1585. if ex.path:
  1586. path.append(self.stack.t.get_section_name(ex.path[0]))
  1587. path.extend(ex.path[1:])
  1588. raise exception.StackValidationFailed(
  1589. error=ex.error,
  1590. path=path,
  1591. message=ex.error_message)
  1592. return validate
  1593. @classmethod
  1594. def validate_deletion_policy(cls, policy):
  1595. path = rsrc_defn.DELETION_POLICY
  1596. if policy not in rsrc_defn.ResourceDefinition.DELETION_POLICIES:
  1597. msg = _('Invalid deletion policy "%s"') % policy
  1598. raise exception.StackValidationFailed(message=msg, path=path)
  1599. if policy == rsrc_defn.ResourceDefinition.SNAPSHOT:
  1600. if not callable(getattr(cls, 'handle_snapshot_delete', None)):
  1601. msg = _('"%s" deletion policy not supported') % policy
  1602. raise exception.StackValidationFailed(message=msg, path=path)
  1603. def _update_replacement_data(self, template_id):
  1604. # Update the replacement resource's replaces field.
  1605. # Make sure that the replacement belongs to the given
  1606. # template and there is no engine working on it.
  1607. if self.replaced_by is None:
  1608. return
  1609. try:
  1610. db_res = resource_objects.Resource.get_obj(
  1611. self.context, self.replaced_by,
  1612. fields=('current_template_id', 'atomic_key'))
  1613. except exception.NotFound:
  1614. LOG.info("Could not find replacement of resource %(name)s "
  1615. "with id %(id)s while updating replaces.",
  1616. {'name': self.name, 'id': self.replaced_by})
  1617. return
  1618. if (db_res.current_template_id == template_id):
  1619. # Following update failure is ignorable; another
  1620. # update might have locked/updated the resource.
  1621. db_res.select_and_update({'replaces': None},
  1622. atomic_key=db_res.atomic_key,
  1623. expected_engine_id=None)
  1624. def delete_convergence(self, template_id, engine_id, timeout,
  1625. progress_callback=None):
  1626. """Destroys the resource if it doesn't belong to given template.
  1627. The given template is suppose to be the current template being
  1628. provisioned.
  1629. Also, since this resource is visited as part of clean-up phase,
  1630. the needed_by should be updated. If this resource was
  1631. replaced by more recent resource, then delete this and update
  1632. the replacement resource's replaces field.
  1633. """
  1634. self._calling_engine_id = engine_id
  1635. if self.current_template_id != template_id:
  1636. # just delete the resources in INIT state
  1637. if self.action == self.INIT:
  1638. try:
  1639. resource_objects.Resource.delete(self.context, self.id)
  1640. except exception.NotFound:
  1641. pass
  1642. else:
  1643. runner = scheduler.TaskRunner(self.delete)
  1644. runner(timeout=timeout,
  1645. progress_callback=progress_callback)
  1646. self._update_replacement_data(template_id)
  1647. def handle_delete(self):
  1648. """Default implementation; should be overridden by resources."""
  1649. if self.entity and self.resource_id is not None:
  1650. with self._default_client_plugin().ignore_not_found:
  1651. obj = getattr(self.client(), self.entity)
  1652. obj.delete(self.resource_id)
  1653. return self.resource_id
  1654. return None
  1655. @scheduler.wrappertask
  1656. def delete(self):
  1657. """A task to delete the resource.
  1658. Subclasses should provide a handle_delete() method to customise
  1659. deletion.
  1660. """
  1661. @excutils.exception_filter
  1662. def should_retry(exc):
  1663. if count >= retry_limit:
  1664. return False
  1665. return (self._default_client_plugin().is_conflict(exc) or
  1666. isinstance(exc, exception.PhysicalResourceExists))
  1667. action = self.DELETE
  1668. if (self.action, self.status) == (self.DELETE, self.COMPLETE):
  1669. return
  1670. # No need to delete if the resource has never been created
  1671. if self.action == self.INIT:
  1672. return
  1673. initial_state = self.state
  1674. # This method can be called when we replace a resource, too. In that
  1675. # case, a hook has already been dealt with in `Resource.update` so we
  1676. # shouldn't do it here again:
  1677. if self.stack.action == self.stack.DELETE:
  1678. yield self._break_if_required(
  1679. self.DELETE, environment.HOOK_PRE_DELETE)
  1680. LOG.info('deleting %s', self)
  1681. if self._stored_properties_data is not None:
  1682. # On delete we can't rely on re-resolving the properties
  1683. # so use the stored frozen_definition instead
  1684. self.properties = self.frozen_definition().properties(
  1685. self.properties_schema, self.context)
  1686. self.translate_properties(self.properties,
  1687. ignore_resolve_error=True)
  1688. with self._action_recorder(action):
  1689. if self.abandon_in_progress:
  1690. deletion_policy = self.t.RETAIN
  1691. else:
  1692. deletion_policy = self.t.deletion_policy()
  1693. if deletion_policy != self.t.RETAIN:
  1694. if deletion_policy == self.t.SNAPSHOT:
  1695. action_args = [[initial_state], 'snapshot']
  1696. else:
  1697. action_args = []
  1698. count = -1
  1699. retry_limit = max(cfg.CONF.action_retry_limit, 0)
  1700. while True:
  1701. count += 1
  1702. LOG.info('delete %(name)s attempt %(attempt)d' %
  1703. {'name': six.text_type(self), 'attempt': count+1})
  1704. if count:
  1705. delay = timeutils.retry_backoff_delay(count,
  1706. jitter_max=2.0)
  1707. waiter = scheduler.TaskRunner(self.pause)
  1708. yield waiter.as_task(timeout=delay)
  1709. with excutils.exception_filter(should_retry):
  1710. yield self.action_handler_task(action,
  1711. *action_args)
  1712. break
  1713. if self.stack.action == self.stack.DELETE:
  1714. yield self._break_if_required(
  1715. self.DELETE, environment.HOOK_POST_DELETE)
  1716. @scheduler.wrappertask
  1717. def destroy(self):
  1718. """A task to delete the resource and remove it from the database."""
  1719. yield self.delete()
  1720. if self.id is None:
  1721. return
  1722. try:
  1723. resource_objects.Resource.delete(self.context, self.id)
  1724. except exception.NotFound:
  1725. # Don't fail on delete if the db entry has
  1726. # not been created yet.
  1727. pass
  1728. self.id = None
  1729. def resource_id_set(self, inst):
  1730. self.resource_id = inst
  1731. if self.id is not None:
  1732. try:
  1733. resource_objects.Resource.update_by_id(
  1734. self.context,
  1735. self.id,
  1736. {'physical_resource_id': self.resource_id})
  1737. except Exception as ex:
  1738. LOG.warning('db error %s', ex)
  1739. def store(self, set_metadata=False, lock=LOCK_NONE):
  1740. """Create the resource in the database.
  1741. If self.id is set, we update the existing stack.
  1742. """
  1743. if not self.root_stack_id:
  1744. self.root_stack_id = self.stack.root_stack_id()
  1745. rs = {'action': self.action,
  1746. 'status': self.status,
  1747. 'status_reason': six.text_type(self.status_reason),
  1748. 'stack_id': self.stack.id,
  1749. 'physical_resource_id': self.resource_id,
  1750. 'name': self.name,
  1751. 'rsrc_prop_data_id':
  1752. self._create_or_replace_rsrc_prop_data(),
  1753. 'needed_by': [],
  1754. 'requires': sorted(self.requires, reverse=True),
  1755. 'replaces': self.replaces,
  1756. 'replaced_by': self.replaced_by,
  1757. 'current_template_id': self.current_template_id,
  1758. 'root_stack_id': self.root_stack_id,
  1759. 'updated_at': self.updated_time,
  1760. 'properties_data': None}
  1761. if set_metadata:
  1762. metadata = self.t.metadata()
  1763. rs['rsrc_metadata'] = metadata
  1764. self._rsrc_metadata = metadata
  1765. if self.id is not None:
  1766. if (lock == self.LOCK_NONE or
  1767. (lock in {self.LOCK_ACQUIRE, self.LOCK_RELEASE} and
  1768. self._calling_engine_id is None)):
  1769. resource_objects.Resource.update_by_id(
  1770. self.context, self.id, rs)
  1771. if lock != self.LOCK_NONE:
  1772. LOG.error('No calling_engine_id in store() %s',
  1773. six.text_type(rs))
  1774. else:
  1775. self._store_with_lock(rs, lock)
  1776. else:
  1777. new_rs = resource_objects.Resource.create(self.context, rs)
  1778. self.id = new_rs.id
  1779. self.uuid = new_rs.uuid
  1780. self.created_time = new_rs.created_at
  1781. def _store_with_lock(self, rs, lock):
  1782. if lock == self.LOCK_ACQUIRE:
  1783. rs['engine_id'] = self._calling_engine_id
  1784. expected_engine_id = None
  1785. elif lock == self.LOCK_RESPECT:
  1786. expected_engine_id = None
  1787. elif lock == self.LOCK_RELEASE:
  1788. expected_engine_id = self._calling_engine_id
  1789. rs['engine_id'] = None
  1790. else:
  1791. assert False, "Invalid lock action: %s" % lock
  1792. if resource_objects.Resource.select_and_update_by_id(
  1793. self.context, self.id, rs, expected_engine_id,
  1794. self._atomic_key):
  1795. self._incr_atomic_key(self._atomic_key)
  1796. else:
  1797. LOG.info('Resource %s is locked or does not exist',
  1798. six.text_type(self))
  1799. LOG.debug('Resource id:%(resource_id)s locked or does not exist. '
  1800. 'Expected atomic_key:%(atomic_key)s, '
  1801. 'accessing from engine_id:%(engine_id)s',
  1802. {'resource_id': self.id,
  1803. 'atomic_key': self._atomic_key,
  1804. 'engine_id': self._calling_engine_id})
  1805. raise exception.UpdateInProgress(self.name)
  1806. def _add_event(self, action, status, reason):
  1807. """Add a state change event to the database."""
  1808. physical_res_id = self.resource_id or self.physical_resource_name()
  1809. ev = event.Event(self.context, self.stack, action, status, reason,
  1810. physical_res_id, self._rsrc_prop_data_id,
  1811. self._stored_properties_data, self.name, self.type())
  1812. ev.store()
  1813. self.stack.dispatch_event(ev)
  1814. @contextlib.contextmanager
  1815. def lock(self, engine_id):
  1816. self._calling_engine_id = engine_id
  1817. try:
  1818. if engine_id is not None:
  1819. self._store_with_lock({}, self.LOCK_ACQUIRE)
  1820. yield
  1821. except exception.UpdateInProgress:
  1822. raise
  1823. except BaseException:
  1824. with excutils.save_and_reraise_exception():
  1825. if engine_id is not None:
  1826. self._store_with_lock({}, self.LOCK_RELEASE)
  1827. else:
  1828. if engine_id is not None:
  1829. self._store_with_lock({}, self.LOCK_RELEASE)
  1830. def _resolve_any_attribute(self, attr):
  1831. """Method for resolving any attribute, including base attributes.
  1832. This method uses basic _resolve_attribute method for resolving
  1833. plugin-specific attributes. Base attributes will be resolved with
  1834. corresponding method, which should be defined in each resource
  1835. class.
  1836. :param attr: attribute name, which will be resolved
  1837. :returns: method of resource class, which resolve base attribute
  1838. """
  1839. if attr in self.base_attributes_schema:
  1840. # check resource_id, because usually it is required for getting
  1841. # information about resource
  1842. if self.resource_id is not None:
  1843. with self._default_client_plugin().ignore_not_found:
  1844. return getattr(self, '_{0}_resource'.format(attr))()
  1845. else:
  1846. with self._default_client_plugin().ignore_not_found:
  1847. return self._resolve_attribute(attr)
  1848. return None
  1849. def _show_resource(self):
  1850. """Default implementation; should be overridden by resources.
  1851. :returns: the map of resource information or None
  1852. """
  1853. if self.entity and self.default_client_name is not None:
  1854. try:
  1855. obj = getattr(self.client(), self.entity)
  1856. resource = obj.get(self.resource_id)
  1857. if isinstance(resource, dict):
  1858. return resource
  1859. else:
  1860. return resource.to_dict()
  1861. except AttributeError as ex:
  1862. LOG.warning("Resolving 'show' attribute has failed : %s",
  1863. ex)
  1864. return None
  1865. def get_live_resource_data(self):
  1866. """Default implementation; can be overridden by resources.
  1867. Get resource data and handle it with exceptions.
  1868. """
  1869. try:
  1870. resource_data = self._show_resource()
  1871. except Exception as ex:
  1872. if self._default_client_plugin().is_not_found(ex):
  1873. raise exception.EntityNotFound(
  1874. entity='Resource', name=self.name)
  1875. raise
  1876. return resource_data
  1877. def parse_live_resource_data(self, resource_properties, resource_data):
  1878. """Default implementation; can be overridden by resources.
  1879. Parse resource data for using it in updating properties with live
  1880. state.
  1881. :param resource_properties: properties of stored resource plugin.
  1882. :param resource_data: data from current live state of a resource.
  1883. """
  1884. resource_result = {}
  1885. for key in self._update_allowed_properties:
  1886. if key in resource_data:
  1887. if key == 'name' and resource_properties.get(key) is None:
  1888. # We use `physical_resource_name` for name property in some
  1889. # resources when name not provided during create, so we
  1890. # shouldn't add name in resource_data if it's None in
  1891. # property (might just the cases that we using
  1892. # `physical_resource_name`).
  1893. continue
  1894. resource_result[key] = resource_data.get(key)
  1895. return resource_result
  1896. def get_live_state(self, resource_properties):
  1897. """Default implementation; should be overridden by resources.
  1898. :param resource_properties: resource's object of Properties class.
  1899. :returns: dict of resource's real state of properties.
  1900. """
  1901. resource_data = self.get_live_resource_data()
  1902. if resource_data is None:
  1903. return {}
  1904. return self.parse_live_resource_data(resource_properties,
  1905. resource_data)
  1906. def _update_properties_with_live_state(self, resource_properties,
  1907. live_properties):
  1908. """Update resource properties data with live state properties.
  1909. Note, that live_properties can contains None values, so there's next
  1910. situation: property equals to some value, but live state has no such
  1911. property, i.e. property equals to None, so during update property
  1912. should be updated with None.
  1913. """
  1914. for key in resource_properties:
  1915. if key in live_properties:
  1916. if resource_properties.get(key) != live_properties.get(key):
  1917. resource_properties.data.update(
  1918. {key: live_properties.get(key)})
  1919. def _resolve_attribute(self, name):
  1920. """Default implementation of resolving resource's attributes.
  1921. Should be overridden by resources, that expose attributes.
  1922. :param name: The attribute to resolve
  1923. :returns: the resource attribute named key
  1924. """
  1925. # By default, no attributes resolve
  1926. pass
  1927. def regenerate_info_schema(self, definition):
  1928. """Default implementation; should be overridden by resources.
  1929. Should be overridden by resources that would require schema refresh
  1930. during update, ex. TemplateResource.
  1931. :definition: Resource Definition
  1932. """
  1933. # By default, do not regenerate
  1934. pass
  1935. def state_reset(self):
  1936. """Reset state to (INIT, COMPLETE)."""
  1937. self.action = self.INIT
  1938. self.status = self.COMPLETE
  1939. def state_set(self, action, status, reason="state changed",
  1940. lock=LOCK_NONE):
  1941. if action not in self.ACTIONS:
  1942. raise ValueError(_("Invalid action %s") % action)
  1943. if status not in self.STATUSES:
  1944. raise ValueError(_("Invalid status %s") % status)
  1945. old_state = (self.action, self.status)
  1946. new_state = (action, status)
  1947. set_metadata = self.action == self.INIT
  1948. self.action = action
  1949. self.status = status
  1950. self.status_reason = reason
  1951. self.store(set_metadata, lock=lock)
  1952. if new_state != old_state:
  1953. self._add_event(action, status, reason)
  1954. if status != self.COMPLETE:
  1955. self.clear_stored_attributes()
  1956. @property
  1957. def state(self):
  1958. """Returns state, tuple of action, status."""
  1959. return (self.action, self.status)
  1960. def store_attributes(self):
  1961. assert self.id is not None
  1962. if self.status != self.COMPLETE or self.action in (self.INIT,
  1963. self.DELETE):
  1964. return
  1965. if not self.attributes.has_new_cached_attrs():
  1966. return
  1967. try:
  1968. attr_data_id = resource_objects.Resource.store_attributes(
  1969. self.context, self.id, self._atomic_key,
  1970. self.attributes.cached_attrs, self._attr_data_id)
  1971. if attr_data_id is not None:
  1972. self._incr_atomic_key(self._atomic_key)
  1973. self._attr_data_id = attr_data_id
  1974. except Exception as ex:
  1975. LOG.error('store_attributes rsrc %(name)s %(id)s DB error %(ex)s',
  1976. {'name': self.name, 'id': self.id, 'ex': ex})
  1977. def clear_stored_attributes(self):
  1978. if self._attr_data_id:
  1979. resource_objects.Resource.attr_data_delete(
  1980. self.context, self.id, self._attr_data_id)
  1981. self.attributes.reset_resolved_values()
  1982. def get_reference_id(self):
  1983. """Default implementation for function get_resource.
  1984. This may be overridden by resource plugins to add extra
  1985. logic specific to the resource implementation.
  1986. """
  1987. if self.resource_id is not None:
  1988. return six.text_type(self.resource_id)
  1989. else:
  1990. return six.text_type(self.name)
  1991. def FnGetRefId(self):
  1992. """For the intrinsic function Ref.
  1993. :results: the id or name of the resource.
  1994. """
  1995. return self.get_reference_id()
  1996. def physical_resource_name_or_FnGetRefId(self):
  1997. res_name = self.physical_resource_name()
  1998. if res_name is not None:
  1999. return six.text_type(res_name)
  2000. else:
  2001. return Resource.get_reference_id(self)
  2002. def get_attribute(self, key, *path):
  2003. """Default implementation for function get_attr and Fn::GetAtt.
  2004. This may be overridden by resource plugins to add extra
  2005. logic specific to the resource implementation.
  2006. """
  2007. try:
  2008. attribute = self.attributes[key]
  2009. except KeyError:
  2010. raise exception.InvalidTemplateAttribute(resource=self.name,
  2011. key=key)
  2012. return attributes.select_from_attribute(attribute, path)
  2013. def FnGetAtt(self, key, *path):
  2014. """For the intrinsic function Fn::GetAtt.
  2015. :param key: the attribute key.
  2016. :param path: a list of path components to select from the attribute.
  2017. :returns: the attribute value.
  2018. """
  2019. cache_custom = ((self.attributes.get_cache_mode(key) !=
  2020. attributes.Schema.CACHE_NONE) and
  2021. (type(self).get_attribute != Resource.get_attribute))
  2022. if cache_custom:
  2023. if path:
  2024. full_key = sync_point.str_pack_tuple((key,) + path)
  2025. else:
  2026. full_key = key
  2027. if full_key in self.attributes.cached_attrs:
  2028. return self.attributes.cached_attrs[full_key]
  2029. attr_val = self.get_attribute(key, *path)
  2030. if cache_custom:
  2031. self.attributes.set_cached_attr(full_key, attr_val)
  2032. return attr_val
  2033. def _signal_check_action(self):
  2034. if self.action in self.no_signal_actions:
  2035. self._add_event(self.action, self.status,
  2036. 'Cannot signal resource during %s' % self.action)
  2037. msg = _('Signal resource during %s') % self.action
  2038. raise exception.NotSupported(feature=msg)
  2039. def _signal_check_hook(self, details):
  2040. if details and 'unset_hook' in details:
  2041. hook = details['unset_hook']
  2042. if not environment.valid_hook_type(hook):
  2043. msg = (_('Invalid hook type "%(hook)s" for %(resource)s') %
  2044. {'hook': hook, 'resource': six.text_type(self)})
  2045. raise exception.InvalidBreakPointHook(message=msg)
  2046. if not self.has_hook(hook):
  2047. msg = (_('The "%(hook)s" hook is not defined '
  2048. 'on %(resource)s') %
  2049. {'hook': hook, 'resource': six.text_type(self)})
  2050. raise exception.InvalidBreakPointHook(message=msg)
  2051. def _unset_hook(self, details):
  2052. # Clear the hook without interfering with resources'
  2053. # `handle_signal` callbacks:
  2054. hook = details['unset_hook']
  2055. self.clear_hook(hook)
  2056. LOG.info('Clearing %(hook)s hook on %(resource)s',
  2057. {'hook': hook, 'resource': six.text_type(self)})
  2058. self._add_event(self.action, self.status,
  2059. "Hook %s is cleared" % hook)
  2060. def _handle_signal(self, details):
  2061. if not callable(getattr(self, 'handle_signal', None)):
  2062. raise exception.ResourceActionNotSupported(action='signal')
  2063. def get_string_details():
  2064. if details is None:
  2065. return 'No signal details provided'
  2066. if isinstance(details, six.string_types):
  2067. return details
  2068. if isinstance(details, dict):
  2069. if all(k in details for k in ('previous', 'current',
  2070. 'reason')):
  2071. # this is from Ceilometer.
  2072. auto = '%(previous)s to %(current)s (%(reason)s)' % details
  2073. return 'alarm state changed from %s' % auto
  2074. return 'Unknown'
  2075. try:
  2076. signal_result = self.handle_signal(details)
  2077. if signal_result:
  2078. reason_string = "Signal: %s" % signal_result
  2079. else:
  2080. reason_string = get_string_details()
  2081. self._add_event('SIGNAL', self.status, reason_string)
  2082. except NoActionRequired:
  2083. # Don't log an event as it just spams the user.
  2084. pass
  2085. except Exception as ex:
  2086. if hasattr(self, '_db_res_is_deleted'):
  2087. # No spam required
  2088. return
  2089. LOG.info('signal %(name)s : %(msg)s',
  2090. {'name': six.text_type(self),
  2091. 'msg': six.text_type(ex)},
  2092. exc_info=True)
  2093. failure = exception.ResourceFailure(ex, self)
  2094. raise failure
  2095. def signal(self, details=None, need_check=True):
  2096. """Signal the resource.
  2097. Returns True if the metadata for all resources in the stack needs to
  2098. be regenerated as a result of the signal, False if it should not be.
  2099. Subclasses should provide a handle_signal() method to implement the
  2100. signal. The base-class raise an exception if no handler is implemented.
  2101. """
  2102. if need_check:
  2103. self._signal_check_hook(details)
  2104. if details and 'unset_hook' in details:
  2105. self._unset_hook(details)
  2106. return False
  2107. if need_check:
  2108. self._signal_check_action()
  2109. with self.frozen_properties():
  2110. self._handle_signal(details)
  2111. return self.signal_needs_metadata_updates
  2112. def handle_update(self, json_snippet, tmpl_diff, prop_diff):
  2113. if prop_diff:
  2114. raise UpdateReplace(self.name)
  2115. def metadata_update(self, new_metadata=None):
  2116. """No-op for resources which don't explicitly override this method."""
  2117. if new_metadata:
  2118. LOG.warning("Resource %s does not implement metadata update",
  2119. self.name)
  2120. @classmethod
  2121. def resource_to_template(cls, resource_type, template_type='cfn'):
  2122. """Generate a provider template that mirrors the resource.
  2123. :param resource_type: The resource type to be displayed in the template
  2124. :param template_type: the template type to generate, cfn or hot.
  2125. :returns: A template where the resource's properties_schema is mapped
  2126. as parameters, and the resource's attributes_schema is mapped as
  2127. outputs
  2128. """
  2129. props_schema = {}
  2130. for name, schema_dict in cls.properties_schema.items():
  2131. schema = properties.Schema.from_legacy(schema_dict)
  2132. if schema.support_status.status != support.HIDDEN:
  2133. props_schema[name] = schema
  2134. params, props = (properties.Properties.
  2135. schema_to_parameters_and_properties(props_schema,
  2136. template_type))
  2137. resource_name = cls.__name__
  2138. outputs = attributes.Attributes.as_outputs(resource_name, cls,
  2139. template_type)
  2140. description = 'Initial template of %s' % resource_name
  2141. return cls.build_template_dict(resource_name, resource_type,
  2142. template_type, params, props,
  2143. outputs, description)
  2144. @staticmethod
  2145. def build_template_dict(res_name, res_type, tmpl_type,
  2146. params, props, outputs, description):
  2147. if tmpl_type == 'hot':
  2148. tmpl_dict = {
  2149. hot_tmpl.HOTemplate20161014.VERSION: '2016-10-14',
  2150. hot_tmpl.HOTemplate20161014.DESCRIPTION: description,
  2151. hot_tmpl.HOTemplate20161014.PARAMETERS: params,
  2152. hot_tmpl.HOTemplate20161014.OUTPUTS: outputs,
  2153. hot_tmpl.HOTemplate20161014.RESOURCES: {
  2154. res_name: {
  2155. hot_tmpl.HOTemplate20161014.RES_TYPE: res_type,
  2156. hot_tmpl.HOTemplate20161014.RES_PROPERTIES: props}}}
  2157. else:
  2158. tmpl_dict = {
  2159. cfn_tmpl.CfnTemplate.ALTERNATE_VERSION: '2012-12-12',
  2160. cfn_tmpl.CfnTemplate.DESCRIPTION: description,
  2161. cfn_tmpl.CfnTemplate.PARAMETERS: params,
  2162. cfn_tmpl.CfnTemplate.RESOURCES: {
  2163. res_name: {
  2164. cfn_tmpl.CfnTemplate.RES_TYPE: res_type,
  2165. cfn_tmpl.CfnTemplate.RES_PROPERTIES: props}
  2166. },
  2167. cfn_tmpl.CfnTemplate.OUTPUTS: outputs}
  2168. return tmpl_dict
  2169. def data(self):
  2170. """Return the resource data for this resource.
  2171. Use methods data_set and data_delete to modify the resource data
  2172. for this resource.
  2173. :returns: a dict representing the resource data for this resource.
  2174. """
  2175. if self._data is None and self.id is not None:
  2176. try:
  2177. self._data = resource_data_objects.ResourceData.get_all(self)
  2178. except exception.NotFound:
  2179. pass
  2180. return self._data or {}
  2181. def data_set(self, key, value, redact=False):
  2182. """Set a key in the resource data."""
  2183. resource_data_objects.ResourceData.set(self, key, value, redact)
  2184. # force fetch all resource data from the database again
  2185. self._data = None
  2186. def data_delete(self, key):
  2187. """Remove a key from the resource data.
  2188. :returns: True if the key existed to delete.
  2189. """
  2190. try:
  2191. resource_data_objects.ResourceData.delete(self, key)
  2192. except exception.NotFound:
  2193. return False
  2194. else:
  2195. # force fetch all resource data from the database again
  2196. self._data = None
  2197. return True
  2198. def _create_or_replace_rsrc_prop_data(self):
  2199. if self._rsrc_prop_data_id is not None:
  2200. return self._rsrc_prop_data_id
  2201. if not self._stored_properties_data:
  2202. return None
  2203. self._rsrc_prop_data_id = \
  2204. rpd_objects.ResourcePropertiesData(self.context).create(
  2205. self.context, self._stored_properties_data).id
  2206. return self._rsrc_prop_data_id
  2207. def is_using_neutron(self):
  2208. try:
  2209. sess_client = self.client('neutron').httpclient
  2210. if not sess_client.get_endpoint():
  2211. return False
  2212. except Exception:
  2213. return False
  2214. return True
  2215. @staticmethod
  2216. def _make_resolver(ref):
  2217. """Return an attribute resolution method.
  2218. This builds a resolver without a strong reference to this resource, to
  2219. break a possible cycle.
  2220. """
  2221. def resolve(attr):
  2222. res = ref()
  2223. if res is None:
  2224. raise RuntimeError("Resource collected")
  2225. return res._resolve_any_attribute(attr)
  2226. return resolve