blob: d736045ab3c9fc662375a3806c7046e7b800a211 [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,
Yuriy Taraday3c3f63c2017-06-29 20:46:20 +040051 'comment': 'Release "{}" was updated'.format(name),
Yuriy Taraday84a21032017-06-27 11:13:16 +040052 }
Yuriy Taraday893b3fb2017-07-03 16:22:57 +040053
54
55def absent(name):
56 exists = __salt__['helm.release_exists'](name)
57 if not exists:
58 return {
59 'name': name,
60 'changes': {},
61 'result': True,
62 'comment': 'Release "{}" doesn\'t exist'.format(name),
63 }
64 result = __salt__['helm.release_delete'](name)
65 if not result:
66 return {
67 'name': name,
68 'changes': {},
69 'result': False,
70 'comment': 'Failed to delete release "{}"'.format(name),
71 }
72 return {
73 'name': name,
74 'changes': {name: 'DELETED'},
75 'result': True,
76 'comment': 'Release "{}" was deleted'.format(name),
77 }