diff --git a/.gitignore b/.gitignore index 6769e21..af00d99 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +# ide/system ignore +.idea +.DS_Store + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/repro/common/helpers.py b/repro/common/helpers.py index a3661d7..0cc136b 100644 --- a/repro/common/helpers.py +++ b/repro/common/helpers.py @@ -9,22 +9,24 @@ API_TIMEOUT = 300 console = Console() + def get_profile_url(profile): return get_profile_data(profile)['server_url'] + def get_profile_api_key(profile): return get_profile_data(profile)['api_key'] + def get_profile_data(profile): cli_config_filename = 'cli.yml' - path = '' match platform.system().lower(): case 'darwin': home_dir = os.environ.get('HOME') path = ( - f'{home_dir}/Library/Application Support/com.cloudtruth.CloudTruth-CLI/' + - f'{cli_config_filename}' + f'{home_dir}/Library/Application Support/com.cloudtruth.CloudTruth-CLI/' + + f'{cli_config_filename}' ) case 'linux': home_dir = os.environ.get('XDG_CONFIG_HOME') @@ -43,7 +45,8 @@ def get_profile_data(profile): return config['profiles'][profile] -def make_request(uri, http_method, headers, body = None): + +def make_request(uri, http_method, headers, body=None): try: response = requests.request( method=http_method.upper(), @@ -56,15 +59,17 @@ def make_request(uri, http_method, headers, body = None): response.raise_for_status() except HTTPError as err: console.log(err) - console.log(locals()) #TODO: Debugging-only!!! + console.log(locals()) # TODO: Debugging-only!!! exit() return response + def get_objects_list(cloudtruth_type, api_url, headers): url = f'{api_url}/{cloudtruth_type}/' return make_request(url, 'get', headers).json().get('results') + def get_object_by_name(name, cloudtruth_type, api_url, headers): objs = get_objects_list(cloudtruth_type, api_url, headers) @@ -74,18 +79,21 @@ def get_object_by_name(name, cloudtruth_type, api_url, headers): return None + def delete_object(obj, cloudtruth_type, api_url, headers): obj_id = obj.get('id') url = f'{api_url}/{cloudtruth_type}/{obj_id}/' make_request(url, 'delete', headers) -def get_object_by_id(id, cloudtruth_type, api_url, headers): + +def get_object_by_id(object_id, cloudtruth_type, api_url, headers): return NOT_IMPLEMENTED -#### PROJECTS -def create_project(name, api_url, headers, parent = None): +# PROJECTS + +def create_project(name, api_url, headers, parent=None): project_url = f'{api_url}/projects/' depends_on = parent.get('url') if parent is not None else '' body = { @@ -97,7 +105,8 @@ def create_project(name, api_url, headers, parent = None): return project -def delete_project(project, api_url, headers, force = False): + +def delete_project(project, api_url, headers, force=False): project_and_dependents = get_project_tree_projects(project, api_url, headers) for project in reversed(project_and_dependents): @@ -106,6 +115,7 @@ def delete_project(project, api_url, headers, force = False): console.log(f"Deleting project: {project.get('name')}") delete_object(project, 'projects', api_url, headers) + def project_has_dependents(project): child_project_urls = project.get('dependents') if len(child_project_urls) > 0: @@ -113,6 +123,7 @@ def project_has_dependents(project): return False + def project_has_parent(project): parent_project_url = project.get('depends_on') if parent_project_url: @@ -120,18 +131,19 @@ def project_has_parent(project): return False -def get_project_tree_projects(project, api_url, headers, projects = None): + +def get_project_tree_projects(project, api_url, headers, projects=None): if project is None: return None if projects is None: projects = [] console.log(f"Adding parent, {project.get('name')} to the list") - projects.append(project) # put the parent in the list on init + projects.append(project) # put the parent in the list on init if project_has_dependents(project): child_project_urls = project.get('dependents') for child_project_url in child_project_urls: child_project = make_request(child_project_url, 'get', headers).json() - if project_has_dependents(child_project): # if the child has dependents + if project_has_dependents(child_project): # if the child has dependents console.log(f"Adding a child to the list, {child_project.get('name')} with dependencies") projects.append(child_project) get_project_tree_projects(child_project, api_url, headers, projects) @@ -141,6 +153,7 @@ def get_project_tree_projects(project, api_url, headers, projects = None): return projects + def get_all_top_level_projects(api_url, headers): all_projects = get_objects_list('projects', api_url, headers) top_level_projects = [] @@ -151,10 +164,11 @@ def get_all_top_level_projects(api_url, headers): return top_level_projects -#### PARAMETERS + +# PARAMETERS def create_parameter(name, project, api_url, headers): - #TODO: handle secrets? + # TODO: handle secrets? project_id = project.get('id') param_url = f"{api_url}/projects/{project_id}/parameters/" @@ -166,6 +180,7 @@ def create_parameter(name, project, api_url, headers): return param + def delete_project_parameters(project, api_url, headers): project_id = project.get('id') project_parameters_url = f"{api_url}/projects/{project_id}/parameters/" @@ -175,15 +190,17 @@ def delete_project_parameters(project, api_url, headers): for parameter in parameters: delete_parameter(project_id, parameter, api_url, headers) + def delete_parameter(project_id, parameter, api_url, headers): parameter_id = parameter.get('id') parameter_url = f"{api_url}/projects/{project_id}/parameters/{parameter_id}" make_request(parameter_url, 'delete', api_url, headers) -#### ENVIRONMENTS -def create_environment(name, api_url, headers, parent = None): +# ENVIRONMENTS + +def create_environment(name, api_url, headers, parent=None): env_url = f'{api_url}/environments/' parent_uri = parent.get('url') if parent is not None else '' body = { @@ -195,7 +212,8 @@ def create_environment(name, api_url, headers, parent = None): return env -#### MISC + +# MISC def nuke(): return NOT_IMPLEMENTED diff --git a/repro/ct_org_repro_init.py b/repro/ct_org_repro_init.py old mode 100644 new mode 100755 index c01f2a3..7710d51 --- a/repro/ct_org_repro_init.py +++ b/repro/ct_org_repro_init.py @@ -15,6 +15,7 @@ EXIT_ERR = 1 EXIT_SUCCESS = 0 + def parse_args(): usage_text = ( 'Creates or deletes projects, envs, templates, and parameters based on inputs' @@ -56,20 +57,20 @@ def parse_args(): '--levels', metavar='N', help=( - 'Number of extra levels to nest (depth).\n' + - 'each level will get the same number of child projects and environments, default is 0' + 'Number of extra levels to nest (depth).\n' + + 'each level will get the same number of child projects and environments, default is 0' ), type=int, default=0, ) parser.add_argument( '--create', - help=('Create given types if empty and not associated, requires one or more base types to be set'), + help='Create given types if empty and not associated, requires one or more base types to be set', action='store_true', ) parser.add_argument( '--delete', - help=('Delete given types if empty and not associated, requires one or more base types to be set'), + help='Delete given types if empty and not associated, requires one or more base types to be set', action='store_true', ) parser.add_argument( @@ -80,16 +81,16 @@ def parse_args(): parser.add_argument( '--force', help=( - 'Used for deleting everything associated with the given type, ' + - 'requires a base type to be set' + 'Used for deleting everything associated with the given type, ' + + 'requires a base type to be set' ), action='store_true', ) parser.add_argument( '--reset-org', help=( - 'The nuclear option. Resets org to default state. ' + - 'Will delete everything, even previously created models' + 'The nuclear option. Resets org to default state. ' + + 'Will delete everything, even previously created models' ), action='store_true', ) @@ -128,7 +129,8 @@ def parse_args(): return parser.parse_args() -def create_projects(count, parent_project_name = None): + +def create_projects(count, parent_project_name=None): created_projects = [] parent_project = None @@ -151,6 +153,7 @@ def create_projects(count, parent_project_name = None): console.log(f'Created {len(created_projects)} projects') + def create_envs(count, parent): i = 0 while i < count: @@ -159,18 +162,21 @@ def create_envs(count, parent): _envs.append(env) i += 1 + def create_params(count, project): i = 0 param_name = PARAMETER_PREFIX + shortuuid.uuid() while i < count: helpers.create_parameter(param_name, project, _api_url, _headers) + def delete_all_parameters(): projects = helpers.get_objects_list('projects', _api_url, _headers) for project in projects: helpers.delete_project_parameters(project, _api_url, _headers) -def delete_all_projects(force = False): + +def delete_all_projects(force=False): console.print('Deleting all projects') projects = helpers.get_all_top_level_projects(_api_url, _headers) @@ -182,7 +188,8 @@ def delete_all_projects(force = False): return True -def delete_single_project(project_root_name = None, force = False): + +def delete_single_project(project_root_name=None, force=False): project = helpers.get_object_by_name(project_root_name, 'projects', _api_url, _headers) if not project: @@ -261,8 +268,6 @@ def main(): if args.templates: console.print('deleting templates') - - # # reset org # if args.reset_org and args.force: # console.print('Nuking everything') @@ -271,7 +276,6 @@ def main(): # console.print('Nuke complete') # exit(foo) - # # create environments # env_count = args.envs # parent_env_name = args.base_env @@ -305,12 +309,13 @@ def main(): # helpers.delete_project(project, _api_url, _headers, delete_params) - #TODO: implement template create - #TODO: implement destroy_params - #TODO: implement destroy_envs - #TODO: implement destroy_projects - #TODO: implement destroy_all - #TODO: implement store data? + # TODO: implement template create + # TODO: implement destroy_params + # TODO: implement destroy_envs + # TODO: implement destroy_projects + # TODO: implement destroy_all + # TODO: implement store data? + if __name__ == "__main__": main()