Skip to content

Commit

Permalink
Merge pull request #30 from IBM/issue/29
Browse files Browse the repository at this point in the history
Issue/29- support structured properties
  • Loading branch information
dvaccarosenna authored Dec 22, 2020
2 parents 977a469 + 017e834 commit 02470c1
Show file tree
Hide file tree
Showing 18 changed files with 315 additions and 41 deletions.
2 changes: 0 additions & 2 deletions docs/user-guide/extracting-outputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ To set the value of an output, use the `setOutput` (or `set_output`) function on
resultBuilder.setOutput('propertyName', 'propertyValue')
```

Any value passed to `setOutput` will be converted to a string (using the `str()` method).

To return a field from an object, first retrieve the object and then set the output:

```python
Expand Down
5 changes: 2 additions & 3 deletions kubedriver/keg/model/v1alpha1_keg.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def __init__(self, api_version=None, kind=None, metadata=None, status=None):
self._api_version = None
self._kind = None
self._metadata = None
self._Status = None
self._status = None
if api_version is not None:
self.api_version = api_version
Expand Down Expand Up @@ -63,8 +62,8 @@ def status(self):
return self._status

@status.setter
def status(self, Status):
self._Status = Status
def status(self, status):
self._status = status

def to_dict(self):
return to_dict(self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def handle(self, operation_name, keg_name, keg_status, location_context, output_
#Make a copy of the outputs so they are clean (in case the holder has been contaminated)
outputs = {}
for k, v in result_holder.outputs.items():
outputs[k] = str(v)
outputs[k] = v
return OutputExtractionResult.success(outputs)

def __load_composition(self, keg_status, api_ctl, helm_client):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class V1alpha1KegdStrategyReportStatus(object):
'state': 'str',
'phase': 'str',
'errors': 'list[str]',
'outputs': 'dict(str, str)',
'outputs': 'dict(str, object)',
'delta': 'V1alpha1KegdCompositionDelta'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def setOutput(self, output_name, output_value):
self.set_output(output_name, output_value)

def set_output(self, output_name, output_value):
self.outputs[output_name] = str(output_value)
self.outputs[output_name] = output_value

def decode(self, encoded_value, encoding='utf-8'):
if encoded_value == None:
Expand Down
2 changes: 1 addition & 1 deletion kubedriver/kubeclient/os_api_ctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, base_kube_client, default_namespace=DEFAULT_NAMESPACE):
self.default_namespace = default_namespace

def __get_resource_client(self, api_version, kind):
return self.dynamic_client.resources.get(api_version=api_version, kind=kind)
return self.dynamic_client.resources.get(api_version=api_version, kind=kind)

def create_object(self, object_config, default_namespace=None):
resource_client = self.__get_resource_client(object_config.api_version, object_config.kind)
Expand Down
3 changes: 2 additions & 1 deletion kubedriver/sandbox/sandbox.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from RestrictedPython import compile_restricted_exec, safe_builtins
from RestrictedPython.Guards import guarded_unpack_sequence
from RestrictedPython.Guards import guarded_unpack_sequence, guarded_iter_unpack_sequence
from RestrictedPython.Eval import default_guarded_getiter
from RestrictedPython.PrintCollector import PrintCollector
from .exceptions import CompileError, ExecuteError
Expand Down Expand Up @@ -54,6 +54,7 @@ def run(self, script, file_name='<inline code>', inputs=None):
def __build_builtins(self):
builtins = safe_builtins.copy()
builtins['_unpack_sequence_'] = guarded_unpack_sequence
builtins['_iter_unpack_sequence_'] = guarded_iter_unpack_sequence
builtins['_getiter_'] = default_guarded_getiter
builtins['_getitem_'] = safer_getitem
return builtins
Expand Down
20 changes: 6 additions & 14 deletions tests/unit/infrastructure/test_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,6 @@
dataValue: A
'''

class ObjectConfigurationMatcher:

def __init__(self, expected_conf):
self.expected_conf = expected_conf

def __eq__(self, other):
return other.conf == self.expected_conf

def __str__(self):
return str(self.expected_conf)

def __repr__(self):
return f'{self.__class__.__name__}({self.expected_conf!r})'


class TestInfrastructureDriver():#unittest.TestCase):

Expand All @@ -61,6 +47,8 @@ def __mock_properties(self):
properties['propA'] = {'type': 'string', 'value': 'A'}
properties['propB'] = {'type': 'string', 'value': 'B'}
properties['propC'] = {'type': 'string', 'value': 'C'}
properties['propD'] = {'type': 'integer', 'value': 1}
properties['propE'] = {'type': 'map', 'value': {'A': 'ValueA'}}
properties_map = PropValueMap(properties)
system_properties = {}
system_properties['resourceId'] = {'type': 'string', 'value': '123'}
Expand Down Expand Up @@ -106,6 +94,10 @@ def test_create_infrastructure_templates_with_properties(self):
'propA': 'A',
'propB': 'B',
'propC': 'C',
'propD': '1',
'propE': {
'A': 'ValueA'
},
'systemProperties': {
'resourceId': '123',
'resourceName': 'Resource-A',
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/kegd/test_kegd_files/check-inputs/kegd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
compose:
- name: Create
deploy:
- objects:
file: simple.yaml
getOutputs: outputs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ system_properties.resource_subdomain }}-a
data:
propertyA: {{ propertyA }}
19 changes: 19 additions & 0 deletions tests/unit/kegd/test_kegd_files/check-inputs/scripts/outputs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def getOutputs(keg, props, resultBuilder, log, *args, **kwargs):
resultBuilder.setOutput('string', props['string_input'])
resultBuilder.setOutput('integer', props['integer_input'])
resultBuilder.setOutput('float', props['float_input'])
resultBuilder.setOutput('timestamp', props['timestamp_input'])
resultBuilder.setOutput('boolean', props['boolean_input'])
resultBuilder.setOutput('map', props['map_input'])
for k,v in props['map_input'].items():
resultBuilder.setOutput('map_' + k, v)
resultBuilder.setOutput('list', props['list_input'])
i = 0
for v in props['list_input']:
resultBuilder.setOutput('list_' + str(i), v)
i = i + 1
resultBuilder.setOutput('custom', props['custom_input'])




6 changes: 6 additions & 0 deletions tests/unit/kegd/test_kegd_files/return-outputs/kegd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
compose:
- name: Create
deploy:
- objects:
file: simple.yaml
getOutputs: outputs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ system_properties.resource_subdomain }}-a
data:
propertyA: {{ propertyA }}
13 changes: 13 additions & 0 deletions tests/unit/kegd/test_kegd_files/return-outputs/scripts/outputs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def getOutputs(keg, props, resultBuilder, log, *args, **kwargs):
resultBuilder.setOutput('string', 'A string')
resultBuilder.setOutput('integer', 123)
resultBuilder.setOutput('float', 1.23)
resultBuilder.setOutput('timestamp', '2020-11-24T11:49:33.305403Z')
resultBuilder.setOutput('boolean', True)
resultBuilder.setOutput('map', {'A': 1, 'B': 2})
resultBuilder.setOutput('list', ['A', 'B'])
resultBuilder.setOutput('custom', {'name': 'Testing', 'age': 42})




Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ metadata:
name: {{ system_properties.resource_subdomain }}-a
data:
propertyA: {{ propertyA }}
propertyB: "Include a number - {{ propertyB }}"
3 changes: 2 additions & 1 deletion tests/unit/kegd/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def generate_render_context():
'resourceName': 'just-testing',
}
resource_properties = {
'propertyA': 'A property'
'propertyA': 'A property',
'propertyB': 123
}
request_properties = {}
deployment_location = {}
Expand Down
Loading

0 comments on commit 02470c1

Please sign in to comment.