From 3e1c3f4e2ef7704dfb2df6f1fb136fd430eed4e5 Mon Sep 17 00:00:00 2001 From: Neill Cox Date: Mon, 18 Sep 2023 11:22:38 +0000 Subject: [PATCH] linter cleanups --- os_migrate_setup.py | 79 +++++++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 21 deletions(-) diff --git a/os_migrate_setup.py b/os_migrate_setup.py index 4d98ebb..2831be5 100644 --- a/os_migrate_setup.py +++ b/os_migrate_setup.py @@ -3,19 +3,23 @@ Quick and dirty script to help setup project, flavors, networks, images """ import argparse import json -import os + +# import os import subprocess + def openstack_cmd(cmd): - cmd = "OS_CLOUD=standalone openstack " + cmd + """Utility function to run an openstack command agains the standalone cloud""" + cmd = "OS_CLOUD=standalone openstack " + cmd result = subprocess.check_output(cmd, shell=True, universal_newlines=True) return result + def parse_args(): """Parse the command line arguments""" - home = os.environ.get('HOME') + # home = os.environ.get('HOME') parser = argparse.ArgumentParser() parser.add_argument("-d", "--project-domain", default="default") @@ -24,18 +28,20 @@ def parse_args(): parser.add_argument("-u", "--username", default="test-user") parser.add_argument("-p", "--password", default="secrete123") parser.add_argument("-c", "--cloud", default="standalone") - + args = parser.parse_args() return args def create_project(args): + """Create the project if it doesn't already exist""" cmd = "project list -f json" - project_exists = [ - x for x in json.loads(openstack_cmd(cmd)) - if x["Name"] == args.project_name - ] + project_exists = [ + x + for x in json.loads(openstack_cmd(cmd)) + if x["Name"] == args.project_name + ] if project_exists: print(f"Project {args.project_name} already exists. Skipping creation") @@ -45,18 +51,19 @@ def create_project(args): cmd = ( f"project create -f json --description '{args.project_description}' " f"{args.project_name} --domain {args.project_domain}" - ) - + ) + args.project_id = json.loads(openstack_cmd(cmd))["id"] - + print(f"Project created - id: {args.project_id}") + def create_user(args): + """Create the user if it doesn't already exist""" cmd = "user list -f json" - user_exists = [ - x for x in json.loads(openstack_cmd(cmd)) - if x["Name"] == args.username - ] + user_exists = [ + x for x in json.loads(openstack_cmd(cmd)) if x["Name"] == args.username + ] if user_exists: print(f"User {args.username} already exists. Skipping creation") @@ -66,55 +73,86 @@ def create_user(args): cmd = ( f"user create -f json --project {args.project_id} " f"--password {args.password} {args.username}" - ) - + ) + args.user_id = json.loads(openstack_cmd(cmd))["id"] - + print(f"User created - id: {args.user_id}") + def assign_member_role(args): + """ + Assign the member role to the user. + + Note: it doesn't matter if the role is assigned multiple times, so not + bothering to check. + """ cmd = f"role add --user {args.username} --project {args.project_id} member" result = openstack_cmd(cmd) - cmd = f"role assignment list --user {args.user_id} --role member -f json" result = json.loads(openstack_cmd(cmd)) if result: print("User has member role") + def create_public_network(args): + """Coming soon - create the public network""" + # pylint: disable=unused-argument print("creating public network - NYI") + def create_private_network(args): + """Coming soon - create the private network""" + # pylint: disable=unused-argument print("creating private network - NYI") + def create_cirros_flavor(args): + """Coming soon - create the cirros flavor""" + # pylint: disable=unused-argument print("creating cirros flavor - NYI") + def create_rhel_flavor(args): + """Coming soon - create the rhel flavor""" + # pylint: disable=unused-argument print("creating rhel flavor - NYI") + def create_cirros_image(args): + """Coming soon - create the cirros image""" + # pylint: disable=unused-argument print("creating cirros image - NYI") + def create_rhel_image(args): + """Coming soon - create the rhel image""" + # pylint: disable=unused-argument print("creating rhel image - NYI") + def create_cirros_instance(args): + """Coming soon - create the cirros instance""" + # pylint: disable=unused-argument print("creating cirros instance - NYI") + def create_rhel_instance(args): + """Coming soon - create the rhel instance""" + # pylint: disable=unused-argument print("creating rhel instance - NYI") + def main(): """main function""" args = parse_args() create_project(args) - print (args.project_id) + print(args.project_id) create_user(args) assign_member_role(args) @@ -131,6 +169,5 @@ def main(): create_rhel_instance(args) - if __name__ == "__main__": main()