@@ -99,6 +99,9 @@ def parse_arguments():
9999 # Optional password argument
100100 parser .add_argument ('--password' , '-p' , help = '\n Management administrator password.' , metavar = "" )
101101
102+ # Optional api key argument
103+ parser .add_argument ('--api-key' , help = '\n Management administrator API key.' , metavar = "" )
104+
102105 # Optional login as root argument
103106 parser .add_argument ('--root' , '-r' , choices = ['true' , 'false' ],
104107 help = '\b {%(choices)s}\n Login as root. When running on the management server, use this flag with value set to \' true\' to login as Super User administrator.' ,
@@ -139,22 +142,46 @@ def parse_arguments():
139142def customize_arguments (parser , args ):
140143 # The user has not entered username
141144 if args .root is None :
142- if args .username is None :
145+ auth_method = ""
146+ if args .username or args .password :
147+ auth_method = "1"
148+ elif args .api_key :
149+ auth_method = "2"
150+
151+ if args .username is None and args .password is None and args .api_key is None :
143152 if sys .version_info >= (3 , 0 ):
144- args . username = input ("Username: " )
153+ auth_method = input ("Select authentication method: \n 1. Username & Password \n 2. API key \n " )
145154 else :
146- args . username = raw_input ("Username: " )
155+ auth_method = raw_input ("Select authentication method: \n 1. Username & Password \n 2. API key \n " )
147156
148- # The user has not entered password
149- if args .password is None :
150- if sys .stdin .isatty ():
151- args .password = getpass .getpass ("Password: " )
152- else :
153- print_msg ("Attention! Your password will be shown on the screen!" )
157+ if auth_method != "1" and auth_method != "2" :
158+ print_msg ("Invalid authentication method input (Expected 1 or 2)" )
159+ exit (1 )
160+
161+ if auth_method == "1" :
162+ if args .username is None :
163+ if sys .version_info >= (3 , 0 ):
164+ args .username = input ("Username: " )
165+ else :
166+ args .username = raw_input ("Username: " )
167+
168+ # The user has not entered password
169+ if args .password is None :
170+ if sys .stdin .isatty ():
171+ args .password = getpass .getpass ("Password: " )
172+ else :
173+ print ("Attention! Your password will be shown on the screen!" )
174+ if sys .version_info >= (3 , 0 ):
175+ args .password = input ("Password: " )
176+ else :
177+ args .password = raw_input ("Password: " )
178+ else :
179+ if args .api_key is None :
180+ print ("Attention! Your API key will be shown on the screen!" )
154181 if sys .version_info >= (3 , 0 ):
155- args .password = input ("Password : " )
182+ args .api_key = input ("API key : " )
156183 else :
157- args .password = raw_input ("Password : " )
184+ args .api_key = raw_input ("API key : " )
158185
159186 # Plan-file should be provided in apply operations only
160187 if (args .import_plan_file is not None ) and (is_apply_operation (args .operation ) is False ):
@@ -723,11 +750,16 @@ def login(user_args, client):
723750 payload = dict ({"read-only" : str (login_read_only )}, ** session_details ))
724751 else :
725752 print_msg (
726- " Error: Command contains ambigious parameters. Management server remote ip is unexpected when logging in as root." )
753+ "Error: Command contains ambigious parameters. Management server remote ip is unexpected when logging "
754+ "in as root." )
727755 exit (1 )
728756 else :
729- login_res = client .login (user_args .username , user_args .password , domain = user_args .domain ,
730- read_only = login_read_only , payload = session_details )
757+ if user_args .api_key and len (user_args .api_key ) > 0 :
758+ login_res = client .login_with_api_key (user_args .api_key , domain = user_args .domain ,
759+ read_only = login_read_only , payload = session_details )
760+ else :
761+ login_res = client .login (user_args .username , user_args .password , domain = user_args .domain ,
762+ read_only = login_read_only , payload = session_details )
731763
732764 exit_failure ("Failed to login." , login_res )
733765
@@ -792,7 +824,8 @@ def is_target_valid_group(target_uid, valid_targets, client, groups):
792824
793825 response = client .api_call (command = "show-group" , payload = {"uid" : target_uid })
794826 if response .success is False :
795- print_msg ("Failed to get group with UID {}. Error: {}" .format (target_uid , response .error_message .encode ('utf-8' )))
827+ print_msg (
828+ "Failed to get group with UID {}. Error: {}" .format (target_uid , response .error_message .encode ('utf-8' )))
796829 return False
797830
798831 members = response .data .get ("members" )
0 commit comments