Skip to content

Commit 47a9cf7

Browse files
committed
add resource_id and is_external properties
add resource_id and is_external properties ok
1 parent e690b2c commit 47a9cf7

4 files changed

+44
-7
lines changed

cloudify/context.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
# * See the License for the specific language governing permissions and
1414
# * limitations under the License.
1515

16-
import errno
1716
import os
17+
import errno
1818
import warnings
1919
from contextlib import contextmanager
2020

21-
from cloudify_rest_client.exceptions import CloudifyClientError
22-
from cloudify.endpoint import ManagerEndpoint, LocalEndpoint
23-
from cloudify.logs import init_cloudify_logger
21+
from cloudify import utils
2422
from cloudify import constants
2523
from cloudify import exceptions
26-
from cloudify import utils
24+
from cloudify.logs import init_cloudify_logger
25+
from cloudify.endpoint import ManagerEndpoint, LocalEndpoint
26+
from cloudify_rest_client.exceptions import CloudifyClientError
2727
from cloudify.constants import DEPLOYMENT, NODE_INSTANCE, RELATIONSHIP_INSTANCE
2828

2929

@@ -404,7 +404,10 @@ def properties(self):
404404
These properties are the properties specified in the blueprint.
405405
"""
406406
self._get_node_if_needed()
407-
return self._node.properties
407+
try:
408+
return self._node.properties
409+
except AttributeError:
410+
return self._node.get('properties')
408411

409412
@property
410413
def type(self):
@@ -424,6 +427,16 @@ def number_of_instances(self):
424427
self._get_node_if_needed()
425428
return self._node.number_of_instances
426429

430+
@property
431+
def is_external(self) -> bool:
432+
"""If this is a resource that Cloudify manages or not"""
433+
return self.properties.get('use_external_resource', False)
434+
435+
@property
436+
def resource_id(self):
437+
"""The resource's ID outside of Cloudify"""
438+
return self.properties.get('resource_id')
439+
427440

428441
class NodeInstanceContext(EntityContext):
429442
def __init__(self, *args, **kwargs):

cloudify/tests/test_context.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ def setUp(self):
5454
os.environ[constants.LOCAL_RESOURCES_ROOT_ENV_KEY] = '/tmp/resources'
5555
self.context = context.CloudifyContext({
5656
'blueprint_id': '',
57-
'tenant': {'name': 'default_tenant'}
57+
'tenant': {'name': 'default_tenant'},
58+
'properties': {
59+
'use_external_resource': False,
60+
'resource_id': 'foo'
61+
}
5862
})
5963
# the context logger will try to publish messages to rabbit, which is
6064
# not available here. instead, we redirect the output to stdout.
@@ -318,6 +322,26 @@ def test_source_target_not_in_relationship(self):
318322
'relationship-instance context but used in a '
319323
'deployment context.', str(cm.exception))
320324

325+
def test_node_ctx_properties(self):
326+
327+
def get_node_mock(*_, **__):
328+
return {
329+
'properties': {
330+
'use_external_resource': False,
331+
'resource_id': 'foo'
332+
}
333+
}
334+
335+
endpoint = Mock()
336+
endpoint.get_node = get_node_mock
337+
kwargs = {
338+
'endpoint': endpoint,
339+
'context': {'node_id': 'node_id'},
340+
}
341+
ctx_node = context.NodeContext(**kwargs)
342+
self.assertFalse(ctx_node.is_external)
343+
self.assertEqual(ctx_node.resource_id, 'foo')
344+
321345
def test_ctx_type(self):
322346
ctx = context.CloudifyContext({})
323347
self.assertEqual(constants.DEPLOYMENT, ctx.type)
Binary file not shown.

dev-requirements.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)