blob: 88f1745fb8699310ca6e068d816e7ab96380a6aa [file] [log] [blame]
Rakesh H Sa3325d62015-04-04 19:42:29 +05301#
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
14import eventlet
15
16from heat.common.i18n import _
17from heat.engine import attributes
18from heat.engine import properties
19from heat.engine import resource
20from heat.engine import support
21from oslo_log import log as logging
22
23LOG = logging.getLogger(__name__)
24
25
26class TestResource(resource.Resource):
27 '''
28 A resource which stores the string value that was provided.
29
30 This resource is to be used only for testing.
31 It has control knobs such as 'update_replace', 'fail', 'wait_secs'
32
33 '''
34
35 support_status = support.SupportStatus(version='2014.1')
36
37 PROPERTIES = (
38 VALUE, UPDATE_REPLACE, FAIL, WAIT_SECS
39 ) = (
40 'value', 'update_replace', 'fail', 'wait_secs'
41 )
42
43 ATTRIBUTES = (
44 OUTPUT,
45 ) = (
46 'output',
47 )
48
49 properties_schema = {
50 VALUE: properties.Schema(
51 properties.Schema.STRING,
52 _('The input string to be stored.'),
53 default='test_string',
54 update_allowed=True
55 ),
56 FAIL: properties.Schema(
57 properties.Schema.BOOLEAN,
58 _('Value which can be set to fail the resource operation '
59 'to test failure scenarios.'),
60 update_allowed=True,
61 default=False
62 ),
63 UPDATE_REPLACE: properties.Schema(
64 properties.Schema.BOOLEAN,
65 _('Value which can be set to trigger update replace for '
66 'the particular resource'),
67 update_allowed=True,
68 default=False
69 ),
70 WAIT_SECS: properties.Schema(
71 properties.Schema.NUMBER,
72 _('Value which can be set for resource to wait after an action '
73 'is performed.'),
74 update_allowed=True,
75 default=0,
76 ),
77 }
78
79 attributes_schema = {
80 OUTPUT: attributes.Schema(
81 _('The string that was stored. This value is '
82 'also available by referencing the resource.'),
83 cache_mode=attributes.Schema.CACHE_NONE
84 ),
85 }
86
87 def handle_create(self):
88 value = self.properties.get(self.VALUE)
89 fail_prop = self.properties.get(self.FAIL)
90 sleep_secs = self.properties.get(self.WAIT_SECS)
91
92 self.data_set('value', value, redact=False)
93 self.resource_id_set(self.physical_resource_name())
94
95 # sleep for specified time
96 if sleep_secs:
97 LOG.debug("Resource %s sleeping for %s seconds",
98 self.name, sleep_secs)
99 eventlet.sleep(sleep_secs)
100
101 # emulate failure
102 if fail_prop:
103 raise Exception("Test Resource failed %s", self.name)
104
105 def handle_update(self, json_snippet=None, tmpl_diff=None, prop_diff=None):
106 value = prop_diff.get(self.VALUE)
107 new_prop = json_snippet._properties
108 if value:
109 update_replace = new_prop.get(self.UPDATE_REPLACE, False)
110 if update_replace:
111 raise resource.UpdateReplace(self.name)
112 else:
113 fail_prop = new_prop.get(self.FAIL, False)
114 sleep_secs = new_prop.get(self.WAIT_SECS, 0)
115 # emulate failure
116 if fail_prop:
117 raise Exception("Test Resource failed %s", self.name)
118 # update in place
119 self.data_set('value', value, redact=False)
120
121 if sleep_secs:
122 LOG.debug("Update of Resource %s sleeping for %s seconds",
123 self.name, sleep_secs)
124 eventlet.sleep(sleep_secs)
125
126 def _resolve_attribute(self, name):
127 if name == self.OUTPUT:
128 return self.data().get('value')
129
130
131def resource_mapping():
132 return {
133 'OS::Heat::TestResource': TestResource,
134 }