def test_iterate_sub_groups_three_levels(self): """ Grupos: + / + /foo + /bla + /baz """ data = { "id": "/", "groups": [ { "id": "/foo", "apps": [], "groups": [ { "id": "/foo/bar", "apps": [], "groups": [{"id": "/foo/bar/baz"}] }, ] }, ], "apps": [] } group = AsgardAppGroup(MarathonGroup.from_json(data)) self.assertEqual(group.id, "/") expected_all_group_ids = ["/", "/foo", "/foo/bar", "/foo/bar/baz"] returned_groups = list(group.iterate_groups()) self.assertEqual(expected_all_group_ids, [g.id for g in returned_groups])
def split(self) -> Apps: if self.is_read_request(): response_content = json.loads(self.response.data) if self.is_list_apps_request(): for app in response_content['apps']: response_app = AsgardApp.from_json(app) app = self.marathon_client.get_app(self.object_id or response_app.id) yield response_app, app return elif self.is_group_request(): response_group = AsgardAppGroup( MarathonGroup.from_json(response_content)) for current_group in response_group.iterate_groups(): group_id = current_group.id group_id_without_namespace = self._remove_namespace_if_exists( self.request.user.current_account.namespace, group_id) original_group = self._get_original_group( self.request.user, group_id_without_namespace) yield current_group, original_group return elif self.is_tasks_request(): for task in response_content['tasks']: response_task = MarathonTask.from_json(task) yield response_task, response_task return elif self.is_deployment(): content = response_content deployments = (MarathonDeployment.from_json(deploy) for deploy in content) for deployment in deployments: yield deployment, deployment return elif self.is_queue_request(): queue_data = response_content queued_apps = (MarathonQueueItem.from_json(queue_item) for queue_item in queue_data['queue']) for queued_app in queued_apps: yield queued_app, queued_app return else: response_app = AsgardApp.from_json( response_content.get('app') or response_content) app = self.marathon_client.get_app(self.object_id) yield response_app, app return if self.is_write_request(): response_content = json.loads(self.response.data) if 'tasks' in response_content: for task in response_content['tasks']: response_task = MarathonTask.from_json(task) yield response_task, response_task return return yield AsgardApp(), self.marathon_client.get_app(self.app_id)
def split(self) -> Apps: if self.is_read_request(): response_content = json.loads(self.response.data) if self.is_list_apps_request(): all_apps = list( AsgardAppGroup.from_json(response_content).iterate_apps() ) for response_app in all_apps: yield response_app, response_app return elif self.is_group_request(): response_group = AsgardAppGroup( MarathonGroup.from_json(response_content) ) for current_group in response_group.iterate_groups(): yield current_group, current_group return elif self.is_tasks_request(): for task in response_content["tasks"]: response_task = MarathonTask.from_json(task) yield response_task, response_task return elif self.is_deployment(): content = response_content deployments = ( MarathonDeployment.from_json(deploy) for deploy in content ) for deployment in deployments: yield deployment, deployment return elif self.is_queue_request(): queue_data = response_content queued_apps = ( MarathonQueueItem.from_json(queue_item) for queue_item in queue_data["queue"] ) for queued_app in queued_apps: yield queued_app, queued_app return else: response_app = AsgardApp.from_json( response_content.get("app") or response_content ) app = self.marathon_client.get_app(self.object_id) yield response_app, app return if self.is_write_request(): response_content = json.loads(self.response.data) if "tasks" in response_content: for task in response_content["tasks"]: response_task = MarathonTask.from_json(task) yield response_task, response_task return return yield AsgardApp(), self.marathon_client.get_app(self.app_id)
def test_iterate_sub_groups_one_level(self): data = { "id": "/", "groups": [ {"id": "/foo", "apps": []}, {"id": "/bla", "apps": []}, ], "apps": [] } group = AsgardAppGroup(MarathonGroup.from_json(data)) self.assertEqual(group.id, "/") all_group_ids = ["/", "/foo", "/bla"] self.assertEqual(all_group_ids, [g.id for g in group.iterate_groups()])
#!/usr/bin/env python # Script que percorre todas as apps e groups de um json (de Grupo) removendo: # app.version # app.fetch # group.version import sys import json from hollowman.marathon.group import AsgardAppGroup from marathon.models.group import MarathonGroup from marathon.util import MarathonJsonEncoder data = open(sys.argv[1]).read() _g = AsgardAppGroup(MarathonGroup.from_json(json.loads(data))) for group in _g.iterate_groups(): del group.version for app in group.apps: del app.version del app.fetch data_output = json.dumps(_g._marathon_group, cls=MarathonJsonEncoder) print(data_output)