blob: c3b70a35af29298550fcaa86f22f3a64c3f85b47 [file] [log] [blame]
Yuriy Taradayaeeaa742017-06-28 15:54:56 +04001import difflib
Yuriy Taraday84a21032017-06-27 11:13:16 +04002import logging
3
Yuriy Taradayaeeaa742017-06-28 15:54:56 +04004from salt.serializers import yaml
5
6def present(name, chart_name, version=None, values=None):
Yuriy Taraday84a21032017-06-27 11:13:16 +04007 exists = __salt__['helm.release_exists'](name)
8 if not exists:
9 result = __salt__['helm.release_create'](
10 name, chart_name, version, values)
11 if result:
12 return {
13 'name': name,
14 'changes': {name: 'CREATED'},
15 'result': True,
16 'comment': 'Release "{}" was created'.format(name),
17 }
18 else:
19 return {
20 'name': name,
21 'changes': {},
22 'result': False,
23 'comment': 'Failed to create release "{}"'.format(name),
24 }
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040025 old_values = __salt__['helm.get_values'](name)
26 result = __salt__['helm.release_upgrade'](
27 name, chart_name, version, values)
28 if not result:
29 return {
30 'name': name,
31 'changes': {},
32 'result': False,
33 'comment': 'Failed to create release "{}"'.format(name),
34 }
35 new_values = __salt__['helm.get_values'](name)
36 if new_values == old_values:
37 return {
38 'name': name,
39 'changes': {},
40 'result': True,
41 'comment': 'Release "{}" already exists'.format(name),
42 }
43 old_str = yaml.serialize(old_values, default_flow_style=False)
44 new_str = yaml.serialize(new_values, default_flow_style=False)
45 diff = difflib.unified_diff(
46 old_str.split('\n'), new_str.split('\n'), lineterm='')
Yuriy Taraday84a21032017-06-27 11:13:16 +040047 return {
48 'name': name,
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040049 'changes': {'values': '\n'.join(diff)},
Yuriy Taraday84a21032017-06-27 11:13:16 +040050 'result': True,
51 'comment': 'Release "{}" already exists'.format(name),
52 }