Browse Source

Refactor tiller commands to handle methods

This is a refactor to any methods in the api and cli to move tiller
operations to a separate handler function to make them uniform
with the rest of the code

Change-Id: Iddd45f9f979307f54ad5bd54c3ee005547dd0909
changes/94/634494/6
Michael Beaver 3 months ago
parent
commit
b458c3f675

+ 16
- 18
armada/api/controller/armada.py View File

@@ -69,29 +69,13 @@ class Apply(api.BaseResource):
69 69
                 "or application/json")
70 70
         try:
71 71
             with self.get_tiller(req, resp) as tiller:
72
-
73
-                armada = Armada(
74
-                    documents,
75
-                    disable_update_pre=req.get_param_as_bool(
76
-                        'disable_update_pre'),
77
-                    disable_update_post=req.get_param_as_bool(
78
-                        'disable_update_post'),
79
-                    enable_chart_cleanup=req.get_param_as_bool(
80
-                        'enable_chart_cleanup'),
81
-                    dry_run=req.get_param_as_bool('dry_run'),
82
-                    force_wait=req.get_param_as_bool('wait'),
83
-                    timeout=req.get_param_as_int('timeout'),
84
-                    tiller=tiller,
85
-                    target_manifest=req.get_param('target_manifest'))
86
-
87
-                msg = armada.sync()
88
-
72
+                msg = self.handle(req, documents, tiller)
89 73
                 resp.body = json.dumps({
90 74
                     'message': msg,
91 75
                 })
92
-
93 76
                 resp.content_type = 'application/json'
94 77
                 resp.status = falcon.HTTP_200
78
+
95 79
         except exceptions.ManifestException as e:
96 80
             self.return_error(resp, falcon.HTTP_400, message=str(e))
97 81
         except Exception as e:
@@ -99,3 +83,17 @@ class Apply(api.BaseResource):
99 83
             err_message = 'Failed to apply manifest: {}'.format(e)
100 84
             self.error(req.context, err_message)
101 85
             self.return_error(resp, falcon.HTTP_500, message=err_message)
86
+
87
+    def handle(self, req, documents, tiller):
88
+        armada = Armada(
89
+            documents,
90
+            disable_update_pre=req.get_param_as_bool('disable_update_pre'),
91
+            disable_update_post=req.get_param_as_bool('disable_update_post'),
92
+            enable_chart_cleanup=req.get_param_as_bool('enable_chart_cleanup'),
93
+            dry_run=req.get_param_as_bool('dry_run'),
94
+            force_wait=req.get_param_as_bool('wait'),
95
+            timeout=req.get_param_as_int('timeout'),
96
+            tiller=tiller,
97
+            target_manifest=req.get_param('target_manifest'))
98
+
99
+        return armada.sync()

+ 17
- 16
armada/api/controller/rollback.py View File

@@ -30,27 +30,28 @@ class Rollback(api.BaseResource):
30 30
     @policy.enforce('armada:rollback_release')
31 31
     def on_post(self, req, resp, release):
32 32
         try:
33
-            dry_run = req.get_param_as_bool('dry_run')
34
-
35 33
             with self.get_tiller(req, resp) as tiller:
36
-
37
-                tiller.rollback_release(
38
-                    release,
39
-                    req.get_param_as_int('version') or 0,
40
-                    wait=req.get_param_as_bool('wait'),
41
-                    timeout=req.get_param_as_int('timeout') or 0,
42
-                    force=req.get_param_as_bool('force'),
43
-                    recreate_pods=req.get_param_as_bool('recreate_pods'))
44
-
34
+                msg = self.handle(req, release, tiller)
45 35
                 resp.body = json.dumps({
46
-                    'message': ('(dry run) ' if dry_run else '') +
47
-                    'Rollback of {} complete.'.format(release),
36
+                    'message': msg,
48 37
                 })
49
-
50
-            resp.content_type = 'application/json'
51
-            resp.status = falcon.HTTP_200
38
+                resp.content_type = 'application/json'
39
+                resp.status = falcon.HTTP_200
52 40
         except Exception as e:
53 41
             self.logger.exception('Caught unexpected exception')
54 42
             err_message = 'Failed to rollback release: {}'.format(e)
55 43
             self.error(req.context, err_message)
56 44
             self.return_error(resp, falcon.HTTP_500, message=err_message)
45
+
46
+    def handle(self, req, release, tiller):
47
+        dry_run = req.get_param_as_bool('dry_run')
48
+        tiller.rollback_release(
49
+            release,
50
+            req.get_param_as_int('version') or 0,
51
+            wait=req.get_param_as_bool('wait'),
52
+            timeout=req.get_param_as_int('timeout') or 0,
53
+            force=req.get_param_as_bool('force'),
54
+            recreate_pods=req.get_param_as_bool('recreate_pods'))
55
+
56
+        return ('(dry run) ' if dry_run else '') + \
57
+            'Rollback of {} complete.'.format(release)

+ 6
- 4
armada/api/controller/test.py View File

@@ -37,10 +37,7 @@ class TestReleasesReleaseNameController(api.BaseResource):
37 37
     @policy.enforce('armada:test_release')
38 38
     def on_get(self, req, resp, release):
39 39
         with self.get_tiller(req, resp) as tiller:
40
-            cleanup = req.get_param_as_bool('cleanup')
41
-
42
-            test_handler = Test({}, release, tiller, cleanup=cleanup)
43
-            success = test_handler.test_release_for_success()
40
+            success = self.handle(req, release, tiller)
44 41
 
45 42
         if success:
46 43
             msg = {
@@ -57,6 +54,11 @@ class TestReleasesReleaseNameController(api.BaseResource):
57 54
         resp.status = falcon.HTTP_200
58 55
         resp.content_type = 'application/json'
59 56
 
57
+    def handle(self, req, release, tiller):
58
+        cleanup = req.get_param_as_bool('cleanup')
59
+        test_handler = Test({}, release, tiller, cleanup=cleanup)
60
+        return test_handler.test_release_for_success()
61
+
60 62
 
61 63
 class TestReleasesManifestController(api.BaseResource):
62 64
     '''

+ 29
- 25
armada/api/controller/tiller.py View File

@@ -34,19 +34,7 @@ class Status(api.BaseResource):
34 34
         '''
35 35
         try:
36 36
             with self.get_tiller(req, resp) as tiller:
37
-
38
-                LOG.debug(
39
-                    'Tiller (Status) at: %s:%s, namespace=%s, '
40
-                    'timeout=%s', tiller.tiller_host, tiller.tiller_port,
41
-                    tiller.tiller_namespace, tiller.timeout)
42
-
43
-                message = {
44
-                    'tiller': {
45
-                        'state': tiller.tiller_status(),
46
-                        'version': tiller.tiller_version()
47
-                    }
48
-                }
49
-
37
+                message = self.handle(tiller)
50 38
                 resp.status = falcon.HTTP_200
51 39
                 resp.body = json.dumps(message)
52 40
                 resp.content_type = 'application/json'
@@ -56,6 +44,19 @@ class Status(api.BaseResource):
56 44
             self.error(req.context, err_message)
57 45
             self.return_error(resp, falcon.HTTP_500, message=err_message)
58 46
 
47
+    def handle(self, tiller):
48
+        LOG.debug('Tiller (Status) at: %s:%s, namespace=%s, '
49
+                  'timeout=%s', tiller.tiller_host, tiller.tiller_port,
50
+                  tiller.tiller_namespace, tiller.timeout)
51
+
52
+        message = {
53
+            'tiller': {
54
+                'state': tiller.tiller_status(),
55
+                'version': tiller.tiller_version()
56
+            }
57
+        }
58
+        return message
59
+
59 60
 
60 61
 class Release(api.BaseResource):
61 62
 
@@ -65,18 +66,10 @@ class Release(api.BaseResource):
65 66
         '''
66 67
         try:
67 68
             with self.get_tiller(req, resp) as tiller:
68
-
69
-                LOG.debug(
70
-                    'Tiller (Release) at: %s:%s, namespace=%s, '
71
-                    'timeout=%s', tiller.tiller_host, tiller.tiller_port,
72
-                    tiller.tiller_namespace, tiller.timeout)
73
-
74
-                releases = {}
75
-                for release in tiller.list_releases():
76
-                    releases.setdefault(release.namespace, [])
77
-                    releases[release.namespace].append(release.name)
78
-
79
-                resp.body = json.dumps({'releases': releases})
69
+                releases = self.handle(tiller)
70
+                resp.body = json.dumps({
71
+                    'releases': releases,
72
+                })
80 73
                 resp.content_type = 'application/json'
81 74
                 resp.status = falcon.HTTP_200
82 75
 
@@ -84,3 +77,14 @@ class Release(api.BaseResource):
84 77
             err_message = 'Unable to find Tiller Releases: {}'.format(e)
85 78
             self.error(req.context, err_message)
86 79
             self.return_error(resp, falcon.HTTP_500, message=err_message)
80
+
81
+    def handle(self, tiller):
82
+        LOG.debug('Tiller (Release) at: %s:%s, namespace=%s, '
83
+                  'timeout=%s', tiller.tiller_host, tiller.tiller_port,
84
+                  tiller.tiller_namespace, tiller.timeout)
85
+
86
+        releases = {}
87
+        for release in tiller.list_releases():
88
+            releases.setdefault(release.namespace, [])
89
+            releases[release.namespace].append(release.name)
90
+        return releases

+ 17
- 14
armada/cli/apply.py View File

@@ -204,20 +204,8 @@ class ApplyManifest(CliAction):
204 204
                     tiller_port=self.tiller_port,
205 205
                     tiller_namespace=self.tiller_namespace,
206 206
                     dry_run=self.dry_run) as tiller:
207
-                armada = Armada(
208
-                    documents,
209
-                    disable_update_pre=self.disable_update_pre,
210
-                    disable_update_post=self.disable_update_post,
211
-                    enable_chart_cleanup=self.enable_chart_cleanup,
212
-                    dry_run=self.dry_run,
213
-                    set_ovr=self.set,
214
-                    force_wait=self.wait,
215
-                    timeout=self.timeout,
216
-                    tiller=tiller,
217
-                    values=self.values,
218
-                    target_manifest=self.target_manifest)
219
-
220
-                resp = armada.sync()
207
+
208
+                resp = self.handle(documents, tiller)
221 209
                 self.output(resp)
222 210
         else:
223 211
             if len(self.values) > 0:
@@ -245,3 +233,18 @@ class ApplyManifest(CliAction):
245 233
                 resp = client.post_apply(
246 234
                     manifest=documents, set=self.set, query=query)
247 235
             self.output(resp.get('message'))
236
+
237
+    def handle(self, documents, tiller):
238
+        armada = Armada(
239
+            documents,
240
+            disable_update_pre=self.disable_update_pre,
241
+            disable_update_post=self.disable_update_post,
242
+            enable_chart_cleanup=self.enable_chart_cleanup,
243
+            dry_run=self.dry_run,
244
+            set_ovr=self.set,
245
+            force_wait=self.wait,
246
+            timeout=self.timeout,
247
+            tiller=tiller,
248
+            values=self.values,
249
+            target_manifest=self.target_manifest)
250
+        return armada.sync()

+ 10
- 7
armada/cli/rollback.py View File

@@ -116,16 +116,19 @@ class Rollback(CliAction):
116 116
                 tiller_namespace=self.tiller_namespace,
117 117
                 dry_run=self.dry_run) as tiller:
118 118
 
119
-            response = tiller.rollback_release(
120
-                self.release,
121
-                self.version,
122
-                wait=self.wait,
123
-                timeout=self.timeout,
124
-                force=self.force,
125
-                recreate_pods=self.recreate_pods)
119
+            response = self.handle(tiller)
126 120
 
127 121
             self.output(response)
128 122
 
123
+    def handle(self, tiller):
124
+        return tiller.rollback_release(
125
+            self.release,
126
+            self.version,
127
+            wait=self.wait,
128
+            timeout=self.timeout,
129
+            force=self.force,
130
+            recreate_pods=self.recreate_pods)
131
+
129 132
     def output(self, response):
130 133
         self.logger.info(('(dry run) ' if self.dry_run else '') +
131 134
                          'Rollback of %s complete.', self.release)

Loading…
Cancel
Save